mihomo/dns/system_windows.go

78 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-04-26 15:57:55 +08:00
//go:build windows
package dns
2023-05-01 00:30:47 +08:00
import (
"net"
"os"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
2023-06-01 12:36:53 +08:00
func dnsReadConfig() (servers []string, err error) {
2023-05-01 00:30:47 +08:00
aas, err := adapterAddresses()
if err != nil {
2023-06-01 12:36:53 +08:00
return
2023-05-01 00:30:47 +08:00
}
for _, aa := range aas {
for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next {
sa, err := dns.Address.Sockaddr.Sockaddr()
if err != nil {
continue
}
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:
2023-06-01 12:36:53 +08:00
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.
continue
}
//continue
2023-05-01 00:30:47 +08:00
default:
// Unexpected type.
continue
}
2023-06-01 12:36:53 +08:00
servers = append(servers, ip.String())
2023-05-01 00:30:47 +08:00
}
}
2023-06-01 12:36:53 +08:00
return
2023-05-01 00:30:47 +08:00
}
// adapterAddresses returns a list of IP adapter and address
// structures. The structure contains an IP adapter and flattened
// multiple IP addresses including unicast, anycast and multicast
// addresses.
func adapterAddresses() ([]*windows.IpAdapterAddresses, error) {
var b []byte
l := uint32(15000) // recommended initial size
for {
b = make([]byte, l)
err := windows.GetAdaptersAddresses(syscall.AF_UNSPEC, windows.GAA_FLAG_INCLUDE_PREFIX, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l)
if err == nil {
if l == 0 {
return nil, nil
}
break
}
if err.(syscall.Errno) != syscall.ERROR_BUFFER_OVERFLOW {
return nil, os.NewSyscallError("getadaptersaddresses", err)
}
if l <= uint32(len(b)) {
return nil, os.NewSyscallError("getadaptersaddresses", err)
}
}
var aas []*windows.IpAdapterAddresses
for aa := (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])); aa != nil; aa = aa.Next {
aas = append(aas, aa)
}
return aas, nil
2023-04-26 15:57:55 +08:00
}