2020-05-08 18:39:37 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2023-02-15 16:53:29 +03:00
|
|
|
"net/netip"
|
2021-05-21 14:55:42 +03:00
|
|
|
"os"
|
2024-03-12 17:45:11 +03:00
|
|
|
"slices"
|
2021-05-28 13:02:59 +03:00
|
|
|
"strings"
|
2021-06-15 17:36:49 +03:00
|
|
|
"time"
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2022-09-07 18:03:18 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2022-08-04 19:05:28 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2023-10-11 17:31:41 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2022-09-29 17:36:01 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
|
2023-07-26 13:18:44 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/client"
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-05-08 18:39:37 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2023-10-11 17:31:41 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2024-04-02 20:22:19 +03:00
|
|
|
"github.com/AdguardTeam/golibs/container"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-09-08 17:28:01 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2024-09-30 20:17:20 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2021-08-09 16:03:37 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2021-09-30 21:17:54 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2020-12-07 17:58:33 +03:00
|
|
|
"github.com/ameshkov/dnscrypt/v2"
|
2020-05-08 18:39:37 +03:00
|
|
|
)
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// ClientsContainer provides information about preconfigured DNS clients.
|
|
|
|
type ClientsContainer interface {
|
|
|
|
// UpstreamConfigByID returns the custom upstream configuration for the
|
|
|
|
// client having id, using boot to initialize the one if necessary. It
|
|
|
|
// returns nil if there is no custom upstream configuration for the client.
|
|
|
|
// The id is expected to be either a string representation of an IP address
|
|
|
|
// or the ClientID.
|
|
|
|
UpstreamConfigByID(
|
|
|
|
id string,
|
|
|
|
boot upstream.Resolver,
|
|
|
|
) (conf *proxy.CustomUpstreamConfig, err error)
|
|
|
|
}
|
|
|
|
|
2024-03-12 17:45:11 +03:00
|
|
|
// Config represents the DNS filtering configuration of AdGuard Home. The zero
|
2023-09-07 17:13:48 +03:00
|
|
|
// Config is empty and ready for use.
|
|
|
|
type Config struct {
|
2020-05-08 18:39:37 +03:00
|
|
|
// Callbacks for other modules
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// FilterHandler is an optional additional filtering callback.
|
2023-09-07 17:13:48 +03:00
|
|
|
FilterHandler func(cliAddr netip.Addr, clientID string, settings *filtering.Settings) `yaml:"-"`
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// ClientsContainer stores the information about special handling of some
|
|
|
|
// DNS clients.
|
|
|
|
ClientsContainer ClientsContainer `yaml:"-"`
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
// Anti-DNS amplification
|
|
|
|
|
2023-03-09 15:39:35 +03:00
|
|
|
// Ratelimit is the maximum number of requests per second from a given IP
|
|
|
|
// (0 to disable).
|
|
|
|
Ratelimit uint32 `yaml:"ratelimit"`
|
|
|
|
|
2023-11-13 17:39:48 +03:00
|
|
|
// RatelimitSubnetLenIPv4 is a subnet length for IPv4 addresses used for
|
|
|
|
// rate limiting requests.
|
|
|
|
RatelimitSubnetLenIPv4 int `yaml:"ratelimit_subnet_len_ipv4"`
|
|
|
|
|
|
|
|
// RatelimitSubnetLenIPv6 is a subnet length for IPv6 addresses used for
|
|
|
|
// rate limiting requests.
|
|
|
|
RatelimitSubnetLenIPv6 int `yaml:"ratelimit_subnet_len_ipv6"`
|
|
|
|
|
2023-03-09 15:39:35 +03:00
|
|
|
// RatelimitWhitelist is the list of whitelisted client IP addresses.
|
2023-12-07 17:23:00 +03:00
|
|
|
RatelimitWhitelist []netip.Addr `yaml:"ratelimit_whitelist"`
|
2023-03-09 15:39:35 +03:00
|
|
|
|
|
|
|
// RefuseAny, if true, refuse ANY requests.
|
|
|
|
RefuseAny bool `yaml:"refuse_any"`
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
// Upstream DNS servers configuration
|
|
|
|
|
2023-03-09 15:39:35 +03: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"`
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
// FallbackDNS is the list of fallback DNS servers used when upstream DNS
|
|
|
|
// servers are not responding.
|
|
|
|
FallbackDNS []string `yaml:"fallback_dns"`
|
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
// UpstreamMode determines the logic through which upstreams will be used.
|
|
|
|
UpstreamMode UpstreamMode `yaml:"upstream_mode"`
|
2023-03-09 15:39:35 +03:00
|
|
|
|
2021-08-27 20:16:07 +03:00
|
|
|
// FastestTimeout replaces the default timeout for dialing IP addresses
|
|
|
|
// when FastestAddr is true.
|
2021-09-30 21:17:54 +03:00
|
|
|
FastestTimeout timeutil.Duration `yaml:"fastest_timeout"`
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
// Access settings
|
|
|
|
|
2023-03-09 15:39:35 +03:00
|
|
|
// AllowedClients is the slice of IP addresses, CIDR networks, and
|
|
|
|
// ClientIDs of allowed clients. If not empty, only these clients are
|
2023-09-07 17:13:48 +03:00
|
|
|
// allowed, and [Config.DisallowedClients] are ignored.
|
2022-11-02 16:18:02 +03: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-09 15:39:35 +03:00
|
|
|
// BlockedHosts is the list of hosts that should be blocked.
|
|
|
|
BlockedHosts []string `yaml:"blocked_hosts"`
|
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
// TrustedProxies is the list of CIDR networks with proxy servers addresses
|
|
|
|
// from which the DoH requests should be handled. The value of nil or an
|
|
|
|
// empty slice for this field makes Proxy not trust any address.
|
|
|
|
TrustedProxies []netutil.Prefix `yaml:"trusted_proxies"`
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
// DNS cache settings
|
|
|
|
|
2023-03-09 15:39:35 +03: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 21:03:56 +03:00
|
|
|
// CacheOptimistic defines if optimistic cache mechanism should be used.
|
|
|
|
CacheOptimistic bool `yaml:"cache_optimistic"`
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
// Other settings
|
|
|
|
|
2023-03-09 15:39:35 +03: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"`
|
2020-09-02 14:13:45 +03:00
|
|
|
|
2023-03-09 15:39:35 +03:00
|
|
|
// EDNSClientSubnet is the settings list for EDNS Client Subnet.
|
|
|
|
EDNSClientSubnet *EDNSClientSubnet `yaml:"edns_client_subnet"`
|
|
|
|
|
|
|
|
// MaxGoroutines is the max number of parallel goroutines for processing
|
|
|
|
// incoming requests.
|
2024-01-30 18:43:51 +03:00
|
|
|
MaxGoroutines uint `yaml:"max_goroutines"`
|
2023-03-09 15:39:35 +03:00
|
|
|
|
|
|
|
// 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 17:55:01 +03:00
|
|
|
//
|
2024-09-30 20:17:20 +03:00
|
|
|
// DOMAIN[,DOMAIN].../IPSET_NAME[,IPSET_NAME]...
|
2021-06-18 17:55:01 +03:00
|
|
|
//
|
2022-09-14 16:36:29 +03:00
|
|
|
// This field is ignored if [IpsetListFileName] is set.
|
2021-06-18 17:55:01 +03:00
|
|
|
IpsetList []string `yaml:"ipset"`
|
2022-09-14 16:36:29 +03:00
|
|
|
|
|
|
|
// IpsetListFileName, if set, points to the file with ipset configuration.
|
|
|
|
// The format is the same as in [IpsetList].
|
|
|
|
IpsetListFileName string `yaml:"ipset_file"`
|
2023-04-12 14:48:42 +03: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 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:39:35 +03:00
|
|
|
// EDNSClientSubnet is the settings list for EDNS Client Subnet.
|
|
|
|
type EDNSClientSubnet struct {
|
|
|
|
// CustomIP for EDNS Client Subnet.
|
2023-04-12 14:48:42 +03:00
|
|
|
CustomIP netip.Addr `yaml:"custom_ip"`
|
2023-03-09 15:39:35 +03: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 18:39:37 +03:00
|
|
|
// TLSConfig is the TLS configuration for HTTPS, DNS-over-HTTPS, and DNS-over-TLS
|
|
|
|
type TLSConfig struct {
|
2022-11-02 16:18:02 +03:00
|
|
|
cert tls.Certificate
|
|
|
|
|
2022-06-02 17:55:48 +03:00
|
|
|
TLSListenAddrs []*net.TCPAddr `yaml:"-" json:"-"`
|
|
|
|
QUICListenAddrs []*net.UDPAddr `yaml:"-" json:"-"`
|
|
|
|
HTTPSListenAddrs []*net.TCPAddr `yaml:"-" json:"-"`
|
2020-05-16 02:02:50 +03:00
|
|
|
|
2020-11-20 17:32:41 +03: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 18:39:37 +03:00
|
|
|
|
|
|
|
CertificateChainData []byte `yaml:"-" json:"-"`
|
|
|
|
PrivateKeyData []byte `yaml:"-" json:"-"`
|
|
|
|
|
2022-02-10 15:42:59 +03:00
|
|
|
// ServerName is the hostname of the server. Currently, it is only being
|
2022-08-17 18:23:30 +03:00
|
|
|
// used for ClientID checking and Discovery of Designated Resolvers (DDR).
|
2021-01-27 18:32:13 +03:00
|
|
|
ServerName string `yaml:"-" json:"-"`
|
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
// DNS names from certificate (SAN) or CN value from Subject
|
|
|
|
dnsNames []string
|
2022-11-02 16:18:02 +03: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:"-"`
|
|
|
|
|
|
|
|
// 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 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:58:33 +03:00
|
|
|
// DNSCryptConfig is the DNSCrypt server configuration struct.
|
|
|
|
type DNSCryptConfig struct {
|
2022-09-07 18:03:18 +03:00
|
|
|
ResolverCert *dnscrypt.Cert
|
|
|
|
ProviderName string
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs []*net.UDPAddr
|
|
|
|
TCPListenAddrs []*net.TCPAddr
|
|
|
|
Enabled bool
|
2020-12-07 17:58:33 +03:00
|
|
|
}
|
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
// ServerConfig represents server configuration.
|
|
|
|
// The zero ServerConfig is empty and ready for use.
|
|
|
|
type ServerConfig struct {
|
2024-05-15 13:34:12 +03:00
|
|
|
// UDPListenAddrs is the list of addresses to listen for DNS-over-UDP.
|
|
|
|
UDPListenAddrs []*net.UDPAddr
|
|
|
|
|
|
|
|
// TCPListenAddrs is the list of addresses to listen for DNS-over-TCP.
|
|
|
|
TCPListenAddrs []*net.TCPAddr
|
|
|
|
|
|
|
|
// UpstreamConfig is the general configuration of upstream DNS servers.
|
|
|
|
UpstreamConfig *proxy.UpstreamConfig
|
|
|
|
|
|
|
|
// PrivateRDNSUpstreamConfig is the configuration of upstream DNS servers
|
|
|
|
// for private reverse DNS.
|
|
|
|
PrivateRDNSUpstreamConfig *proxy.UpstreamConfig
|
2023-07-26 13:18:44 +03:00
|
|
|
|
|
|
|
// AddrProcConf defines the configuration for the client IP processor.
|
|
|
|
// If nil, [client.EmptyAddrProc] is used.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): The use of [client.EmptyAddrProc] is a crutch for tests.
|
|
|
|
// Remove that.
|
|
|
|
AddrProcConf *client.DefaultAddrProcConfig
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
Config
|
2020-05-08 18:39:37 +03:00
|
|
|
TLSConfig
|
2020-12-07 17:58:33 +03:00
|
|
|
DNSCryptConfig
|
2021-06-18 18:13:36 +03:00
|
|
|
TLSAllowUnencryptedDoH bool
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2021-06-15 17:36:49 +03:00
|
|
|
// UpstreamTimeout is the timeout for querying upstream servers.
|
|
|
|
UpstreamTimeout time.Duration
|
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
TLSv12Roots *x509.CertPool // list of root CAs for TLSv1.2
|
2022-11-02 16:18:02 +03:00
|
|
|
|
|
|
|
// TLSCiphers are the IDs of TLS cipher suites to use.
|
|
|
|
TLSCiphers []uint16
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
// Called when the configuration is changed by HTTP request
|
|
|
|
ConfigModified func()
|
|
|
|
|
|
|
|
// Register an HTTP handler
|
2022-08-04 19:05:28 +03:00
|
|
|
HTTPRegister aghhttp.RegisterFunc
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2022-10-03 18:52:20 +03:00
|
|
|
// LocalPTRResolvers is a slice of addresses to be used as upstreams for
|
|
|
|
// resolving PTR queries for local addresses.
|
|
|
|
LocalPTRResolvers []string
|
|
|
|
|
2023-02-01 15:41:34 +03:00
|
|
|
// DNS64Prefixes is a slice of NAT64 prefixes to be used for DNS64.
|
2023-02-15 16:53:29 +03:00
|
|
|
DNS64Prefixes []netip.Prefix
|
2023-02-01 15:41:34 +03:00
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
// UsePrivateRDNS defines if the PTR requests for unknown addresses from
|
|
|
|
// locally-served networks should be resolved via private PTR resolvers.
|
|
|
|
UsePrivateRDNS bool
|
|
|
|
|
2023-02-01 15:41:34 +03:00
|
|
|
// UseDNS64 defines if DNS64 is enabled for incoming requests.
|
|
|
|
UseDNS64 bool
|
|
|
|
|
2022-10-03 18:52:20 +03: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
|
2023-12-07 17:23:00 +03:00
|
|
|
|
|
|
|
// ServePlainDNS defines if plain DNS is allowed for incoming requests.
|
|
|
|
ServePlainDNS bool
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
// UpstreamMode is a enumeration of upstream mode representations. See
|
|
|
|
// [proxy.UpstreamModeType].
|
2024-09-30 20:17:20 +03:00
|
|
|
//
|
|
|
|
// TODO(d.kolyshev): Consider using [proxy.UpstreamMode].
|
2024-01-30 18:43:51 +03:00
|
|
|
type UpstreamMode string
|
|
|
|
|
|
|
|
const (
|
|
|
|
UpstreamModeLoadBalance UpstreamMode = "load_balance"
|
|
|
|
UpstreamModeParallel UpstreamMode = "parallel"
|
|
|
|
UpstreamModeFastestAddr UpstreamMode = "fastest_addr"
|
|
|
|
)
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// newProxyConfig creates and validates configuration for the main proxy.
|
|
|
|
func (s *Server) newProxyConfig() (conf *proxy.Config, err error) {
|
2022-09-07 18:03:18 +03:00
|
|
|
srvConf := s.conf
|
2024-01-30 18:43:51 +03:00
|
|
|
trustedPrefixes := netutil.UnembedPrefixes(srvConf.TrustedProxies)
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
conf = &proxy.Config{
|
2024-09-30 20:17:20 +03:00
|
|
|
Logger: s.baseLogger.With(slogutil.KeyPrefix, "dnsproxy"),
|
2024-05-15 13:34:12 +03:00
|
|
|
HTTP3: srvConf.ServeHTTP3,
|
|
|
|
Ratelimit: int(srvConf.Ratelimit),
|
|
|
|
RatelimitSubnetLenIPv4: srvConf.RatelimitSubnetLenIPv4,
|
|
|
|
RatelimitSubnetLenIPv6: srvConf.RatelimitSubnetLenIPv6,
|
|
|
|
RatelimitWhitelist: srvConf.RatelimitWhitelist,
|
|
|
|
RefuseAny: srvConf.RefuseAny,
|
|
|
|
TrustedProxies: netutil.SliceSubnetSet(trustedPrefixes),
|
|
|
|
CacheMinTTL: srvConf.CacheMinTTL,
|
|
|
|
CacheMaxTTL: srvConf.CacheMaxTTL,
|
|
|
|
CacheOptimistic: srvConf.CacheOptimistic,
|
|
|
|
UpstreamConfig: srvConf.UpstreamConfig,
|
|
|
|
PrivateRDNSUpstreamConfig: srvConf.PrivateRDNSUpstreamConfig,
|
|
|
|
BeforeRequestHandler: s,
|
|
|
|
RequestHandler: s.handleDNSRequest,
|
|
|
|
HTTPSServerName: aghhttp.UserAgent(),
|
|
|
|
EnableEDNSClientSubnet: srvConf.EDNSClientSubnet.Enabled,
|
|
|
|
MaxGoroutines: srvConf.MaxGoroutines,
|
|
|
|
UseDNS64: srvConf.UseDNS64,
|
|
|
|
DNS64Prefs: srvConf.DNS64Prefixes,
|
|
|
|
UsePrivateRDNS: srvConf.UsePrivateRDNS,
|
|
|
|
PrivateSubnets: s.privateNets,
|
|
|
|
MessageConstructor: s,
|
2020-06-10 15:33:23 +03:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:39:35 +03:00
|
|
|
if srvConf.EDNSClientSubnet.UseCustom {
|
|
|
|
// TODO(s.chzhen): Use netip.Addr instead of net.IP inside dnsproxy.
|
2023-04-12 14:48:42 +03:00
|
|
|
conf.EDNSAddr = net.IP(srvConf.EDNSClientSubnet.CustomIP.AsSlice())
|
2023-03-09 15:39:35 +03:00
|
|
|
}
|
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
err = setProxyUpstreamMode(conf, srvConf.UpstreamMode, srvConf.FastestTimeout.Duration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("upstream mode: %w", err)
|
2020-06-16 15:13:38 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
conf.BogusNXDomain, err = parseBogusNXDOMAIN(srvConf.BogusNXDomain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("bogus_nxdomain: %w", err)
|
|
|
|
}
|
2022-01-25 18:54:37 +03:00
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
err = s.prepareTLS(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("validating tls: %w", err)
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
err = s.preparePlain(conf)
|
2020-05-08 18:39:37 +03:00
|
|
|
if err != nil {
|
2023-12-07 17:23:00 +03:00
|
|
|
return nil, fmt.Errorf("validating plain: %w", err)
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2022-09-07 18:03:18 +03: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 17:58:33 +03:00
|
|
|
}
|
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
conf, err = prepareCacheConfig(conf,
|
|
|
|
srvConf.CacheSize,
|
|
|
|
srvConf.CacheMinTTL,
|
|
|
|
srvConf.CacheMaxTTL,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareCacheConfig prepares the cache configuration and returns an error if
|
|
|
|
// there is one.
|
|
|
|
func prepareCacheConfig(
|
|
|
|
conf *proxy.Config,
|
|
|
|
size uint32,
|
|
|
|
minTTL uint32,
|
|
|
|
maxTTL uint32,
|
|
|
|
) (prepared *proxy.Config, err error) {
|
|
|
|
if size != 0 {
|
|
|
|
conf.CacheEnabled = true
|
|
|
|
conf.CacheSizeBytes = int(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateCacheTTL(minTTL, maxTTL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("validating cache ttl: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-07 18:03:18 +03:00
|
|
|
return conf, nil
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// parseBogusNXDOMAIN parses the bogus NXDOMAIN strings into valid subnets.
|
|
|
|
func parseBogusNXDOMAIN(confBogusNXDOMAIN []string) (subnets []netip.Prefix, err error) {
|
|
|
|
for i, s := range confBogusNXDOMAIN {
|
|
|
|
var subnet netip.Prefix
|
|
|
|
subnet, err = aghnet.ParseSubnet(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("subnet at index %d: %w", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
subnets = append(subnets, subnet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return subnets, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:39:37 +03: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 12:32:07 +03:00
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
if len(s.conf.BootstrapDNS) == 0 {
|
|
|
|
s.conf.BootstrapDNS = defaultBootstrap
|
|
|
|
}
|
2021-03-23 12:32:07 +03:00
|
|
|
|
|
|
|
if s.conf.UDPListenAddrs == nil {
|
2023-09-07 17:13:48 +03:00
|
|
|
s.conf.UDPListenAddrs = defaultUDPListenAddrs
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2021-03-23 12:32:07 +03:00
|
|
|
|
|
|
|
if s.conf.TCPListenAddrs == nil {
|
2023-09-07 17:13:48 +03:00
|
|
|
s.conf.TCPListenAddrs = defaultTCPListenAddrs
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2021-03-23 12:32:07 +03:00
|
|
|
|
2020-09-10 12:32:36 +03:00
|
|
|
if len(s.conf.BlockedHosts) == 0 {
|
|
|
|
s.conf.BlockedHosts = defaultBlockedHosts
|
|
|
|
}
|
2021-06-15 17:36:49 +03:00
|
|
|
|
|
|
|
if s.conf.UpstreamTimeout == 0 {
|
|
|
|
s.conf.UpstreamTimeout = DefaultTimeout
|
|
|
|
}
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2022-09-14 16:36:29 +03:00
|
|
|
// prepareIpsetListSettings reads and prepares the ipset configuration either
|
|
|
|
// from a file or from the data in the configuration file.
|
2024-09-30 20:17:20 +03:00
|
|
|
func (s *Server) prepareIpsetListSettings() (ipsets []string, err error) {
|
2022-09-14 16:36:29 +03:00
|
|
|
fn := s.conf.IpsetListFileName
|
|
|
|
if fn == "" {
|
2024-09-30 20:17:20 +03:00
|
|
|
return s.conf.IpsetList, nil
|
2022-09-14 16:36:29 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 14:10:40 +03:00
|
|
|
// #nosec G304 -- Trust the path explicitly given by the user.
|
2022-09-14 16:36:29 +03:00
|
|
|
data, err := os.ReadFile(fn)
|
|
|
|
if err != nil {
|
2024-09-30 20:17:20 +03:00
|
|
|
return nil, err
|
2022-09-14 16:36:29 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 20:17:20 +03:00
|
|
|
ipsets = stringutil.SplitTrimmed(string(data), "\n")
|
|
|
|
ipsets = slices.DeleteFunc(ipsets, IsCommentOrEmpty)
|
2022-09-14 16:36:29 +03:00
|
|
|
|
|
|
|
log.Debug("dns: using %d ipset rules from file %q", len(ipsets), fn)
|
|
|
|
|
2024-09-30 20:17:20 +03:00
|
|
|
return ipsets, nil
|
2022-09-14 16:36:29 +03:00
|
|
|
}
|
|
|
|
|
2024-05-15 13:34:12 +03:00
|
|
|
// loadUpstreams parses upstream DNS servers from the configured file or from
|
|
|
|
// the configuration itself.
|
|
|
|
func (conf *ServerConfig) loadUpstreams() (upstreams []string, err error) {
|
|
|
|
if conf.UpstreamDNSFileName == "" {
|
|
|
|
return stringutil.FilterOut(conf.UpstreamDNS, IsCommentOrEmpty), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
data, err = os.ReadFile(conf.UpstreamDNSFileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading upstream from file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
upstreams = stringutil.SplitTrimmed(string(data), "\n")
|
|
|
|
|
|
|
|
log.Debug("dnsforward: got %d upstreams in %q", len(upstreams), conf.UpstreamDNSFileName)
|
|
|
|
|
|
|
|
return stringutil.FilterOut(upstreams, IsCommentOrEmpty), nil
|
|
|
|
}
|
|
|
|
|
2023-10-11 17:31:41 +03:00
|
|
|
// collectListenAddr adds addrPort to addrs. It also adds its port to
|
|
|
|
// unspecPorts if its address is unspecified.
|
|
|
|
func collectListenAddr(
|
|
|
|
addrPort netip.AddrPort,
|
2024-04-02 20:22:19 +03:00
|
|
|
addrs *container.MapSet[netip.AddrPort],
|
|
|
|
unspecPorts *container.MapSet[uint16],
|
2023-10-11 17:31:41 +03:00
|
|
|
) {
|
|
|
|
if addrPort == (netip.AddrPort{}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:22:19 +03:00
|
|
|
addrs.Add(addrPort)
|
2023-10-11 17:31:41 +03:00
|
|
|
if addrPort.Addr().IsUnspecified() {
|
2024-04-02 20:22:19 +03:00
|
|
|
unspecPorts.Add(addrPort.Port())
|
2023-10-11 17:31:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collectDNSAddrs returns configured set of listening addresses. It also
|
|
|
|
// returns a set of ports of each unspecified listening address.
|
2024-04-02 20:22:19 +03:00
|
|
|
func (conf *ServerConfig) collectDNSAddrs() (
|
|
|
|
addrs *container.MapSet[netip.AddrPort],
|
|
|
|
unspecPorts *container.MapSet[uint16],
|
|
|
|
) {
|
|
|
|
addrs = container.NewMapSet[netip.AddrPort]()
|
|
|
|
unspecPorts = container.NewMapSet[uint16]()
|
2023-10-11 17:31:41 +03:00
|
|
|
|
|
|
|
for _, laddr := range conf.TCPListenAddrs {
|
|
|
|
collectListenAddr(laddr.AddrPort(), addrs, unspecPorts)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, laddr := range conf.UDPListenAddrs {
|
|
|
|
collectListenAddr(laddr.AddrPort(), addrs, unspecPorts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, unspecPorts
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultPlainDNSPort is the default port for plain DNS.
|
|
|
|
const defaultPlainDNSPort uint16 = 53
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// addrPortSet is a set of [netip.AddrPort] values.
|
|
|
|
type addrPortSet interface {
|
|
|
|
// Has returns true if addrPort is in the set.
|
|
|
|
Has(addrPort netip.AddrPort) (ok bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ addrPortSet = emptyAddrPortSet{}
|
|
|
|
|
|
|
|
// emptyAddrPortSet is the [addrPortSet] containing no values.
|
|
|
|
type emptyAddrPortSet struct{}
|
|
|
|
|
|
|
|
// Has implements the [addrPortSet] interface for [emptyAddrPortSet].
|
|
|
|
func (emptyAddrPortSet) Has(_ netip.AddrPort) (ok bool) { return false }
|
|
|
|
|
|
|
|
// combinedAddrPortSet is the [addrPortSet] defined by some IP addresses along
|
|
|
|
// with ports, any combination of which is considered being in the set.
|
|
|
|
type combinedAddrPortSet struct {
|
2024-04-02 20:22:19 +03:00
|
|
|
// TODO(e.burkov): Use container.SliceSet when available.
|
|
|
|
ports *container.MapSet[uint16]
|
|
|
|
addrs *container.MapSet[netip.Addr]
|
2023-12-07 17:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ addrPortSet = (*combinedAddrPortSet)(nil)
|
|
|
|
|
|
|
|
// Has implements the [addrPortSet] interface for [*combinedAddrPortSet].
|
|
|
|
func (m *combinedAddrPortSet) Has(addrPort netip.AddrPort) (ok bool) {
|
2024-04-02 20:22:19 +03:00
|
|
|
return m.ports.Has(addrPort.Port()) && m.addrs.Has(addrPort.Addr())
|
2023-12-07 17:23:00 +03:00
|
|
|
}
|
2023-10-11 17:31:41 +03:00
|
|
|
|
2024-05-15 13:34:12 +03:00
|
|
|
// filterOutAddrs filters out all the upstreams that match um. It returns all
|
|
|
|
// the closing errors joined.
|
2023-12-07 17:23:00 +03:00
|
|
|
func filterOutAddrs(upsConf *proxy.UpstreamConfig, set addrPortSet) (err error) {
|
2023-10-11 17:31:41 +03:00
|
|
|
var errs []error
|
|
|
|
delFunc := func(u upstream.Upstream) (ok bool) {
|
|
|
|
// TODO(e.burkov): We should probably consider the protocol of u to
|
|
|
|
// only filter out the listening addresses of the same protocol.
|
|
|
|
addr, parseErr := aghnet.ParseAddrPort(u.Address(), defaultPlainDNSPort)
|
2023-12-07 17:23:00 +03:00
|
|
|
if parseErr != nil || !set.Has(addr) {
|
2023-10-11 17:31:41 +03:00
|
|
|
// Don't filter out the upstream if it either cannot be parsed, or
|
2023-12-07 17:23:00 +03:00
|
|
|
// does not match m.
|
2023-10-11 17:31:41 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
errs = append(errs, u.Close())
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
upsConf.Upstreams = slices.DeleteFunc(upsConf.Upstreams, delFunc)
|
|
|
|
for d, ups := range upsConf.DomainReservedUpstreams {
|
|
|
|
upsConf.DomainReservedUpstreams[d] = slices.DeleteFunc(ups, delFunc)
|
|
|
|
}
|
|
|
|
for d, ups := range upsConf.SpecifiedDomainUpstreams {
|
|
|
|
upsConf.SpecifiedDomainUpstreams[d] = slices.DeleteFunc(ups, delFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Join(errs...)
|
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// ourAddrsSet returns an addrPortSet that contains all the configured listening
|
2023-10-11 17:31:41 +03:00
|
|
|
// addresses.
|
2023-12-07 17:23:00 +03:00
|
|
|
func (conf *ServerConfig) ourAddrsSet() (m addrPortSet, err error) {
|
2023-10-11 17:31:41 +03:00
|
|
|
addrs, unspecPorts := conf.collectDNSAddrs()
|
2023-12-07 17:23:00 +03:00
|
|
|
switch {
|
2024-04-02 20:22:19 +03:00
|
|
|
case addrs.Len() == 0:
|
2023-10-11 17:31:41 +03:00
|
|
|
log.Debug("dnsforward: no listen addresses")
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
return emptyAddrPortSet{}, nil
|
2024-04-02 20:22:19 +03:00
|
|
|
case unspecPorts.Len() == 0:
|
2023-10-11 17:31:41 +03:00
|
|
|
log.Debug("dnsforward: filtering out addresses %s", addrs)
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
return addrs, nil
|
|
|
|
default:
|
2023-10-11 17:31:41 +03:00
|
|
|
var ifaceAddrs []netip.Addr
|
|
|
|
ifaceAddrs, err = aghnet.CollectAllIfacesAddrs()
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("dnsforward: filtering out addresses %s on ports %d", ifaceAddrs, unspecPorts)
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
return &combinedAddrPortSet{
|
|
|
|
ports: unspecPorts,
|
2024-04-02 20:22:19 +03:00
|
|
|
addrs: container.NewMapSet(ifaceAddrs...),
|
2023-12-07 17:23:00 +03:00
|
|
|
}, nil
|
2023-10-11 17:31:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
// prepareTLS - prepares TLS configuration for the DNS proxy
|
2022-11-02 16:18:02 +03:00
|
|
|
func (s *Server) prepareTLS(proxyConfig *proxy.Config) (err error) {
|
2020-09-08 17:20:24 +03:00
|
|
|
if len(s.conf.CertificateChainData) == 0 || len(s.conf.PrivateKeyData) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:32:07 +03:00
|
|
|
if s.conf.TLSListenAddrs == nil && s.conf.QUICListenAddrs == nil {
|
2020-09-08 17:20:24 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 18:03:18 +03:00
|
|
|
proxyConfig.TLSListenAddr = aghalg.CoalesceSlice(
|
|
|
|
s.conf.TLSListenAddrs,
|
|
|
|
proxyConfig.TLSListenAddr,
|
|
|
|
)
|
2020-09-08 17:20:24 +03:00
|
|
|
|
2022-09-07 18:03:18 +03:00
|
|
|
proxyConfig.QUICListenAddr = aghalg.CoalesceSlice(
|
|
|
|
s.conf.QUICListenAddrs,
|
|
|
|
proxyConfig.QUICListenAddr,
|
|
|
|
)
|
2020-09-08 17:20:24 +03:00
|
|
|
|
|
|
|
s.conf.cert, err = tls.X509KeyPair(s.conf.CertificateChainData, s.conf.PrivateKeyData)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("failed to parse TLS keypair: %w", err)
|
2020-09-08 17:20:24 +03:00
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03: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 17:20:24 +03:00
|
|
|
if s.conf.StrictSNICheck {
|
2022-11-02 16:18:02 +03:00
|
|
|
if len(cert.DNSNames) != 0 {
|
|
|
|
s.conf.dnsNames = cert.DNSNames
|
2023-04-12 14:48:42 +03:00
|
|
|
log.Debug("dns: using certificate's SAN as DNS names: %v", cert.DNSNames)
|
2023-03-09 15:39:35 +03:00
|
|
|
slices.Sort(s.conf.dnsNames)
|
2020-09-08 17:20:24 +03:00
|
|
|
} else {
|
2022-11-02 16:18:02 +03:00
|
|
|
s.conf.dnsNames = append(s.conf.dnsNames, cert.Subject.CommonName)
|
2023-04-12 14:48:42 +03:00
|
|
|
log.Debug("dns: using certificate's CN as DNS name: %s", cert.Subject.CommonName)
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2020-09-08 17:20:24 +03:00
|
|
|
}
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2020-09-08 17:20:24 +03:00
|
|
|
proxyConfig.TLSConfig = &tls.Config{
|
|
|
|
GetCertificate: s.onGetCertificate,
|
2022-11-02 16:18:02 +03:00
|
|
|
CipherSuites: s.conf.TLSCiphers,
|
2020-09-08 17:20:24 +03:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2020-09-08 17:20:24 +03:00
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-28 13:02:59 +03:00
|
|
|
// isWildcard returns true if host is a wildcard hostname.
|
|
|
|
func isWildcard(host string) (ok bool) {
|
2023-12-07 17:23:00 +03:00
|
|
|
return strings.HasPrefix(host, "*.")
|
2021-05-28 13:02:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03-09 15:39:35 +03:00
|
|
|
// Check sni is either a valid hostname or a valid IP address.
|
2024-09-30 20:17:20 +03:00
|
|
|
if !netutil.IsValidHostname(sni) && !netutil.IsValidIPString(sni) {
|
2021-05-28 13:02:59 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:39:35 +03:00
|
|
|
if _, ok = slices.BinarySearch(dnsNames, sni); ok {
|
2021-05-28 13:02:59 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dn := range dnsNames {
|
|
|
|
if matchesDomainWildcard(sni, dn) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:39:37 +03: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 13:02:59 +03:00
|
|
|
if s.conf.StrictSNICheck && !anyNameMatches(s.conf.dnsNames, ch.ServerName) {
|
2021-03-25 16:00:27 +03:00
|
|
|
log.Info("dns: tls: unknown SNI in Client Hello: %s", ch.ServerName)
|
2020-05-08 18:39:37 +03:00
|
|
|
return nil, fmt.Errorf("invalid SNI")
|
|
|
|
}
|
|
|
|
return &s.conf.cert, nil
|
|
|
|
}
|
2023-04-12 14:48:42 +03:00
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// preparePlain prepares the plain-DNS configuration for the DNS proxy.
|
|
|
|
// preparePlain assumes that prepareTLS has already been called.
|
|
|
|
func (s *Server) preparePlain(proxyConf *proxy.Config) (err error) {
|
|
|
|
if s.conf.ServePlainDNS {
|
|
|
|
proxyConf.UDPListenAddr = s.conf.UDPListenAddrs
|
|
|
|
proxyConf.TCPListenAddr = s.conf.TCPListenAddrs
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lenEncrypted := len(proxyConf.DNSCryptTCPListenAddr) +
|
|
|
|
len(proxyConf.DNSCryptUDPListenAddr) +
|
|
|
|
len(proxyConf.HTTPSListenAddr) +
|
|
|
|
len(proxyConf.QUICListenAddr) +
|
|
|
|
len(proxyConf.TLSListenAddr)
|
|
|
|
if lenEncrypted == 0 {
|
|
|
|
// TODO(a.garipov): Support full disabling of all DNS.
|
|
|
|
return errors.Error("disabling plain dns requires at least one encrypted protocol")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("dnsforward: warning: plain dns is disabled")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:48:42 +03:00
|
|
|
// UpdatedProtectionStatus updates protection state, if the protection was
|
|
|
|
// disabled temporarily. Returns the updated state of protection.
|
|
|
|
func (s *Server) UpdatedProtectionStatus() (enabled bool, disabledUntil *time.Time) {
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
enabled, disabledUntil = s.dnsFilter.ProtectionStatus()
|
2023-04-12 14:48:42 +03:00
|
|
|
if disabledUntil == nil {
|
2023-09-07 17:13:48 +03:00
|
|
|
return enabled, nil
|
2023-04-12 14:48:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().Before(*disabledUntil) {
|
|
|
|
return false, disabledUntil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the values in a separate goroutine, unless an update is already in
|
|
|
|
// progress. Since this method is called very often, and this update is a
|
|
|
|
// relatively rare situation, do not lock s.serverLock for writing, as that
|
|
|
|
// can lead to freezes.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/5661.
|
|
|
|
if s.protectionUpdateInProgress.CompareAndSwap(false, true) {
|
|
|
|
go s.enableProtectionAfterPause()
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// enableProtectionAfterPause sets the protection configuration to enabled
|
|
|
|
// values. It is intended to be used as a goroutine.
|
|
|
|
func (s *Server) enableProtectionAfterPause() {
|
|
|
|
defer log.OnPanic("dns: enabling protection after pause")
|
|
|
|
|
|
|
|
defer s.protectionUpdateInProgress.Store(false)
|
|
|
|
|
|
|
|
defer s.conf.ConfigModified()
|
|
|
|
|
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
s.dnsFilter.SetProtectionStatus(true, nil)
|
2023-04-12 14:48:42 +03:00
|
|
|
|
|
|
|
log.Info("dns: protection is restarted after pause")
|
|
|
|
}
|
2024-01-30 18:43:51 +03:00
|
|
|
|
|
|
|
// validateCacheTTL returns an error if the configuration of the cache TTL
|
|
|
|
// invalid.
|
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Move to dnsproxy.
|
|
|
|
func validateCacheTTL(minTTL, maxTTL uint32) (err error) {
|
|
|
|
if minTTL == 0 && maxTTL == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if maxTTL > 0 && minTTL > maxTTL {
|
|
|
|
return errors.Error("cache_ttl_min must be less than or equal to cache_ttl_max")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|