Skip to content

Commit

Permalink
Fix windows tap device create failure
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <zyl.skysniper@gmail.com>
  • Loading branch information
yilunzhang committed Jul 10, 2022
1 parent 3e37639 commit d3e25f2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 23 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ require (
github.com/nknorg/nkngomobile v0.0.0-20220125080321-848ddd2e5157
github.com/nknorg/tuna v0.0.0-20220314110655-e94a8631389d
github.com/shadowsocks/go-shadowsocks2 v0.1.2
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)
24 changes: 2 additions & 22 deletions tun_windows.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
package main

import (
"fmt"
"io"
"net"
"os/exec"

"github.com/songgao/water"
"github.com/eycorsican/go-tun2socks/tun"
)

const (
tunComponentID = "tap0901"
)

func OpenTunDevice(name, addr, gw, mask string, dnsServers []string, persist bool) (io.ReadWriteCloser, error) {
cmd := exec.Command("netsh", "interface", "ip", "set", "address", name, "static", addr, mask)
cmd.Run()
cmd = exec.Command("netsh", "interface", "ip", "set", "dns", name, "static", dnsServers[0])
cmd.Run()
if len(dnsServers) >= 2 {
cmd = exec.Command("netsh", "interface", "ip", "add", "dns", name, dnsServers[1], "index=2")
cmd.Run()
}
prefix, _ := net.IPMask(net.ParseIP(mask).To4()).Size()
network := fmt.Sprintf("%v/%v", addr, prefix)
return water.New(water.Config{
DeviceType: water.TUN,
PlatformSpecificParams: water.PlatformSpecificParams{
ComponentID: tunComponentID,
InterfaceName: name,
Network: network,
},
})
return tun.OpenTunDevice(name, addr, gw, mask, dnsServers, persist)
}

0 comments on commit d3e25f2

Please sign in to comment.