1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-09 10:26:02 +08:00

fix tun for windows

This commit is contained in:
fumiama
2021-12-29 20:00:45 +08:00
parent bfffcdd547
commit e6b19e093b
5 changed files with 105 additions and 6 deletions

8
lower/config_unix.go Normal file
View File

@@ -0,0 +1,8 @@
//go:build !windows
// +build !windows
package lower
import "github.com/songgao/water"
var tuncfg = water.Config{DeviceType: water.TUN}

15
lower/config_windows.go Normal file
View File

@@ -0,0 +1,15 @@
//go:build windows
// +build windows
package lower
import "github.com/songgao/water"
var tuncfg = water.Config{
DeviceType: water.TUN,
PlatformSpecificParams: water.PlatformSpecificParams{
ComponentID: "root\\tap0901",
InterfaceName: "OpenVPN TAP-Windows6",
Network: "192.168.233.0/24",
},
}

View File

@@ -23,7 +23,7 @@ type NIC struct {
// NewNIC 新建 TUN 网络接口卡
// 网卡地址为 ip, 所属子网为 subnet
func NewNIC(ip, subnet string) (n *NIC) {
ifce, err := water.New(water.Config{DeviceType: water.TUN})
ifce, err := water.New(tuncfg)
if err != nil {
panic(err)
}

View File

@@ -3,16 +3,22 @@
package lower
import "net"
func (n *NIC) prepare() {
execute("/sbin/ip", "link", "set", "dev", ifcename, "mtu", "1500")
execute("/sbin/ip", "addr", "add", ip, "dev", ifcename)
execute("/sbin/ip", "route", "add", subnet, "dev", ifcename)
_, ipn, err := net.ParseCIDR(n.subnet)
if err != nil {
panic(err)
}
execute("cmd", "/c", "netsh interface ip set address name=\""+n.ifce.Name()+"\" source=static addr=\""+n.ip+"\" mask=\""+(net.IP)(ipn.Mask).String()+"\" gateway=none")
}
func (n *NIC) Up() {
execute("/sbin/ip", "link", "set", "dev", ifcename, "up")
// execute("netsh", "interface", "set", "interface", n.ifce.Name(), "enabled")
// don't need to bring up the device by hand
}
func (n *NIC) Down() {
execute("/sbin/ip", "link", "set", "dev", ifcename, "down")
// execute("netsh", "interface", "set", "interface", n.ifce.Name(), "disabled")
// don't need to bring up the device by hand
}