Only used saved LAN servers if available

This commit is contained in:
Cuong Manh Le
2025-01-07 21:15:12 +07:00
committed by Cuong Manh Le
parent a5c776c846
commit db6e977e3a
2 changed files with 54 additions and 19 deletions
+14 -2
View File
@@ -78,7 +78,7 @@ func availableNameservers() []string {
if _, ok := machineIPsMap[ns]; ok {
continue
}
if testNameserver(ns) {
if testNameServerFn(ns) {
nss = append(nss, ns)
}
}
@@ -122,7 +122,16 @@ func initializeOsResolver(servers []string) []string {
or.initializedLanServers.CompareAndSwap(nil, &lanNss)
}
if len(lanNss) == 0 {
or.lanServers.Store(or.initializedLanServers.Load())
var nss []string
p := or.initializedLanServers.Load()
if p != nil {
for _, ns := range *p {
if testNameServerFn(ns) {
nss = append(nss, ns)
}
}
}
or.lanServers.Store(&nss)
} else {
or.lanServers.Store(&lanNss)
}
@@ -133,6 +142,9 @@ func initializeOsResolver(servers []string) []string {
return slices.Concat(lanNss, publicNss)
}
// testNameserverFn sends a test query to DNS nameserver to check if the server is available.
var testNameServerFn = testNameserver
// testPlainDnsNameserver sends a test query to DNS nameserver to check if the server is available.
func testNameserver(addr string) bool {
msg := new(dns.Msg)