Allow configuration of dns listener host/port (#74)

* Allow configuration of dns listener host/port

* Make DNS listen host/port configuration HUP-able
This commit is contained in:
Robin B 2019-12-12 02:42:55 +01:00 committed by Nathan Brown
parent 5c99ea26c9
commit a086d60edc
3 changed files with 39 additions and 14 deletions

View File

@ -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)
}

View File

@ -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

13
main.go
View File

@ -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)
}