cmd/ctrld: use RFC1918 addresses as nameservers if required

This commit is contained in:
Cuong Manh Le
2023-07-25 00:28:26 +00:00
committed by Cuong Manh Le
parent 28f32bd7e5
commit 12c8ab696f
2 changed files with 39 additions and 24 deletions
+22 -11
View File
@@ -80,7 +80,6 @@ func (p *prog) serveDNS(listenerNum string) error {
} }
}) })
needRFC1918Listeners := listenerConfig.IP == "127.0.0.1" && listenerConfig.Port == 53
g, ctx := errgroup.WithContext(context.Background()) g, ctx := errgroup.WithContext(context.Background())
for _, proto := range []string{"udp", "tcp"} { for _, proto := range []string{"udp", "tcp"} {
proto := proto proto := proto
@@ -101,17 +100,11 @@ func (p *prog) serveDNS(listenerNum string) error {
} }
// When we spawn a listener on 127.0.0.1, also spawn listeners on the RFC1918 // When we spawn a listener on 127.0.0.1, also spawn listeners on the RFC1918
// addresses of the machine. So ctrld could receive queries from LAN clients. // addresses of the machine. So ctrld could receive queries from LAN clients.
if needRFC1918Listeners { if needRFC1918Listeners(listenerConfig) {
g.Go(func() error { g.Go(func() error {
interfaces.ForeachInterface(func(i interfaces.Interface, prefixes []netip.Prefix) { for _, addr := range rfc1918Addresses() {
addrs, _ := i.Addrs()
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok || !ipNet.IP.IsPrivate() {
continue
}
func() { func() {
listenAddr := net.JoinHostPort(ipNet.IP.String(), "53") listenAddr := net.JoinHostPort(addr, strconv.Itoa(listenerConfig.Port))
s, errCh := runDNSServer(listenAddr, proto, handler) s, errCh := runDNSServer(listenAddr, proto, handler)
defer s.Shutdown() defer s.Shutdown()
select { select {
@@ -124,7 +117,6 @@ func (p *prog) serveDNS(listenerNum string) error {
} }
}() }()
} }
})
return nil return nil
}) })
} }
@@ -556,3 +548,22 @@ func (p *prog) getClientInfo(ip, mac string) *ctrld.ClientInfo {
ci.Hostname = p.ciTable.LookupHostname(ci.IP, ci.Mac) ci.Hostname = p.ciTable.LookupHostname(ci.IP, ci.Mac)
return ci return ci
} }
func needRFC1918Listeners(lc *ctrld.ListenerConfig) bool {
return lc.IP == "127.0.0.1" && lc.Port == 53
}
func rfc1918Addresses() []string {
var res []string
interfaces.ForeachInterface(func(i interfaces.Interface, prefixes []netip.Prefix) {
addrs, _ := i.Addrs()
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok || !ipNet.IP.IsPrivate() {
continue
}
res = append(res, ipNet.IP.String())
}
})
return res
}
+5 -1
View File
@@ -239,7 +239,11 @@ func (p *prog) setDNS() {
// so we could just use lc.IP as nameserver. // so we could just use lc.IP as nameserver.
} }
if err := setDNS(netIface, []string{ns}); err != nil { nameservers := []string{ns}
if needRFC1918Listeners(lc) {
nameservers = append(nameservers, rfc1918Addresses()...)
}
if err := setDNS(netIface, nameservers); err != nil {
logger.Error().Err(err).Msgf("could not set DNS for interface") logger.Error().Err(err).Msgf("could not set DNS for interface")
return return
} }