tun: add Windows support
This commit is contained in:
parent
4a9731a512
commit
c3ca971f6c
@ -1,5 +1,3 @@
|
|||||||
// +build linux
|
|
||||||
|
|
||||||
package gost
|
package gost
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
15
tun_win.go
15
tun_win.go
@ -1,15 +0,0 @@
|
|||||||
// +build windows
|
|
||||||
|
|
||||||
package gost
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
//TODO: wintun for Windows: https://www.wintun.net/
|
|
||||||
// https://godoc.org/golang.zx2c4.com/wireguard/tun/wintun
|
|
||||||
func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
|
||||||
err = errors.New("tun is not supported on Windows")
|
|
||||||
return
|
|
||||||
}
|
|
51
tun_windows.go
Normal file
51
tun_windows.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package gost
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/go-log/log"
|
||||||
|
"github.com/songgao/water"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
||||||
|
ip, ipNet, err := net.ParseCIDR(cfg.Addr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ifce, err := water.New(water.Config{
|
||||||
|
DeviceType: water.TUN,
|
||||||
|
PlatformSpecificParams: water.PlatformSpecificParams{
|
||||||
|
ComponentID: "tap0901",
|
||||||
|
InterfaceName: cfg.Name,
|
||||||
|
Network: cfg.Addr,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
|
||||||
|
"source=static addr=%s mask=%s gateway=none",
|
||||||
|
ifce.Name(), ip.String(), ipMask(ipNet.Mask))
|
||||||
|
log.Log("[tun]", cmd)
|
||||||
|
if er := exec.Command("netsh",
|
||||||
|
"interface", "ip", "set", "address",
|
||||||
|
"name="+ifce.Name(), "source=static",
|
||||||
|
"addr="+ip.String(), "mask="+ipMask(ipNet.Mask), "gateway=none").Run(); er != nil {
|
||||||
|
err = fmt.Errorf("%s: %v", cmd, er)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conn = &tunConn{
|
||||||
|
ifce: ifce,
|
||||||
|
addr: &net.IPAddr{IP: ip},
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ipMask(mask net.IPMask) string {
|
||||||
|
return fmt.Sprintf("%d.%d.%d.%d", mask[0], mask[1], mask[2], mask[3])
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user