From 001b4b981f0fd9563fa87724fe44a32c5d7bbc4b Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Tue, 9 Jul 2019 18:57:41 +0300 Subject: [PATCH] * move "dhcpServer" to "config" --- home/clients.go | 4 ++-- home/config.go | 1 + home/dhcp.go | 24 +++++++++++------------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/home/clients.go b/home/clients.go index d0767e47..0c003a56 100644 --- a/home/clients.go +++ b/home/clients.go @@ -122,7 +122,7 @@ func (clients *clientsContainer) Find(ip string) (Client, bool) { if err != nil { continue } - ipAddr := dhcpServer.FindIPbyMAC(mac) + ipAddr := config.dhcpServer.FindIPbyMAC(mac) if ipAddr == nil { continue } @@ -395,7 +395,7 @@ func handleGetClients(w http.ResponseWriter, r *http.Request) { if len(c.MAC) != 0 { hwAddr, _ := net.ParseMAC(c.MAC) - ipAddr := dhcpServer.FindIPbyMAC(hwAddr) + ipAddr := config.dhcpServer.FindIPbyMAC(hwAddr) if ipAddr != nil { cj.IP = ipAddr.String() } diff --git a/home/config.go b/home/config.go index e3e1c0ab..0900c9d8 100644 --- a/home/config.go +++ b/home/config.go @@ -70,6 +70,7 @@ type configuration struct { versionCheckJSON []byte versionCheckLastTime time.Time + dhcpServer dhcpd.Server httpServer *http.Server httpsServer HTTPSServer diff --git a/home/dhcp.go b/home/dhcp.go index 066e6843..078647f0 100644 --- a/home/dhcp.go +++ b/home/dhcp.go @@ -18,8 +18,6 @@ import ( "github.com/joomcode/errorx" ) -var dhcpServer = dhcpd.Server{} - // []dhcpd.Lease -> JSON func convertLeases(inputLeases []dhcpd.Lease, includeExpires bool) []map[string]string { leases := []map[string]string{} @@ -41,8 +39,8 @@ func convertLeases(inputLeases []dhcpd.Lease, includeExpires bool) []map[string] func handleDHCPStatus(w http.ResponseWriter, r *http.Request) { log.Tracef("%s %v", r.Method, r.URL) - leases := convertLeases(dhcpServer.Leases(), true) - staticLeases := convertLeases(dhcpServer.StaticLeases(), false) + leases := convertLeases(config.dhcpServer.Leases(), true) + staticLeases := convertLeases(config.dhcpServer.StaticLeases(), false) status := map[string]interface{}{ "config": config.DHCP, "leases": leases, @@ -77,18 +75,18 @@ func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { return } - err = dhcpServer.CheckConfig(newconfig.ServerConfig) + err = config.dhcpServer.CheckConfig(newconfig.ServerConfig) if err != nil { httpError(w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err) return } - err = dhcpServer.Stop() + err = config.dhcpServer.Stop() if err != nil { log.Error("failed to stop the DHCP server: %s", err) } - err = dhcpServer.Init(newconfig.ServerConfig) + err = config.dhcpServer.Init(newconfig.ServerConfig) if err != nil { httpError(w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err) return @@ -105,7 +103,7 @@ func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { } } - err = dhcpServer.Start() + err = config.dhcpServer.Start() if err != nil { httpError(w, http.StatusBadRequest, "Failed to start DHCP server: %s", err) return @@ -389,7 +387,7 @@ func handleDHCPAddStaticLease(w http.ResponseWriter, r *http.Request) { HWAddr: mac, Hostname: lj.Hostname, } - err = dhcpServer.AddStaticLease(lease) + err = config.dhcpServer.AddStaticLease(lease) if err != nil { httpError(w, http.StatusBadRequest, "%s", err) return @@ -420,7 +418,7 @@ func handleDHCPRemoveStaticLease(w http.ResponseWriter, r *http.Request) { HWAddr: mac, Hostname: lj.Hostname, } - err = dhcpServer.RemoveStaticLease(lease) + err = config.dhcpServer.RemoveStaticLease(lease) if err != nil { httpError(w, http.StatusBadRequest, "%s", err) return @@ -434,12 +432,12 @@ func startDHCPServer() error { return nil } - err := dhcpServer.Init(config.DHCP) + err := config.dhcpServer.Init(config.DHCP) if err != nil { return errorx.Decorate(err, "Couldn't init DHCP server") } - err = dhcpServer.Start() + err = config.dhcpServer.Start() if err != nil { return errorx.Decorate(err, "Couldn't start DHCP server") } @@ -451,7 +449,7 @@ func stopDHCPServer() error { return nil } - err := dhcpServer.Stop() + err := config.dhcpServer.Stop() if err != nil { return errorx.Decorate(err, "Couldn't stop DHCP server") }