Feature: windows tun more params support

Supported params:

- Name
- MTU
- ComponentID
- Network
This commit is contained in:
xjasonlyu
2021-02-06 16:20:45 +08:00
parent 38b9357469
commit b1700c62f4
3 changed files with 50 additions and 5 deletions

View File

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

15
engine/tun.go Normal file
View File

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

33
engine/tun_windows.go Normal file
View File

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