From 179b76da77140342a7c6d8f895a581adb2d11dc3 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Fri, 26 Mar 2021 13:29:33 +0300 Subject: [PATCH] Pull request: dnsforward: imp code, decr cyclo Updates #2646. Squashed commit of the following: commit b362aff81f6b91509b65bdb5e5efc55652449eaf Author: Ainar Garipov Date: Thu Mar 25 21:13:30 2021 +0300 dnsforward: imp code, decr cyclo --- internal/dnsforward/http.go | 78 ++++++++++++++++++------------------- scripts/make/go-lint.sh | 2 +- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/internal/dnsforward/http.go b/internal/dnsforward/http.go index 0cba335d..b2f5476f 100644 --- a/internal/dnsforward/http.go +++ b/internal/dnsforward/http.go @@ -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 { diff --git a/scripts/make/go-lint.sh b/scripts/make/go-lint.sh index 95decef7..661edbe8 100644 --- a/scripts/make/go-lint.sh +++ b/scripts/make/go-lint.sh @@ -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