mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
feat: support system dns
This commit is contained in:
parent
fda4fdc6d5
commit
c556482ab7
|
@ -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
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":
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user