2022-10-10 14:05:24 +03:00
|
|
|
package configmgr
|
|
|
|
|
|
|
|
import (
|
2023-06-29 14:34:06 +03:00
|
|
|
"fmt"
|
2022-10-10 14:05:24 +03:00
|
|
|
"net/netip"
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
"github.com/AdguardTeam/golibs/container"
|
2023-06-29 14:34:06 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2022-10-10 14:05:24 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// config is the top-level on-disk configuration structure.
|
|
|
|
type config struct {
|
|
|
|
DNS *dnsConfig `yaml:"dns"`
|
|
|
|
HTTP *httpConfig `yaml:"http"`
|
2023-06-29 19:10:39 +03:00
|
|
|
Log *logConfig `yaml:"log"`
|
2022-10-10 14:05:24 +03:00
|
|
|
// TODO(a.garipov): Use.
|
|
|
|
SchemaVersion int `yaml:"schema_version"`
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// type check
|
|
|
|
var _ validator = (*config)(nil)
|
2023-06-29 14:34:06 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// validate implements the [validator] interface for *config.
|
2023-06-29 14:34:06 +03:00
|
|
|
func (c *config) validate() (err error) {
|
|
|
|
if c == nil {
|
2024-11-13 15:44:21 +03:00
|
|
|
return errors.ErrNoValue
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(a.garipov): Add more validations.
|
|
|
|
|
|
|
|
// Keep this in the same order as the fields in the config.
|
2024-11-13 15:44:21 +03:00
|
|
|
validators := container.KeyValues[string, validator]{{
|
|
|
|
Key: "dns",
|
|
|
|
Value: c.DNS,
|
2023-06-29 14:34:06 +03:00
|
|
|
}, {
|
2024-11-13 15:44:21 +03:00
|
|
|
Key: "http",
|
|
|
|
Value: c.HTTP,
|
2023-06-29 19:10:39 +03:00
|
|
|
}, {
|
2024-11-13 15:44:21 +03:00
|
|
|
Key: "log",
|
|
|
|
Value: c.Log,
|
2023-06-29 14:34:06 +03:00
|
|
|
}}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
for _, kv := range validators {
|
|
|
|
err = kv.Value.validate()
|
2023-06-29 14:34:06 +03:00
|
|
|
if err != nil {
|
2024-11-13 15:44:21 +03:00
|
|
|
return fmt.Errorf("%s: %w", kv.Key, err)
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// dnsConfig is the on-disk DNS configuration.
|
|
|
|
type dnsConfig struct {
|
2023-06-28 12:48:53 +03:00
|
|
|
Addresses []netip.AddrPort `yaml:"addresses"`
|
|
|
|
BootstrapDNS []string `yaml:"bootstrap_dns"`
|
|
|
|
UpstreamDNS []string `yaml:"upstream_dns"`
|
|
|
|
DNS64Prefixes []netip.Prefix `yaml:"dns64_prefixes"`
|
|
|
|
UpstreamTimeout timeutil.Duration `yaml:"upstream_timeout"`
|
|
|
|
BootstrapPreferIPv6 bool `yaml:"bootstrap_prefer_ipv6"`
|
|
|
|
UseDNS64 bool `yaml:"use_dns64"`
|
2022-10-10 14:05:24 +03:00
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// type check
|
|
|
|
var _ validator = (*dnsConfig)(nil)
|
|
|
|
|
|
|
|
// validate implements the [validator] interface for *dnsConfig.
|
2023-06-29 14:34:06 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add more validations.
|
|
|
|
func (c *dnsConfig) validate() (err error) {
|
|
|
|
// TODO(a.garipov): Add more validations.
|
|
|
|
switch {
|
|
|
|
case c == nil:
|
2024-11-13 15:44:21 +03:00
|
|
|
return errors.ErrNoValue
|
2023-06-29 14:34:06 +03:00
|
|
|
case c.UpstreamTimeout.Duration <= 0:
|
2024-11-13 15:44:21 +03:00
|
|
|
return newErrNotPositive("upstream_timeout", c.UpstreamTimeout)
|
2023-06-29 14:34:06 +03:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// httpConfig is the on-disk web API configuration.
|
|
|
|
type httpConfig struct {
|
2023-07-20 18:57:06 +03:00
|
|
|
Pprof *httpPprofConfig `yaml:"pprof"`
|
|
|
|
|
2023-06-29 19:10:39 +03:00
|
|
|
// TODO(a.garipov): Document the configuration change.
|
2022-10-10 14:05:24 +03:00
|
|
|
Addresses []netip.AddrPort `yaml:"addresses"`
|
|
|
|
SecureAddresses []netip.AddrPort `yaml:"secure_addresses"`
|
|
|
|
Timeout timeutil.Duration `yaml:"timeout"`
|
|
|
|
ForceHTTPS bool `yaml:"force_https"`
|
|
|
|
}
|
2023-06-29 14:34:06 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// type check
|
|
|
|
var _ validator = (*httpConfig)(nil)
|
|
|
|
|
|
|
|
// validate implements the [validator] interface for *httpConfig.
|
2023-06-29 14:34:06 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add more validations.
|
|
|
|
func (c *httpConfig) validate() (err error) {
|
|
|
|
switch {
|
|
|
|
case c == nil:
|
2024-11-13 15:44:21 +03:00
|
|
|
return errors.ErrNoValue
|
2023-06-29 14:34:06 +03:00
|
|
|
case c.Timeout.Duration <= 0:
|
2024-11-13 15:44:21 +03:00
|
|
|
return newErrNotPositive("timeout", c.Timeout)
|
2023-06-29 14:34:06 +03:00
|
|
|
default:
|
2023-07-20 18:57:06 +03:00
|
|
|
return c.Pprof.validate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// httpPprofConfig is the on-disk pprof configuration.
|
|
|
|
type httpPprofConfig struct {
|
|
|
|
Port uint16 `yaml:"port"`
|
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// type check
|
|
|
|
var _ validator = (*httpPprofConfig)(nil)
|
|
|
|
|
|
|
|
// validate implements the [validator] interface for *httpPprofConfig.
|
2023-07-20 18:57:06 +03:00
|
|
|
func (c *httpPprofConfig) validate() (err error) {
|
|
|
|
if c == nil {
|
2024-11-13 15:44:21 +03:00
|
|
|
return errors.ErrNoValue
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|
2023-07-20 18:57:06 +03:00
|
|
|
|
|
|
|
return nil
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|
2023-06-29 19:10:39 +03:00
|
|
|
|
|
|
|
// logConfig is the on-disk web API configuration.
|
|
|
|
type logConfig struct {
|
|
|
|
// TODO(a.garipov): Use.
|
|
|
|
Verbose bool `yaml:"verbose"`
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// type check
|
|
|
|
var _ validator = (*logConfig)(nil)
|
|
|
|
|
|
|
|
// validate implements the [validator] interface for *logConfig.
|
2023-06-29 19:10:39 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add more validations.
|
|
|
|
func (c *logConfig) validate() (err error) {
|
|
|
|
if c == nil {
|
2024-11-13 15:44:21 +03:00
|
|
|
return errors.ErrNoValue
|
2023-06-29 19:10:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|