Adding more source for getting available DNS

On some platforms, the gateway may not be a usable DNS. So extending the
current approach to allow retrieving DNS from many sources.
This commit is contained in:
Cuong Manh Le
2023-03-31 12:37:37 +07:00
committed by Cuong Manh Le
parent b65a5ac283
commit 42d29b626b
6 changed files with 76 additions and 23 deletions
+29
View File
@@ -0,0 +1,29 @@
package ctrld
import "net"
type dnsFn func() []string
func nameservers() []string {
var dns []string
seen := make(map[string]bool)
ch := make(chan []string)
fns := dnsFns()
for _, fn := range fns {
go func(fn dnsFn) {
ch <- fn()
}(fn)
}
for range fns {
for _, ns := range <-ch {
if seen[ns] {
continue
}
seen[ns] = true
dns = append(dns, net.JoinHostPort(ns, "53"))
}
}
return dns
}