2020-05-08 08:39:37 -07:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2023-02-06 07:17:51 -07:00
|
|
|
"net/netip"
|
2021-05-21 04:55:42 -07:00
|
|
|
"os"
|
2021-05-28 03:02:59 -07:00
|
|
|
"strings"
|
2021-06-15 07:36:49 -07:00
|
|
|
"time"
|
2020-05-08 08:39:37 -07:00
|
|
|
|
2022-09-06 07:09:54 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2022-08-04 09:05:28 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2022-11-02 04:37:41 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
|
2021-05-21 06:15:47 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-05-08 08:39:37 -07:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2021-05-24 07:28:11 -07:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-09-08 07:28:01 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 06:03:37 -07:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-07-29 07:40:31 -07:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2021-09-30 11:17:54 -07:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2020-12-07 07:58:33 -07:00
|
|
|
"github.com/ameshkov/dnscrypt/v2"
|
2023-02-21 06:38:22 -07:00
|
|
|
"golang.org/x/exp/slices"
|
2020-05-08 08:39:37 -07:00
|
|
|
)
|
|
|
|
|
2021-06-17 04:09:16 -07:00
|
|
|
// BlockingMode is an enum of all allowed blocking modes.
|
|
|
|
type BlockingMode string
|
|
|
|
|
|
|
|
// Allowed blocking modes.
|
|
|
|
const (
|
|
|
|
// BlockingModeCustomIP means respond with a custom IP address.
|
|
|
|
BlockingModeCustomIP BlockingMode = "custom_ip"
|
|
|
|
|
|
|
|
// BlockingModeDefault is the same as BlockingModeNullIP for
|
|
|
|
// Adblock-style rules, but responds with the IP address specified in
|
|
|
|
// the rule when blocked by an `/etc/hosts`-style rule.
|
|
|
|
BlockingModeDefault BlockingMode = "default"
|
|
|
|
|
|
|
|
// BlockingModeNullIP means respond with a zero IP address: "0.0.0.0"
|
|
|
|
// for A requests and "::" for AAAA ones.
|
|
|
|
BlockingModeNullIP BlockingMode = "null_ip"
|
|
|
|
|
|
|
|
// BlockingModeNXDOMAIN means respond with the NXDOMAIN code.
|
|
|
|
BlockingModeNXDOMAIN BlockingMode = "nxdomain"
|
|
|
|
|
|
|
|
// BlockingModeREFUSED means respond with the REFUSED code.
|
|
|
|
BlockingModeREFUSED BlockingMode = "refused"
|
|
|
|
)
|
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
// FilteringConfig represents the DNS filtering configuration of AdGuard Home
|
|
|
|
// The zero FilteringConfig is empty and ready for use.
|
|
|
|
type FilteringConfig struct {
|
|
|
|
// Callbacks for other modules
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// FilterHandler is an optional additional filtering callback.
|
2021-05-21 06:15:47 -07:00
|
|
|
FilterHandler func(clientAddr net.IP, clientID string, settings *filtering.Settings) `yaml:"-"`
|
2020-05-08 08:39:37 -07:00
|
|
|
|
2021-05-28 03:02:59 -07:00
|
|
|
// GetCustomUpstreamByClient is a callback that returns upstreams
|
|
|
|
// configuration based on the client IP address or ClientID. It returns
|
|
|
|
// nil if there are no custom upstreams for the client.
|
|
|
|
GetCustomUpstreamByClient func(id string) (conf *proxy.UpstreamConfig, err error) `yaml:"-"`
|
2020-05-08 08:39:37 -07:00
|
|
|
|
|
|
|
// Protection configuration
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// ProtectionEnabled defines whether or not use any of filtering features.
|
|
|
|
ProtectionEnabled bool `yaml:"protection_enabled"`
|
2020-05-08 08:39:37 -07:00
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// BlockingMode defines the way how blocked responses are constructed.
|
|
|
|
BlockingMode BlockingMode `yaml:"blocking_mode"`
|
|
|
|
|
|
|
|
// BlockingIPv4 is the IP address to be returned for a blocked A request.
|
|
|
|
BlockingIPv4 net.IP `yaml:"blocking_ipv4"`
|
|
|
|
|
|
|
|
// BlockingIPv6 is the IP address to be returned for a blocked AAAA
|
|
|
|
// request.
|
|
|
|
BlockingIPv6 net.IP `yaml:"blocking_ipv6"`
|
|
|
|
|
|
|
|
// BlockedResponseTTL is the time-to-live value for blocked responses. If
|
|
|
|
// 0, then default value is used (3600).
|
|
|
|
BlockedResponseTTL uint32 `yaml:"blocked_response_ttl"`
|
|
|
|
|
2023-03-24 05:11:47 -07:00
|
|
|
// ProtectionDisabledUntil is the timestamp until when the protection is
|
|
|
|
// disabled.
|
|
|
|
ProtectionDisabledUntil *time.Time `yaml:"protection_disabled_until"`
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// ParentalBlockHost is the IP (or domain name) which is used to respond to
|
|
|
|
// DNS requests blocked by parental control.
|
|
|
|
ParentalBlockHost string `yaml:"parental_block_host"`
|
|
|
|
|
|
|
|
// SafeBrowsingBlockHost is the IP (or domain name) which is used to
|
|
|
|
// respond to DNS requests blocked by safe-browsing.
|
2020-05-08 08:39:37 -07:00
|
|
|
SafeBrowsingBlockHost string `yaml:"safebrowsing_block_host"`
|
|
|
|
|
|
|
|
// Anti-DNS amplification
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// Ratelimit is the maximum number of requests per second from a given IP
|
|
|
|
// (0 to disable).
|
|
|
|
Ratelimit uint32 `yaml:"ratelimit"`
|
|
|
|
|
|
|
|
// RatelimitWhitelist is the list of whitelisted client IP addresses.
|
|
|
|
RatelimitWhitelist []string `yaml:"ratelimit_whitelist"`
|
|
|
|
|
|
|
|
// RefuseAny, if true, refuse ANY requests.
|
|
|
|
RefuseAny bool `yaml:"refuse_any"`
|
2020-05-08 08:39:37 -07:00
|
|
|
|
|
|
|
// Upstream DNS servers configuration
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// UpstreamDNS is the list of upstream DNS servers.
|
|
|
|
UpstreamDNS []string `yaml:"upstream_dns"`
|
|
|
|
|
|
|
|
// UpstreamDNSFileName, if set, points to the file which contains upstream
|
|
|
|
// DNS servers.
|
|
|
|
UpstreamDNSFileName string `yaml:"upstream_dns_file"`
|
|
|
|
|
|
|
|
// BootstrapDNS is the list of bootstrap DNS servers for DoH and DoT
|
|
|
|
// resolvers (plain DNS only).
|
|
|
|
BootstrapDNS []string `yaml:"bootstrap_dns"`
|
|
|
|
|
|
|
|
// AllServers, if true, parallel queries to all configured upstream servers
|
|
|
|
// are enabled.
|
|
|
|
AllServers bool `yaml:"all_servers"`
|
|
|
|
|
|
|
|
// FastestAddr, if true, use Fastest Address algorithm.
|
|
|
|
FastestAddr bool `yaml:"fastest_addr"`
|
|
|
|
|
2021-08-27 10:16:07 -07:00
|
|
|
// FastestTimeout replaces the default timeout for dialing IP addresses
|
|
|
|
// when FastestAddr is true.
|
2021-09-30 11:17:54 -07:00
|
|
|
FastestTimeout timeutil.Duration `yaml:"fastest_timeout"`
|
2020-05-08 08:39:37 -07:00
|
|
|
|
|
|
|
// Access settings
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// AllowedClients is the slice of IP addresses, CIDR networks, and
|
|
|
|
// ClientIDs of allowed clients. If not empty, only these clients are
|
|
|
|
// allowed, and [FilteringConfig.DisallowedClients] are ignored.
|
2022-10-24 06:29:44 -07:00
|
|
|
AllowedClients []string `yaml:"allowed_clients"`
|
|
|
|
|
|
|
|
// DisallowedClients is the slice of IP addresses, CIDR networks, and
|
|
|
|
// ClientIDs of disallowed clients.
|
|
|
|
DisallowedClients []string `yaml:"disallowed_clients"`
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// BlockedHosts is the list of hosts that should be blocked.
|
|
|
|
BlockedHosts []string `yaml:"blocked_hosts"`
|
|
|
|
|
2021-12-22 11:27:36 -07:00
|
|
|
// TrustedProxies is the list of IP addresses and CIDR networks to detect
|
|
|
|
// proxy servers addresses the DoH requests from which should be handled.
|
|
|
|
// The value of nil or an empty slice for this field makes Proxy not trust
|
|
|
|
// any address.
|
2021-07-26 09:30:35 -07:00
|
|
|
TrustedProxies []string `yaml:"trusted_proxies"`
|
2020-05-08 08:39:37 -07:00
|
|
|
|
|
|
|
// DNS cache settings
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// CacheSize is the DNS cache size (in bytes).
|
|
|
|
CacheSize uint32 `yaml:"cache_size"`
|
|
|
|
|
|
|
|
// CacheMinTTL is the override TTL value (minimum) received from upstream
|
|
|
|
// server.
|
|
|
|
CacheMinTTL uint32 `yaml:"cache_ttl_min"`
|
|
|
|
|
|
|
|
// CacheMaxTTL is the override TTL value (maximum) received from upstream
|
|
|
|
// server.
|
|
|
|
CacheMaxTTL uint32 `yaml:"cache_ttl_max"`
|
|
|
|
|
2021-07-14 11:03:56 -07:00
|
|
|
// CacheOptimistic defines if optimistic cache mechanism should be used.
|
|
|
|
CacheOptimistic bool `yaml:"cache_optimistic"`
|
2020-05-08 08:39:37 -07:00
|
|
|
|
|
|
|
// Other settings
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// BogusNXDomain is the list of IP addresses, responses with them will be
|
|
|
|
// transformed to NXDOMAIN.
|
|
|
|
BogusNXDomain []string `yaml:"bogus_nxdomain"`
|
|
|
|
|
|
|
|
// AAAADisabled, if true, respond with an empty answer to all AAAA
|
|
|
|
// requests.
|
|
|
|
AAAADisabled bool `yaml:"aaaa_disabled"`
|
|
|
|
|
|
|
|
// EnableDNSSEC, if true, set AD flag in outcoming DNS request.
|
|
|
|
EnableDNSSEC bool `yaml:"enable_dnssec"`
|
|
|
|
|
|
|
|
// EDNSClientSubnet is the settings list for EDNS Client Subnet.
|
|
|
|
EDNSClientSubnet *EDNSClientSubnet `yaml:"edns_client_subnet"`
|
2020-09-02 04:13:45 -07:00
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// MaxGoroutines is the max number of parallel goroutines for processing
|
|
|
|
// incoming requests.
|
|
|
|
MaxGoroutines uint32 `yaml:"max_goroutines"`
|
|
|
|
|
|
|
|
// HandleDDR, if true, handle DDR requests
|
|
|
|
HandleDDR bool `yaml:"handle_ddr"`
|
|
|
|
|
|
|
|
// IpsetList is the ipset configuration that allows AdGuard Home to add IP
|
|
|
|
// addresses of the specified domain names to an ipset list. Syntax:
|
2021-06-18 07:55:01 -07:00
|
|
|
//
|
2022-09-12 06:11:32 -07:00
|
|
|
// DOMAIN[,DOMAIN].../IPSET_NAME
|
2021-06-18 07:55:01 -07:00
|
|
|
//
|
2022-09-12 06:11:32 -07:00
|
|
|
// This field is ignored if [IpsetListFileName] is set.
|
2021-06-18 07:55:01 -07:00
|
|
|
IpsetList []string `yaml:"ipset"`
|
2022-09-09 04:51:10 -07:00
|
|
|
|
|
|
|
// IpsetListFileName, if set, points to the file with ipset configuration.
|
2022-09-12 06:11:32 -07:00
|
|
|
// The format is the same as in [IpsetList].
|
2022-09-09 04:51:10 -07:00
|
|
|
IpsetListFileName string `yaml:"ipset_file"`
|
2023-04-04 03:28:21 -07:00
|
|
|
|
|
|
|
// BootstrapPreferIPv6, if true, instructs the bootstrapper to prefer IPv6
|
|
|
|
// addresses to IPv4 ones for DoH, DoQ, and DoT.
|
|
|
|
BootstrapPreferIPv6 bool `yaml:"bootstrap_prefer_ipv6"`
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
// EDNSClientSubnet is the settings list for EDNS Client Subnet.
|
|
|
|
type EDNSClientSubnet struct {
|
|
|
|
// CustomIP for EDNS Client Subnet.
|
2023-03-22 03:42:20 -07:00
|
|
|
CustomIP netip.Addr `yaml:"custom_ip"`
|
2023-03-01 06:16:16 -07:00
|
|
|
|
|
|
|
// Enabled defines if EDNS Client Subnet is enabled.
|
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
|
|
|
|
// UseCustom defines if CustomIP should be used.
|
|
|
|
UseCustom bool `yaml:"use_custom"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
// TLSConfig is the TLS configuration for HTTPS, DNS-over-HTTPS, and DNS-over-TLS
|
|
|
|
type TLSConfig struct {
|
2022-11-02 04:37:41 -07:00
|
|
|
cert tls.Certificate
|
|
|
|
|
2022-05-31 07:28:50 -07:00
|
|
|
TLSListenAddrs []*net.TCPAddr `yaml:"-" json:"-"`
|
|
|
|
QUICListenAddrs []*net.UDPAddr `yaml:"-" json:"-"`
|
|
|
|
HTTPSListenAddrs []*net.TCPAddr `yaml:"-" json:"-"`
|
2020-05-15 16:02:50 -07:00
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
// PEM-encoded certificates chain
|
|
|
|
CertificateChain string `yaml:"certificate_chain" json:"certificate_chain"`
|
|
|
|
// PEM-encoded private key
|
|
|
|
PrivateKey string `yaml:"private_key" json:"private_key"`
|
|
|
|
|
|
|
|
CertificatePath string `yaml:"certificate_path" json:"certificate_path"`
|
|
|
|
PrivateKeyPath string `yaml:"private_key_path" json:"private_key_path"`
|
2020-05-08 08:39:37 -07:00
|
|
|
|
|
|
|
CertificateChainData []byte `yaml:"-" json:"-"`
|
|
|
|
PrivateKeyData []byte `yaml:"-" json:"-"`
|
|
|
|
|
2022-02-10 05:42:59 -07:00
|
|
|
// ServerName is the hostname of the server. Currently, it is only being
|
2022-04-27 01:39:48 -07:00
|
|
|
// used for ClientID checking and Discovery of Designated Resolvers (DDR).
|
2021-01-27 08:32:13 -07:00
|
|
|
ServerName string `yaml:"-" json:"-"`
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
// DNS names from certificate (SAN) or CN value from Subject
|
|
|
|
dnsNames []string
|
2022-09-21 19:58:46 -07:00
|
|
|
|
2022-10-14 10:14:07 -07:00
|
|
|
// OverrideTLSCiphers, when set, contains the names of the cipher suites to
|
|
|
|
// use. If the slice is empty, the default safe suites are used.
|
|
|
|
OverrideTLSCiphers []string `yaml:"override_tls_ciphers,omitempty" json:"-"`
|
2022-11-02 04:37:41 -07:00
|
|
|
|
|
|
|
// StrictSNICheck controls if the connections with SNI mismatching the
|
|
|
|
// certificate's ones should be rejected.
|
|
|
|
StrictSNICheck bool `yaml:"strict_sni_check" json:"-"`
|
|
|
|
|
|
|
|
// hasIPAddrs is set during the certificate parsing and is true if the
|
|
|
|
// configured certificate contains at least a single IP address.
|
|
|
|
hasIPAddrs bool
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
2020-12-07 07:58:33 -07:00
|
|
|
// DNSCryptConfig is the DNSCrypt server configuration struct.
|
|
|
|
type DNSCryptConfig struct {
|
2022-08-29 05:54:41 -07:00
|
|
|
ResolverCert *dnscrypt.Cert
|
|
|
|
ProviderName string
|
2021-03-23 02:32:07 -07:00
|
|
|
UDPListenAddrs []*net.UDPAddr
|
|
|
|
TCPListenAddrs []*net.TCPAddr
|
|
|
|
Enabled bool
|
2020-12-07 07:58:33 -07:00
|
|
|
}
|
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
// ServerConfig represents server configuration.
|
|
|
|
// The zero ServerConfig is empty and ready for use.
|
|
|
|
type ServerConfig struct {
|
2021-03-23 02:32:07 -07:00
|
|
|
UDPListenAddrs []*net.UDPAddr // UDP listen address
|
|
|
|
TCPListenAddrs []*net.TCPAddr // TCP listen address
|
2020-05-13 10:31:43 -07:00
|
|
|
UpstreamConfig *proxy.UpstreamConfig // Upstream DNS servers config
|
|
|
|
OnDNSRequest func(d *proxy.DNSContext)
|
2020-05-08 08:39:37 -07:00
|
|
|
|
|
|
|
FilteringConfig
|
|
|
|
TLSConfig
|
2020-12-07 07:58:33 -07:00
|
|
|
DNSCryptConfig
|
2021-06-18 08:13:36 -07:00
|
|
|
TLSAllowUnencryptedDoH bool
|
2020-05-08 08:39:37 -07:00
|
|
|
|
2021-06-15 07:36:49 -07:00
|
|
|
// UpstreamTimeout is the timeout for querying upstream servers.
|
|
|
|
UpstreamTimeout time.Duration
|
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
TLSv12Roots *x509.CertPool // list of root CAs for TLSv1.2
|
2022-10-14 10:14:07 -07:00
|
|
|
|
|
|
|
// TLSCiphers are the IDs of TLS cipher suites to use.
|
|
|
|
TLSCiphers []uint16
|
2020-05-08 08:39:37 -07:00
|
|
|
|
|
|
|
// Called when the configuration is changed by HTTP request
|
|
|
|
ConfigModified func()
|
|
|
|
|
|
|
|
// Register an HTTP handler
|
2022-08-04 09:05:28 -07:00
|
|
|
HTTPRegister aghhttp.RegisterFunc
|
2021-04-07 10:16:06 -07:00
|
|
|
|
2022-10-03 08:08:05 -07:00
|
|
|
// LocalPTRResolvers is a slice of addresses to be used as upstreams for
|
|
|
|
// resolving PTR queries for local addresses.
|
|
|
|
LocalPTRResolvers []string
|
|
|
|
|
2023-01-23 09:10:56 -07:00
|
|
|
// DNS64Prefixes is a slice of NAT64 prefixes to be used for DNS64.
|
2023-02-06 07:17:51 -07:00
|
|
|
DNS64Prefixes []netip.Prefix
|
2023-01-23 09:10:56 -07:00
|
|
|
|
2021-04-07 10:16:06 -07:00
|
|
|
// ResolveClients signals if the RDNS should resolve clients' addresses.
|
|
|
|
ResolveClients bool
|
|
|
|
|
2021-05-26 07:55:19 -07:00
|
|
|
// UsePrivateRDNS defines if the PTR requests for unknown addresses from
|
|
|
|
// locally-served networks should be resolved via private PTR resolvers.
|
|
|
|
UsePrivateRDNS bool
|
|
|
|
|
2023-01-23 09:10:56 -07:00
|
|
|
// UseDNS64 defines if DNS64 is enabled for incoming requests.
|
|
|
|
UseDNS64 bool
|
|
|
|
|
2022-10-03 08:08:05 -07:00
|
|
|
// ServeHTTP3 defines if HTTP/3 is be allowed for incoming requests.
|
|
|
|
ServeHTTP3 bool
|
|
|
|
|
|
|
|
// UseHTTP3Upstreams defines if HTTP/3 is be allowed for DNS-over-HTTPS
|
|
|
|
// upstreams.
|
|
|
|
UseHTTP3Upstreams bool
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// if any of ServerConfig values are zero, then default values from below are used
|
|
|
|
var defaultValues = ServerConfig{
|
2021-03-23 02:32:07 -07:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{Port: 53}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{Port: 53}},
|
2020-05-08 08:39:37 -07:00
|
|
|
FilteringConfig: FilteringConfig{BlockedResponseTTL: 3600},
|
|
|
|
}
|
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
// createProxyConfig creates and validates configuration for the main proxy.
|
|
|
|
func (s *Server) createProxyConfig() (conf proxy.Config, err error) {
|
|
|
|
srvConf := s.conf
|
|
|
|
conf = proxy.Config{
|
|
|
|
UDPListenAddr: srvConf.UDPListenAddrs,
|
|
|
|
TCPListenAddr: srvConf.TCPListenAddrs,
|
2022-10-03 08:08:05 -07:00
|
|
|
HTTP3: srvConf.ServeHTTP3,
|
2022-08-29 05:54:41 -07:00
|
|
|
Ratelimit: int(srvConf.Ratelimit),
|
|
|
|
RatelimitWhitelist: srvConf.RatelimitWhitelist,
|
|
|
|
RefuseAny: srvConf.RefuseAny,
|
|
|
|
TrustedProxies: srvConf.TrustedProxies,
|
|
|
|
CacheMinTTL: srvConf.CacheMinTTL,
|
|
|
|
CacheMaxTTL: srvConf.CacheMaxTTL,
|
|
|
|
CacheOptimistic: srvConf.CacheOptimistic,
|
|
|
|
UpstreamConfig: srvConf.UpstreamConfig,
|
2020-05-13 10:31:43 -07:00
|
|
|
BeforeRequestHandler: s.beforeRequestHandler,
|
|
|
|
RequestHandler: s.handleDNSRequest,
|
2023-03-01 06:16:16 -07:00
|
|
|
EnableEDNSClientSubnet: srvConf.EDNSClientSubnet.Enabled,
|
2022-08-29 05:54:41 -07:00
|
|
|
MaxGoroutines: int(srvConf.MaxGoroutines),
|
2023-02-06 07:17:51 -07:00
|
|
|
UseDNS64: srvConf.UseDNS64,
|
|
|
|
DNS64Prefs: srvConf.DNS64Prefixes,
|
2020-06-10 05:33:23 -07:00
|
|
|
}
|
|
|
|
|
2023-03-01 06:16:16 -07:00
|
|
|
if srvConf.EDNSClientSubnet.UseCustom {
|
|
|
|
// TODO(s.chzhen): Use netip.Addr instead of net.IP inside dnsproxy.
|
2023-03-22 03:42:20 -07:00
|
|
|
conf.EDNSAddr = net.IP(srvConf.EDNSClientSubnet.CustomIP.AsSlice())
|
2023-03-01 06:16:16 -07:00
|
|
|
}
|
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
if srvConf.CacheSize != 0 {
|
|
|
|
conf.CacheEnabled = true
|
|
|
|
conf.CacheSizeBytes = int(srvConf.CacheSize)
|
2020-06-16 05:13:38 -07:00
|
|
|
}
|
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
setProxyUpstreamMode(
|
|
|
|
&conf,
|
|
|
|
srvConf.AllServers,
|
|
|
|
srvConf.FastestAddr,
|
|
|
|
srvConf.FastestTimeout.Duration,
|
|
|
|
)
|
2020-05-08 08:39:37 -07:00
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
for i, s := range srvConf.BogusNXDomain {
|
|
|
|
var subnet *net.IPNet
|
|
|
|
subnet, err = netutil.ParseSubnet(s)
|
2022-01-25 08:54:37 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Error("subnet at index %d: %s", i, err)
|
|
|
|
|
|
|
|
continue
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
2022-01-25 08:54:37 -07:00
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
conf.BogusNXDomain = append(conf.BogusNXDomain, subnet)
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
err = s.prepareTLS(&conf)
|
2020-05-08 08:39:37 -07:00
|
|
|
if err != nil {
|
2023-03-22 03:42:20 -07:00
|
|
|
return proxy.Config{}, fmt.Errorf("validating tls: %w", err)
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
if c := srvConf.DNSCryptConfig; c.Enabled {
|
|
|
|
conf.DNSCryptUDPListenAddr = c.UDPListenAddrs
|
|
|
|
conf.DNSCryptTCPListenAddr = c.TCPListenAddrs
|
|
|
|
conf.DNSCryptProviderName = c.ProviderName
|
|
|
|
conf.DNSCryptResolverCert = c.ResolverCert
|
2020-12-07 07:58:33 -07:00
|
|
|
}
|
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
if conf.UpstreamConfig == nil || len(conf.UpstreamConfig.Upstreams) == 0 {
|
2023-03-22 03:42:20 -07:00
|
|
|
return proxy.Config{}, errors.Error("no default upstream servers configured")
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
return conf, nil
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
2022-07-28 10:18:07 -07:00
|
|
|
const (
|
|
|
|
defaultSafeBrowsingBlockHost = "standard-block.dns.adguard.com"
|
|
|
|
defaultParentalBlockHost = "family-block.dns.adguard.com"
|
|
|
|
)
|
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
// initDefaultSettings initializes default settings if nothing
|
|
|
|
// is configured
|
|
|
|
func (s *Server) initDefaultSettings() {
|
|
|
|
if len(s.conf.UpstreamDNS) == 0 {
|
|
|
|
s.conf.UpstreamDNS = defaultDNS
|
|
|
|
}
|
2021-03-23 02:32:07 -07:00
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
if len(s.conf.BootstrapDNS) == 0 {
|
|
|
|
s.conf.BootstrapDNS = defaultBootstrap
|
|
|
|
}
|
2021-03-23 02:32:07 -07:00
|
|
|
|
2022-07-28 10:18:07 -07:00
|
|
|
if s.conf.ParentalBlockHost == "" {
|
|
|
|
s.conf.ParentalBlockHost = defaultParentalBlockHost
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
2021-03-23 02:32:07 -07:00
|
|
|
|
2022-07-28 10:18:07 -07:00
|
|
|
if s.conf.SafeBrowsingBlockHost == "" {
|
|
|
|
s.conf.SafeBrowsingBlockHost = defaultSafeBrowsingBlockHost
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
2021-03-23 02:32:07 -07:00
|
|
|
|
|
|
|
if s.conf.UDPListenAddrs == nil {
|
|
|
|
s.conf.UDPListenAddrs = defaultValues.UDPListenAddrs
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
2021-03-23 02:32:07 -07:00
|
|
|
|
|
|
|
if s.conf.TCPListenAddrs == nil {
|
|
|
|
s.conf.TCPListenAddrs = defaultValues.TCPListenAddrs
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
2021-03-23 02:32:07 -07:00
|
|
|
|
2020-09-10 02:32:36 -07:00
|
|
|
if len(s.conf.BlockedHosts) == 0 {
|
|
|
|
s.conf.BlockedHosts = defaultBlockedHosts
|
|
|
|
}
|
2021-06-15 07:36:49 -07:00
|
|
|
|
|
|
|
if s.conf.UpstreamTimeout == 0 {
|
|
|
|
s.conf.UpstreamTimeout = DefaultTimeout
|
|
|
|
}
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
2022-10-03 08:08:05 -07:00
|
|
|
// UpstreamHTTPVersions returns the HTTP versions for upstream configuration
|
|
|
|
// depending on configuration.
|
|
|
|
func UpstreamHTTPVersions(http3 bool) (v []upstream.HTTPVersion) {
|
|
|
|
if !http3 {
|
|
|
|
return upstream.DefaultHTTPVersions
|
|
|
|
}
|
|
|
|
|
|
|
|
return []upstream.HTTPVersion{
|
|
|
|
upstream.HTTPVersion3,
|
|
|
|
upstream.HTTPVersion2,
|
|
|
|
upstream.HTTPVersion11,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
// prepareUpstreamSettings - prepares upstream DNS server settings
|
|
|
|
func (s *Server) prepareUpstreamSettings() error {
|
2022-10-14 10:14:07 -07:00
|
|
|
// We're setting a customized set of RootCAs. The reason is that Go default
|
|
|
|
// mechanism of loading TLS roots does not always work properly on some
|
|
|
|
// routers so we're loading roots manually and pass it here.
|
|
|
|
//
|
|
|
|
// See [aghtls.SystemRootCAs].
|
2020-09-08 07:28:01 -07:00
|
|
|
upstream.RootCAs = s.conf.TLSv12Roots
|
2022-10-14 10:14:07 -07:00
|
|
|
upstream.CipherSuites = s.conf.TLSCiphers
|
2020-09-08 07:28:01 -07:00
|
|
|
|
2020-09-09 04:35:11 -07:00
|
|
|
// Load upstreams either from the file, or from the settings
|
2020-09-01 04:33:35 -07:00
|
|
|
var upstreams []string
|
|
|
|
if s.conf.UpstreamDNSFileName != "" {
|
2021-05-21 04:55:42 -07:00
|
|
|
data, err := os.ReadFile(s.conf.UpstreamDNSFileName)
|
2020-09-01 04:33:35 -07:00
|
|
|
if err != nil {
|
2022-09-06 07:09:54 -07:00
|
|
|
return fmt.Errorf("reading upstream from file: %w", err)
|
2020-09-01 04:33:35 -07:00
|
|
|
}
|
2021-07-29 07:40:31 -07:00
|
|
|
|
|
|
|
upstreams = stringutil.SplitTrimmed(string(data), "\n")
|
|
|
|
|
2021-03-25 06:00:27 -07:00
|
|
|
log.Debug("dns: using %d upstream servers from file %s", len(upstreams), s.conf.UpstreamDNSFileName)
|
2020-09-01 04:33:35 -07:00
|
|
|
} else {
|
|
|
|
upstreams = s.conf.UpstreamDNS
|
|
|
|
}
|
2021-03-11 02:17:54 -07:00
|
|
|
|
2022-10-03 08:08:05 -07:00
|
|
|
httpVersions := UpstreamHTTPVersions(s.conf.UseHTTP3Upstreams)
|
2021-07-29 07:40:31 -07:00
|
|
|
upstreams = stringutil.FilterOut(upstreams, IsCommentOrEmpty)
|
2021-03-11 02:17:54 -07:00
|
|
|
upstreamConfig, err := proxy.ParseUpstreamsConfig(
|
|
|
|
upstreams,
|
2021-06-29 05:53:28 -07:00
|
|
|
&upstream.Options{
|
2022-10-03 08:08:05 -07:00
|
|
|
Bootstrap: s.conf.BootstrapDNS,
|
|
|
|
Timeout: s.conf.UpstreamTimeout,
|
|
|
|
HTTPVersions: httpVersions,
|
2023-04-04 03:28:21 -07:00
|
|
|
PreferIPv6: s.conf.BootstrapPreferIPv6,
|
2021-03-03 05:27:25 -07:00
|
|
|
},
|
|
|
|
)
|
2020-05-08 08:39:37 -07:00
|
|
|
if err != nil {
|
2022-09-06 07:09:54 -07:00
|
|
|
return fmt.Errorf("parsing upstream config: %w", err)
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
2020-09-09 08:12:11 -07:00
|
|
|
|
|
|
|
if len(upstreamConfig.Upstreams) == 0 {
|
2021-02-16 08:46:49 -07:00
|
|
|
log.Info("warning: no default upstream servers specified, using %v", defaultDNS)
|
2021-06-29 05:53:28 -07:00
|
|
|
var uc *proxy.UpstreamConfig
|
2021-03-11 11:30:52 -07:00
|
|
|
uc, err = proxy.ParseUpstreamsConfig(
|
2021-03-11 02:17:54 -07:00
|
|
|
defaultDNS,
|
2021-06-29 05:53:28 -07:00
|
|
|
&upstream.Options{
|
2022-10-03 08:08:05 -07:00
|
|
|
Bootstrap: s.conf.BootstrapDNS,
|
|
|
|
Timeout: s.conf.UpstreamTimeout,
|
|
|
|
HTTPVersions: httpVersions,
|
2023-04-04 03:28:21 -07:00
|
|
|
PreferIPv6: s.conf.BootstrapPreferIPv6,
|
2021-03-03 05:27:25 -07:00
|
|
|
},
|
|
|
|
)
|
2020-09-09 08:12:11 -07:00
|
|
|
if err != nil {
|
2022-09-06 07:09:54 -07:00
|
|
|
return fmt.Errorf("parsing default upstreams: %w", err)
|
2020-09-09 08:12:11 -07:00
|
|
|
}
|
2022-09-06 07:09:54 -07:00
|
|
|
|
2020-09-09 08:12:11 -07:00
|
|
|
upstreamConfig.Upstreams = uc.Upstreams
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
s.conf.UpstreamConfig = upstreamConfig
|
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-29 05:54:41 -07:00
|
|
|
// setProxyUpstreamMode sets the upstream mode and related settings in conf
|
|
|
|
// based on provided parameters.
|
|
|
|
func setProxyUpstreamMode(
|
|
|
|
conf *proxy.Config,
|
|
|
|
allServers bool,
|
|
|
|
fastestAddr bool,
|
|
|
|
fastestTimeout time.Duration,
|
|
|
|
) {
|
|
|
|
if allServers {
|
|
|
|
conf.UpstreamMode = proxy.UModeParallel
|
|
|
|
} else if fastestAddr {
|
|
|
|
conf.UpstreamMode = proxy.UModeFastestAddr
|
|
|
|
conf.FastestPingTimeout = fastestTimeout
|
|
|
|
} else {
|
|
|
|
conf.UpstreamMode = proxy.UModeLoadBalance
|
|
|
|
}
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
2022-09-12 06:11:32 -07:00
|
|
|
// prepareIpsetListSettings reads and prepares the ipset configuration either
|
|
|
|
// from a file or from the data in the configuration file.
|
|
|
|
func (s *Server) prepareIpsetListSettings() (err error) {
|
|
|
|
fn := s.conf.IpsetListFileName
|
|
|
|
if fn == "" {
|
|
|
|
return s.ipset.init(s.conf.IpsetList)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := os.ReadFile(fn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ipsets := stringutil.SplitTrimmed(string(data), "\n")
|
|
|
|
|
|
|
|
log.Debug("dns: using %d ipset rules from file %q", len(ipsets), fn)
|
|
|
|
|
|
|
|
return s.ipset.init(ipsets)
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
// prepareTLS - prepares TLS configuration for the DNS proxy
|
2022-11-02 04:37:41 -07:00
|
|
|
func (s *Server) prepareTLS(proxyConfig *proxy.Config) (err error) {
|
2020-09-08 07:20:24 -07:00
|
|
|
if len(s.conf.CertificateChainData) == 0 || len(s.conf.PrivateKeyData) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 02:32:07 -07:00
|
|
|
if s.conf.TLSListenAddrs == nil && s.conf.QUICListenAddrs == nil {
|
2020-09-08 07:20:24 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 07:09:54 -07:00
|
|
|
proxyConfig.TLSListenAddr = aghalg.CoalesceSlice(
|
|
|
|
s.conf.TLSListenAddrs,
|
|
|
|
proxyConfig.TLSListenAddr,
|
|
|
|
)
|
2020-09-08 07:20:24 -07:00
|
|
|
|
2022-09-06 07:09:54 -07:00
|
|
|
proxyConfig.QUICListenAddr = aghalg.CoalesceSlice(
|
|
|
|
s.conf.QUICListenAddrs,
|
|
|
|
proxyConfig.QUICListenAddr,
|
|
|
|
)
|
2020-09-08 07:20:24 -07:00
|
|
|
|
|
|
|
s.conf.cert, err = tls.X509KeyPair(s.conf.CertificateChainData, s.conf.PrivateKeyData)
|
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("failed to parse TLS keypair: %w", err)
|
2020-09-08 07:20:24 -07:00
|
|
|
}
|
|
|
|
|
2022-11-02 04:37:41 -07:00
|
|
|
cert, err := x509.ParseCertificate(s.conf.cert.Certificate[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("x509.ParseCertificate(): %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.conf.hasIPAddrs = aghtls.CertificateHasIP(cert)
|
|
|
|
|
2020-09-08 07:20:24 -07:00
|
|
|
if s.conf.StrictSNICheck {
|
2022-11-02 04:37:41 -07:00
|
|
|
if len(cert.DNSNames) != 0 {
|
|
|
|
s.conf.dnsNames = cert.DNSNames
|
|
|
|
log.Debug("dnsforward: using certificate's SAN as DNS names: %v", cert.DNSNames)
|
2023-02-21 06:38:22 -07:00
|
|
|
slices.Sort(s.conf.dnsNames)
|
2020-09-08 07:20:24 -07:00
|
|
|
} else {
|
2022-11-02 04:37:41 -07:00
|
|
|
s.conf.dnsNames = append(s.conf.dnsNames, cert.Subject.CommonName)
|
|
|
|
log.Debug("dnsforward: using certificate's CN as DNS name: %s", cert.Subject.CommonName)
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
2020-09-08 07:20:24 -07:00
|
|
|
}
|
2020-05-08 08:39:37 -07:00
|
|
|
|
2020-09-08 07:20:24 -07:00
|
|
|
proxyConfig.TLSConfig = &tls.Config{
|
|
|
|
GetCertificate: s.onGetCertificate,
|
2022-10-14 10:14:07 -07:00
|
|
|
CipherSuites: s.conf.TLSCiphers,
|
2020-09-08 07:20:24 -07:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2020-05-08 08:39:37 -07:00
|
|
|
}
|
2020-09-08 07:20:24 -07:00
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-28 03:02:59 -07:00
|
|
|
// isWildcard returns true if host is a wildcard hostname.
|
|
|
|
func isWildcard(host string) (ok bool) {
|
|
|
|
return len(host) >= 2 && host[0] == '*' && host[1] == '.'
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchesDomainWildcard returns true if host matches the domain wildcard
|
|
|
|
// pattern pat.
|
|
|
|
func matchesDomainWildcard(host, pat string) (ok bool) {
|
|
|
|
return isWildcard(pat) && strings.HasSuffix(host, pat[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// anyNameMatches returns true if sni, the client's SNI value, matches any of
|
|
|
|
// the DNS names and patterns from certificate. dnsNames must be sorted.
|
|
|
|
func anyNameMatches(dnsNames []string, sni string) (ok bool) {
|
2023-02-21 06:52:33 -07:00
|
|
|
// Check sni is either a valid hostname or a valid IP address.
|
|
|
|
if netutil.ValidateHostname(sni) != nil && net.ParseIP(sni) == nil {
|
2021-05-28 03:02:59 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
if _, ok = slices.BinarySearch(dnsNames, sni); ok {
|
2021-05-28 03:02:59 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dn := range dnsNames {
|
|
|
|
if matchesDomainWildcard(sni, dn) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:39:37 -07:00
|
|
|
// Called by 'tls' package when Client Hello is received
|
|
|
|
// If the server name (from SNI) supplied by client is incorrect - we terminate the ongoing TLS handshake.
|
|
|
|
func (s *Server) onGetCertificate(ch *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
2021-05-28 03:02:59 -07:00
|
|
|
if s.conf.StrictSNICheck && !anyNameMatches(s.conf.dnsNames, ch.ServerName) {
|
2021-03-25 06:00:27 -07:00
|
|
|
log.Info("dns: tls: unknown SNI in Client Hello: %s", ch.ServerName)
|
2020-05-08 08:39:37 -07:00
|
|
|
return nil, fmt.Errorf("invalid SNI")
|
|
|
|
}
|
|
|
|
return &s.conf.cert, nil
|
|
|
|
}
|
2023-03-24 05:11:47 -07:00
|
|
|
|
|
|
|
// UpdatedProtectionStatus updates protection state, if the protection was
|
|
|
|
// disabled temporarily. Returns the updated state of protection.
|
|
|
|
func (s *Server) UpdatedProtectionStatus() (enabled bool) {
|
|
|
|
changed := false
|
|
|
|
defer func() {
|
|
|
|
if changed {
|
|
|
|
log.Info("dns: protection is restarted after pause")
|
|
|
|
s.conf.ConfigModified()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
|
|
|
|
|
|
|
disabledUntil := s.conf.ProtectionDisabledUntil
|
|
|
|
if disabledUntil == nil {
|
|
|
|
return s.conf.ProtectionEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().Before(*disabledUntil) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
s.conf.ProtectionEnabled = true
|
|
|
|
s.conf.ProtectionDisabledUntil = nil
|
|
|
|
changed = true
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|