Skip to content

Commit

Permalink
Pull request #976: all: imp cyclomatic complexity, minor improvements
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from less-cyclo to master

Updates #2646.

Squashed commit of the following:

commit 42f81c9
Merge: f61e236 42b88c3
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Feb 5 15:07:04 2021 +0300

    Merge branch 'master' into less-cyclo

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

    home: imp docs

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

    all: imp cyclomatic complexity, minor improvements
  • Loading branch information
ainar-g committed Feb 5, 2021
1 parent 42b88c3 commit e84efff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
52 changes: 38 additions & 14 deletions internal/dhcpd/dhcphttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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

Expand All @@ -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
}
}
Expand All @@ -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
}

Expand All @@ -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
}
}
Expand Down
23 changes: 10 additions & 13 deletions internal/home/whois.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,36 +83,33 @@ 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://"):]
}
}
}

// 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
Expand Down
2 changes: 1 addition & 1 deletion scripts/make/go-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ golint --set_exit_status ./...

"$GO" vet ./...

gocyclo --over 20 .
gocyclo --over 19 .

gosec --quiet .

Expand Down

0 comments on commit e84efff

Please sign in to comment.