diff --git a/internal/aghalg/aghalg.go b/internal/aghalg/aghalg.go index 4705173e034..c3b6e906fe6 100644 --- a/internal/aghalg/aghalg.go +++ b/internal/aghalg/aghalg.go @@ -18,16 +18,16 @@ type comparable = interface{} type UniqChecker map[comparable]int64 // Add adds a value to the validator. v must not be nil. -func (v UniqChecker) Add(elems ...comparable) { +func (uc UniqChecker) Add(elems ...comparable) { for _, e := range elems { - v[e]++ + uc[e]++ } } // Merge returns a validator containing data from both v and other. -func (v UniqChecker) Merge(other UniqChecker) (merged UniqChecker) { - merged = make(UniqChecker, len(v)+len(other)) - for elem, num := range v { +func (uc UniqChecker) Merge(other UniqChecker) (merged UniqChecker) { + merged = make(UniqChecker, len(uc)+len(other)) + for elem, num := range uc { merged[elem] += num } @@ -41,9 +41,9 @@ func (v UniqChecker) Merge(other UniqChecker) (merged UniqChecker) { // Validate returns an error enumerating all elements that aren't unique. // isBefore is an optional sorting function to make the error message // deterministic. -func (v UniqChecker) Validate(isBefore func(a, b comparable) (less bool)) (err error) { +func (uc UniqChecker) Validate(isBefore func(a, b comparable) (less bool)) (err error) { var dup []comparable - for elem, num := range v { + for elem, num := range uc { if num > 1 { dup = append(dup, elem) } @@ -62,13 +62,13 @@ func (v UniqChecker) Validate(isBefore func(a, b comparable) (less bool)) (err e return fmt.Errorf("duplicated values: %v", dup) } -// IntIsBefore is a helper sort function for UniquenessValidator.Validate. +// IntIsBefore is a helper sort function for UniqChecker.Validate. // a and b must be of type int. func IntIsBefore(a, b comparable) (less bool) { return a.(int) < b.(int) } -// StringIsBefore is a helper sort function for UniquenessValidator.Validate. +// StringIsBefore is a helper sort function for UniqChecker.Validate. // a and b must be of type string. func StringIsBefore(a, b comparable) (less bool) { return a.(string) < b.(string) diff --git a/internal/dnsforward/access.go b/internal/dnsforward/access.go index 72f4771b058..76145fdfee5 100644 --- a/internal/dnsforward/access.go +++ b/internal/dnsforward/access.go @@ -223,13 +223,13 @@ func validateAccessSet(list *accessListJSON) (err error) { } // validateStrUniq returns an informative error if clients are not unique. -func validateStrUniq(clients []string) (uv aghalg.UniqChecker, err error) { - uv = make(aghalg.UniqChecker, len(clients)) +func validateStrUniq(clients []string) (uc aghalg.UniqChecker, err error) { + uc = make(aghalg.UniqChecker, len(clients)) for _, c := range clients { - uv.Add(c) + uc.Add(c) } - return uv, uv.Validate(aghalg.StringIsBefore) + return uc, uc.Validate(aghalg.StringIsBefore) } func (s *Server) handleAccessSet(w http.ResponseWriter, r *http.Request) { diff --git a/internal/home/config.go b/internal/home/config.go index 5823b074eb4..a624716441e 100644 --- a/internal/home/config.go +++ b/internal/home/config.go @@ -288,9 +288,9 @@ func parseConfig() (err error) { return err } - uv := aghalg.UniqChecker{} + uc := aghalg.UniqChecker{} addPorts( - uv, + uc, config.BindPort, config.BetaBindPort, config.DNS.Port, @@ -298,14 +298,14 @@ func parseConfig() (err error) { if config.TLS.Enabled { addPorts( - uv, + uc, config.TLS.PortHTTPS, config.TLS.PortDNSOverTLS, config.TLS.PortDNSOverQUIC, config.TLS.PortDNSCrypt, ) } - if err = uv.Validate(aghalg.IntIsBefore); err != nil { + if err = uc.Validate(aghalg.IntIsBefore); err != nil { return fmt.Errorf("validating ports: %w", err) } @@ -321,10 +321,10 @@ func parseConfig() (err error) { } // addPorts is a helper for ports validation. It skips zero ports. -func addPorts(uv aghalg.UniqChecker, ports ...int) { +func addPorts(uc aghalg.UniqChecker, ports ...int) { for _, p := range ports { if p != 0 { - uv.Add(p) + uc.Add(p) } } } diff --git a/internal/home/controlinstall.go b/internal/home/controlinstall.go index 4cff68047b8..cd13edd8ebc 100644 --- a/internal/home/controlinstall.go +++ b/internal/home/controlinstall.go @@ -104,14 +104,14 @@ type checkConfResp struct { // validateWeb returns error is the web part if the initial configuration can't // be set. -func (req *checkConfReq) validateWeb(uv aghalg.UniqChecker) (err error) { +func (req *checkConfReq) validateWeb(uc aghalg.UniqChecker) (err error) { defer func() { err = errors.Annotate(err, "validating ports: %w") }() port := req.Web.Port - addPorts(uv, config.BetaBindPort, port) - if err = uv.Validate(aghalg.IntIsBefore); err != nil { + addPorts(uc, config.BetaBindPort, port) + if err = uc.Validate(aghalg.IntIsBefore); err != nil { // Avoid duplicating the error into the status of DNS. - uv[port] = 1 + uc[port] = 1 return err } @@ -130,12 +130,12 @@ func (req *checkConfReq) validateWeb(uv aghalg.UniqChecker) (err error) { // validateDNS returns error if the DNS part of the initial configuration can't // be set. autofix is true if the port can be unbound by AdGuard Home // automatically. -func (req *checkConfReq) validateDNS(uv aghalg.UniqChecker) (canAutofix bool, err error) { +func (req *checkConfReq) validateDNS(uc aghalg.UniqChecker) (canAutofix bool, err error) { defer func() { err = errors.Annotate(err, "validating ports: %w") }() port := req.DNS.Port - addPorts(uv, port) - if err = uv.Validate(aghalg.IntIsBefore); err != nil { + addPorts(uc, port) + if err = uc.Validate(aghalg.IntIsBefore); err != nil { return false, err } @@ -184,13 +184,13 @@ func (web *Web) handleInstallCheckConfig(w http.ResponseWriter, r *http.Request) } resp := &checkConfResp{} - uv := aghalg.UniqChecker{} + uc := aghalg.UniqChecker{} - if err = req.validateWeb(uv); err != nil { + if err = req.validateWeb(uc); err != nil { resp.Web.Status = err.Error() } - if resp.DNS.CanAutofix, err = req.validateDNS(uv); err != nil { + if resp.DNS.CanAutofix, err = req.validateDNS(uc); err != nil { resp.DNS.Status = err.Error() } else if !req.DNS.IP.IsUnspecified() { resp.StaticIP = handleStaticIP(req.DNS.IP, req.SetStaticIP) diff --git a/internal/home/home.go b/internal/home/home.go index 57dae6326f8..90674d481f6 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -296,23 +296,23 @@ func setupConfig(args options) (err error) { Context.clients.Init(config.Clients, Context.dhcpServer, Context.etcHosts) if args.bindPort != 0 { - uv := aghalg.UniqChecker{} + uc := aghalg.UniqChecker{} addPorts( - uv, + uc, args.bindPort, config.BetaBindPort, config.DNS.Port, ) if config.TLS.Enabled { addPorts( - uv, + uc, config.TLS.PortHTTPS, config.TLS.PortDNSOverTLS, config.TLS.PortDNSOverQUIC, config.TLS.PortDNSCrypt, ) } - if err = uv.Validate(aghalg.IntIsBefore); err != nil { + if err = uc.Validate(aghalg.IntIsBefore); err != nil { return fmt.Errorf("validating ports: %w", err) } diff --git a/internal/home/tls.go b/internal/home/tls.go index d5ffc4f4b29..a201be1440b 100644 --- a/internal/home/tls.go +++ b/internal/home/tls.go @@ -251,9 +251,9 @@ func (t *TLSMod) handleTLSValidate(w http.ResponseWriter, r *http.Request) { } if setts.Enabled { - uv := aghalg.UniqChecker{} + uc := aghalg.UniqChecker{} addPorts( - uv, + uc, config.BindPort, config.BetaBindPort, config.DNS.Port, @@ -263,7 +263,7 @@ func (t *TLSMod) handleTLSValidate(w http.ResponseWriter, r *http.Request) { setts.PortDNSCrypt, ) - err = uv.Validate(aghalg.IntIsBefore) + err = uc.Validate(aghalg.IntIsBefore) if err != nil { aghhttp.Error(r, w, http.StatusBadRequest, "validating ports: %s", err) @@ -344,9 +344,9 @@ func (t *TLSMod) handleTLSConfigure(w http.ResponseWriter, r *http.Request) { } if data.Enabled { - uv := aghalg.UniqChecker{} + uc := aghalg.UniqChecker{} addPorts( - uv, + uc, config.BindPort, config.BetaBindPort, config.DNS.Port, @@ -356,7 +356,7 @@ func (t *TLSMod) handleTLSConfigure(w http.ResponseWriter, r *http.Request) { data.PortDNSCrypt, ) - err = uv.Validate(aghalg.IntIsBefore) + err = uc.Validate(aghalg.IntIsBefore) if err != nil { aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)