Skip to content

Commit

Permalink
Fix set windows dns
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 12, 2024
1 parent 321c2d2 commit 7c2abbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
20 changes: 12 additions & 8 deletions tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ func (o *Options) Inet4GatewayAddr() netip.Addr {
case "darwin":
return o.Inet4Address[0].Addr()
default:
if HasNextAddress(o.Inet4Address[0], 1) {
return o.Inet4Address[0].Addr().Next()
} else {
return o.Inet4Address[0].Addr()
if !o.InterfaceScope {

Check failure on line 101 in tun.go

View workflow job for this annotation

GitHub Actions / macOS

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 101 in tun.go

View workflow job for this annotation

GitHub Actions / Build

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 101 in tun.go

View workflow job for this annotation

GitHub Actions / Linux

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 101 in tun.go

View workflow job for this annotation

GitHub Actions / Linux (Go 1.21)

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 101 in tun.go

View workflow job for this annotation

GitHub Actions / Linux (Go 1.20)

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 101 in tun.go

View workflow job for this annotation

GitHub Actions / Linux (Go 1.22)

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 101 in tun.go

View workflow job for this annotation

GitHub Actions / Windows

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)
if HasNextAddress(o.Inet4Address[0], 1) {
return o.Inet4Address[0].Addr().Next()
} else {
return o.Inet4Address[0].Addr()
}
}
}
}
Expand All @@ -122,10 +124,12 @@ func (o *Options) Inet6GatewayAddr() netip.Addr {
case "darwin":
return o.Inet6Address[0].Addr()
default:
if HasNextAddress(o.Inet6Address[0], 1) {
return o.Inet6Address[0].Addr().Next()
} else {
return o.Inet6Address[0].Addr()
if !o.InterfaceScope {

Check failure on line 127 in tun.go

View workflow job for this annotation

GitHub Actions / macOS

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 127 in tun.go

View workflow job for this annotation

GitHub Actions / Build

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope) (typecheck)

Check failure on line 127 in tun.go

View workflow job for this annotation

GitHub Actions / Linux

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 127 in tun.go

View workflow job for this annotation

GitHub Actions / Linux (Go 1.21)

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 127 in tun.go

View workflow job for this annotation

GitHub Actions / Linux (Go 1.20)

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 127 in tun.go

View workflow job for this annotation

GitHub Actions / Linux (Go 1.22)

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)

Check failure on line 127 in tun.go

View workflow job for this annotation

GitHub Actions / Windows

o.InterfaceScope undefined (type *Options has no field or method InterfaceScope)
if HasNextAddress(o.Inet6Address[0], 1) {
return o.Inet6Address[0].Addr().Next()
} else {
return o.Inet6Address[0].Addr()
}
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (t *NativeTun) configure() error {
if err != nil {
return E.Cause(err, "set ipv4 address")
}
if !t.options.EXP_DisableDNSHijack {
if t.options.AutoRoute && !t.options.EXP_DisableDNSHijack {
dnsServers := common.Filter(t.options.DNSServers, netip.Addr.Is4)
if len(dnsServers) == 0 && HasNextAddress(t.options.Inet4Address[0], 1) {
dnsServers = []netip.Addr{t.options.Inet4Address[0].Addr().Next()}
Expand All @@ -83,14 +83,19 @@ func (t *NativeTun) configure() error {
return E.Cause(err, "set ipv4 dns")
}
}
} else {
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET), nil, nil)
if err != nil {
return E.Cause(err, "set ipv4 dns")
}
}
}
if len(t.options.Inet6Address) > 0 {
err := luid.SetIPAddressesForFamily(winipcfg.AddressFamily(windows.AF_INET6), t.options.Inet6Address)
if err != nil {
return E.Cause(err, "set ipv6 address")
}
if !t.options.EXP_DisableDNSHijack {
if t.options.AutoRoute && !t.options.EXP_DisableDNSHijack {
dnsServers := common.Filter(t.options.DNSServers, netip.Addr.Is6)
if len(dnsServers) == 0 && HasNextAddress(t.options.Inet6Address[0], 1) {
dnsServers = []netip.Addr{t.options.Inet6Address[0].Addr().Next()}
Expand All @@ -101,6 +106,11 @@ func (t *NativeTun) configure() error {
return E.Cause(err, "set ipv6 dns")
}
}
} else {
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET6), nil, nil)
if err != nil {
return E.Cause(err, "set ipv6 dns")
}
}
}
if len(t.options.Inet4Address) > 0 || len(t.options.Inet6Address) > 0 {
Expand Down

0 comments on commit 7c2abbd

Please sign in to comment.