From 150c6ccd2514459cb160c4722aeb760fbbc23a61 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Mon, 23 Sep 2024 08:54:07 +0800 Subject: [PATCH] chore: skip duplicates nameserver when parse --- config/config.go | 39 +++++++++++++++++++-------------------- dns/util.go | 4 ---- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/config/config.go b/config/config.go index 75616edc..2a75ddd6 100644 --- a/config/config.go +++ b/config/config.go @@ -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{ - Net: dnsNetType, - Addr: addr, - ProxyName: proxyName, - Params: params, - PreferH3: preferH3, - }, - ) + 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 } diff --git a/dns/util.go b/dns/util.go index 92a86dbc..50459cc1 100644 --- a/dns/util.go +++ b/dns/util.go @@ -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":