Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade nkn tunnel to support udp #53

Merged
merged 5 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
20 changes: 20 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/nknorg/nconnect/util"
"github.com/nknorg/nkn/v2/common"
"github.com/nknorg/tuna/types"
)

const (
Expand All @@ -23,6 +24,7 @@ const (
DefaultTunNameLinux = "nConnect-tun0"
DefaultTunNameNonLinux = "nConnect-tap0"
FallbackTunaMaxPrice = "0.01"
DefaultUDPTimeout = time.Hour * 720
)

var (
Expand All @@ -33,6 +35,20 @@ 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"`

TunaNode *types.Node
}

type Config struct {
path string

Expand Down Expand Up @@ -94,6 +110,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
75 changes: 70 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,85 @@
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
)
Loading