Skip to content

Commit

Permalink
Upgrade nkn tunnel to support udp
Browse files Browse the repository at this point in the history
Signed-off-by: bill fort <fxbao@hotmail.com>
  • Loading branch information
billfort committed Apr 20, 2023
1 parent 68cc072 commit 9b67a00
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
7 changes: 4 additions & 3 deletions bin/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"log"
"os"

"github.com/jessevdk/go-flags"
"github.com/nknorg/nconnect"
"github.com/nknorg/nconnect/config"
"log"
"os"
)

func main() {
Expand All @@ -15,7 +16,7 @@ func main() {
}
}()

var opts config.NConfig
var opts config.Opts
_, err := flags.Parse(&opts)
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
Expand Down
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {
rand.Seed(time.Now().UnixNano())
}

type NConfig struct {
type Opts struct {
Client bool `short:"c" long:"client" description:"Client mode"`
Server bool `short:"s" long:"server" description:"Server mode"`

Expand All @@ -60,7 +60,6 @@ type Config struct {
// Cipher config
Cipher string `json:"cipher,omitempty" long:"cipher" description:"Socks proxy cipher. Dummy (no cipher) will not reduce security because NKN tunnel already has end to end encryption." choice:"dummy" choice:"chacha20-ietf-poly1305" choice:"aes-128-gcm" choice:"aes-256-gcm" default:"chacha20-ietf-poly1305"`
Password string `json:"password,omitempty" long:"password" description:"Socks proxy password"`
UDP bool `json:"udp,omitempty" long:"udp" description:"Support udp proxy"`

// Session config
DialTimeout int32 `json:"dialTimeout,omitempty" long:"dial-timeout" description:"dial timeout in milliseconds"`
Expand Down Expand Up @@ -108,6 +107,10 @@ type Config struct {
TunaDisableMeasureBandwidth bool `json:"tunaDisableMeasureBandwidth,omitempty" long:"tuna-disable-measure-bandwidth" description:"(server only) Disable Tuna measure bandwidth when selecting service nodes"`
TunaMeasureStoragePath string `json:"tunaMeasureStoragePath,omitempty" long:"tuna-measure-storage-path" description:"(server only) Path to store Tuna measurement results" default:"."`

// UDP config
UDP bool `json:"udp,omitempty" long:"udp" description:"Support udp proxy"`
UDPIdleTime int32 `json:"udpIdleTime,omitempty" long:"udp-idle-time" description:"UDP connections will be purged after idle time (in seconds). 0 is for no purge" default:"0"`

// Admin config
AdminIdentifier string `json:"adminIdentifier,omitempty" long:"admin-identifier" description:"(server only) Admin NKN client identifier prefix" default:"nConnect"`
AdminHTTPAddr string `json:"adminHttpAddr,omitempty" long:"admin-http" description:"(server only) Admin web GUI listen address (e.g. 127.0.0.1:8000)"`
Expand Down
14 changes: 4 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ require (
github.com/eycorsican/go-tun2socks v1.16.11
github.com/gin-contrib/gzip v0.0.3
github.com/gin-gonic/gin v1.7.7
github.com/imdario/mergo v0.3.13
github.com/imdario/mergo v0.3.15
github.com/jessevdk/go-flags v1.5.0
github.com/nknorg/ncp-go v1.0.6-0.20230228002512-f4cd1740bebd
github.com/nknorg/nkn-sdk-go v1.4.5
github.com/nknorg/nkn-tuna-session v0.2.6-0.20230328055742-9a596c57b4bb
github.com/nknorg/nkn-tunnel v0.3.5-0.20230328060135-8eb315c90047
github.com/nknorg/nkn-sdk-go v1.4.6-0.20230404044330-ad192f36d07e
github.com/nknorg/nkn-tuna-session v0.2.6-0.20230415032955-7a3fc7be9634
github.com/nknorg/nkn-tunnel v0.3.5-0.20230418225220-c505086e1505
github.com/nknorg/nkn/v2 v2.2.0
github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9
github.com/nknorg/tuna v0.0.0-20230328054959-0bc6eb5bf369
Expand Down Expand Up @@ -81,9 +81,3 @@ require (
google.golang.org/protobuf v1.29.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

//replace github.com/nknorg/tuna v0.0.0-20230128070346-2e1102d13fcb => github.com/nknorg/tuna v0.0.0-20230128070346-2e1102d13fcb
//
//replace github.com/nknorg/nkn-tuna-session v0.2.5 => github.com/nknorg/nkn-tuna-session v0.2.6-0.20230129080815-6bcf65132ef1
//
//replace github.com/nknorg/nkn-tunnel v0.3.4 => github.com/nknorg/nkn-tunnel v0.3.5-0.20230129081143-f866961b9676
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/itchyny/base58-go v0.0.5/go.mod h1:SrMWPE3DFuJJp1M/RUhu4fccp/y9AlB8AL3o3duPToU=
github.com/itchyny/base58-go v0.2.1 h1:wtnhAVdOcW3WuHEASmGHMms4juOB8yEpj/KJxlB57+k=
Expand Down Expand Up @@ -302,10 +304,16 @@ github.com/nknorg/ncp-go v1.0.6-0.20230228002512-f4cd1740bebd h1:ZAXKeWKjkbS9QQh
github.com/nknorg/ncp-go v1.0.6-0.20230228002512-f4cd1740bebd/go.mod h1:T7ThlxmBjVIv3Ll3gJOHbQTuAFN3ZCYWvbux6JOX5wQ=
github.com/nknorg/nkn-sdk-go v1.4.5 h1:vIa5STqvGIAaz/pO38WXZeBxotZygMNAKJ3lQS13JPk=
github.com/nknorg/nkn-sdk-go v1.4.5/go.mod h1:Ix6w1Jfyoll/pzbrgeZqrvRDQl4iRTl4nGQ9EkffOrM=
github.com/nknorg/nkn-sdk-go v1.4.6-0.20230404044330-ad192f36d07e h1:lWKUEfqOJ9NImCX60Bden+Y6VgRDhlx4gc6B09S32RQ=
github.com/nknorg/nkn-sdk-go v1.4.6-0.20230404044330-ad192f36d07e/go.mod h1:mnI1+17p2cI+5wv+3CWRyCjSALqUg5k1jTaWC2h0f/M=
github.com/nknorg/nkn-tuna-session v0.2.6-0.20230328055742-9a596c57b4bb h1:g20T/mMqS7MYGEuBt6wgGWBmqAd3o2h5QDiN3653XpE=
github.com/nknorg/nkn-tuna-session v0.2.6-0.20230328055742-9a596c57b4bb/go.mod h1:NS+WEvWMPdzFl7NRJQI7LtI6BmXfcHu/gDPU1CxSL6w=
github.com/nknorg/nkn-tuna-session v0.2.6-0.20230415032955-7a3fc7be9634 h1:rJLBTXnxZM1Gi0KgjWtzQZ4T/Dbj1uB6uJAwmtNxx9I=
github.com/nknorg/nkn-tuna-session v0.2.6-0.20230415032955-7a3fc7be9634/go.mod h1:XTSeZ7WVFQ+bI8KJoJQ56CG8+9QUFZt/MKogrwHub4o=
github.com/nknorg/nkn-tunnel v0.3.5-0.20230328060135-8eb315c90047 h1:A/P6ulx+LDq4M28k1F71RNHQws+gThAQp2Zs717nLSU=
github.com/nknorg/nkn-tunnel v0.3.5-0.20230328060135-8eb315c90047/go.mod h1:BX8VcFR98VY+2Juu5bZFZnwuEmaf0M/Fu0FntM70pKU=
github.com/nknorg/nkn-tunnel v0.3.5-0.20230418225220-c505086e1505 h1:N/PPFap5KGs04UCiMBKxA28jMOCIxpIDZSZI+8EU5e8=
github.com/nknorg/nkn-tunnel v0.3.5-0.20230418225220-c505086e1505/go.mod h1:HLQCn2AtCNs+vHQwhlZW5Nz8BNaTq3vtp7dy7wbbpfg=
github.com/nknorg/nkn/v2 v2.1.7/go.mod h1:4xzrHJCI/FDFZmlt606Mn9ScKY4UUCFoaWydL1TzQRs=
github.com/nknorg/nkn/v2 v2.2.0 h1:sXOawvVF/T3bBTuWbzBCyrGuxldA3be+f+BDjoWcOEA=
github.com/nknorg/nkn/v2 v2.2.0/go.mod h1:yv3jkg0aOtN9BDHS4yerNSZJtJNBfGvlaD5K6wL6U3E=
Expand Down
14 changes: 6 additions & 8 deletions nconnct.go → nconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/nknorg/nconnect/util"
"github.com/nknorg/ncp-go"
"github.com/nknorg/nkn-sdk-go"
session "github.com/nknorg/nkn-tuna-session"
ts "github.com/nknorg/nkn-tuna-session"
tunnel "github.com/nknorg/nkn-tunnel"
"github.com/nknorg/nkn/v2/common"
"github.com/nknorg/nkn/v2/util/address"
Expand All @@ -37,7 +37,7 @@ const (
mtu = 1500
)

func Run(opts *config.NConfig) {
func Run(opts *config.Opts) {
err := (&opts.Config).SetPlatformSpecificDefaultValues()
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -194,8 +194,7 @@ func Run(opts *config.NConfig) {
opts.TunaMaxPrice = price
}

tsConfig := &session.Config{
NumTunaListeners: 1,
tsConfig := &ts.Config{
TunaMaxPrice: opts.TunaMaxPrice,
TunaMinNanoPayFee: opts.TunaMinFee,
TunaNanoPayFeeRatio: opts.TunaFeeRatio,
Expand All @@ -220,7 +219,8 @@ func Run(opts *config.NConfig) {
DialConfig: dialConfig,
TunaSessionConfig: tsConfig,
Verbose: opts.Verbose,
Udp: true,
UDP: opts.UDP,
UDPIdleTime: opts.UDPIdleTime,
}

port, err := util.GetFreePort()
Expand All @@ -237,6 +237,7 @@ func Run(opts *config.NConfig) {

Verbose: opts.Verbose,
UDPTimeout: config.DefaultUDPTimeout,
UDP: opts.UDP,
}

if opts.UDP && opts.Client {
Expand Down Expand Up @@ -395,9 +396,6 @@ func Run(opts *config.NConfig) {
if err != nil {
log.Fatal(err)
}
if opts.UDP {
ssConfig.UDP = true
}

ssConfig.Server = ssAddr

Expand Down
11 changes: 6 additions & 5 deletions tests/socks5_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/nknorg/nconnect"
"github.com/nknorg/nconnect/config"
"github.com/txthinking/brook"
"io"
"log"
"net/http"
"net/url"
"os"
"testing"
"time"

"github.com/nknorg/nconnect"
"github.com/nknorg/nconnect/config"
"github.com/txthinking/brook"
)

const (
Expand All @@ -25,7 +26,7 @@ func hello(w http.ResponseWriter, req *http.Request) {
}

func runServer() {
var serverConfig config.NConfig
var serverConfig config.Opts
b, err := os.ReadFile("server.json")
if err != nil {
log.Fatalf("read config file err: %v", err)
Expand All @@ -40,7 +41,7 @@ func runServer() {
}

func runClient() {
var clientConfig config.NConfig
var clientConfig config.Opts
b, err := os.ReadFile("client.json")
if err != nil {
log.Fatalf("read config file err: %v", err)
Expand Down

0 comments on commit 9b67a00

Please sign in to comment.