mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
By attempting to reset DNS before starting new ctrld process. This way, ctrld will read the correct system DNS settings before changing itself. While at it, some optimizations are made: - "ctrld start" won't set DNS anymore, since "ctrld run" has already did this, start command could just query socket control server and emittin proper message to users. - The gateway won't be included as nameservers on Windows anymore, since the GetAdaptersAddresses Windows API always returns the correct DNS servers of the interfaces. - The nameservers list that OS resolver is using will be shown during ctrld startup, making it easier for debugging.
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package ctrld
|
|
|
|
import (
|
|
"net"
|
|
"syscall"
|
|
|
|
"golang.org/x/sys/windows"
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
|
)
|
|
|
|
func dnsFns() []dnsFn {
|
|
return []dnsFn{dnsFromAdapter}
|
|
}
|
|
|
|
func dnsFromAdapter() []string {
|
|
aas, err := winipcfg.GetAdaptersAddresses(syscall.AF_UNSPEC, winipcfg.GAAFlagIncludeGateways|winipcfg.GAAFlagIncludePrefix)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
ns := make([]string, 0, len(aas)*2)
|
|
seen := make(map[string]bool)
|
|
do := func(addr windows.SocketAddress) {
|
|
sa, err := addr.Sockaddr.Sockaddr()
|
|
if err != nil {
|
|
return
|
|
}
|
|
var ip net.IP
|
|
switch sa := sa.(type) {
|
|
case *syscall.SockaddrInet4:
|
|
ip = net.IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
|
|
case *syscall.SockaddrInet6:
|
|
ip = make(net.IP, net.IPv6len)
|
|
copy(ip, sa.Addr[:])
|
|
if ip[0] == 0xfe && ip[1] == 0xc0 {
|
|
// Ignore these fec0/10 ones. Windows seems to
|
|
// populate them as defaults on its misc rando
|
|
// interfaces.
|
|
return
|
|
}
|
|
default:
|
|
return
|
|
|
|
}
|
|
if ip.IsLoopback() || seen[ip.String()] {
|
|
return
|
|
}
|
|
seen[ip.String()] = true
|
|
ns = append(ns, ip.String())
|
|
}
|
|
for _, aa := range aas {
|
|
for dns := aa.FirstDNSServerAddress; dns != nil; dns = dns.Next {
|
|
do(dns.Address)
|
|
}
|
|
}
|
|
return ns
|
|
}
|
|
|
|
func nameserversFromResolvconf() []string {
|
|
return nil
|
|
}
|