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

View File

@ -149,7 +149,7 @@ golint --set_exit_status ./...
"$GO" vet ./...
# 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