mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-04 01:07:49 +02:00
cmd/ctrld: use RFC1918 addresses as nameservers if required
This commit is contained in:
committed by
Cuong Manh Le
parent
28f32bd7e5
commit
12c8ab696f
+34
-23
@@ -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,30 +100,23 @@ 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()
|
func() {
|
||||||
for _, addr := range addrs {
|
listenAddr := net.JoinHostPort(addr, strconv.Itoa(listenerConfig.Port))
|
||||||
ipNet, ok := addr.(*net.IPNet)
|
s, errCh := runDNSServer(listenAddr, proto, handler)
|
||||||
if !ok || !ipNet.IP.IsPrivate() {
|
defer s.Shutdown()
|
||||||
continue
|
select {
|
||||||
|
case <-p.stopCh:
|
||||||
|
case <-ctx.Done():
|
||||||
|
case err := <-errCh:
|
||||||
|
// RFC1918 listener should not terminate ctrld.
|
||||||
|
// It's a workaround for a quirk on system with systemd-resolved.
|
||||||
|
mainLog.Warn().Err(err).Msgf("could not listen on %s: %s", proto, listenAddr)
|
||||||
}
|
}
|
||||||
func() {
|
}()
|
||||||
listenAddr := net.JoinHostPort(ipNet.IP.String(), "53")
|
}
|
||||||
s, errCh := runDNSServer(listenAddr, proto, handler)
|
|
||||||
defer s.Shutdown()
|
|
||||||
select {
|
|
||||||
case <-p.stopCh:
|
|
||||||
case <-ctx.Done():
|
|
||||||
case err := <-errCh:
|
|
||||||
// RFC1918 listener should not terminate ctrld.
|
|
||||||
// It's a workaround for a quirk on system with systemd-resolved.
|
|
||||||
mainLog.Warn().Err(err).Msgf("could not listen on %s: %s", proto, listenAddr)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
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
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user