diff --git a/engine/parse.go b/engine/parse.go index ba109a4..44fc5fb 100644 --- a/engine/parse.go +++ b/engine/parse.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/xjasonlyu/tun2socks/device" - "github.com/xjasonlyu/tun2socks/device/tun" "github.com/xjasonlyu/tun2socks/proxy" ) @@ -22,14 +21,12 @@ func parseDevice(s string, mtu uint32) (device.Device, error) { return nil, err } - name := u.Host - driver := u.Scheme - var d device.Device + driver := strings.ToLower(u.Scheme) switch driver { case "tun": - d, err = tun.Open(tun.WithName(name), tun.WithMTU(mtu)) + d, err = openTUN(u, mtu) default: err = fmt.Errorf("unsupported driver: %s", driver) } diff --git a/engine/tun.go b/engine/tun.go new file mode 100644 index 0000000..cb9c989 --- /dev/null +++ b/engine/tun.go @@ -0,0 +1,15 @@ +// +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)) +} diff --git a/engine/tun_windows.go b/engine/tun_windows.go new file mode 100644 index 0000000..95b1acc --- /dev/null +++ b/engine/tun_windows.go @@ -0,0 +1,33 @@ +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), + ) +}