chore: skip duplicates nameserver when parse

This commit is contained in:
wwqgtxx 2024-09-23 08:54:07 +08:00
parent 33823f1728
commit 150c6ccd25
2 changed files with 19 additions and 24 deletions

View File

@ -1162,16 +1162,6 @@ func parseNameServer(servers []string, respectRules bool, preferH3 bool) ([]dns.
var nameservers []dns.NameServer
for idx, server := range servers {
if strings.HasPrefix(server, "dhcp://") {
nameservers = append(
nameservers,
dns.NameServer{
Net: "dhcp",
Addr: server[len("dhcp://"):],
},
)
continue
}
server = parsePureDNSServer(server)
u, err := url.Parse(server)
if err != nil {
@ -1222,6 +1212,13 @@ func parseNameServer(servers []string, respectRules bool, preferH3 bool) ([]dns.
dnsNetType = "quic" // DNS over QUIC
case "system":
dnsNetType = "system" // System DNS
case "dhcp":
addr = server[len("dhcp://"):] // some special notation cannot be parsed by url
dnsNetType = "dhcp" // UDP from DHCP
if addr == "system" { // Compatible with old writing "dhcp://system"
dnsNetType = "system"
addr = ""
}
case "rcode":
dnsNetType = "rcode"
addr = u.Host
@ -1247,16 +1244,18 @@ func parseNameServer(servers []string, respectRules bool, preferH3 bool) ([]dns.
proxyName = dns.RespectRules
}
nameservers = append(
nameservers,
dns.NameServer{
nameserver := dns.NameServer{
Net: dnsNetType,
Addr: addr,
ProxyName: proxyName,
Params: params,
PreferH3: preferH3,
},
)
}
if slices.ContainsFunc(nameservers, nameserver.Equal) {
continue // skip duplicates nameserver
}
nameservers = append(nameservers, nameserver)
}
return nameservers, nil
}

View File

@ -99,10 +99,6 @@ func transform(servers []NameServer, resolver *Resolver) []dnsClient {
ret = append(ret, newDoHClient(s.Addr, resolver, s.PreferH3, s.Params, s.ProxyAdapter, s.ProxyName))
continue
case "dhcp":
if s.Addr == "system" { // Compatible with old writing
ret = append(ret, newSystemClient())
continue
}
ret = append(ret, newDHCPClient(s.Addr))
continue
case "system":