chore: cleanup system dns code

This commit is contained in:
wwqgtxx 2023-06-01 12:36:53 +08:00
parent 1120c8185d
commit 7fa3d3aa0b
3 changed files with 43 additions and 57 deletions

View file

@ -1,37 +1,21 @@
//go:build !windows
package dns package dns
import ( import (
"fmt" "net"
"os"
"regexp"
)
var (
// nameserver xxx.xxx.xxx.xxx
nameserverPattern = regexp.MustCompile(`nameserver\s+(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})`)
) )
func loadSystemResolver() (clients []dnsClient, err error) { func loadSystemResolver() (clients []dnsClient, err error) {
content, err := os.ReadFile("/etc/resolv.conf") nameservers, err := dnsReadConfig()
if err != nil { if err != nil {
err = fmt.Errorf("failed to read /etc/resolv.conf: %w", err)
return return
} }
nameservers := make([]string, 0)
for _, line := range nameserverPattern.FindAllStringSubmatch(string(content), -1) {
addr := line[1]
nameservers = append(nameservers, addr)
}
if len(nameservers) == 0 { if len(nameservers) == 0 {
err = fmt.Errorf("no nameserver found in /etc/resolv.conf")
return return
} }
servers := make([]NameServer, 0, len(nameservers)) servers := make([]NameServer, 0, len(nameservers))
for _, addr := range nameservers { for _, addr := range nameservers {
servers = append(servers, NameServer{ servers = append(servers, NameServer{
Addr: fmt.Sprintf("%s:%d", addr, 53), Addr: net.JoinHostPort(addr, "53"),
Net: "udp", Net: "udp",
}) })
} }

27
dns/system_posix.go Normal file
View file

@ -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<ip>\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
}

View file

@ -3,7 +3,6 @@
package dns package dns
import ( import (
"fmt"
"net" "net"
"os" "os"
"syscall" "syscall"
@ -12,34 +11,10 @@ import (
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
type dnsConfig struct { func dnsReadConfig() (servers []string, err error) {
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{}
aas, err := adapterAddresses() aas, err := adapterAddresses()
if err != nil { if err != nil {
return conf, err return
} }
for _, aa := range aas { for _, aa := range aas {
for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next { for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next {
@ -52,23 +27,23 @@ func dnsReadConfig() (conf *dnsConfig, err error) {
case *syscall.SockaddrInet4: case *syscall.SockaddrInet4:
ip = net.IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3]) ip = net.IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
case *syscall.SockaddrInet6: case *syscall.SockaddrInet6:
//ip = make(net.IP, net.IPv6len) ip = make(net.IP, net.IPv6len)
//copy(ip, sa.Addr[:]) copy(ip, sa.Addr[:])
//if ip[0] == 0xfe && ip[1] == 0xc0 { if ip[0] == 0xfe && ip[1] == 0xc0 {
// // Ignore these fec0/10 ones. Windows seems to // Ignore these fec0/10 ones. Windows seems to
// // populate them as defaults on its misc rando // populate them as defaults on its misc rando
// // interfaces. // interfaces.
// continue continue
//} }
continue //continue
default: default:
// Unexpected type. // Unexpected type.
continue 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 // adapterAddresses returns a list of IP adapter and address