diff --git a/dns/system.go b/dns/system.go index 2e52f90f..f5ab0efb 100644 --- a/dns/system.go +++ b/dns/system.go @@ -1,37 +1,21 @@ -//go:build !windows - package dns import ( - "fmt" - "os" - "regexp" -) - -var ( - // nameserver xxx.xxx.xxx.xxx - nameserverPattern = regexp.MustCompile(`nameserver\s+(?P\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})`) + "net" ) func loadSystemResolver() (clients []dnsClient, err error) { - content, err := os.ReadFile("/etc/resolv.conf") + nameservers, err := dnsReadConfig() if err != nil { - err = fmt.Errorf("failed to read /etc/resolv.conf: %w", err) return } - nameservers := make([]string, 0) - for _, line := range nameserverPattern.FindAllStringSubmatch(string(content), -1) { - addr := line[1] - nameservers = append(nameservers, addr) - } if len(nameservers) == 0 { - err = fmt.Errorf("no nameserver found in /etc/resolv.conf") return } servers := make([]NameServer, 0, len(nameservers)) for _, addr := range nameservers { servers = append(servers, NameServer{ - Addr: fmt.Sprintf("%s:%d", addr, 53), + Addr: net.JoinHostPort(addr, "53"), Net: "udp", }) } diff --git a/dns/system_posix.go b/dns/system_posix.go new file mode 100644 index 00000000..d486b4fb --- /dev/null +++ b/dns/system_posix.go @@ -0,0 +1,27 @@ +//go:build !windows + +package dns + +import ( + "fmt" + "os" + "regexp" +) + +var ( + // nameserver xxx.xxx.xxx.xxx + nameserverPattern = regexp.MustCompile(`nameserver\s+(?P\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})`) +) + +func dnsReadConfig() (servers []string, err error) { + content, err := os.ReadFile("/etc/resolv.conf") + if err != nil { + err = fmt.Errorf("failed to read /etc/resolv.conf: %w", err) + return + } + for _, line := range nameserverPattern.FindAllStringSubmatch(string(content), -1) { + addr := line[1] + servers = append(servers, addr) + } + return +} diff --git a/dns/system_windows.go b/dns/system_windows.go index 2e0aa237..47c1ebaa 100644 --- a/dns/system_windows.go +++ b/dns/system_windows.go @@ -3,7 +3,6 @@ package dns import ( - "fmt" "net" "os" "syscall" @@ -12,34 +11,10 @@ import ( "golang.org/x/sys/windows" ) -type dnsConfig struct { - servers []string // server addresses (in host:port form) to use -} - -func loadSystemResolver() (clients []dnsClient, err error) { - content, err := dnsReadConfig() - if err != nil { - err = fmt.Errorf("failed to read system DNS: %w", err) - } - nameservers := content.servers - if len(nameservers) == 0 { - return - } - servers := make([]NameServer, 0, len(nameservers)) - for _, addr := range nameservers { - servers = append(servers, NameServer{ - Addr: addr, - Net: "udp", - }) - } - return transform(servers, nil), nil -} - -func dnsReadConfig() (conf *dnsConfig, err error) { - conf = &dnsConfig{} +func dnsReadConfig() (servers []string, err error) { aas, err := adapterAddresses() if err != nil { - return conf, err + return } for _, aa := range aas { for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next { @@ -52,23 +27,23 @@ func dnsReadConfig() (conf *dnsConfig, err error) { 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. - // continue - //} - continue + 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 default: // Unexpected type. continue } - conf.servers = append(conf.servers, net.JoinHostPort(ip.String(), "53")) + servers = append(servers, ip.String()) } } - return conf, nil + return } // adapterAddresses returns a list of IP adapter and address