From 9c4b791621344edb7ed36e4cc6aa88bed9876681 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Wed, 28 Nov 2018 18:57:20 +0300 Subject: [PATCH] coredns reload -> dnsServer.Reconfigure() --- control.go | 12 ++++-------- coredns.go | 27 ++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/control.go b/control.go index d2011cb0..25c059b0 100644 --- a/control.go +++ b/control.go @@ -38,17 +38,13 @@ var client = &http.Client{ // ------------------- // coredns run control // ------------------- -func tellCoreDNSToReload() { - corednsplugin.Reload <- true -} - func writeAllConfigsAndReloadCoreDNS() error { err := writeAllConfigs() if err != nil { log.Printf("Couldn't write all configs: %s", err) return err } - tellCoreDNSToReload() + reconfigureDNSServer() return nil } @@ -155,7 +151,7 @@ func handleSetUpstreamDNS(w http.ResponseWriter, r *http.Request) { http.Error(w, errorText, http.StatusInternalServerError) return } - tellCoreDNSToReload() + reconfigureDNSServer() _, err = fmt.Fprintf(w, "OK %d servers\n", len(hosts)) if err != nil { errorText := fmt.Sprintf("Couldn't write body: %s", err) @@ -386,7 +382,7 @@ func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) { return } - tellCoreDNSToReload() + reconfigureDNSServer() _, err = fmt.Fprintf(w, "OK %d rules\n", filter.RulesCount) if err != nil { @@ -571,7 +567,7 @@ func refreshFiltersIfNeccessary(force bool) int { config.Unlock() if updateCount > 0 { - tellCoreDNSToReload() + reconfigureDNSServer() } return updateCount } diff --git a/coredns.go b/coredns.go index b612c8d0..348b4241 100644 --- a/coredns.go +++ b/coredns.go @@ -15,11 +15,7 @@ func isRunning() bool { return dnsServer.IsRunning() } -func startDNSServer() error { - if isRunning() { - return fmt.Errorf("Unable to start coreDNS: Already running") - } - +func generateServerConfig() dnsforward.ServerConfig { filters := []dnsforward.Filter{} for _, filter := range config.Filters { filters = append(filters, dnsforward.Filter{ @@ -43,7 +39,15 @@ func startDNSServer() error { } newconfig.Upstreams = append(newconfig.Upstreams, upstream) } + return newconfig +} +func startDNSServer() error { + if isRunning() { + return fmt.Errorf("Unable to start forwarding DNS server: Already running") + } + + newconfig := generateServerConfig() err := dnsServer.Start(&newconfig) if err != nil { return errorx.Decorate(err, "Couldn't start forwarding DNS server") @@ -51,3 +55,16 @@ func startDNSServer() error { return nil } + +func reconfigureDNSServer() error { + if !isRunning() { + return fmt.Errorf("Refusing to reconfigure forwarding DNS server: not running") + } + + err := dnsServer.Reconfigure(generateServerConfig()) + if err != nil { + return errorx.Decorate(err, "Couldn't start forwarding DNS server") + } + + return nil +}