feat: support system dns
This commit is contained in:
parent
19b403da86
commit
d6931ec491
4 changed files with 57 additions and 1 deletions
|
@ -841,7 +841,7 @@ func parseHosts(cfg *RawConfig) (*trie.DomainTrie[resolver.HostValue], error) {
|
||||||
} else {
|
} else {
|
||||||
ips := make([]netip.Addr, 0)
|
ips := make([]netip.Addr, 0)
|
||||||
for _, addr := range addrs {
|
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 {
|
if ip, err := netip.ParseAddr(ipnet.IP.String()); err == nil {
|
||||||
ips = append(ips, ip)
|
ips = append(ips, ip)
|
||||||
}
|
}
|
||||||
|
@ -938,6 +938,8 @@ func parseNameServer(servers []string, preferH3 bool) ([]dns.NameServer, error)
|
||||||
case "quic":
|
case "quic":
|
||||||
addr, err = hostWithDefaultPort(u.Host, "853")
|
addr, err = hostWithDefaultPort(u.Host, "853")
|
||||||
dnsNetType = "quic" // DNS over QUIC
|
dnsNetType = "quic" // DNS over QUIC
|
||||||
|
case "system":
|
||||||
|
dnsNetType = "system" // System DNS
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("DNS NameServer[%d] unsupport scheme: %s", idx, u.Scheme)
|
return nil, fmt.Errorf("DNS NameServer[%d] unsupport scheme: %s", idx, u.Scheme)
|
||||||
}
|
}
|
||||||
|
|
39
dns/system.go
Normal file
39
dns/system.go
Normal 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
7
dns/system_windows.go
Normal 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")
|
||||||
|
}
|
|
@ -79,6 +79,14 @@ func transform(servers []NameServer, resolver *Resolver) []dnsClient {
|
||||||
case "dhcp":
|
case "dhcp":
|
||||||
ret = append(ret, newDHCPClient(s.Addr))
|
ret = append(ret, newDHCPClient(s.Addr))
|
||||||
continue
|
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":
|
case "quic":
|
||||||
if doq, err := newDoQ(resolver, s.Addr, s.ProxyAdapter, s.ProxyName); err == nil {
|
if doq, err := newDoQ(resolver, s.Addr, s.ProxyAdapter, s.ProxyName); err == nil {
|
||||||
ret = append(ret, doq)
|
ret = append(ret, doq)
|
||||||
|
|
Loading…
Reference in a new issue