2019-06-10 09:33:19 +01:00
|
|
|
package home
|
2018-08-30 15:25:33 +01:00
|
|
|
|
|
|
|
import (
|
2022-08-08 13:50:54 +01:00
|
|
|
"bytes"
|
2021-12-28 14:00:52 +00:00
|
|
|
"fmt"
|
2022-10-14 13:29:44 +01:00
|
|
|
"net/netip"
|
2018-08-30 15:25:33 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2018-10-30 14:16:20 +00:00
|
|
|
|
2022-01-19 17:45:50 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2022-10-14 18:14:07 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2021-05-21 14:15:47 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
2023-06-14 13:08:57 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/schedule"
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
2021-08-27 18:16:07 +01:00
|
|
|
"github.com/AdguardTeam/dnsproxy/fastip"
|
2021-05-24 15:28:11 +01:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-25 13:44:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-09-30 19:17:54 +01:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2021-04-06 18:48:36 +01:00
|
|
|
"github.com/google/renameio/maybe"
|
2023-02-21 13:38:22 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2022-08-08 13:50:54 +01:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2018-08-30 15:25:33 +01:00
|
|
|
)
|
|
|
|
|
2022-09-23 11:23:35 +01:00
|
|
|
// dataDir is the name of a directory under the working one to store some
|
|
|
|
// persistent data.
|
|
|
|
const dataDir = "data"
|
2018-10-30 14:16:20 +00:00
|
|
|
|
2022-08-08 13:50:54 +01:00
|
|
|
// logSettings are the logging settings part of the configuration file.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Put them into a separate object.
|
2019-02-04 10:54:53 +00:00
|
|
|
type logSettings struct {
|
2022-08-08 13:50:54 +01:00
|
|
|
// File is the path to the log file. If empty, logs are written to stdout.
|
|
|
|
// If "syslog", logs are written to syslog.
|
|
|
|
File string `yaml:"log_file"`
|
|
|
|
|
|
|
|
// MaxBackups is the maximum number of old log files to retain.
|
|
|
|
//
|
|
|
|
// NOTE: MaxAge may still cause them to get deleted.
|
|
|
|
MaxBackups int `yaml:"log_max_backups"`
|
|
|
|
|
|
|
|
// MaxSize is the maximum size of the log file before it gets rotated, in
|
|
|
|
// megabytes. The default value is 100 MB.
|
|
|
|
MaxSize int `yaml:"log_max_size"`
|
|
|
|
|
|
|
|
// MaxAge is the maximum duration for retaining old log files, in days.
|
|
|
|
MaxAge int `yaml:"log_max_age"`
|
|
|
|
|
|
|
|
// Compress determines, if the rotated log files should be compressed using
|
|
|
|
// gzip.
|
|
|
|
Compress bool `yaml:"log_compress"`
|
|
|
|
|
|
|
|
// LocalTime determines, if the time used for formatting the timestamps in
|
|
|
|
// is the computer's local time.
|
|
|
|
LocalTime bool `yaml:"log_localtime"`
|
|
|
|
|
|
|
|
// Verbose determines, if verbose (aka debug) logging is enabled.
|
|
|
|
Verbose bool `yaml:"verbose"`
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 14:35:34 +01:00
|
|
|
// osConfig contains OS-related configuration.
|
|
|
|
type osConfig struct {
|
|
|
|
// Group is the name of the group which AdGuard Home must switch to on
|
|
|
|
// startup. Empty string means no switching.
|
|
|
|
Group string `yaml:"group"`
|
|
|
|
// User is the name of the user which AdGuard Home must switch to on
|
|
|
|
// startup. Empty string means no switching.
|
|
|
|
User string `yaml:"user"`
|
|
|
|
// RlimitNoFile is the maximum number of opened fd's per process. Zero
|
|
|
|
// means use the default value.
|
|
|
|
RlimitNoFile uint64 `yaml:"rlimit_nofile"`
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:04:16 +01:00
|
|
|
type clientsConfig struct {
|
|
|
|
// Sources defines the set of sources to fetch the runtime clients from.
|
2023-03-01 10:27:24 +00:00
|
|
|
Sources *clientSourcesConfig `yaml:"runtime_sources"`
|
2022-04-26 11:04:16 +01:00
|
|
|
// Persistent are the configured clients.
|
|
|
|
Persistent []*clientObject `yaml:"persistent"`
|
|
|
|
}
|
|
|
|
|
2023-03-01 10:27:24 +00:00
|
|
|
// clientSourceConfig is used to configure where the runtime clients will be
|
|
|
|
// obtained from.
|
|
|
|
type clientSourcesConfig struct {
|
|
|
|
WHOIS bool `yaml:"whois"`
|
|
|
|
ARP bool `yaml:"arp"`
|
|
|
|
RDNS bool `yaml:"rdns"`
|
|
|
|
DHCP bool `yaml:"dhcp"`
|
|
|
|
HostsFile bool `yaml:"hosts"`
|
|
|
|
}
|
|
|
|
|
2018-08-30 15:25:33 +01:00
|
|
|
// configuration is loaded from YAML
|
2018-11-27 17:51:12 +00:00
|
|
|
// field ordering is important -- yaml fields will mirror ordering from here
|
2018-08-30 15:25:33 +01:00
|
|
|
type configuration struct {
|
2019-04-30 12:38:24 +01:00
|
|
|
// Raw file data to avoid re-reading of configuration file
|
|
|
|
// It's reset after config is parsed
|
|
|
|
fileData []byte
|
|
|
|
|
2022-10-14 13:29:44 +01:00
|
|
|
// BindHost is the address for the web interface server to listen on.
|
|
|
|
BindHost netip.Addr `yaml:"bind_host"`
|
|
|
|
// BindPort is the port for the web interface server to listen on.
|
|
|
|
BindPort int `yaml:"bind_port"`
|
|
|
|
|
|
|
|
// Users are the clients capable for accessing the web interface.
|
|
|
|
Users []webUser `yaml:"users"`
|
2021-04-27 16:56:32 +01:00
|
|
|
// AuthAttempts is the maximum number of failed login attempts a user
|
|
|
|
// can do before being blocked.
|
|
|
|
AuthAttempts uint `yaml:"auth_attempts"`
|
|
|
|
// AuthBlockMin is the duration, in minutes, of the block of new login
|
|
|
|
// attempts after AuthAttempts unsuccessful login attempts.
|
2022-10-14 13:29:44 +01:00
|
|
|
AuthBlockMin uint `yaml:"block_auth_min"`
|
|
|
|
// ProxyURL is the address of proxy server for the internal HTTP client.
|
|
|
|
ProxyURL string `yaml:"http_proxy"`
|
|
|
|
// Language is a two-letter ISO 639-1 language code.
|
|
|
|
Language string `yaml:"language"`
|
2023-01-13 16:24:17 +00:00
|
|
|
// Theme is a UI theme for current user.
|
|
|
|
Theme Theme `yaml:"theme"`
|
2022-10-14 13:29:44 +01:00
|
|
|
// DebugPProf defines if the profiling HTTP handler will listen on :6060.
|
|
|
|
DebugPProf bool `yaml:"debug_pprof"`
|
2019-03-27 14:09:48 +00:00
|
|
|
|
2019-11-12 11:23:00 +00:00
|
|
|
// TTL for a web session (in hours)
|
|
|
|
// An active session is automatically refreshed once a day.
|
|
|
|
WebSessionTTLHours uint32 `yaml:"web_session_ttl"`
|
|
|
|
|
2023-02-07 14:50:39 +00:00
|
|
|
DNS dnsConfig `yaml:"dns"`
|
|
|
|
TLS tlsConfigSettings `yaml:"tls"`
|
|
|
|
QueryLog queryLogConfig `yaml:"querylog"`
|
2023-02-13 15:15:33 +00:00
|
|
|
Stats statsConfig `yaml:"statistics"`
|
2020-02-26 16:58:25 +00:00
|
|
|
|
2022-09-23 11:23:35 +01:00
|
|
|
// Filters reflects the filters from [filtering.Config]. It's cloned to the
|
|
|
|
// config used in the filtering module at the startup. Afterwards it's
|
|
|
|
// cloned from the filtering module back here.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Move all the filtering configuration fields into the
|
|
|
|
// only configuration subsection covering the changes with a single
|
2022-10-14 13:29:44 +01:00
|
|
|
// migration. Also keep the blocked services in mind.
|
2022-09-23 11:23:35 +01:00
|
|
|
Filters []filtering.FilterYAML `yaml:"filters"`
|
|
|
|
WhitelistFilters []filtering.FilterYAML `yaml:"whitelist_filters"`
|
|
|
|
UserRules []string `yaml:"user_rules"`
|
2020-02-26 16:58:25 +00:00
|
|
|
|
2022-01-25 16:47:02 +00:00
|
|
|
DHCP *dhcpd.ServerConfig `yaml:"dhcp"`
|
2018-08-30 15:25:33 +01:00
|
|
|
|
2021-12-13 12:24:35 +00:00
|
|
|
// Clients contains the YAML representations of the persistent clients.
|
|
|
|
// This field is only used for reading and writing persistent client data.
|
|
|
|
// Keep this field sorted to ensure consistent ordering.
|
2022-04-26 11:04:16 +01:00
|
|
|
Clients *clientsConfig `yaml:"clients"`
|
2019-04-26 14:04:22 +01:00
|
|
|
|
2019-02-04 10:54:53 +00:00
|
|
|
logSettings `yaml:",inline"`
|
|
|
|
|
2021-06-04 14:35:34 +01:00
|
|
|
OSConfig *osConfig `yaml:"os"`
|
|
|
|
|
2018-10-06 22:58:59 +01:00
|
|
|
sync.RWMutex `yaml:"-"`
|
2018-11-27 17:51:12 +00:00
|
|
|
|
|
|
|
SchemaVersion int `yaml:"schema_version"` // keeping last so that users will be less tempted to change it -- used when upgrading between versions
|
2018-08-30 15:25:33 +01:00
|
|
|
}
|
|
|
|
|
2018-11-27 17:51:12 +00:00
|
|
|
// field ordering is important -- yaml fields will mirror ordering from here
|
2018-12-05 17:29:00 +00:00
|
|
|
type dnsConfig struct {
|
2022-10-14 13:29:44 +01:00
|
|
|
BindHosts []netip.Addr `yaml:"bind_hosts"`
|
|
|
|
Port int `yaml:"port"`
|
2018-11-28 15:24:04 +00:00
|
|
|
|
2022-10-14 13:29:44 +01:00
|
|
|
// AnonymizeClientIP defines if clients' IP addresses should be anonymized
|
|
|
|
// in query log and statistics.
|
|
|
|
AnonymizeClientIP bool `yaml:"anonymize_client_ip"`
|
2019-10-30 08:52:58 +00:00
|
|
|
|
2018-11-28 15:24:04 +00:00
|
|
|
dnsforward.FilteringConfig `yaml:",inline"`
|
|
|
|
|
2022-09-23 11:23:35 +01:00
|
|
|
DnsfilterConf *filtering.Config `yaml:",inline"`
|
2021-03-25 13:00:27 +00:00
|
|
|
|
2021-06-15 15:36:49 +01:00
|
|
|
// UpstreamTimeout is the timeout for querying upstream servers.
|
2021-09-30 19:17:54 +01:00
|
|
|
UpstreamTimeout timeutil.Duration `yaml:"upstream_timeout"`
|
2021-06-15 15:36:49 +01:00
|
|
|
|
2022-03-18 10:37:27 +00:00
|
|
|
// PrivateNets is the set of IP networks for which the private reverse DNS
|
|
|
|
// resolver should be used.
|
|
|
|
PrivateNets []string `yaml:"private_networks"`
|
|
|
|
|
2021-05-26 15:55:19 +01:00
|
|
|
// UsePrivateRDNS defines if the PTR requests for unknown addresses from
|
|
|
|
// locally-served networks should be resolved via private PTR resolvers.
|
|
|
|
UsePrivateRDNS bool `yaml:"use_private_ptr_resolvers"`
|
|
|
|
|
2021-04-07 18:16:06 +01:00
|
|
|
// LocalPTRResolvers is the slice of addresses to be used as upstreams
|
|
|
|
// for PTR queries for locally-served networks.
|
|
|
|
LocalPTRResolvers []string `yaml:"local_ptr_upstreams"`
|
2022-10-03 16:08:05 +01:00
|
|
|
|
2023-01-23 16:10:56 +00:00
|
|
|
// UseDNS64 defines if DNS64 should be used for incoming requests.
|
|
|
|
UseDNS64 bool `yaml:"use_dns64"`
|
|
|
|
|
|
|
|
// DNS64Prefixes is the list of NAT64 prefixes to be used for DNS64.
|
2023-02-06 14:17:51 +00:00
|
|
|
DNS64Prefixes []netip.Prefix `yaml:"dns64_prefixes"`
|
2023-01-23 16:10:56 +00:00
|
|
|
|
2022-10-03 16:08:05 +01:00
|
|
|
// ServeHTTP3 defines if HTTP/3 is be allowed for incoming requests.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add to the UI when HTTP/3 support is no longer
|
|
|
|
// experimental.
|
|
|
|
ServeHTTP3 bool `yaml:"serve_http3"`
|
|
|
|
|
|
|
|
// UseHTTP3Upstreams defines if HTTP/3 is be allowed for DNS-over-HTTPS
|
|
|
|
// upstreams.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add to the UI when HTTP/3 support is no longer
|
|
|
|
// experimental.
|
|
|
|
UseHTTP3Upstreams bool `yaml:"use_http3_upstreams"`
|
2018-08-30 15:25:33 +01:00
|
|
|
}
|
|
|
|
|
2019-02-13 08:08:07 +00:00
|
|
|
type tlsConfigSettings struct {
|
2021-06-18 16:13:36 +01:00
|
|
|
Enabled bool `yaml:"enabled" json:"enabled"` // Enabled is the encryption (DoT/DoH/HTTPS) status
|
2020-08-27 13:03:07 +01:00
|
|
|
ServerName string `yaml:"server_name" json:"server_name,omitempty"` // ServerName is the hostname of your HTTPS/TLS server
|
2021-12-13 10:48:08 +00:00
|
|
|
ForceHTTPS bool `yaml:"force_https" json:"force_https"` // ForceHTTPS: if true, forces HTTP->HTTPS redirect
|
2020-08-27 13:03:07 +01:00
|
|
|
PortHTTPS int `yaml:"port_https" json:"port_https,omitempty"` // HTTPS port. If 0, HTTPS will be disabled
|
2021-06-18 16:13:36 +01:00
|
|
|
PortDNSOverTLS int `yaml:"port_dns_over_tls" json:"port_dns_over_tls,omitempty"` // DNS-over-TLS port. If 0, DoT will be disabled
|
2021-03-23 09:32:07 +00:00
|
|
|
PortDNSOverQUIC int `yaml:"port_dns_over_quic" json:"port_dns_over_quic,omitempty"` // DNS-over-QUIC port. If 0, DoQ will be disabled
|
2019-02-12 14:23:38 +00:00
|
|
|
|
2020-12-07 14:58:33 +00:00
|
|
|
// PortDNSCrypt is the port for DNSCrypt requests. If it's zero,
|
|
|
|
// DNSCrypt is disabled.
|
|
|
|
PortDNSCrypt int `yaml:"port_dnscrypt" json:"port_dnscrypt"`
|
|
|
|
// DNSCryptConfigFile is the path to the DNSCrypt config file. Must be
|
|
|
|
// set if PortDNSCrypt is not zero.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/dnsproxy and
|
|
|
|
// https://github.com/ameshkov/dnscrypt.
|
|
|
|
DNSCryptConfigFile string `yaml:"dnscrypt_config_file" json:"dnscrypt_config_file"`
|
|
|
|
|
2021-06-18 16:13:36 +01:00
|
|
|
// Allow DoH queries via unencrypted HTTP (e.g. for reverse proxying)
|
|
|
|
AllowUnencryptedDoH bool `yaml:"allow_unencrypted_doh" json:"allow_unencrypted_doh"`
|
2019-12-13 12:59:36 +00:00
|
|
|
|
2019-02-12 14:23:38 +00:00
|
|
|
dnsforward.TLSConfig `yaml:",inline" json:",inline"`
|
2019-02-13 08:08:07 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:50:39 +00:00
|
|
|
type queryLogConfig struct {
|
2023-03-23 10:46:57 +00:00
|
|
|
// Ignored is the list of host names, which should not be written to log.
|
|
|
|
Ignored []string `yaml:"ignored"`
|
2023-02-07 14:50:39 +00:00
|
|
|
|
|
|
|
// Interval is the interval for query log's files rotation.
|
|
|
|
Interval timeutil.Duration `yaml:"interval"`
|
|
|
|
|
2023-03-23 10:46:57 +00:00
|
|
|
// MemSize is the number of entries kept in memory before they are flushed
|
|
|
|
// to disk.
|
2023-02-07 14:50:39 +00:00
|
|
|
MemSize uint32 `yaml:"size_memory"`
|
|
|
|
|
2023-03-23 10:46:57 +00:00
|
|
|
// Enabled defines if the query log is enabled.
|
2023-02-13 15:15:33 +00:00
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
|
2023-03-23 10:46:57 +00:00
|
|
|
// FileEnabled defines, if the query log is written to the file.
|
|
|
|
FileEnabled bool `yaml:"file_enabled"`
|
|
|
|
}
|
2023-02-13 15:15:33 +00:00
|
|
|
|
2023-03-23 10:46:57 +00:00
|
|
|
type statsConfig struct {
|
2023-02-13 15:15:33 +00:00
|
|
|
// Ignored is the list of host names, which should not be counted.
|
2023-02-07 14:50:39 +00:00
|
|
|
Ignored []string `yaml:"ignored"`
|
2023-03-23 10:46:57 +00:00
|
|
|
|
|
|
|
// Interval is the retention interval for statistics.
|
|
|
|
Interval timeutil.Duration `yaml:"interval"`
|
|
|
|
|
|
|
|
// Enabled defines if the statistics are enabled.
|
|
|
|
Enabled bool `yaml:"enabled"`
|
2023-02-07 14:50:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 14:00:36 +01:00
|
|
|
// config is the global configuration structure.
|
|
|
|
//
|
2021-12-27 16:40:39 +00:00
|
|
|
// TODO(a.garipov, e.burkov): This global is awful and must be removed.
|
2021-09-13 14:00:36 +01:00
|
|
|
var config = &configuration{
|
2022-09-23 11:23:35 +01:00
|
|
|
BindPort: 3000,
|
2022-10-14 13:29:44 +01:00
|
|
|
BindHost: netip.IPv4Unspecified(),
|
2022-09-23 11:23:35 +01:00
|
|
|
AuthAttempts: 5,
|
|
|
|
AuthBlockMin: 15,
|
|
|
|
WebSessionTTLHours: 30 * 24,
|
2018-12-05 17:29:00 +00:00
|
|
|
DNS: dnsConfig{
|
2023-02-13 15:15:33 +00:00
|
|
|
BindHosts: []netip.Addr{netip.IPv4Unspecified()},
|
|
|
|
Port: defaultPortDNS,
|
2018-11-28 15:24:04 +00:00
|
|
|
FilteringConfig: dnsforward.FilteringConfig{
|
2022-09-06 15:09:54 +01:00
|
|
|
ProtectionEnabled: true, // whether or not use any of filtering features
|
|
|
|
BlockingMode: dnsforward.BlockingModeDefault,
|
|
|
|
BlockedResponseTTL: 10, // in seconds
|
2019-10-30 08:52:58 +00:00
|
|
|
Ratelimit: 20,
|
|
|
|
RefuseAny: true,
|
|
|
|
AllServers: false,
|
2022-04-27 09:39:48 +01:00
|
|
|
HandleDDR: true,
|
2021-09-30 19:17:54 +01:00
|
|
|
FastestTimeout: timeutil.Duration{
|
2021-08-27 18:16:07 +01:00
|
|
|
Duration: fastip.DefaultPingWaitTimeout,
|
|
|
|
},
|
2020-11-05 10:26:13 +00:00
|
|
|
|
2021-07-26 17:30:35 +01:00
|
|
|
TrustedProxies: []string{"127.0.0.0/8", "::1/128"},
|
2022-10-11 17:49:35 +01:00
|
|
|
CacheSize: 4 * 1024 * 1024,
|
2021-07-26 17:30:35 +01:00
|
|
|
|
2023-03-07 11:37:14 +00:00
|
|
|
EDNSClientSubnet: &dnsforward.EDNSClientSubnet{
|
2023-03-22 10:42:20 +00:00
|
|
|
CustomIP: netip.Addr{},
|
2023-03-07 11:37:14 +00:00
|
|
|
Enabled: false,
|
|
|
|
UseCustom: false,
|
|
|
|
},
|
|
|
|
|
2020-11-05 10:26:13 +00:00
|
|
|
// set default maximum concurrent queries to 300
|
|
|
|
// we introduced a default limit due to this:
|
|
|
|
// https://github.com/AdguardTeam/AdGuardHome/issues/2015#issuecomment-674041912
|
|
|
|
// was later increased to 300 due to https://github.com/AdguardTeam/AdGuardHome/issues/2257
|
|
|
|
MaxGoroutines: 300,
|
2018-11-28 15:24:04 +00:00
|
|
|
},
|
2022-09-23 11:23:35 +01:00
|
|
|
DnsfilterConf: &filtering.Config{
|
|
|
|
FilteringEnabled: true,
|
|
|
|
FiltersUpdateIntervalHours: 24,
|
2023-04-13 18:17:59 +01:00
|
|
|
|
|
|
|
ParentalEnabled: false,
|
|
|
|
SafeBrowsingEnabled: false,
|
|
|
|
|
|
|
|
SafeBrowsingCacheSize: 1 * 1024 * 1024,
|
|
|
|
SafeSearchCacheSize: 1 * 1024 * 1024,
|
|
|
|
ParentalCacheSize: 1 * 1024 * 1024,
|
|
|
|
CacheTime: 30,
|
|
|
|
|
|
|
|
SafeSearchConf: filtering.SafeSearchConfig{
|
|
|
|
Enabled: false,
|
|
|
|
Bing: true,
|
|
|
|
DuckDuckGo: true,
|
|
|
|
Google: true,
|
|
|
|
Pixabay: true,
|
|
|
|
Yandex: true,
|
|
|
|
YouTube: true,
|
|
|
|
},
|
2023-06-14 13:08:57 +01:00
|
|
|
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
|
|
|
IDs: []string{},
|
|
|
|
},
|
2022-09-23 11:23:35 +01:00
|
|
|
},
|
|
|
|
UpstreamTimeout: timeutil.Duration{Duration: dnsforward.DefaultTimeout},
|
|
|
|
UsePrivateRDNS: true,
|
2018-08-30 15:25:33 +01:00
|
|
|
},
|
2020-02-19 12:28:06 +00:00
|
|
|
TLS: tlsConfigSettings{
|
2021-09-17 16:31:07 +01:00
|
|
|
PortHTTPS: defaultPortHTTPS,
|
|
|
|
PortDNSOverTLS: defaultPortTLS, // needs to be passed through to dnsproxy
|
|
|
|
PortDNSOverQUIC: defaultPortQUIC,
|
2019-02-11 18:52:39 +00:00
|
|
|
},
|
2023-02-07 14:50:39 +00:00
|
|
|
QueryLog: queryLogConfig{
|
|
|
|
Enabled: true,
|
|
|
|
FileEnabled: true,
|
|
|
|
Interval: timeutil.Duration{Duration: 90 * timeutil.Day},
|
|
|
|
MemSize: 1000,
|
|
|
|
Ignored: []string{},
|
|
|
|
},
|
2023-02-13 15:15:33 +00:00
|
|
|
Stats: statsConfig{
|
|
|
|
Enabled: true,
|
2023-03-23 10:46:57 +00:00
|
|
|
Interval: timeutil.Duration{Duration: 1 * timeutil.Day},
|
2023-02-13 15:15:33 +00:00
|
|
|
Ignored: []string{},
|
|
|
|
},
|
2022-12-08 13:27:10 +00:00
|
|
|
// NOTE: Keep these parameters in sync with the one put into
|
|
|
|
// client/src/helpers/filters/filters.js by scripts/vetted-filters.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Think of a way to make scripts/vetted-filters update
|
|
|
|
// these as well if necessary.
|
2022-09-23 11:23:35 +01:00
|
|
|
Filters: []filtering.FilterYAML{{
|
|
|
|
Filter: filtering.Filter{ID: 1},
|
|
|
|
Enabled: true,
|
2022-12-08 13:27:10 +00:00
|
|
|
URL: "https://adguardteam.github.io/HostlistsRegistry/assets/filter_1.txt",
|
2022-09-23 11:23:35 +01:00
|
|
|
Name: "AdGuard DNS filter",
|
|
|
|
}, {
|
|
|
|
Filter: filtering.Filter{ID: 2},
|
|
|
|
Enabled: false,
|
2022-12-08 13:27:10 +00:00
|
|
|
URL: "https://adguardteam.github.io/HostlistsRegistry/assets/filter_2.txt",
|
2022-09-23 11:23:35 +01:00
|
|
|
Name: "AdAway Default Blocklist",
|
|
|
|
}},
|
2022-01-25 16:47:02 +00:00
|
|
|
DHCP: &dhcpd.ServerConfig{
|
|
|
|
LocalDomainName: "lan",
|
2022-09-23 11:23:35 +01:00
|
|
|
Conf4: dhcpd.V4ServerConf{
|
|
|
|
LeaseDuration: dhcpd.DefaultDHCPLeaseTTL,
|
|
|
|
ICMPTimeout: dhcpd.DefaultDHCPTimeoutICMP,
|
|
|
|
},
|
|
|
|
Conf6: dhcpd.V6ServerConf{
|
|
|
|
LeaseDuration: dhcpd.DefaultDHCPLeaseTTL,
|
|
|
|
},
|
2022-01-25 16:47:02 +00:00
|
|
|
},
|
2022-04-26 11:04:16 +01:00
|
|
|
Clients: &clientsConfig{
|
2023-03-01 10:27:24 +00:00
|
|
|
Sources: &clientSourcesConfig{
|
2022-04-26 11:04:16 +01:00
|
|
|
WHOIS: true,
|
|
|
|
ARP: true,
|
|
|
|
RDNS: true,
|
|
|
|
DHCP: true,
|
|
|
|
HostsFile: true,
|
|
|
|
},
|
|
|
|
},
|
2020-06-02 12:20:12 +01:00
|
|
|
logSettings: logSettings{
|
2022-08-08 13:50:54 +01:00
|
|
|
Compress: false,
|
|
|
|
LocalTime: false,
|
|
|
|
MaxBackups: 0,
|
|
|
|
MaxSize: 100,
|
|
|
|
MaxAge: 3,
|
2020-06-02 12:20:12 +01:00
|
|
|
},
|
2021-06-04 14:35:34 +01:00
|
|
|
OSConfig: &osConfig{},
|
2018-12-05 21:29:38 +00:00
|
|
|
SchemaVersion: currentSchemaVersion,
|
2023-01-13 16:24:17 +00:00
|
|
|
Theme: ThemeAuto,
|
2018-08-30 15:25:33 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 17:35:48 +00:00
|
|
|
// getConfigFilename returns path to the current config file
|
|
|
|
func (c *configuration) getConfigFilename() string {
|
2020-02-13 15:42:07 +00:00
|
|
|
configFile, err := filepath.EvalSymlinks(Context.configFilename)
|
2019-03-14 15:06:53 +00:00
|
|
|
if err != nil {
|
2021-01-27 15:32:13 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2019-03-14 15:06:53 +00:00
|
|
|
log.Error("unexpected error while config file path evaluation: %s", err)
|
|
|
|
}
|
2020-02-13 15:42:07 +00:00
|
|
|
configFile = Context.configFilename
|
2019-03-14 15:06:53 +00:00
|
|
|
}
|
2019-02-05 17:35:48 +00:00
|
|
|
if !filepath.IsAbs(configFile) {
|
2020-02-13 15:42:07 +00:00
|
|
|
configFile = filepath.Join(Context.workDir, configFile)
|
2019-02-05 17:35:48 +00:00
|
|
|
}
|
|
|
|
return configFile
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// readLogSettings reads logging settings from the config file. We do it in a
|
|
|
|
// separate method in order to configure logger before the actual configuration
|
|
|
|
// is parsed and applied.
|
|
|
|
func readLogSettings() (ls *logSettings) {
|
|
|
|
ls = &logSettings{}
|
|
|
|
|
2019-02-04 10:54:53 +00:00
|
|
|
yamlFile, err := readConfigFile()
|
2019-04-30 12:38:24 +01:00
|
|
|
if err != nil {
|
2023-05-10 14:30:03 +01:00
|
|
|
return ls
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
2023-05-10 14:30:03 +01:00
|
|
|
|
|
|
|
err = yaml.Unmarshal(yamlFile, ls)
|
2019-02-04 10:54:53 +00:00
|
|
|
if err != nil {
|
2019-02-25 13:44:22 +00:00
|
|
|
log.Error("Couldn't get logging settings from the configuration: %s", err)
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
2023-05-10 14:30:03 +01:00
|
|
|
|
|
|
|
return ls
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 16:57:53 +01:00
|
|
|
// validateBindHosts returns error if any of binding hosts from configuration is
|
|
|
|
// not a valid IP address.
|
|
|
|
func validateBindHosts(conf *configuration) (err error) {
|
|
|
|
if !conf.BindHost.IsValid() {
|
|
|
|
return errors.Error("bind_host is not a valid ip address")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, addr := range conf.DNS.BindHosts {
|
|
|
|
if !addr.IsValid() {
|
|
|
|
return fmt.Errorf("dns.bind_hosts at index %d is not a valid ip address", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:54:53 +00:00
|
|
|
// parseConfig loads configuration from the YAML file
|
2021-12-16 17:54:59 +00:00
|
|
|
func parseConfig() (err error) {
|
|
|
|
var fileData []byte
|
|
|
|
fileData, err = readConfigFile()
|
2018-08-30 15:25:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-16 17:54:59 +00:00
|
|
|
|
2019-04-30 12:38:24 +01:00
|
|
|
config.fileData = nil
|
2021-12-16 17:54:59 +00:00
|
|
|
err = yaml.Unmarshal(fileData, &config)
|
2018-08-30 15:25:33 +01:00
|
|
|
if err != nil {
|
2023-04-21 16:57:53 +01:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateBindHosts(config)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
2021-12-16 17:54:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-03 12:36:18 +01:00
|
|
|
tcpPorts := aghalg.UniqChecker[tcpPort]{}
|
2023-01-23 09:47:59 +00:00
|
|
|
addPorts(tcpPorts, tcpPort(config.BindPort))
|
2022-08-03 12:36:18 +01:00
|
|
|
|
|
|
|
udpPorts := aghalg.UniqChecker[udpPort]{}
|
|
|
|
addPorts(udpPorts, udpPort(config.DNS.Port))
|
2021-12-28 14:00:52 +00:00
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
if config.TLS.Enabled {
|
2021-12-28 14:00:52 +00:00
|
|
|
addPorts(
|
2022-08-03 12:36:18 +01:00
|
|
|
tcpPorts,
|
2022-03-23 13:00:32 +00:00
|
|
|
tcpPort(config.TLS.PortHTTPS),
|
|
|
|
tcpPort(config.TLS.PortDNSOverTLS),
|
|
|
|
tcpPort(config.TLS.PortDNSCrypt),
|
2021-12-16 17:54:59 +00:00
|
|
|
)
|
2022-08-03 12:36:18 +01:00
|
|
|
|
|
|
|
// TODO(e.burkov): Consider adding a udpPort with the same value when
|
|
|
|
// we add support for HTTP/3 for web admin interface.
|
|
|
|
addPorts(udpPorts, udpPort(config.TLS.PortDNSOverQUIC))
|
2021-12-16 17:54:59 +00:00
|
|
|
}
|
2022-10-14 18:14:07 +01:00
|
|
|
|
2022-08-03 12:36:18 +01:00
|
|
|
if err = tcpPorts.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("validating tcp ports: %w", err)
|
|
|
|
} else if err = udpPorts.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("validating udp ports: %w", err)
|
2018-08-30 15:25:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-23 11:23:35 +01:00
|
|
|
if !filtering.ValidateUpdateIvl(config.DNS.DnsfilterConf.FiltersUpdateIntervalHours) {
|
|
|
|
config.DNS.DnsfilterConf.FiltersUpdateIntervalHours = 24
|
2019-09-04 12:12:00 +01:00
|
|
|
}
|
2019-08-08 10:41:00 +01:00
|
|
|
|
2021-06-15 15:36:49 +01:00
|
|
|
if config.DNS.UpstreamTimeout.Duration == 0 {
|
2021-09-30 19:17:54 +01:00
|
|
|
config.DNS.UpstreamTimeout = timeutil.Duration{Duration: dnsforward.DefaultTimeout}
|
2021-06-15 15:36:49 +01:00
|
|
|
}
|
|
|
|
|
2022-10-14 18:14:07 +01:00
|
|
|
err = setContextTLSCipherIDs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-30 15:25:33 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:00:32 +00:00
|
|
|
// udpPort is the port number for UDP protocol.
|
|
|
|
type udpPort int
|
|
|
|
|
|
|
|
// tcpPort is the port number for TCP protocol.
|
|
|
|
type tcpPort int
|
|
|
|
|
2022-08-03 12:36:18 +01:00
|
|
|
// addPorts is a helper for ports validation that skips zero ports.
|
|
|
|
func addPorts[T tcpPort | udpPort](uc aghalg.UniqChecker[T], ports ...T) {
|
2021-12-28 14:00:52 +00:00
|
|
|
for _, p := range ports {
|
2022-08-03 12:36:18 +01:00
|
|
|
if p != 0 {
|
|
|
|
uc.Add(p)
|
2021-12-28 14:00:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
// readConfigFile reads configuration file contents.
|
|
|
|
func readConfigFile() (fileData []byte, err error) {
|
|
|
|
if len(config.fileData) > 0 {
|
2019-04-30 12:38:24 +01:00
|
|
|
return config.fileData, nil
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
name := config.getConfigFilename()
|
|
|
|
log.Debug("reading config file: %s", name)
|
|
|
|
|
|
|
|
// Do not wrap the error because it's informative enough as is.
|
|
|
|
return os.ReadFile(name)
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 09:24:59 +00:00
|
|
|
// Saves configuration to the YAML file and also saves the user filter contents to a file
|
2022-08-08 13:50:54 +01:00
|
|
|
func (c *configuration) write() (err error) {
|
2018-11-29 10:31:50 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2019-04-26 14:04:22 +01:00
|
|
|
|
2020-02-13 15:42:07 +00:00
|
|
|
if Context.auth != nil {
|
|
|
|
config.Users = Context.auth.GetUsers()
|
2019-08-29 10:34:07 +01:00
|
|
|
}
|
2022-08-08 13:50:54 +01:00
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
if Context.tls != nil {
|
|
|
|
tlsConf := tlsConfigSettings{}
|
|
|
|
Context.tls.WriteDiskConfig(&tlsConf)
|
|
|
|
config.TLS = tlsConf
|
|
|
|
}
|
2019-08-29 10:34:07 +01:00
|
|
|
|
2019-12-11 09:38:58 +00:00
|
|
|
if Context.stats != nil {
|
2023-02-13 15:15:33 +00:00
|
|
|
statsConf := stats.Config{}
|
|
|
|
Context.stats.WriteDiskConfig(&statsConf)
|
2023-03-23 10:46:57 +00:00
|
|
|
config.Stats.Interval = timeutil.Duration{Duration: statsConf.Limit}
|
2023-02-13 15:15:33 +00:00
|
|
|
config.Stats.Enabled = statsConf.Enabled
|
|
|
|
config.Stats.Ignored = statsConf.Ignored.Values()
|
2023-02-21 13:38:22 +00:00
|
|
|
slices.Sort(config.Stats.Ignored)
|
2019-09-25 13:36:09 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 09:38:58 +00:00
|
|
|
if Context.queryLog != nil {
|
2020-05-28 13:29:36 +01:00
|
|
|
dc := querylog.Config{}
|
2019-12-11 09:38:58 +00:00
|
|
|
Context.queryLog.WriteDiskConfig(&dc)
|
2020-03-03 17:21:53 +00:00
|
|
|
config.DNS.AnonymizeClientIP = dc.AnonymizeClientIP
|
2023-02-07 14:50:39 +00:00
|
|
|
config.QueryLog.Enabled = dc.Enabled
|
|
|
|
config.QueryLog.FileEnabled = dc.FileEnabled
|
|
|
|
config.QueryLog.Interval = timeutil.Duration{Duration: dc.RotationIvl}
|
|
|
|
config.QueryLog.MemSize = dc.MemSize
|
|
|
|
config.QueryLog.Ignored = dc.Ignored.Values()
|
2023-02-21 13:38:22 +00:00
|
|
|
slices.Sort(config.Stats.Ignored)
|
2019-09-27 16:58:57 +01:00
|
|
|
}
|
|
|
|
|
2022-09-23 11:23:35 +01:00
|
|
|
if Context.filters != nil {
|
|
|
|
Context.filters.WriteDiskConfig(config.DNS.DnsfilterConf)
|
|
|
|
config.Filters = config.DNS.DnsfilterConf.Filters
|
|
|
|
config.WhitelistFilters = config.DNS.DnsfilterConf.WhitelistFilters
|
|
|
|
config.UserRules = config.DNS.DnsfilterConf.UserRules
|
2019-10-09 17:51:26 +01:00
|
|
|
}
|
|
|
|
|
2021-04-07 18:16:06 +01:00
|
|
|
if s := Context.dnsServer; s != nil {
|
2019-10-30 08:52:58 +00:00
|
|
|
c := dnsforward.FilteringConfig{}
|
2021-04-07 18:16:06 +01:00
|
|
|
s.WriteDiskConfig(&c)
|
2021-05-26 15:55:19 +01:00
|
|
|
dns := &config.DNS
|
|
|
|
dns.FilteringConfig = c
|
2022-04-26 11:04:16 +01:00
|
|
|
dns.LocalPTRResolvers, config.Clients.Sources.RDNS, dns.UsePrivateRDNS = s.RDNSSettings()
|
2019-10-30 08:52:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 09:38:58 +00:00
|
|
|
if Context.dhcpServer != nil {
|
2022-09-13 21:45:35 +01:00
|
|
|
Context.dhcpServer.WriteDiskConfig(config.DHCP)
|
2019-11-22 11:21:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 11:04:16 +01:00
|
|
|
config.Clients.Persistent = Context.clients.forConfig()
|
2021-12-13 12:18:21 +00:00
|
|
|
|
2019-02-05 17:35:48 +00:00
|
|
|
configFile := config.getConfigFilename()
|
2022-08-08 13:50:54 +01:00
|
|
|
log.Debug("writing config file %q", configFile)
|
2021-04-06 18:48:36 +01:00
|
|
|
|
2022-08-08 13:50:54 +01:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
enc := yaml.NewEncoder(buf)
|
|
|
|
enc.SetIndent(2)
|
2021-04-06 18:48:36 +01:00
|
|
|
|
2022-08-08 13:50:54 +01:00
|
|
|
err = enc.Encode(config)
|
2018-08-30 15:25:33 +01:00
|
|
|
if err != nil {
|
2022-08-08 13:50:54 +01:00
|
|
|
return fmt.Errorf("generating config file: %w", err)
|
|
|
|
}
|
2021-04-06 18:48:36 +01:00
|
|
|
|
2022-08-08 13:50:54 +01:00
|
|
|
err = maybe.WriteFile(configFile, buf.Bytes(), 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("writing config file: %w", err)
|
2018-08-30 15:25:33 +01:00
|
|
|
}
|
2018-10-29 23:17:24 +00:00
|
|
|
|
2018-11-28 17:15:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-14 18:14:07 +01:00
|
|
|
|
|
|
|
// setContextTLSCipherIDs sets the TLS cipher suite IDs to use.
|
|
|
|
func setContextTLSCipherIDs() (err error) {
|
|
|
|
if len(config.TLS.OverrideTLSCiphers) == 0 {
|
|
|
|
log.Info("tls: using default ciphers")
|
|
|
|
|
|
|
|
Context.tlsCipherIDs = aghtls.SaferCipherSuites()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("tls: overriding ciphers: %s", config.TLS.OverrideTLSCiphers)
|
|
|
|
|
|
|
|
Context.tlsCipherIDs, err = aghtls.ParseCiphers(config.TLS.OverrideTLSCiphers)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing override ciphers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|