Pull request: all: imp uniq validation err msgs

Updates #3975.

Squashed commit of the following:

commit f8578c2afb1bb5786e7b855a1715e0757bc08510
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 28 16:39:13 2021 +0300

    aghalgo: imp docs

commit d9fc625f7c4ede2cf4b0683ad5efd0ddf9b966b1
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 28 16:21:24 2021 +0300

    all: imp uniq validation err msgs
This commit is contained in:
Ainar Garipov
2021-12-28 17:00:52 +03:00
parent 2ed1f939b5
commit d2ce06e1ca
10 changed files with 166 additions and 128 deletions

View File

@@ -1,11 +1,13 @@
package home
import (
"fmt"
"net"
"os"
"path/filepath"
"sync"
"github.com/AdguardTeam/AdGuardHome/internal/aghalgo"
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
@@ -286,22 +288,25 @@ func parseConfig() (err error) {
return err
}
pm := portsMap{}
pm.add(
uv := aghalgo.UniquenessValidator{}
addPorts(
uv,
config.BindPort,
config.BetaBindPort,
config.DNS.Port,
)
if config.TLS.Enabled {
pm.add(
addPorts(
uv,
config.TLS.PortHTTPS,
config.TLS.PortDNSOverTLS,
config.TLS.PortDNSOverQUIC,
config.TLS.PortDNSCrypt,
)
}
if err = pm.validate(); err != nil {
return err
if err = uv.Validate(aghalgo.IntIsBefore); err != nil {
return fmt.Errorf("validating ports: %w", err)
}
if !checkFiltersUpdateIntervalHours(config.DNS.FiltersUpdateIntervalHours) {
@@ -315,6 +320,15 @@ func parseConfig() (err error) {
return nil
}
// addPorts is a helper for ports validation. It skips zero ports.
func addPorts(uv aghalgo.UniquenessValidator, ports ...int) {
for _, p := range ports {
if p != 0 {
uv.Add(p)
}
}
}
// readConfigFile reads configuration file contents.
func readConfigFile() (fileData []byte, err error) {
if len(config.fileData) > 0 {

View File

@@ -14,6 +14,7 @@ import (
"strings"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghalgo"
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/golibs/errors"
@@ -102,9 +103,15 @@ func (web *Web) handleInstallCheckConfig(w http.ResponseWriter, r *http.Request)
return
}
pm := portsMap{}
pm.add(config.BindPort, config.BetaBindPort, reqData.Web.Port)
if err = pm.validate(); err != nil {
uv := aghalgo.UniquenessValidator{}
addPorts(
uv,
config.BindPort,
config.BetaBindPort,
reqData.Web.Port,
)
if err = uv.Validate(aghalgo.IntIsBefore); err != nil {
err = fmt.Errorf("validating ports: %w", err)
respData.Web.Status = err.Error()
} else if reqData.Web.Port != 0 {
err = aghnet.CheckPort("tcp", reqData.Web.IP, reqData.Web.Port)
@@ -113,8 +120,9 @@ func (web *Web) handleInstallCheckConfig(w http.ResponseWriter, r *http.Request)
}
}
pm.add(reqData.DNS.Port)
if err = pm.validate(); err != nil {
addPorts(uv, reqData.DNS.Port)
if err = uv.Validate(aghalgo.IntIsBefore); err != nil {
err = fmt.Errorf("validating ports: %w", err)
respData.DNS.Status = err.Error()
} else if reqData.DNS.Port != 0 {
err = aghnet.CheckPort("udp", reqData.DNS.IP, reqData.DNS.Port)

View File

@@ -19,6 +19,7 @@ import (
"syscall"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghalgo"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
@@ -295,22 +296,24 @@ func setupConfig(args options) (err error) {
Context.clients.Init(config.Clients, Context.dhcpServer, Context.etcHosts)
if args.bindPort != 0 {
pm := portsMap{}
pm.add(
uv := aghalgo.UniquenessValidator{}
addPorts(
uv,
args.bindPort,
config.BetaBindPort,
config.DNS.Port,
)
if config.TLS.Enabled {
pm.add(
addPorts(
uv,
config.TLS.PortHTTPS,
config.TLS.PortDNSOverTLS,
config.TLS.PortDNSOverQUIC,
config.TLS.PortDNSCrypt,
)
}
if err = pm.validate(); err != nil {
return err
if err = uv.Validate(aghalgo.IntIsBefore); err != nil {
return fmt.Errorf("validating ports: %w", err)
}
config.BindPort = args.bindPort
@@ -374,7 +377,7 @@ func fatalOnError(err error) {
}
}
// run performs configurating and starts AdGuard Home.
// run configures and starts AdGuard Home.
func run(args options, clientBuildFS fs.FS) {
var err error

View File

@@ -1,63 +0,0 @@
package home
import (
"fmt"
"strconv"
"strings"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/stringutil"
)
// portsMap is a helper type for mapping a network port to the number of its
// users.
type portsMap map[int]int
// add binds each of ps. Zeroes are skipped.
func (pm portsMap) add(ps ...int) {
for _, p := range ps {
if p == 0 {
continue
}
pm[p]++
}
}
// validate returns an error about all the ports bound several times.
func (pm portsMap) validate() (err error) {
overbound := []int{}
for p, num := range pm {
if num > 1 {
overbound = append(overbound, p)
pm[p] = 1
}
}
switch len(overbound) {
case 0:
return nil
case 1:
return fmt.Errorf("port %d is already used", overbound[0])
default:
b := &strings.Builder{}
// TODO(e.burkov, a.garipov): Add JoinToBuilder helper to stringutil.
stringutil.WriteToBuilder(b, "ports ", strconv.Itoa(overbound[0]))
for _, p := range overbound[1:] {
stringutil.WriteToBuilder(b, ", ", strconv.Itoa(p))
}
stringutil.WriteToBuilder(b, " are already used")
return errors.Error(b.String())
}
}
// validatePorts is a helper function for a single-step ports binding
// validation.
func validatePorts(ps ...int) (err error) {
pm := portsMap{}
pm.add(ps...)
return pm.validate()
}

View File

@@ -157,7 +157,7 @@ func sendSigReload() {
// it is specified when we register a service, and it indicates to the app
// that it is being run as a service/daemon.
func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
// Call chooseSystem expicitly to introduce OpenBSD support for service
// Call chooseSystem explicitly to introduce OpenBSD support for service
// package. It's a noop for other GOOS values.
chooseSystem()

View File

@@ -173,7 +173,7 @@ func (s *openbsdRunComService) template() (t *template.Template) {
)))
}
// execPath returns the absolute path to the excutable to be run as a service.
// execPath returns the absolute path to the executable to be run as a service.
func (s *openbsdRunComService) execPath() (path string, err error) {
if c := s.cfg; c != nil && len(c.Executable) != 0 {
return filepath.Abs(c.Executable)

View File

@@ -20,6 +20,7 @@ import (
"sync"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghalgo"
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/AdguardTeam/golibs/errors"
@@ -250,7 +251,9 @@ func (t *TLSMod) handleTLSValidate(w http.ResponseWriter, r *http.Request) {
}
if setts.Enabled {
if err = validatePorts(
uv := aghalgo.UniquenessValidator{}
addPorts(
uv,
config.BindPort,
config.BetaBindPort,
config.DNS.Port,
@@ -258,8 +261,11 @@ func (t *TLSMod) handleTLSValidate(w http.ResponseWriter, r *http.Request) {
setts.PortDNSOverTLS,
setts.PortDNSOverQUIC,
setts.PortDNSCrypt,
); err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
)
err = uv.Validate(aghalgo.IntIsBefore)
if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "validating ports: %s", err)
return
}
@@ -338,7 +344,9 @@ func (t *TLSMod) handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
}
if data.Enabled {
if err = validatePorts(
uv := aghalgo.UniquenessValidator{}
addPorts(
uv,
config.BindPort,
config.BetaBindPort,
config.DNS.Port,
@@ -346,7 +354,10 @@ func (t *TLSMod) handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
data.PortDNSOverTLS,
data.PortDNSOverQUIC,
data.PortDNSCrypt,
); err != nil {
)
err = uv.Validate(aghalgo.IntIsBefore)
if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
return