Browse Source

Feature: persistent wintun with GUID option (#301)

Fixes: #300
pull/302/head
Jason Lyu 1 year ago
committed by GitHub
parent
commit
631fa59182
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      engine/parse.go
  2. 14
      engine/parse_unix.go
  3. 34
      engine/parse_windows.go
  4. 1
      go.mod
  5. 2
      go.sum

9
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)

14
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)
}

34
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)
}

1
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

2
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=

Loading…
Cancel
Save