From 631fa59182e1de93228439101f3447429518c68c Mon Sep 17 00:00:00 2001 From: Jason Lyu Date: Fri, 29 Sep 2023 04:36:29 +0800 Subject: [PATCH] Feature: persistent wintun with GUID option (#301) Fixes: #300 --- engine/parse.go | 9 ++++++--- engine/parse_unix.go | 14 ++++++++++++++ engine/parse_windows.go | 34 ++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 engine/parse_unix.go create mode 100644 engine/parse_windows.go diff --git a/engine/parse.go b/engine/parse.go index c413cc7..7bb09bb 100644 --- a/engine/parse.go +++ b/engine/parse.go @@ -51,19 +51,22 @@ func parseDevice(s string, mtu uint32) (device.Device, error) { return nil, err } - name := u.Host driver := strings.ToLower(u.Scheme) switch driver { case fdbased.Driver: - return fdbased.Open(name, mtu, 0) + return parseFD(u, mtu) case tun.Driver: - return tun.Open(name, mtu) + return parseTUN(u, mtu) default: return nil, fmt.Errorf("unsupported driver: %s", driver) } } +func parseFD(u *url.URL, mtu uint32) (device.Device, error) { + return fdbased.Open(u.Host, mtu, 0) +} + func parseProxy(s string) (proxy.Proxy, error) { if !strings.Contains(s, "://") { s = fmt.Sprintf("%s://%s", proto.Socks5 /* default protocol */, s) diff --git a/engine/parse_unix.go b/engine/parse_unix.go new file mode 100644 index 0000000..6382c7e --- /dev/null +++ b/engine/parse_unix.go @@ -0,0 +1,14 @@ +//go:build unix + +package engine + +import ( + "net/url" + + "github.com/xjasonlyu/tun2socks/v2/core/device" + "github.com/xjasonlyu/tun2socks/v2/core/device/tun" +) + +func parseTUN(u *url.URL, mtu uint32) (device.Device, error) { + return tun.Open(u.Host, mtu) +} diff --git a/engine/parse_windows.go b/engine/parse_windows.go new file mode 100644 index 0000000..478a9f6 --- /dev/null +++ b/engine/parse_windows.go @@ -0,0 +1,34 @@ +package engine + +import ( + "net/url" + + "github.com/gorilla/schema" + "golang.org/x/sys/windows" + wun "golang.zx2c4.com/wireguard/tun" + + "github.com/xjasonlyu/tun2socks/v2/core/device" + "github.com/xjasonlyu/tun2socks/v2/core/device/tun" + "github.com/xjasonlyu/tun2socks/v2/internal/version" +) + +func init() { + wun.WintunTunnelType = version.Name +} + +func parseTUN(u *url.URL, mtu uint32) (device.Device, error) { + opts := struct { + GUID string + }{} + if err := schema.NewDecoder().Decode(&opts, u.Query()); err != nil { + return nil, err + } + if opts.GUID != "" { + guid, err := windows.GUIDFromString(opts.GUID) + if err != nil { + return nil, err + } + wun.WintunStaticRequestedGUID = &guid + } + return tun.Open(u.Host, mtu) +} diff --git a/go.mod b/go.mod index e296ad1..9d19368 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/go-chi/cors v1.2.1 github.com/go-chi/render v1.0.3 github.com/google/uuid v1.3.1 + github.com/gorilla/schema v1.2.0 github.com/gorilla/websocket v1.5.0 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.7.1 diff --git a/go.sum b/go.sum index 5ad05f8..486f751 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU= github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc= +github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=