feat: support system dns

This commit is contained in:
sleshep 2023-04-26 15:57:55 +08:00 committed by Larvan2
parent 19b403da86
commit d6931ec491
4 changed files with 57 additions and 1 deletions

View file

@ -841,7 +841,7 @@ func parseHosts(cfg *RawConfig) (*trie.DomainTrie[resolver.HostValue], error) {
} else {
ips := make([]netip.Addr, 0)
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback()&&!ipnet.IP.IsLinkLocalUnicast() {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && !ipnet.IP.IsLinkLocalUnicast() {
if ip, err := netip.ParseAddr(ipnet.IP.String()); err == nil {
ips = append(ips, ip)
}
@ -938,6 +938,8 @@ func parseNameServer(servers []string, preferH3 bool) ([]dns.NameServer, error)
case "quic":
addr, err = hostWithDefaultPort(u.Host, "853")
dnsNetType = "quic" // DNS over QUIC
case "system":
dnsNetType = "system" // System DNS
default:
return nil, fmt.Errorf("DNS NameServer[%d] unsupport scheme: %s", idx, u.Scheme)
}

39
dns/system.go Normal file
View file

@ -0,0 +1,39 @@
//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 loadSystemResolver() (clients []dnsClient, err error) {
content, err := os.ReadFile("/etc/resolv.conf")
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),
Net: "udp",
})
}
return transform(servers, nil), nil
}

7
dns/system_windows.go Normal file
View file

@ -0,0 +1,7 @@
//go:build windows
package dns
func loadSystemResolver() (clients []dnsClient, err error) {
return nil, errors.New("system resolver is not yet supported on Windows")
}

View file

@ -79,6 +79,14 @@ func transform(servers []NameServer, resolver *Resolver) []dnsClient {
case "dhcp":
ret = append(ret, newDHCPClient(s.Addr))
continue
case "system":
clients, err := loadSystemResolver()
if err != nil {
log.Warnln("[DNS:system] load system resolver failed: %s", err.Error())
continue
}
ret = append(ret, clients...)
continue
case "quic":
if doq, err := newDoQ(resolver, s.Addr, s.ProxyAdapter, s.ProxyName); err == nil {
ret = append(ret, doq)