Feature: use wintun for windows

This commit is contained in:
xjasonlyu
2021-02-06 20:01:11 +08:00
parent b79ab7335a
commit cc268bc2a3
9 changed files with 16 additions and 141 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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
View File

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