mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-29 10:28:53 +03:00
306c1983a2
Merge in DNS/adguard-home from 1472-edns-custom-ip-api to master Updates #1472 Squashed commit of the following: commit 7605ec5bd5467ddd28a650385193eb2332653bb6 Merge: 8b2ac227194ead34
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 22 13:39:25 2023 +0300 Merge branch 'master' into 1472-edns-custom-ip-api commit 8b2ac22793a51d2555d32c1f5b5c118118807d1f Merge: d5ca8b6ec3edab43
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 21 18:26:55 2023 +0300 Merge branch 'master' into 1472-edns-custom-ip-api commit d5ca8b6e1d87480d4ee4afd346e6bf04907fca95 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 21 18:26:24 2023 +0300 dnsforward: imp tests commit 1302586d22812c22755ea1c7e7fcd32330d707c5 Author: Vladislav Abdulmyanov <v.abdulmyanov@adguard.com> Date: Tue Mar 21 16:47:56 2023 +0200 client: change validation for custom edns ip commit 44e4dc6d1e47de6597ed9ee328db9639e38b4868 Author: Vladislav Abdulmyanov <v.abdulmyanov@adguard.com> Date: Tue Mar 21 16:31:42 2023 +0200 client: implement edns custom ip commit 8a3e7ad8ebf16262818821340da23baa5f004bff Merge: 04ac1112f736d85e
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 21 15:04:40 2023 +0300 Merge branch 'master' into 1472-edns-custom-ip-api commit 04ac1112dda2e778243555b2f54819e5ba586e05 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 21 15:03:39 2023 +0300 dnsforward: imp tests commit b44f6d0ccb3ba7b7a5be07c9cf293dad9c83c794 Merge: 19c6851e48431f8b
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 17:55:49 2023 +0300 Merge branch 'master' into 1472-edns-custom-ip-api commit 19c6851e30f30b0572334822e8639e03760de986 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 10 10:40:15 2023 +0300 all: fix chlog commit 6dcdcbd666ebf3a56a38251e8ead09c605068ce3 Merge: a7f1bf71a2053526
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 10 10:23:37 2023 +0300 Merge branch 'master' into 1472-edns-custom-ip-api commit a7f1bf715e3557c710b700b9bd923868eb07715b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 7 19:24:18 2023 +0300 home: fix default value commit 0311a9bb6571975963d747ef4ef427b59dca03bc Merge: 7e0bb3df1011b8f9
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 7 19:04:18 2023 +0300 Merge branch 'master' into 1472-edns-custom-ip-api commit 7e0bb3df78f10f4b4ae0fd49681d1aa0040521c5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 7 19:03:24 2023 +0300 all: fix chlog commit 202d7ccf4721ccf39726da01d237e07317bfaa58 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 7 11:35:41 2023 +0300 dnsforward: fix typo commit fe95e003a0c3b316a44b5ec0b848a60ddd4c85cf Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 7 11:28:21 2023 +0300 all: fix docs commit 66835a9aa22b3015f9238c1d6f5aa9bd6067db8a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 7 10:48:08 2023 +0300 dnsforward: add todo commit b58255e1e6660a8229bb9c40f2acddebb3dbdc66 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 6 15:40:02 2023 +0300 all: upd chlog commit 9b2be7facba30c815144e08a7835353cad14c405 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 3 11:22:19 2023 +0300 dnsforward: edns custom ip api
637 lines
20 KiB
Go
637 lines
20 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/ameshkov/dnscrypt/v2"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
// 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"
|
|
)
|
|
|
|
// 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
|
|
|
|
// FilterHandler is an optional additional filtering callback.
|
|
FilterHandler func(clientAddr net.IP, clientID string, settings *filtering.Settings) `yaml:"-"`
|
|
|
|
// 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:"-"`
|
|
|
|
// Protection configuration
|
|
|
|
// ProtectionEnabled defines whether or not use any of filtering features.
|
|
ProtectionEnabled bool `yaml:"protection_enabled"`
|
|
|
|
// 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"`
|
|
|
|
// 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.
|
|
SafeBrowsingBlockHost string `yaml:"safebrowsing_block_host"`
|
|
|
|
// Anti-DNS amplification
|
|
|
|
// 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"`
|
|
|
|
// Upstream DNS servers configuration
|
|
|
|
// 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"`
|
|
|
|
// FastestTimeout replaces the default timeout for dialing IP addresses
|
|
// when FastestAddr is true.
|
|
FastestTimeout timeutil.Duration `yaml:"fastest_timeout"`
|
|
|
|
// Access settings
|
|
|
|
// 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.
|
|
AllowedClients []string `yaml:"allowed_clients"`
|
|
|
|
// DisallowedClients is the slice of IP addresses, CIDR networks, and
|
|
// ClientIDs of disallowed clients.
|
|
DisallowedClients []string `yaml:"disallowed_clients"`
|
|
|
|
// BlockedHosts is the list of hosts that should be blocked.
|
|
BlockedHosts []string `yaml:"blocked_hosts"`
|
|
|
|
// 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.
|
|
TrustedProxies []string `yaml:"trusted_proxies"`
|
|
|
|
// DNS cache settings
|
|
|
|
// 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"`
|
|
|
|
// CacheOptimistic defines if optimistic cache mechanism should be used.
|
|
CacheOptimistic bool `yaml:"cache_optimistic"`
|
|
|
|
// Other settings
|
|
|
|
// 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"`
|
|
|
|
// 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:
|
|
//
|
|
// DOMAIN[,DOMAIN].../IPSET_NAME
|
|
//
|
|
// This field is ignored if [IpsetListFileName] is set.
|
|
IpsetList []string `yaml:"ipset"`
|
|
|
|
// IpsetListFileName, if set, points to the file with ipset configuration.
|
|
// The format is the same as in [IpsetList].
|
|
IpsetListFileName string `yaml:"ipset_file"`
|
|
}
|
|
|
|
// EDNSClientSubnet is the settings list for EDNS Client Subnet.
|
|
type EDNSClientSubnet struct {
|
|
// CustomIP for EDNS Client Subnet.
|
|
CustomIP netip.Addr `yaml:"custom_ip"`
|
|
|
|
// Enabled defines if EDNS Client Subnet is enabled.
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
// UseCustom defines if CustomIP should be used.
|
|
UseCustom bool `yaml:"use_custom"`
|
|
}
|
|
|
|
// TLSConfig is the TLS configuration for HTTPS, DNS-over-HTTPS, and DNS-over-TLS
|
|
type TLSConfig struct {
|
|
cert tls.Certificate
|
|
|
|
TLSListenAddrs []*net.TCPAddr `yaml:"-" json:"-"`
|
|
QUICListenAddrs []*net.UDPAddr `yaml:"-" json:"-"`
|
|
HTTPSListenAddrs []*net.TCPAddr `yaml:"-" json:"-"`
|
|
|
|
// 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"`
|
|
|
|
CertificateChainData []byte `yaml:"-" json:"-"`
|
|
PrivateKeyData []byte `yaml:"-" json:"-"`
|
|
|
|
// ServerName is the hostname of the server. Currently, it is only being
|
|
// used for ClientID checking and Discovery of Designated Resolvers (DDR).
|
|
ServerName string `yaml:"-" json:"-"`
|
|
|
|
// DNS names from certificate (SAN) or CN value from Subject
|
|
dnsNames []string
|
|
|
|
// 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
|
|
}
|
|
|
|
// DNSCryptConfig is the DNSCrypt server configuration struct.
|
|
type DNSCryptConfig struct {
|
|
ResolverCert *dnscrypt.Cert
|
|
ProviderName string
|
|
UDPListenAddrs []*net.UDPAddr
|
|
TCPListenAddrs []*net.TCPAddr
|
|
Enabled bool
|
|
}
|
|
|
|
// ServerConfig represents server configuration.
|
|
// The zero ServerConfig is empty and ready for use.
|
|
type ServerConfig struct {
|
|
UDPListenAddrs []*net.UDPAddr // UDP listen address
|
|
TCPListenAddrs []*net.TCPAddr // TCP listen address
|
|
UpstreamConfig *proxy.UpstreamConfig // Upstream DNS servers config
|
|
OnDNSRequest func(d *proxy.DNSContext)
|
|
|
|
FilteringConfig
|
|
TLSConfig
|
|
DNSCryptConfig
|
|
TLSAllowUnencryptedDoH bool
|
|
|
|
// UpstreamTimeout is the timeout for querying upstream servers.
|
|
UpstreamTimeout time.Duration
|
|
|
|
TLSv12Roots *x509.CertPool // list of root CAs for TLSv1.2
|
|
|
|
// TLSCiphers are the IDs of TLS cipher suites to use.
|
|
TLSCiphers []uint16
|
|
|
|
// Called when the configuration is changed by HTTP request
|
|
ConfigModified func()
|
|
|
|
// Register an HTTP handler
|
|
HTTPRegister aghhttp.RegisterFunc
|
|
|
|
// LocalPTRResolvers is a slice of addresses to be used as upstreams for
|
|
// resolving PTR queries for local addresses.
|
|
LocalPTRResolvers []string
|
|
|
|
// DNS64Prefixes is a slice of NAT64 prefixes to be used for DNS64.
|
|
DNS64Prefixes []netip.Prefix
|
|
|
|
// ResolveClients signals if the RDNS should resolve clients' addresses.
|
|
ResolveClients bool
|
|
|
|
// UsePrivateRDNS defines if the PTR requests for unknown addresses from
|
|
// locally-served networks should be resolved via private PTR resolvers.
|
|
UsePrivateRDNS bool
|
|
|
|
// UseDNS64 defines if DNS64 is enabled for incoming requests.
|
|
UseDNS64 bool
|
|
|
|
// 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
|
|
}
|
|
|
|
// if any of ServerConfig values are zero, then default values from below are used
|
|
var defaultValues = ServerConfig{
|
|
UDPListenAddrs: []*net.UDPAddr{{Port: 53}},
|
|
TCPListenAddrs: []*net.TCPAddr{{Port: 53}},
|
|
FilteringConfig: FilteringConfig{BlockedResponseTTL: 3600},
|
|
}
|
|
|
|
// 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,
|
|
HTTP3: srvConf.ServeHTTP3,
|
|
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,
|
|
BeforeRequestHandler: s.beforeRequestHandler,
|
|
RequestHandler: s.handleDNSRequest,
|
|
EnableEDNSClientSubnet: srvConf.EDNSClientSubnet.Enabled,
|
|
MaxGoroutines: int(srvConf.MaxGoroutines),
|
|
UseDNS64: srvConf.UseDNS64,
|
|
DNS64Prefs: srvConf.DNS64Prefixes,
|
|
}
|
|
|
|
if srvConf.EDNSClientSubnet.UseCustom {
|
|
// TODO(s.chzhen): Use netip.Addr instead of net.IP inside dnsproxy.
|
|
conf.EDNSAddr = net.IP(srvConf.EDNSClientSubnet.CustomIP.AsSlice())
|
|
}
|
|
|
|
if srvConf.CacheSize != 0 {
|
|
conf.CacheEnabled = true
|
|
conf.CacheSizeBytes = int(srvConf.CacheSize)
|
|
}
|
|
|
|
setProxyUpstreamMode(
|
|
&conf,
|
|
srvConf.AllServers,
|
|
srvConf.FastestAddr,
|
|
srvConf.FastestTimeout.Duration,
|
|
)
|
|
|
|
for i, s := range srvConf.BogusNXDomain {
|
|
var subnet *net.IPNet
|
|
subnet, err = netutil.ParseSubnet(s)
|
|
if err != nil {
|
|
log.Error("subnet at index %d: %s", i, err)
|
|
|
|
continue
|
|
}
|
|
|
|
conf.BogusNXDomain = append(conf.BogusNXDomain, subnet)
|
|
}
|
|
|
|
err = s.prepareTLS(&conf)
|
|
if err != nil {
|
|
return proxy.Config{}, fmt.Errorf("validating tls: %w", err)
|
|
}
|
|
|
|
if c := srvConf.DNSCryptConfig; c.Enabled {
|
|
conf.DNSCryptUDPListenAddr = c.UDPListenAddrs
|
|
conf.DNSCryptTCPListenAddr = c.TCPListenAddrs
|
|
conf.DNSCryptProviderName = c.ProviderName
|
|
conf.DNSCryptResolverCert = c.ResolverCert
|
|
}
|
|
|
|
if conf.UpstreamConfig == nil || len(conf.UpstreamConfig.Upstreams) == 0 {
|
|
return proxy.Config{}, errors.Error("no default upstream servers configured")
|
|
}
|
|
|
|
return conf, nil
|
|
}
|
|
|
|
const (
|
|
defaultSafeBrowsingBlockHost = "standard-block.dns.adguard.com"
|
|
defaultParentalBlockHost = "family-block.dns.adguard.com"
|
|
)
|
|
|
|
// initDefaultSettings initializes default settings if nothing
|
|
// is configured
|
|
func (s *Server) initDefaultSettings() {
|
|
if len(s.conf.UpstreamDNS) == 0 {
|
|
s.conf.UpstreamDNS = defaultDNS
|
|
}
|
|
|
|
if len(s.conf.BootstrapDNS) == 0 {
|
|
s.conf.BootstrapDNS = defaultBootstrap
|
|
}
|
|
|
|
if s.conf.ParentalBlockHost == "" {
|
|
s.conf.ParentalBlockHost = defaultParentalBlockHost
|
|
}
|
|
|
|
if s.conf.SafeBrowsingBlockHost == "" {
|
|
s.conf.SafeBrowsingBlockHost = defaultSafeBrowsingBlockHost
|
|
}
|
|
|
|
if s.conf.UDPListenAddrs == nil {
|
|
s.conf.UDPListenAddrs = defaultValues.UDPListenAddrs
|
|
}
|
|
|
|
if s.conf.TCPListenAddrs == nil {
|
|
s.conf.TCPListenAddrs = defaultValues.TCPListenAddrs
|
|
}
|
|
|
|
if len(s.conf.BlockedHosts) == 0 {
|
|
s.conf.BlockedHosts = defaultBlockedHosts
|
|
}
|
|
|
|
if s.conf.UpstreamTimeout == 0 {
|
|
s.conf.UpstreamTimeout = DefaultTimeout
|
|
}
|
|
}
|
|
|
|
// 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,
|
|
}
|
|
}
|
|
|
|
// prepareUpstreamSettings - prepares upstream DNS server settings
|
|
func (s *Server) prepareUpstreamSettings() error {
|
|
// 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].
|
|
upstream.RootCAs = s.conf.TLSv12Roots
|
|
upstream.CipherSuites = s.conf.TLSCiphers
|
|
|
|
// Load upstreams either from the file, or from the settings
|
|
var upstreams []string
|
|
if s.conf.UpstreamDNSFileName != "" {
|
|
data, err := os.ReadFile(s.conf.UpstreamDNSFileName)
|
|
if err != nil {
|
|
return fmt.Errorf("reading upstream from file: %w", err)
|
|
}
|
|
|
|
upstreams = stringutil.SplitTrimmed(string(data), "\n")
|
|
|
|
log.Debug("dns: using %d upstream servers from file %s", len(upstreams), s.conf.UpstreamDNSFileName)
|
|
} else {
|
|
upstreams = s.conf.UpstreamDNS
|
|
}
|
|
|
|
httpVersions := UpstreamHTTPVersions(s.conf.UseHTTP3Upstreams)
|
|
upstreams = stringutil.FilterOut(upstreams, IsCommentOrEmpty)
|
|
upstreamConfig, err := proxy.ParseUpstreamsConfig(
|
|
upstreams,
|
|
&upstream.Options{
|
|
Bootstrap: s.conf.BootstrapDNS,
|
|
Timeout: s.conf.UpstreamTimeout,
|
|
HTTPVersions: httpVersions,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing upstream config: %w", err)
|
|
}
|
|
|
|
if len(upstreamConfig.Upstreams) == 0 {
|
|
log.Info("warning: no default upstream servers specified, using %v", defaultDNS)
|
|
var uc *proxy.UpstreamConfig
|
|
uc, err = proxy.ParseUpstreamsConfig(
|
|
defaultDNS,
|
|
&upstream.Options{
|
|
Bootstrap: s.conf.BootstrapDNS,
|
|
Timeout: s.conf.UpstreamTimeout,
|
|
HTTPVersions: httpVersions,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing default upstreams: %w", err)
|
|
}
|
|
|
|
upstreamConfig.Upstreams = uc.Upstreams
|
|
}
|
|
|
|
s.conf.UpstreamConfig = upstreamConfig
|
|
|
|
return nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// prepareTLS - prepares TLS configuration for the DNS proxy
|
|
func (s *Server) prepareTLS(proxyConfig *proxy.Config) (err error) {
|
|
if len(s.conf.CertificateChainData) == 0 || len(s.conf.PrivateKeyData) == 0 {
|
|
return nil
|
|
}
|
|
|
|
if s.conf.TLSListenAddrs == nil && s.conf.QUICListenAddrs == nil {
|
|
return nil
|
|
}
|
|
|
|
proxyConfig.TLSListenAddr = aghalg.CoalesceSlice(
|
|
s.conf.TLSListenAddrs,
|
|
proxyConfig.TLSListenAddr,
|
|
)
|
|
|
|
proxyConfig.QUICListenAddr = aghalg.CoalesceSlice(
|
|
s.conf.QUICListenAddrs,
|
|
proxyConfig.QUICListenAddr,
|
|
)
|
|
|
|
s.conf.cert, err = tls.X509KeyPair(s.conf.CertificateChainData, s.conf.PrivateKeyData)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse TLS keypair: %w", err)
|
|
}
|
|
|
|
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)
|
|
|
|
if s.conf.StrictSNICheck {
|
|
if len(cert.DNSNames) != 0 {
|
|
s.conf.dnsNames = cert.DNSNames
|
|
log.Debug("dnsforward: using certificate's SAN as DNS names: %v", cert.DNSNames)
|
|
slices.Sort(s.conf.dnsNames)
|
|
} else {
|
|
s.conf.dnsNames = append(s.conf.dnsNames, cert.Subject.CommonName)
|
|
log.Debug("dnsforward: using certificate's CN as DNS name: %s", cert.Subject.CommonName)
|
|
}
|
|
}
|
|
|
|
proxyConfig.TLSConfig = &tls.Config{
|
|
GetCertificate: s.onGetCertificate,
|
|
CipherSuites: s.conf.TLSCiphers,
|
|
MinVersion: tls.VersionTLS12,
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// 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) {
|
|
// Check sni is either a valid hostname or a valid IP address.
|
|
if netutil.ValidateHostname(sni) != nil && net.ParseIP(sni) == nil {
|
|
return false
|
|
}
|
|
|
|
if _, ok = slices.BinarySearch(dnsNames, sni); ok {
|
|
return true
|
|
}
|
|
|
|
for _, dn := range dnsNames {
|
|
if matchesDomainWildcard(sni, dn) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// 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) {
|
|
if s.conf.StrictSNICheck && !anyNameMatches(s.conf.dnsNames, ch.ServerName) {
|
|
log.Info("dns: tls: unknown SNI in Client Hello: %s", ch.ServerName)
|
|
return nil, fmt.Errorf("invalid SNI")
|
|
}
|
|
return &s.conf.cert, nil
|
|
}
|