From 6f69fb73af7901b52a4717833de8554069c0b43b Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Tue, 5 Mar 2019 18:04:49 +0300 Subject: [PATCH] * control: safely restart DHCP server * control: use mutex in all POST,PUT,DELETE handlers --- control.go | 3 +++ dhcp.go | 12 ++++++------ helpers.go | 6 ++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/control.go b/control.go index 0cd9d504..0bd7a5d7 100644 --- a/control.go +++ b/control.go @@ -12,6 +12,7 @@ import ( "sort" "strconv" "strings" + "sync" "time" "github.com/AdguardTeam/AdGuardHome/dnsforward" @@ -36,6 +37,8 @@ var client = &http.Client{ Timeout: time.Second * 30, } +var controlLock sync.Mutex + // ---------------- // helper functions // ---------------- diff --git a/dhcp.go b/dhcp.go index fe780305..58a3f161 100644 --- a/dhcp.go +++ b/dhcp.go @@ -50,6 +50,11 @@ func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { return } + err = dhcpServer.Stop() + if err != nil { + log.Error("failed to stop the DHCP server: %s", err) + } + if newconfig.Enabled { err := dhcpServer.Start(&newconfig) if err != nil { @@ -57,12 +62,7 @@ func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { return } } - if !newconfig.Enabled { - err := dhcpServer.Stop() - if err != nil { - log.Error("failed to stop the DHCP server: %s", err) - } - } + config.DHCP = newconfig httpUpdateConfigReloadDNSReturnOK(w, r) } diff --git a/helpers.go b/helpers.go index 25e35205..9d02ad3c 100644 --- a/helpers.go +++ b/helpers.go @@ -27,6 +27,12 @@ func ensure(method string, handler func(http.ResponseWriter, *http.Request)) fun http.Error(w, "This request must be "+method, http.StatusMethodNotAllowed) return } + + if method == "POST" || method == "PUT" || method == "DELETE" { + controlLock.Lock() + defer controlLock.Unlock() + } + handler(w, r) } }