From e84effffc386fccfb3165279ff959f4537c3c6d0 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Fri, 5 Feb 2021 15:17:18 +0300 Subject: [PATCH] Pull request #976: all: imp cyclomatic complexity, minor improvements Merge in DNS/adguard-home from less-cyclo to master Updates #2646. Squashed commit of the following: commit 42f81c9f716ca7a2878d481b96d31d86f7c4151b Merge: f61e2366 42b88c32 Author: Ainar Garipov Date: Fri Feb 5 15:07:04 2021 +0300 Merge branch 'master' into less-cyclo commit f61e2366ea10a289cec2f76b700e7117850cd4e2 Author: Ainar Garipov Date: Fri Feb 5 15:06:02 2021 +0300 home: imp docs commit e3c2310ce09cb78dd53ebbe728de4d2e72e6caf7 Author: Ainar Garipov Date: Wed Jan 27 21:14:30 2021 +0300 all: imp cyclomatic complexity, minor improvements --- internal/dhcpd/dhcphttp.go | 52 ++++++++++++++++++++++++++++---------- internal/home/whois.go | 23 ++++++++--------- scripts/make/go-lint.sh | 2 +- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/internal/dhcpd/dhcphttp.go b/internal/dhcpd/dhcphttp.go index e38afbca..b6b5c729 100644 --- a/internal/dhcpd/dhcphttp.go +++ b/internal/dhcpd/dhcphttp.go @@ -90,6 +90,28 @@ type dhcpServerConfigJSON struct { V6 v6ServerConfJSON `json:"v6"` } +func (s *Server) enableDHCP(ifaceName string) (code int, err error) { + var hasStaticIP bool + hasStaticIP, err = sysutil.IfaceHasStaticIP(ifaceName) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("checking static ip: %w", err) + } + + if !hasStaticIP { + err = sysutil.IfaceSetStaticIP(ifaceName) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("setting static ip: %w", err) + } + } + + err = s.Start() + if err != nil { + return http.StatusBadRequest, fmt.Errorf("starting dhcp server: %w", err) + } + + return 0, nil +} + func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { newconfig := dhcpServerConfigJSON{} newconfig.Enabled = s.conf.Enabled @@ -98,6 +120,7 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { js, err := jsonutil.DecodeObject(&newconfig, r.Body) if err != nil { httpError(r, w, http.StatusBadRequest, "Failed to parse new DHCP config json: %s", err) + return } @@ -112,6 +135,7 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { if len(v4conf.RangeStart) == 0 { v4conf.Enabled = false } + v4Enabled = v4conf.Enabled v4conf.InterfaceName = newconfig.InterfaceName @@ -122,7 +146,8 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { s4, err = v4Create(v4conf) if err != nil { - httpError(r, w, http.StatusBadRequest, "Invalid DHCPv4 configuration: %s", err) + httpError(r, w, http.StatusBadRequest, "invalid dhcpv4 configuration: %s", err) + return } } @@ -133,18 +158,22 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { if len(v6conf.RangeStart) == 0 { v6conf.Enabled = false } + v6Enabled = v6conf.Enabled v6conf.InterfaceName = newconfig.InterfaceName v6conf.notify = s.onNotify + s6, err = v6Create(v6conf) if err != nil { - httpError(r, w, http.StatusBadRequest, "Invalid DHCPv6 configuration: %s", err) + httpError(r, w, http.StatusBadRequest, "invalid dhcpv6 configuration: %s", err) + return } } if newconfig.Enabled && !v4Enabled && !v6Enabled { - httpError(r, w, http.StatusBadRequest, "DHCPv4 or DHCPv6 configuration must be complete") + httpError(r, w, http.StatusBadRequest, "dhcpv4 or dhcpv6 configuration must be complete") + return } @@ -161,25 +190,20 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { if s4 != nil { s.srv4 = s4 } + if s6 != nil { s.srv6 = s6 } + s.conf.ConfigModified() s.dbLoad() if s.conf.Enabled { - staticIP, err := sysutil.IfaceHasStaticIP(newconfig.InterfaceName) - if !staticIP && err == nil { - err = sysutil.IfaceSetStaticIP(newconfig.InterfaceName) - if err != nil { - httpError(r, w, http.StatusInternalServerError, "Failed to configure static IP: %s", err) - return - } - } - - err = s.Start() + var code int + code, err = s.enableDHCP(newconfig.InterfaceName) if err != nil { - httpError(r, w, http.StatusBadRequest, "Failed to start DHCP server: %s", err) + httpError(r, w, code, "enabling dhcp: %s", err) + return } } diff --git a/internal/home/whois.go b/internal/home/whois.go index 26c674dc..20f035e2 100644 --- a/internal/home/whois.go +++ b/internal/home/whois.go @@ -83,23 +83,16 @@ func whoisParse(data string) map[string]string { switch k { case "org-name": m["orgname"] = trimValue(v) - case "orgname": - fallthrough - case "city": - fallthrough - case "country": + case "city", "country", "orgname": m[k] = trimValue(v) - case "descr": if len(descr) == 0 { descr = v } case "netname": netname = v - case "whois": // "whois: whois.arin.net" m["whois"] = v - case "referralserver": // "ReferralServer: whois://whois.ripe.net" if strings.HasPrefix(v, "whois://") { m["whois"] = v[len("whois://"):] @@ -107,12 +100,16 @@ func whoisParse(data string) map[string]string { } } - // descr or netname -> orgname _, ok := m["orgname"] - if !ok && len(descr) != 0 { - m["orgname"] = trimValue(descr) - } else if !ok && len(netname) != 0 { - m["orgname"] = trimValue(netname) + if !ok { + // Set orgname from either descr or netname for the frontent. + // + // TODO(a.garipov): Perhaps don't do that in the V1 HTTP API? + if descr != "" { + m["orgname"] = trimValue(descr) + } else if netname != "" { + m["orgname"] = trimValue(netname) + } } return m diff --git a/scripts/make/go-lint.sh b/scripts/make/go-lint.sh index e1aeb9ef..30ed6141 100644 --- a/scripts/make/go-lint.sh +++ b/scripts/make/go-lint.sh @@ -132,7 +132,7 @@ golint --set_exit_status ./... "$GO" vet ./... -gocyclo --over 20 . +gocyclo --over 19 . gosec --quiet .