2022-10-10 14:05:24 +03:00
|
|
|
package configmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2024-12-12 16:20:13 +03:00
|
|
|
"github.com/AdguardTeam/golibs/validate"
|
2022-10-10 14:05:24 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2024-12-12 16:20:13 +03:00
|
|
|
var _ validate.Interface = (*config)(nil)
|
2023-06-29 14:34:06 +03:00
|
|
|
|
2024-12-12 16:20:13 +03:00
|
|
|
// Validate implements the [validate.Interface] interface for *config.
|
|
|
|
func (c *config) Validate() (err error) {
|
2023-06-29 14:34:06 +03:00
|
|
|
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-12-12 16:20:13 +03:00
|
|
|
validators := container.KeyValues[string, validate.Interface]{{
|
2024-11-13 15:44:21 +03:00
|
|
|
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-12-12 16:20:13 +03:00
|
|
|
var errs []error
|
2024-11-13 15:44:21 +03:00
|
|
|
for _, kv := range validators {
|
2024-12-12 16:20:13 +03:00
|
|
|
errs = validate.Append(errs, kv.Key, kv.Value)
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|
|
|
|
|
2024-12-12 16:20:13 +03:00
|
|
|
return errors.Join(errs...)
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|
|
|
|
|
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
|
2024-12-12 16:20:13 +03:00
|
|
|
var _ validate.Interface = (*dnsConfig)(nil)
|
2024-11-13 15:44:21 +03:00
|
|
|
|
2024-12-12 16:20:13 +03:00
|
|
|
// Validate implements the [validate.Interface] interface for *dnsConfig.
|
2023-06-29 14:34:06 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add more validations.
|
2024-12-12 16:20:13 +03:00
|
|
|
func (c *dnsConfig) 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
|
|
|
}
|
2024-12-12 16:20:13 +03:00
|
|
|
|
|
|
|
// TODO(a.garipov): Add more validations.
|
|
|
|
|
|
|
|
return validate.Positive("upstream_timeout", c.UpstreamTimeout)
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|
|
|
|
|
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
|
2024-12-12 16:20:13 +03:00
|
|
|
var _ validate.Interface = (*httpConfig)(nil)
|
2024-11-13 15:44:21 +03:00
|
|
|
|
2024-12-12 16:20:13 +03:00
|
|
|
// Validate implements the [validate.Interface] interface for *httpConfig.
|
2023-06-29 14:34:06 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add more validations.
|
2024-12-12 16:20:13 +03:00
|
|
|
func (c *httpConfig) Validate() (err error) {
|
|
|
|
if c == nil {
|
2024-11-13 15:44:21 +03:00
|
|
|
return errors.ErrNoValue
|
2023-07-20 18:57:06 +03:00
|
|
|
}
|
2024-12-12 16:20:13 +03:00
|
|
|
|
|
|
|
errs := []error{
|
|
|
|
validate.Positive("timeout", c.Timeout),
|
|
|
|
}
|
|
|
|
|
|
|
|
errs = validate.Append(errs, "pprof", c.Pprof)
|
|
|
|
|
|
|
|
return errors.Join(errs...)
|
2023-07-20 18:57:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2024-12-12 16:20:13 +03:00
|
|
|
var _ validate.Interface = (*httpPprofConfig)(nil)
|
2024-11-13 15:44:21 +03:00
|
|
|
|
2024-12-12 16:20:13 +03:00
|
|
|
// Validate implements the [validate.Interface] interface for *httpPprofConfig.
|
|
|
|
func (c *httpPprofConfig) Validate() (err error) {
|
2023-07-20 18:57:06 +03:00
|
|
|
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 {
|
2024-12-12 16:20:13 +03:00
|
|
|
// TODO(a.garipov): Use.
|
2023-06-29 19:10:39 +03:00
|
|
|
Verbose bool `yaml:"verbose"`
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// type check
|
2024-12-12 16:20:13 +03:00
|
|
|
var _ validate.Interface = (*logConfig)(nil)
|
2024-11-13 15:44:21 +03:00
|
|
|
|
2024-12-12 16:20:13 +03:00
|
|
|
// Validate implements the [validate.Interface] interface for *logConfig.
|
2023-06-29 19:10:39 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add more validations.
|
2024-12-12 16:20:13 +03:00
|
|
|
func (c *logConfig) Validate() (err error) {
|
2023-06-29 19:10:39 +03:00
|
|
|
if c == nil {
|
2024-11-13 15:44:21 +03:00
|
|
|
return errors.ErrNoValue
|
2023-06-29 19:10:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|