Pull request: dnsforward: imp code, decr cyclo

Updates #2646.

Squashed commit of the following:

commit b362aff81f6b91509b65bdb5e5efc55652449eaf
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 25 21:13:30 2021 +0300

    dnsforward: imp code, decr cyclo
This commit is contained in:
Ainar Garipov 2021-03-26 13:29:33 +03:00
parent 9631eff608
commit 179b76da77
2 changed files with 38 additions and 42 deletions

View file

@ -209,7 +209,10 @@ func (s *Server) handleSetConfig(w http.ResponseWriter, r *http.Request) {
return return
} }
if s.setConfig(req) { restart := s.setConfig(req)
s.conf.ConfigModified()
if restart {
if err := s.Reconfigure(nil); err != nil { if err := s.Reconfigure(nil); err != nil {
httpError(r, w, http.StatusInternalServerError, "%s", err) httpError(r, w, http.StatusInternalServerError, "%s", err)
return return
@ -217,9 +220,7 @@ func (s *Server) handleSetConfig(w http.ResponseWriter, r *http.Request) {
} }
} }
func (s *Server) setConfig(dc dnsConfig) (restart bool) { func (s *Server) setConfigRestartable(dc dnsConfig) (restart bool) {
s.Lock()
if dc.Upstreams != nil { if dc.Upstreams != nil {
s.conf.UpstreamDNS = *dc.Upstreams s.conf.UpstreamDNS = *dc.Upstreams
restart = true restart = true
@ -235,22 +236,8 @@ func (s *Server) setConfig(dc dnsConfig) (restart bool) {
restart = true restart = true
} }
if dc.ProtectionEnabled != nil {
s.conf.ProtectionEnabled = *dc.ProtectionEnabled
}
if dc.BlockingMode != nil {
s.conf.BlockingMode = *dc.BlockingMode
if *dc.BlockingMode == "custom_ip" {
s.conf.BlockingIPv4 = dc.BlockingIPv4.To4()
s.conf.BlockingIPv6 = dc.BlockingIPv6.To16()
}
}
if dc.RateLimit != nil { if dc.RateLimit != nil {
if s.conf.Ratelimit != *dc.RateLimit { restart = restart || s.conf.Ratelimit != *dc.RateLimit
restart = true
}
s.conf.Ratelimit = *dc.RateLimit s.conf.Ratelimit = *dc.RateLimit
} }
@ -259,14 +246,6 @@ func (s *Server) setConfig(dc dnsConfig) (restart bool) {
restart = true restart = true
} }
if dc.DNSSECEnabled != nil {
s.conf.EnableDNSSEC = *dc.DNSSECEnabled
}
if dc.DisableIPv6 != nil {
s.conf.AAAADisabled = *dc.DisableIPv6
}
if dc.CacheSize != nil { if dc.CacheSize != nil {
s.conf.CacheSize = *dc.CacheSize s.conf.CacheSize = *dc.CacheSize
restart = true restart = true
@ -282,22 +261,39 @@ func (s *Server) setConfig(dc dnsConfig) (restart bool) {
restart = true restart = true
} }
if dc.UpstreamMode != nil { return restart
switch *dc.UpstreamMode { }
case "parallel":
s.conf.AllServers = true func (s *Server) setConfig(dc dnsConfig) (restart bool) {
s.conf.FastestAddr = false s.Lock()
case "fastest_addr": defer s.Unlock()
s.conf.AllServers = false
s.conf.FastestAddr = true if dc.ProtectionEnabled != nil {
default: s.conf.ProtectionEnabled = *dc.ProtectionEnabled
s.conf.AllServers = false }
s.conf.FastestAddr = false
if dc.BlockingMode != nil {
s.conf.BlockingMode = *dc.BlockingMode
if *dc.BlockingMode == "custom_ip" {
s.conf.BlockingIPv4 = dc.BlockingIPv4.To4()
s.conf.BlockingIPv6 = dc.BlockingIPv6.To16()
} }
} }
s.Unlock()
s.conf.ConfigModified() if dc.DNSSECEnabled != nil {
return restart s.conf.EnableDNSSEC = *dc.DNSSECEnabled
}
if dc.DisableIPv6 != nil {
s.conf.AAAADisabled = *dc.DisableIPv6
}
if dc.UpstreamMode != nil {
s.conf.AllServers = *dc.UpstreamMode == "parallel"
s.conf.FastestAddr = *dc.UpstreamMode == "fastest_addr"
}
return s.setConfigRestartable(dc)
} }
type upstreamJSON struct { type upstreamJSON struct {

View file

@ -149,7 +149,7 @@ golint --set_exit_status ./...
"$GO" vet ./... "$GO" vet ./...
# Here and below, don't use quotes to get word splitting. # Here and below, don't use quotes to get word splitting.
gocyclo --over 18 $go_files gocyclo --over 17 $go_files
gosec --quiet $go_files gosec --quiet $go_files