From a086d60edc50f44bae36b2afee9c0da69c06a153 Mon Sep 17 00:00:00 2001 From: Robin B Date: Thu, 12 Dec 2019 02:42:55 +0100 Subject: [PATCH] Allow configuration of dns listener host/port (#74) * Allow configuration of dns listener host/port * Make DNS listen host/port configuration HUP-able --- dns_server.go | 36 ++++++++++++++++++++++++++++-------- examples/config.yml | 4 ++++ main.go | 13 +++++++------ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/dns_server.go b/dns_server.go index 705d7b9..3fec6b6 100644 --- a/dns_server.go +++ b/dns_server.go @@ -12,6 +12,8 @@ import ( // This whole thing should be rewritten to use context var dnsR *dnsRecords +var dnsServer *dns.Server +var dnsAddr string type dnsRecords struct { sync.RWMutex @@ -106,20 +108,38 @@ func handleDnsRequest(w dns.ResponseWriter, r *dns.Msg) { w.WriteMsg(m) } -func dnsMain(hostMap *HostMap) { - +func dnsMain(hostMap *HostMap, c *Config) { dnsR = newDnsRecords(hostMap) // attach request handler func dns.HandleFunc(".", handleDnsRequest) - // start server - port := 53 - server := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: "udp"} - l.Debugf("Starting DNS responder at %d\n", port) - err := server.ListenAndServe() - defer server.Shutdown() + c.RegisterReloadCallback(reloadDns) + startDns(c) +} + +func getDnsServerAddr(c *Config) string { + return c.GetString("lighthouse.dns.host", "") + ":" + strconv.Itoa(c.GetInt("lighthouse.dns.port", 53)) +} + +func startDns(c *Config) { + dnsAddr = getDnsServerAddr(c) + dnsServer = &dns.Server{Addr: dnsAddr, Net: "udp"} + l.Debugf("Starting DNS responder at %s\n", dnsAddr) + err := dnsServer.ListenAndServe() + defer dnsServer.Shutdown() if err != nil { l.Errorf("Failed to start server: %s\n ", err.Error()) } } + +func reloadDns(c *Config) { + if dnsAddr == getDnsServerAddr(c) { + l.Debug("No DNS server config change detected") + return + } + + l.Debug("Restarting DNS server") + dnsServer.Shutdown() + go startDns(c) +} diff --git a/examples/config.yml b/examples/config.yml index b25a48e..d48c347 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -27,6 +27,10 @@ lighthouse: # serve_dns optionally starts a dns listener that responds to various queries and can even be # delegated to for resolution #serve_dns: false + #dns: + # The DNS host defines the IP to bind the dns listener to. This also allows binding to the nebula node IP. + #host: 0.0.0.0 + #port: 53 # interval is the number of seconds between updates from this node to a lighthouse. # during updates, a node sends information about its current IP addresses to each node. interval: 60 diff --git a/main.go b/main.go index 5a99e88..39c10ab 100644 --- a/main.go +++ b/main.go @@ -204,7 +204,6 @@ func Main(configPath string, configTest bool, buildVersion string) { lighthouseHosts[i] = ip2int(ip) } - serveDns := config.GetBool("lighthouse.serve_dns", false) lightHouse := NewLightHouse( amLighthouse, ip2int(tunCidr.IP), @@ -216,11 +215,6 @@ func Main(configPath string, configTest bool, buildVersion string) { punchBack, ) - if amLighthouse && serveDns { - l.Debugln("Starting dns server") - go dnsMain(hostMap) - } - //TODO: Move all of this inside functions in lighthouse.go for k, v := range config.GetMap("static_host_map", map[interface{}]interface{}{}) { vpnIp := net.ParseIP(fmt.Sprintf("%v", k)) @@ -264,6 +258,7 @@ func Main(configPath string, configTest bool, buildVersion string) { //handshakeMACKey := config.GetString("handshake_mac.key", "") //handshakeAcceptedMACKeys := config.GetStringSlice("handshake_mac.accepted_keys", []string{}) + serveDns := config.GetBool("lighthouse.serve_dns", false) checkInterval := config.GetInt("timers.connection_alive_interval", 5) pendingDeletionInterval := config.GetInt("timers.pending_deletion_interval", 10) ifConfig := &InterfaceConfig{ @@ -313,6 +308,12 @@ func Main(configPath string, configTest bool, buildVersion string) { attachCommands(ssh, hostMap, handshakeManager.pendingHostMap, lightHouse, ifce) ifce.Run(config.GetInt("tun.routines", 1), udpQueues, buildVersion) + // Start DNS server last to allow using the nebula IP as lighthouse.dns.host + if amLighthouse && serveDns { + l.Debugln("Starting dns server") + go dnsMain(hostMap, config) + } + // Just sit here and be friendly, main thread. shutdownBlock(ifce) }