1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package main
import ( "flag" "fmt" "net/netip" "os" "os/exec"
"golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" )
func main() { flag.Parse()
if flag.NArg() < 1 { fmt.Println("please input ip with mask x.x.x.x/xx") os.Exit(1) } addrWithMask := flag.Arg(0)
dev, err := tun.CreateTUN("MyNIC", 0) if err != nil { panic(err) } defer dev.Close()
devName, err := dev.Name() if err != nil { panic(err) }
nativeTunDevice := dev.(*tun.NativeTun)
link := winipcfg.LUID(nativeTunDevice.LUID())
ip, err := netip.ParsePrefix(addrWithMask) if err != nil { panic(err) }
err = link.SetIPAddresses([]netip.Prefix{ip}) if err != nil { panic(err) }
cmd := exec.Command("netsh", "interface", "ip", "set", "interface", devName, "metric=1") fmt.Println(cmd) if err = cmd.Run(); err != nil { panic(err) }
select {} }
|