Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows tap device create failure #41

Merged
merged 2 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ 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
golang.org/x/sys v0.5.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,9 @@ golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
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)
}