Browse Source

Feature: use wintun for windows

pull/76/head
xjasonlyu 4 years ago
parent
commit
cc268bc2a3
  1. 5
      device/tun/offset_unix.go
  2. 3
      device/tun/offset_windows.go
  3. 13
      device/tun/opts_windows.go
  4. 4
      device/tun/tun_wg.go
  5. 65
      device/tun/tun_windows.go
  6. 18
      engine/parse.go
  7. 15
      engine/tun.go
  8. 33
      engine/tun_windows.go
  9. 1
      go.mod

5
device/tun/offset_unix.go

@ -0,0 +1,5 @@
// +build darwin freebsd openbsd
package tun
const offset = 4 /* 4 bytes TUN_PI */

3
device/tun/offset_windows.go

@ -0,0 +1,3 @@
package tun
const offset = 0

13
device/tun/opts_windows.go

@ -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
}
}

4
device/tun/tun_unix.go → 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

65
device/tun/tun_windows.go

@ -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()
}

18
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)
}
if err != nil {
return nil, err
return nil, fmt.Errorf("unsupported driver: %s", driver)
}
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) {

15
engine/tun.go

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

33
engine/tun_windows.go

@ -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),
)
}

1
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

Loading…
Cancel
Save