From cc268bc2a348f043a9a0dd347d469860d9b31cb9 Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Sat, 6 Feb 2021 20:01:11 +0800 Subject: [PATCH] Feature: use wintun for windows --- device/tun/offset_unix.go | 5 +++ device/tun/offset_windows.go | 3 ++ device/tun/opts_windows.go | 13 ------ device/tun/{tun_unix.go => tun_wg.go} | 4 +- device/tun/tun_windows.go | 65 --------------------------- engine/parse.go | 18 +++----- engine/tun.go | 15 ------- engine/tun_windows.go | 33 -------------- go.mod | 1 - 9 files changed, 16 insertions(+), 141 deletions(-) create mode 100644 device/tun/offset_unix.go create mode 100644 device/tun/offset_windows.go delete mode 100755 device/tun/opts_windows.go rename device/tun/{tun_unix.go => tun_wg.go} (94%) delete mode 100755 device/tun/tun_windows.go delete mode 100644 engine/tun.go delete mode 100644 engine/tun_windows.go diff --git a/device/tun/offset_unix.go b/device/tun/offset_unix.go new file mode 100644 index 0000000..c667634 --- /dev/null +++ b/device/tun/offset_unix.go @@ -0,0 +1,5 @@ +// +build darwin freebsd openbsd + +package tun + +const offset = 4 /* 4 bytes TUN_PI */ diff --git a/device/tun/offset_windows.go b/device/tun/offset_windows.go new file mode 100644 index 0000000..0d36697 --- /dev/null +++ b/device/tun/offset_windows.go @@ -0,0 +1,3 @@ +package tun + +const offset = 0 diff --git a/device/tun/opts_windows.go b/device/tun/opts_windows.go deleted file mode 100755 index ea0bd29..0000000 --- a/device/tun/opts_windows.go +++ /dev/null @@ -1,13 +0,0 @@ -package tun - -func WithComponentID(componentID string) Option { - return func(t *TUN) { - t.componentID = componentID - } -} - -func WithNetwork(network string) Option { - return func(t *TUN) { - t.network = network - } -} diff --git a/device/tun/tun_unix.go b/device/tun/tun_wg.go similarity index 94% rename from device/tun/tun_unix.go rename to device/tun/tun_wg.go index 21c738e..d7bfb4b 100755 --- a/device/tun/tun_unix.go +++ b/device/tun/tun_wg.go @@ -1,4 +1,4 @@ -// +build darwin freebsd openbsd +// +build !linux package tun @@ -12,8 +12,6 @@ import ( "golang.zx2c4.com/wireguard/tun" ) -const offset = 4 /* 4 bytes TUN_PI */ - type TUN struct { *rwbased.Endpoint diff --git a/device/tun/tun_windows.go b/device/tun/tun_windows.go deleted file mode 100755 index 425130a..0000000 --- a/device/tun/tun_windows.go +++ /dev/null @@ -1,65 +0,0 @@ -package tun - -import ( - "fmt" - - "github.com/xjasonlyu/tun2socks/device" - "github.com/xjasonlyu/tun2socks/device/rwbased" - - "github.com/songgao/water" -) - -const defaultMTU = 1500 - -type TUN struct { - *rwbased.Endpoint - - iface *water.Interface - mtu uint32 - name string - - // windows only - componentID string - network string -} - -func Open(opts ...Option) (device.Device, error) { - t := &TUN{} - - for _, opt := range opts { - opt(t) - } - - iface, err := water.New(water.Config{ - DeviceType: water.TUN, - PlatformSpecificParams: water.PlatformSpecificParams{ - ComponentID: t.componentID, - InterfaceName: t.name, - Network: t.network, - }, - }) - if err != nil { - return nil, fmt.Errorf("create tun: %w", err) - } - t.iface = iface - - if t.mtu == 0 { - t.mtu = defaultMTU - } - - ep, err := rwbased.New(iface, t.mtu) - if err != nil { - return nil, fmt.Errorf("create endpoint: %w", err) - } - t.Endpoint = ep - - return t, nil -} - -func (t *TUN) Name() string { - return t.name -} - -func (t *TUN) Close() error { - return t.iface.Close() -} diff --git a/engine/parse.go b/engine/parse.go index 838559a..8fd095a 100644 --- a/engine/parse.go +++ b/engine/parse.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/xjasonlyu/tun2socks/device" + "github.com/xjasonlyu/tun2socks/device/tun" "github.com/xjasonlyu/tun2socks/proxy" ) @@ -21,20 +22,15 @@ func parseDevice(s string, mtu uint32) (device.Device, error) { return nil, err } - var d device.Device - + name := u.Host driver := strings.ToLower(u.Scheme) + switch driver { case "tun": - d, err = openTUN(u, mtu) + return tun.Open(tun.WithName(name), tun.WithMTU(mtu)) default: - err = fmt.Errorf("unsupported driver: %s", driver) + return nil, fmt.Errorf("unsupported driver: %s", driver) } - if err != nil { - return nil, err - } - - return d, nil } func parseProxy(s string) (proxy.Proxy, error) { @@ -57,9 +53,9 @@ func parseProxy(s string) (proxy.Proxy, error) { return proxy.NewSocks5(parseSocks(u)) case "ss": return proxy.NewShadowsocks(parseShadowsocks(u)) + default: + return nil, fmt.Errorf("unsupported protocol: %s", proto) } - - return nil, fmt.Errorf("unsupported protocol: %s", proto) } func parseSocks(u *url.URL) (address, username, password string) { diff --git a/engine/tun.go b/engine/tun.go deleted file mode 100644 index cb9c989..0000000 --- a/engine/tun.go +++ /dev/null @@ -1,15 +0,0 @@ -// +build !windows - -package engine - -import ( - "net/url" - - "github.com/xjasonlyu/tun2socks/device" - "github.com/xjasonlyu/tun2socks/device/tun" -) - -func openTUN(u *url.URL, mtu uint32) (device.Device, error) { - name := u.Host - return tun.Open(tun.WithName(name), tun.WithMTU(mtu)) -} diff --git a/engine/tun_windows.go b/engine/tun_windows.go deleted file mode 100644 index 95b1acc..0000000 --- a/engine/tun_windows.go +++ /dev/null @@ -1,33 +0,0 @@ -package engine - -import ( - "net/url" - - "github.com/xjasonlyu/tun2socks/device" - "github.com/xjasonlyu/tun2socks/device/tun" -) - -func openTUN(u *url.URL, mtu uint32) (device.Device, error) { - /* - e.g. tun://TUN0/?id=tap0901&network=10.10.10.10/24 - */ - - name := u.Host - - componentID := u.Query().Get("id") - network := u.Query().Get("network") - - if componentID == "" { - componentID = "tap0901" /* default */ - } - if network == "" { - network = "10.10.10.10/24" /* default */ - } - - return tun.Open( - tun.WithName(name), - tun.WithMTU(mtu), - tun.WithComponentID(componentID), - tun.WithNetwork(network), - ) -} diff --git a/go.mod b/go.mod index 722e085..b538648 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/gofrs/uuid v4.0.0+incompatible github.com/gorilla/websocket v1.4.2 github.com/sirupsen/logrus v1.7.0 - github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.5.1 go.uber.org/atomic v1.7.0