mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-06 09:16:58 +08:00
Feature: use wintun for windows
This commit is contained in:
5
device/tun/offset_unix.go
Normal file
5
device/tun/offset_unix.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
// +build darwin freebsd openbsd
|
||||||
|
|
||||||
|
package tun
|
||||||
|
|
||||||
|
const offset = 4 /* 4 bytes TUN_PI */
|
3
device/tun/offset_windows.go
Normal file
3
device/tun/offset_windows.go
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package tun
|
||||||
|
|
||||||
|
const offset = 0
|
@@ -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
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,4 +1,4 @@
|
|||||||
// +build darwin freebsd openbsd
|
// +build !linux
|
||||||
|
|
||||||
package tun
|
package tun
|
||||||
|
|
||||||
@@ -12,8 +12,6 @@ import (
|
|||||||
"golang.zx2c4.com/wireguard/tun"
|
"golang.zx2c4.com/wireguard/tun"
|
||||||
)
|
)
|
||||||
|
|
||||||
const offset = 4 /* 4 bytes TUN_PI */
|
|
||||||
|
|
||||||
type TUN struct {
|
type TUN struct {
|
||||||
*rwbased.Endpoint
|
*rwbased.Endpoint
|
||||||
|
|
@@ -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()
|
|
||||||
}
|
|
@@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/xjasonlyu/tun2socks/device"
|
"github.com/xjasonlyu/tun2socks/device"
|
||||||
|
"github.com/xjasonlyu/tun2socks/device/tun"
|
||||||
"github.com/xjasonlyu/tun2socks/proxy"
|
"github.com/xjasonlyu/tun2socks/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,20 +22,15 @@ func parseDevice(s string, mtu uint32) (device.Device, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var d device.Device
|
name := u.Host
|
||||||
|
|
||||||
driver := strings.ToLower(u.Scheme)
|
driver := strings.ToLower(u.Scheme)
|
||||||
|
|
||||||
switch driver {
|
switch driver {
|
||||||
case "tun":
|
case "tun":
|
||||||
d, err = openTUN(u, mtu)
|
return tun.Open(tun.WithName(name), tun.WithMTU(mtu))
|
||||||
default:
|
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) {
|
func parseProxy(s string) (proxy.Proxy, error) {
|
||||||
@@ -57,9 +53,9 @@ func parseProxy(s string) (proxy.Proxy, error) {
|
|||||||
return proxy.NewSocks5(parseSocks(u))
|
return proxy.NewSocks5(parseSocks(u))
|
||||||
case "ss":
|
case "ss":
|
||||||
return proxy.NewShadowsocks(parseShadowsocks(u))
|
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) {
|
func parseSocks(u *url.URL) (address, username, password string) {
|
||||||
|
@@ -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))
|
|
||||||
}
|
|
@@ -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
1
go.mod
@@ -10,7 +10,6 @@ require (
|
|||||||
github.com/gofrs/uuid v4.0.0+incompatible
|
github.com/gofrs/uuid v4.0.0+incompatible
|
||||||
github.com/gorilla/websocket v1.4.2
|
github.com/gorilla/websocket v1.4.2
|
||||||
github.com/sirupsen/logrus v1.7.0
|
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/spf13/pflag v1.0.5
|
||||||
github.com/stretchr/testify v1.5.1
|
github.com/stretchr/testify v1.5.1
|
||||||
go.uber.org/atomic v1.7.0
|
go.uber.org/atomic v1.7.0
|
||||||
|
Reference in New Issue
Block a user