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:
parent
5c99ea26c9
commit
a086d60edc
|
@ -12,6 +12,8 @@ import (
|
||||||
// This whole thing should be rewritten to use context
|
// This whole thing should be rewritten to use context
|
||||||
|
|
||||||
var dnsR *dnsRecords
|
var dnsR *dnsRecords
|
||||||
|
var dnsServer *dns.Server
|
||||||
|
var dnsAddr string
|
||||||
|
|
||||||
type dnsRecords struct {
|
type dnsRecords struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
|
@ -106,20 +108,38 @@ func handleDnsRequest(w dns.ResponseWriter, r *dns.Msg) {
|
||||||
w.WriteMsg(m)
|
w.WriteMsg(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func dnsMain(hostMap *HostMap) {
|
func dnsMain(hostMap *HostMap, c *Config) {
|
||||||
|
|
||||||
dnsR = newDnsRecords(hostMap)
|
dnsR = newDnsRecords(hostMap)
|
||||||
|
|
||||||
// attach request handler func
|
// attach request handler func
|
||||||
dns.HandleFunc(".", handleDnsRequest)
|
dns.HandleFunc(".", handleDnsRequest)
|
||||||
|
|
||||||
// start server
|
c.RegisterReloadCallback(reloadDns)
|
||||||
port := 53
|
startDns(c)
|
||||||
server := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: "udp"}
|
}
|
||||||
l.Debugf("Starting DNS responder at %d\n", port)
|
|
||||||
err := server.ListenAndServe()
|
func getDnsServerAddr(c *Config) string {
|
||||||
defer server.Shutdown()
|
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 {
|
if err != nil {
|
||||||
l.Errorf("Failed to start server: %s\n ", err.Error())
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -27,6 +27,10 @@ lighthouse:
|
||||||
# serve_dns optionally starts a dns listener that responds to various queries and can even be
|
# serve_dns optionally starts a dns listener that responds to various queries and can even be
|
||||||
# delegated to for resolution
|
# delegated to for resolution
|
||||||
#serve_dns: false
|
#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.
|
# 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.
|
# during updates, a node sends information about its current IP addresses to each node.
|
||||||
interval: 60
|
interval: 60
|
||||||
|
|
13
main.go
13
main.go
|
@ -204,7 +204,6 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
||||||
lighthouseHosts[i] = ip2int(ip)
|
lighthouseHosts[i] = ip2int(ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
serveDns := config.GetBool("lighthouse.serve_dns", false)
|
|
||||||
lightHouse := NewLightHouse(
|
lightHouse := NewLightHouse(
|
||||||
amLighthouse,
|
amLighthouse,
|
||||||
ip2int(tunCidr.IP),
|
ip2int(tunCidr.IP),
|
||||||
|
@ -216,11 +215,6 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
||||||
punchBack,
|
punchBack,
|
||||||
)
|
)
|
||||||
|
|
||||||
if amLighthouse && serveDns {
|
|
||||||
l.Debugln("Starting dns server")
|
|
||||||
go dnsMain(hostMap)
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: Move all of this inside functions in lighthouse.go
|
//TODO: Move all of this inside functions in lighthouse.go
|
||||||
for k, v := range config.GetMap("static_host_map", map[interface{}]interface{}{}) {
|
for k, v := range config.GetMap("static_host_map", map[interface{}]interface{}{}) {
|
||||||
vpnIp := net.ParseIP(fmt.Sprintf("%v", k))
|
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", "")
|
//handshakeMACKey := config.GetString("handshake_mac.key", "")
|
||||||
//handshakeAcceptedMACKeys := config.GetStringSlice("handshake_mac.accepted_keys", []string{})
|
//handshakeAcceptedMACKeys := config.GetStringSlice("handshake_mac.accepted_keys", []string{})
|
||||||
|
|
||||||
|
serveDns := config.GetBool("lighthouse.serve_dns", false)
|
||||||
checkInterval := config.GetInt("timers.connection_alive_interval", 5)
|
checkInterval := config.GetInt("timers.connection_alive_interval", 5)
|
||||||
pendingDeletionInterval := config.GetInt("timers.pending_deletion_interval", 10)
|
pendingDeletionInterval := config.GetInt("timers.pending_deletion_interval", 10)
|
||||||
ifConfig := &InterfaceConfig{
|
ifConfig := &InterfaceConfig{
|
||||||
|
@ -313,6 +308,12 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
||||||
attachCommands(ssh, hostMap, handshakeManager.pendingHostMap, lightHouse, ifce)
|
attachCommands(ssh, hostMap, handshakeManager.pendingHostMap, lightHouse, ifce)
|
||||||
ifce.Run(config.GetInt("tun.routines", 1), udpQueues, buildVersion)
|
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.
|
// Just sit here and be friendly, main thread.
|
||||||
shutdownBlock(ifce)
|
shutdownBlock(ifce)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue