Skip to content

Commit

Permalink
skip duplicates nameserver when parse
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 23, 2024
1 parent a0045f5 commit 388bfc1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
42 changes: 20 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
T "github.com/metacubex/mihomo/tunnel"

orderedmap "github.com/wk8/go-ordered-map/v2"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -898,16 +899,6 @@ func parseNameServer(servers []string, respectRules bool) ([]dns.NameServer, err
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 {
Expand Down Expand Up @@ -954,11 +945,15 @@ func parseNameServer(servers []string, respectRules bool) ([]dns.NameServer, err
}
}
}
case "dhcp":
addr = u.Host
dnsNetType = "dhcp" // UDP from DHCP
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
Expand All @@ -984,15 +979,18 @@ func parseNameServer(servers []string, respectRules bool) ([]dns.NameServer, err
proxyName = dns.RespectRules
}

nameservers = append(
nameservers,
dns.NameServer{
Net: dnsNetType,
Addr: addr,
ProxyName: proxyName,
Params: params,
},
)
nameserver := dns.NameServer{
Net: dnsNetType,
Addr: addr,
ProxyName: proxyName,
Params: params,
}

if slices.ContainsFunc(nameservers, nameserver.Equal) {
continue // skip duplicates nameserver
}

nameservers = append(nameservers, nameserver)
}
return nameservers, nil
}
Expand Down
4 changes: 0 additions & 4 deletions dns/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ func transform(servers []NameServer, resolver *Resolver) []dnsClient {
ret = append(ret, newDoHClient(s.Addr, resolver, s.ProxyAdapter, s.ProxyName, s.Params))
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":
Expand Down

0 comments on commit 388bfc1

Please sign in to comment.