Commit 77fb2b0a authored by ale's avatar ale

DNS listening address are not necessarily public IPs

It is best to just make the DNS server listen on all non-loopback IP
addresses from all interfaces. The loopback exclusion is just to
integrate nicely with an eventual DNS cache running on the same host.
parent 9e4db386
Pipeline #5608 passed with stages
in 5 minutes and 13 seconds
...@@ -52,6 +52,27 @@ func shortHostname() string { ...@@ -52,6 +52,27 @@ func shortHostname() string {
return hostname return hostname
} }
// Returns the list of all non-loopback addresses (IPv4 and IPv6) for
// all interfaces.
func nonLocalAddrs() []net.IP {
var ips []net.IP
// nolint: errcheck
interfaces, _ := net.Interfaces()
for _, intf := range interfaces {
addrs, _ := intf.Addrs()
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil || ip.IsLoopback() {
continue
}
ips = append(ips, ip)
}
}
return ips
}
func main() { func main() {
log.SetFlags(0) log.SetFlags(0)
flag.Parse() flag.Parse()
...@@ -136,8 +157,10 @@ func main() { ...@@ -136,8 +157,10 @@ func main() {
log.Fatalf("could not initialize node: %v", err) log.Fatalf("could not initialize node: %v", err)
} }
// Start all the network services. // Start all the network services. DNS will listen on all
srv := node.NewServer(n, *domain, strings.Split(*nameservers, ","), *publicIPs, *peerIP, *httpPort, *dnsPort, *gossipPort, autoradio.IcecastPort, *metricsPort) // non-loopback addresses on all interfaces, to let people run
// a loopback cache if necessary.
srv := node.NewServer(n, *domain, strings.Split(*nameservers, ","), nonLocalAddrs(), *peerIP, *httpPort, *dnsPort, *gossipPort, autoradio.IcecastPort, *metricsPort)
// Wait until the Node and the Server terminate. A failure in // Wait until the Node and the Server terminate. A failure in
// either the network services or the Node itself should cause // either the network services or the Node itself should cause
......
...@@ -80,10 +80,10 @@ func (s *Server) Wait() error { ...@@ -80,10 +80,10 @@ func (s *Server) Wait() error {
// build all the necessary addr/port combinations. // build all the necessary addr/port combinations.
// //
// The main http handler will bind on all available interfaces. The // The main http handler will bind on all available interfaces. The
// DNS servers will bind only to the publicAddrs (both TCP and // DNS servers will bind only to the dnsAddrs (both TCP and
// UDP). The metrics and the status services, which are internal, will // UDP). The metrics and the status services, which are internal, will
// bind on peerAddr. // bind on peerAddr.
func NewServer(n *Node, domain string, nameservers []string, publicAddrs []net.IP, peerAddr net.IP, httpPort, dnsPort, gossipPort, icecastPort, metricsPort int) *Server { func NewServer(n *Node, domain string, nameservers []string, dnsAddrs []net.IP, peerAddr net.IP, httpPort, dnsPort, gossipPort, icecastPort, metricsPort int) *Server {
httpHandler := newHTTPHandler(n, icecastPort, domain) httpHandler := newHTTPHandler(n, icecastPort, domain)
dnsHandler := newDNSHandler(n, domain, nameservers) dnsHandler := newDNSHandler(n, domain, nameservers)
...@@ -92,7 +92,7 @@ func NewServer(n *Node, domain string, nameservers []string, publicAddrs []net.I ...@@ -92,7 +92,7 @@ func NewServer(n *Node, domain string, nameservers []string, publicAddrs []net.I
newHTTPServer("main", fmt.Sprintf(":%d", httpPort), httpHandler), newHTTPServer("main", fmt.Sprintf(":%d", httpPort), httpHandler),
newHTTPServer("metrics", fmt.Sprintf(":%d", metricsPort), newMetricsHandler()), newHTTPServer("metrics", fmt.Sprintf(":%d", metricsPort), newMetricsHandler()),
} }
for _, ip := range publicAddrs { for _, ip := range dnsAddrs {
servers = append(servers, servers = append(servers,
newDNSServer("dns(udp)", mkaddr(ip, dnsPort), "udp", dnsHandler), newDNSServer("dns(udp)", mkaddr(ip, dnsPort), "udp", dnsHandler),
newDNSServer("dns(tcp)", mkaddr(ip, dnsPort), "tcp", dnsHandler), newDNSServer("dns(tcp)", mkaddr(ip, dnsPort), "tcp", dnsHandler),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment