Skip to content

Commit

Permalink
add udp support
Browse files Browse the repository at this point in the history
  • Loading branch information
bufrr authored and billfort committed Apr 19, 2023
1 parent 9ddaddf commit 337540f
Show file tree
Hide file tree
Showing 18 changed files with 498 additions and 677 deletions.
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ XGO_MODULE=github.com/nknorg/nconnect
XGO_BUILD=xgo -ldflags $(LDFLAGS) --targets=$(XGO_TARGET) $(XGOFLAGS)
BUILD_DIR=build
BIN_NAME=nConnect
MAIN=bin/main.go
ifdef GOARM
BIN_DIR=$(GOOS)-$(GOARCH)v$(GOARM)
XGO_TARGET=$(GOOS)/$(GOARCH)-$(GOARM)
Expand All @@ -21,7 +22,7 @@ web/dist: $(shell find web/src -type f -not -path "web/src/node_modules/*" -not

.PHONY: local
local: web/dist
$(BUILD) -o $(BIN_NAME)$(EXT) .
$(BUILD) -o $(BIN_NAME)$(EXT) $(MAIN)

.PHONY: local_with_proxy
local_with_proxy: web/dist
Expand Down
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![GitHub license](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE) [![Go Report Card](https://goreportcard.com/badge/github.com/nknorg/nconnect)](https://goreportcard.com/report/github.com/nknorg/nconnect) [![Build Status](https://travis-ci.org/nknorg/nconnect.svg?branch=master)](https://travis-ci.org/nknorg/nconnect) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](#contributing)

nConnect allows you to securely connect to remote machines without the need of
any server, public IP address, or publicly exposed ports. It features end to end
any server, public IP address, or publicly exposed ports. It features end-to-end
encryption for top level security, and multi-path aggregation for maximum
throughput.

Expand Down Expand Up @@ -63,7 +63,7 @@ The minimal arguments to start nConnect in server mode is just
./nConnect -s
```

But most of the times you might want to start nConnect server with a few useful
But most of the time you might want to start nConnect server with a few useful
arguments:

```shell
Expand All @@ -79,7 +79,7 @@ arguments:
`http://127.0.0.1:8001`. You can visit this address in your browser to change
various config (e.g. access control), bind with nConnect mobile client, etc.
Do not make this port public as anyone who can access this endpoint can change
your configuration. If you want best security, disable the admin dashboard
your configuration. If you want the best security, disable the admin dashboard
once you have done using it.

#### Access Control
Expand Down Expand Up @@ -155,7 +155,7 @@ In the console you should see one or more `Adding route <local-ip>/32`. You can
then connect to server machine using any one of these local IP addresses as if
they are in the same local network, e.g. `ssh user@<local-ip>`.

By default all local IP addresses on the server machine will be added to routes,
By default, all local IP addresses on the server machine will be added to routes,
but you can manually specify which IP or IP range you would like to route
through the VPN using `--vpn-route` arguments. Use `./nConnect -h` for all available arguments.

Expand Down Expand Up @@ -184,7 +184,7 @@ connections routed via this device will be tunneled to nConnect server. You will
need to modify system routing table yourself to determine what traffic should be
routed through the TUN device.

You can also change the name, IP, etc of the TUN device. Use `./nConnect -h` for
You can also change the name, IP, gateway, network mask and DNS resolvers of the TUN device. Use `./nConnect -h` for
all available arguments.

If you start multiple nConnect clients in TUN device mode, make sure to use
Expand Down Expand Up @@ -222,13 +222,26 @@ nConnect server side. You can get your client address using:
The address typically contains one or more dot, with the part after last dot
being your client public key.

### UDP support

You can enable UDP support when starting nConnect server with tuna mode,

```shell
./nConnect -s --tuna --udp
```

### Use nConnect as library

You can also use nConnect as library. Please check [socks5_proxy_test.go](tests/socks5_proxy_test.go) for usages.


### Use pre-built Docker image

*Prerequirement*: Have working docker software installed. For help with that
*Pre-requirement*: Have working docker software installed. For help with that
*visit [official docker
*docs](https://docs.docker.com/install/#supported-platforms)

We host latest Docker image on our official Docker Hub account. You can get
We host the latest Docker image on our official Docker Hub account. You can get
it by

```shell
Expand Down
6 changes: 3 additions & 3 deletions route_darwin.go → arch/route_darwin.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main
package arch

import (
"net"
"os/exec"
)

func addRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
func AddRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
b, err := exec.Command("route", "-n", "add", "-net", dest.String(), gateway).Output()
if err == nil {
return b, nil
}
return exec.Command("route", "-n", "change", "-net", dest.String(), gateway).Output()
}

func deleteRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
func DeleteRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
return exec.Command("route", "-n", "delete", "-net", dest.String(), gateway).Output()
}
6 changes: 3 additions & 3 deletions route_linux.go → arch/route_linux.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main
package arch

import (
"net"
"os/exec"
)

func addRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
func AddRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
out, err := exec.Command("ip", "route", "add", dest.String(), "via", gateway, "dev", devName).Output()
if err == nil {
return out, nil
Expand All @@ -21,7 +21,7 @@ func addRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
return exec.Command("route", "-n", "change", dest.String(), "gw", gateway).Output()
}

func deleteRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
func DeleteRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
out, err := exec.Command("ip", "route", "del", dest.String(), "via", gateway, "dev", devName).Output()
if err == nil {
return out, nil
Expand Down
6 changes: 3 additions & 3 deletions route_windows.go → arch/route_windows.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main
package arch

import (
"net"
"os/exec"
)

func addRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
func AddRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
out, err := exec.Command("netsh", "interface", "ipv4", "add", "route", dest.String(), "nexthop="+gateway, "interface="+devName, "metric=0", "store=active").Output()
if err == nil {
return out, nil
}
return exec.Command("netsh", "interface", "ipv4", "set", "route", dest.String(), "nexthop="+gateway, "interface="+devName, "metric=0", "store=active").Output()
}

func deleteRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
func DeleteRouteCmd(dest *net.IPNet, gateway, devName string) ([]byte, error) {
return exec.Command("netsh", "interface", "ipv4", "delete", "route", dest.String(), "interface="+devName).Output()
}
2 changes: 1 addition & 1 deletion tun_darwin.go → arch/tun_darwin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package arch

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion tun_linux.go → arch/tun_linux.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package arch

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion tun_windows.go → arch/tun_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package arch

import (
"io"
Expand Down
28 changes: 28 additions & 0 deletions bin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"log"
"os"

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

func main() {
defer func() {
if r := recover(); r != nil {
log.Fatalf("Panic: %+v", r)
}
}()

var opts config.Opts
_, err := flags.Parse(&opts)
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
log.Fatal(err)
}
nconnect.Run(&opts)
}
19 changes: 17 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
DefaultTunNameLinux = "nConnect-tun0"
DefaultTunNameNonLinux = "nConnect-tap0"
FallbackTunaMaxPrice = "0.01"
DefaultUDPTimeout = time.Hour * 720
)

var (
Expand All @@ -33,6 +34,18 @@ func init() {
rand.Seed(time.Now().UnixNano())
}

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

Config
ConfigFile string `short:"f" long:"config-file" default:"config.json" description:"Config file path"`

Address bool `long:"address" description:"Print client address (client mode) or admin address (server mode)"`
WalletAddress bool `long:"wallet-address" description:"Print wallet address (server only)"`
Version bool `long:"version" description:"Print version"`
}

type Config struct {
path string

Expand All @@ -45,8 +58,10 @@ type Config struct {
ConnectRetries int32 `json:"connectRetries,omitempty" long:"connect-retries" description:"client connect retries, a negative value means unlimited retries."`

// 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"`
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"`
UDPIdleTime int32 `json:"udpIdleTime,omitempty" long:"udp-idle-time" description:"UDP connection to be purges after idle time, in second. Value 0 is no purge." default:"0"`

// Session config
DialTimeout int32 `json:"dialTimeout,omitempty" long:"dial-timeout" description:"dial timeout in milliseconds"`
Expand Down
81 changes: 76 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,91 @@
module github.com/nknorg/nconnect

go 1.13
go 1.19

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
github.com/shadowsocks/go-shadowsocks2 v0.1.2
github.com/txthinking/brook v0.0.0-20230418095906-76ced63f1803
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/gaukas/godicttls v0.0.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/itchyny/base58-go v0.2.1 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/krolaw/dhcp4 v0.0.0-20190909130307-a50d88189771 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/miekg/dns v1.1.51 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/nknorg/encrypted-stream v1.0.2-0.20230320101720-9891f770de86 // indirect
github.com/onsi/ginkgo/v2 v2.2.0 // indirect
github.com/oschwald/geoip2-golang v1.4.0 // indirect
github.com/oschwald/maxminddb-golang v1.6.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/phuslu/iploc v1.0.20230201 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/quic-go/qtls-go1-18 v0.2.0 // indirect
github.com/quic-go/qtls-go1-19 v0.2.0 // indirect
github.com/quic-go/qtls-go1-20 v0.1.0 // indirect
github.com/quic-go/quic-go v0.32.0 // indirect
github.com/rdegges/go-ipify v0.0.0-20150526035502-2d94a6a86c40 // indirect
github.com/refraction-networking/utls v1.3.2 // indirect
github.com/riobard/go-bloom v0.0.0-20200213042214-218e1707c495 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b // indirect
github.com/tdewolff/minify v2.3.6+incompatible // indirect
github.com/tdewolff/parse v2.3.4+incompatible // indirect
github.com/txthinking/crypto v0.0.0-20210716135230-de9624a415a4 // indirect
github.com/txthinking/runnergroup v0.0.0-20230211072751-d11f16258c86 // indirect
github.com/txthinking/socks5 v0.0.0-20230307062227-0e1677eca4ba // indirect
github.com/txthinking/x v0.0.0-20220929041811-1b4d914e9133 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/urfave/negroni v1.0.0 // indirect
github.com/xtaci/smux v2.0.1+incompatible // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.6.0 // indirect
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
Loading

0 comments on commit 337540f

Please sign in to comment.