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 <A.Garipov@AdGuard.COM>
Date:   Fri Feb 5 15:07:04 2021 +0300

    Merge branch 'master' into less-cyclo

commit f61e2366ea10a289cec2f76b700e7117850cd4e2
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Feb 5 15:06:02 2021 +0300

    home: imp docs

commit e3c2310ce09cb78dd53ebbe728de4d2e72e6caf7
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Jan 27 21:14:30 2021 +0300

    all: imp cyclomatic complexity, minor improvements
This commit is contained in:
Ainar Garipov 2021-02-05 15:17:18 +03:00
parent 42b88c32fc
commit e84effffc3
3 changed files with 49 additions and 28 deletions

View File

@ -90,6 +90,28 @@ type dhcpServerConfigJSON struct {
V6 v6ServerConfJSON `json:"v6"` 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) { func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
newconfig := dhcpServerConfigJSON{} newconfig := dhcpServerConfigJSON{}
newconfig.Enabled = s.conf.Enabled 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) js, err := jsonutil.DecodeObject(&newconfig, r.Body)
if err != nil { if err != nil {
httpError(r, w, http.StatusBadRequest, "Failed to parse new DHCP config json: %s", err) httpError(r, w, http.StatusBadRequest, "Failed to parse new DHCP config json: %s", err)
return return
} }
@ -112,6 +135,7 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
if len(v4conf.RangeStart) == 0 { if len(v4conf.RangeStart) == 0 {
v4conf.Enabled = false v4conf.Enabled = false
} }
v4Enabled = v4conf.Enabled v4Enabled = v4conf.Enabled
v4conf.InterfaceName = newconfig.InterfaceName v4conf.InterfaceName = newconfig.InterfaceName
@ -122,7 +146,8 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
s4, err = v4Create(v4conf) s4, err = v4Create(v4conf)
if err != nil { if err != nil {
httpError(r, w, http.StatusBadRequest, "Invalid DHCPv4 configuration: %s", err) httpError(r, w, http.StatusBadRequest, "invalid dhcpv4 configuration: %s", err)
return return
} }
} }
@ -133,18 +158,22 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
if len(v6conf.RangeStart) == 0 { if len(v6conf.RangeStart) == 0 {
v6conf.Enabled = false v6conf.Enabled = false
} }
v6Enabled = v6conf.Enabled v6Enabled = v6conf.Enabled
v6conf.InterfaceName = newconfig.InterfaceName v6conf.InterfaceName = newconfig.InterfaceName
v6conf.notify = s.onNotify v6conf.notify = s.onNotify
s6, err = v6Create(v6conf) s6, err = v6Create(v6conf)
if err != nil { if err != nil {
httpError(r, w, http.StatusBadRequest, "Invalid DHCPv6 configuration: %s", err) httpError(r, w, http.StatusBadRequest, "invalid dhcpv6 configuration: %s", err)
return return
} }
} }
if newconfig.Enabled && !v4Enabled && !v6Enabled { 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 return
} }
@ -161,25 +190,20 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
if s4 != nil { if s4 != nil {
s.srv4 = s4 s.srv4 = s4
} }
if s6 != nil { if s6 != nil {
s.srv6 = s6 s.srv6 = s6
} }
s.conf.ConfigModified() s.conf.ConfigModified()
s.dbLoad() s.dbLoad()
if s.conf.Enabled { if s.conf.Enabled {
staticIP, err := sysutil.IfaceHasStaticIP(newconfig.InterfaceName) var code int
if !staticIP && err == nil { code, err = s.enableDHCP(newconfig.InterfaceName)
err = sysutil.IfaceSetStaticIP(newconfig.InterfaceName)
if err != nil {
httpError(r, w, http.StatusInternalServerError, "Failed to configure static IP: %s", err)
return
}
}
err = s.Start()
if err != nil { if err != nil {
httpError(r, w, http.StatusBadRequest, "Failed to start DHCP server: %s", err) httpError(r, w, code, "enabling dhcp: %s", err)
return return
} }
} }

View File

@ -83,23 +83,16 @@ func whoisParse(data string) map[string]string {
switch k { switch k {
case "org-name": case "org-name":
m["orgname"] = trimValue(v) m["orgname"] = trimValue(v)
case "orgname": case "city", "country", "orgname":
fallthrough
case "city":
fallthrough
case "country":
m[k] = trimValue(v) m[k] = trimValue(v)
case "descr": case "descr":
if len(descr) == 0 { if len(descr) == 0 {
descr = v descr = v
} }
case "netname": case "netname":
netname = v netname = v
case "whois": // "whois: whois.arin.net" case "whois": // "whois: whois.arin.net"
m["whois"] = v m["whois"] = v
case "referralserver": // "ReferralServer: whois://whois.ripe.net" case "referralserver": // "ReferralServer: whois://whois.ripe.net"
if strings.HasPrefix(v, "whois://") { if strings.HasPrefix(v, "whois://") {
m["whois"] = v[len("whois://"):] m["whois"] = v[len("whois://"):]
@ -107,12 +100,16 @@ func whoisParse(data string) map[string]string {
} }
} }
// descr or netname -> orgname
_, ok := m["orgname"] _, ok := m["orgname"]
if !ok && len(descr) != 0 { if !ok {
m["orgname"] = trimValue(descr) // Set orgname from either descr or netname for the frontent.
} else if !ok && len(netname) != 0 { //
m["orgname"] = trimValue(netname) // 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 return m

View File

@ -132,7 +132,7 @@ golint --set_exit_status ./...
"$GO" vet ./... "$GO" vet ./...
gocyclo --over 20 . gocyclo --over 19 .
gosec --quiet . gosec --quiet .