mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
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:
@@ -14,6 +14,7 @@ func TestUpstreamConfig_SetupBootstrapIP(t *testing.T) {
|
|||||||
uc.Init()
|
uc.Init()
|
||||||
uc.setupBootstrapIP(false)
|
uc.setupBootstrapIP(false)
|
||||||
if uc.BootstrapIP == "" {
|
if uc.BootstrapIP == "" {
|
||||||
|
t.Log(availableNameservers())
|
||||||
t.Fatal("could not bootstrap ip without bootstrap DNS")
|
t.Fatal("could not bootstrap ip without bootstrap DNS")
|
||||||
}
|
}
|
||||||
t.Log(uc)
|
t.Log(uc)
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
+22
-5
@@ -4,14 +4,20 @@ package ctrld
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"golang.org/x/net/route"
|
"golang.org/x/net/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
func osNameservers() []string {
|
func dnsFns() []dnsFn {
|
||||||
|
return []dnsFn{dnsFromRIB, dnsFromIPConfig}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsFromRIB() []string {
|
||||||
var dns []string
|
var dns []string
|
||||||
seen := make(map[string]bool)
|
|
||||||
rib, err := route.FetchRIB(syscall.AF_UNSPEC, route.RIBTypeRoute, 0)
|
rib, err := route.FetchRIB(syscall.AF_UNSPEC, route.RIBTypeRoute, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -33,17 +39,28 @@ func osNameservers() []string {
|
|||||||
if dst == nil || gw == nil {
|
if dst == nil || gw == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if gw.IsLoopback() || seen[gw.String()] {
|
if gw.IsLoopback() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if dst.Equal(net.IPv4zero) || dst.Equal(net.IPv6zero) {
|
if dst.Equal(net.IPv4zero) || dst.Equal(net.IPv6zero) {
|
||||||
seen[gw.String()] = true
|
dns = append(dns, gw.String())
|
||||||
dns = append(dns, net.JoinHostPort(gw.String(), "53"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dns
|
return dns
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dnsFromIPConfig() []string {
|
||||||
|
if runtime.GOOS != "darwin" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
cmd := exec.Command("ipconfig", "getoption", "", "domain_name_server")
|
||||||
|
out, _ := cmd.Output()
|
||||||
|
if ip := net.ParseIP(strings.TrimSpace(string(out))); ip != nil {
|
||||||
|
return []string{ip.String()}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func toNetIP(addr route.Addr) net.IP {
|
func toNetIP(addr route.Addr) net.IP {
|
||||||
switch t := addr.(type) {
|
switch t := addr.(type) {
|
||||||
case *route.Inet4Addr:
|
case *route.Inet4Addr:
|
||||||
|
|||||||
+18
-9
@@ -6,6 +6,8 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/Control-D-Inc/ctrld/internal/dns/resolvconffile"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -13,13 +15,8 @@ const (
|
|||||||
v6RouteFile = "/proc/net/ipv6_route"
|
v6RouteFile = "/proc/net/ipv6_route"
|
||||||
)
|
)
|
||||||
|
|
||||||
func osNameservers() []string {
|
func dnsFns() []dnsFn {
|
||||||
ns4 := dns4()
|
return []dnsFn{dns4, dns6, dnsFromSystemdResolver}
|
||||||
ns6 := dns6()
|
|
||||||
ns := make([]string, len(ns4)+len(ns6))
|
|
||||||
ns = append(ns, ns4...)
|
|
||||||
ns = append(ns, ns6...)
|
|
||||||
return ns
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func dns4() []string {
|
func dns4() []string {
|
||||||
@@ -53,7 +50,7 @@ func dns4() []string {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
seen[ip.String()] = true
|
seen[ip.String()] = true
|
||||||
dns = append(dns, net.JoinHostPort(ip.String(), "53"))
|
dns = append(dns, ip.String())
|
||||||
}
|
}
|
||||||
return dns
|
return dns
|
||||||
}
|
}
|
||||||
@@ -82,7 +79,19 @@ func dns6() []string {
|
|||||||
if ip.Equal(net.IPv6zero) {
|
if ip.Equal(net.IPv6zero) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
dns = append(dns, net.JoinHostPort(ip.String(), "53"))
|
dns = append(dns, ip.String())
|
||||||
}
|
}
|
||||||
return dns
|
return dns
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dnsFromSystemdResolver() []string {
|
||||||
|
c, err := resolvconffile.ParseFile("/run/systemd/resolve/resolv.conf")
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ns := make([]string, 0, len(c.Nameservers))
|
||||||
|
for _, nameserver := range c.Nameservers {
|
||||||
|
ns = append(ns, nameserver.String())
|
||||||
|
}
|
||||||
|
return ns
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
//go:build unix
|
|
||||||
|
|
||||||
package ctrld
|
|
||||||
|
|
||||||
func nameservers() []string {
|
|
||||||
return osNameservers()
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,11 @@ import (
|
|||||||
"golang.org/x/sys/windows"
|
"golang.org/x/sys/windows"
|
||||||
)
|
)
|
||||||
|
|
||||||
func nameservers() []string {
|
func dnsFns() []dnsFn {
|
||||||
|
return []dnsFn{dnsFromAdapter}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsFromAdapter() []string {
|
||||||
aas, err := winipcfg.GetAdaptersAddresses(syscall.AF_UNSPEC, winipcfg.GAAFlagIncludeGateways|winipcfg.GAAFlagIncludePrefix)
|
aas, err := winipcfg.GetAdaptersAddresses(syscall.AF_UNSPEC, winipcfg.GAAFlagIncludeGateways|winipcfg.GAAFlagIncludePrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -42,7 +46,7 @@ func nameservers() []string {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
seen[ip.String()] = true
|
seen[ip.String()] = true
|
||||||
ns = append(ns, net.JoinHostPort(ip.String(), "53"))
|
ns = append(ns, ip.String())
|
||||||
}
|
}
|
||||||
for _, aa := range aas {
|
for _, aa := range aas {
|
||||||
for dns := aa.FirstDNSServerAddress; dns != nil; dns = dns.Next {
|
for dns := aa.FirstDNSServerAddress; dns != nil; dns = dns.Next {
|
||||||
|
|||||||
Reference in New Issue
Block a user