2020-05-08 18:39:37 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-05-21 14:55:42 +03:00
|
|
|
"os"
|
2020-05-08 18:39:37 +03:00
|
|
|
"sort"
|
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
|
|
|
|
2021-05-28 13:02:59 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
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"
|
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
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"
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2020-12-07 17:58:33 +03:00
|
|
|
"github.com/ameshkov/dnscrypt/v2"
|
2020-05-08 18:39:37 +03:00
|
|
|
)
|
|
|
|
|
2021-06-17 14:09:16 +03: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 18:39:37 +03: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 18:32:13 +03:00
|
|
|
// FilterHandler is an optional additional filtering callback.
|
2021-05-21 16:15:47 +03:00
|
|
|
FilterHandler func(clientAddr net.IP, clientID string, settings *filtering.Settings) `yaml:"-"`
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2021-05-28 13:02:59 +03: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 18:39:37 +03:00
|
|
|
|
|
|
|
// Protection configuration
|
|
|
|
// --
|
|
|
|
|
2021-06-17 14:09:16 +03:00
|
|
|
ProtectionEnabled bool `yaml:"protection_enabled"` // whether or not use any of filtering features
|
|
|
|
BlockingMode BlockingMode `yaml:"blocking_mode"` // mode how to answer filtered requests
|
|
|
|
BlockingIPv4 net.IP `yaml:"blocking_ipv4"` // IP address to be returned for a blocked A request
|
|
|
|
BlockingIPv6 net.IP `yaml:"blocking_ipv6"` // IP address to be returned for a blocked AAAA request
|
|
|
|
BlockedResponseTTL uint32 `yaml:"blocked_response_ttl"` // if 0, then default is used (3600)
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
// IP (or domain name) which is used to respond to DNS requests blocked by parental control or safe-browsing
|
|
|
|
ParentalBlockHost string `yaml:"parental_block_host"`
|
|
|
|
SafeBrowsingBlockHost string `yaml:"safebrowsing_block_host"`
|
|
|
|
|
|
|
|
// Anti-DNS amplification
|
|
|
|
// --
|
|
|
|
|
|
|
|
Ratelimit uint32 `yaml:"ratelimit"` // max number of requests per second from a given IP (0 to disable)
|
|
|
|
RatelimitWhitelist []string `yaml:"ratelimit_whitelist"` // a list of whitelisted client IP addresses
|
|
|
|
RefuseAny bool `yaml:"refuse_any"` // if true, refuse ANY requests
|
|
|
|
|
|
|
|
// Upstream DNS servers configuration
|
|
|
|
// --
|
|
|
|
|
2020-09-01 14:33:35 +03:00
|
|
|
UpstreamDNS []string `yaml:"upstream_dns"`
|
|
|
|
UpstreamDNSFileName string `yaml:"upstream_dns_file"`
|
|
|
|
BootstrapDNS []string `yaml:"bootstrap_dns"` // a list of bootstrap DNS for DoH and DoT (plain DNS only)
|
|
|
|
AllServers bool `yaml:"all_servers"` // if true, parallel queries to all configured upstream servers are enabled
|
|
|
|
FastestAddr bool `yaml:"fastest_addr"` // use Fastest Address algorithm
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
// Access settings
|
|
|
|
// --
|
|
|
|
|
|
|
|
AllowedClients []string `yaml:"allowed_clients"` // IP addresses of whitelist clients
|
|
|
|
DisallowedClients []string `yaml:"disallowed_clients"` // IP addresses of clients that should be blocked
|
|
|
|
BlockedHosts []string `yaml:"blocked_hosts"` // hosts that should be blocked
|
2021-07-26 19:30:35 +03: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.
|
|
|
|
TrustedProxies []string `yaml:"trusted_proxies"`
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
// DNS cache settings
|
|
|
|
// --
|
|
|
|
|
|
|
|
CacheSize uint32 `yaml:"cache_size"` // DNS cache size (in bytes)
|
|
|
|
CacheMinTTL uint32 `yaml:"cache_ttl_min"` // override TTL value (minimum) received from upstream server
|
|
|
|
CacheMaxTTL uint32 `yaml:"cache_ttl_max"` // override TTL value (maximum) received from upstream server
|
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
|
|
|
|
// --
|
|
|
|
|
2020-05-08 19:02:16 +03:00
|
|
|
BogusNXDomain []string `yaml:"bogus_nxdomain"` // transform responses with these IP addresses to NXDOMAIN
|
|
|
|
AAAADisabled bool `yaml:"aaaa_disabled"` // Respond with an empty answer to all AAAA requests
|
|
|
|
EnableDNSSEC bool `yaml:"enable_dnssec"` // Set DNSSEC flag in outcoming DNS request
|
|
|
|
EnableEDNSClientSubnet bool `yaml:"edns_client_subnet"` // Enable EDNS Client Subnet option
|
2020-08-14 19:27:36 +03:00
|
|
|
MaxGoroutines uint32 `yaml:"max_goroutines"` // Max. number of parallel goroutines for processing incoming requests
|
2020-09-02 14:13:45 +03:00
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
// 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
|
|
|
|
//
|
|
|
|
IpsetList []string `yaml:"ipset"`
|
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 {
|
2021-03-23 12:32:07 +03:00
|
|
|
TLSListenAddrs []*net.TCPAddr `yaml:"-" json:"-"`
|
|
|
|
QUICListenAddrs []*net.UDPAddr `yaml:"-" json:"-"`
|
2020-05-16 02:02:50 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
// Reject connection if the client uses server name (in SNI) that doesn't match the certificate
|
|
|
|
StrictSNICheck bool `yaml:"strict_sni_check" json:"-"`
|
2020-05-08 18:39:37 +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:"-"`
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// ServerName is the hostname of the server. Currently, it is only
|
|
|
|
// being used for client ID checking.
|
|
|
|
ServerName string `yaml:"-" json:"-"`
|
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
cert tls.Certificate
|
|
|
|
// DNS names from certificate (SAN) or CN value from Subject
|
|
|
|
dnsNames []string
|
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 {
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs []*net.UDPAddr
|
|
|
|
TCPListenAddrs []*net.TCPAddr
|
|
|
|
ProviderName string
|
|
|
|
ResolverCert *dnscrypt.Cert
|
|
|
|
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 {
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs []*net.UDPAddr // UDP listen address
|
|
|
|
TCPListenAddrs []*net.TCPAddr // TCP listen address
|
2020-05-13 20:31:43 +03:00
|
|
|
UpstreamConfig *proxy.UpstreamConfig // Upstream DNS servers config
|
|
|
|
OnDNSRequest func(d *proxy.DNSContext)
|
2020-05-08 18:39:37 +03:00
|
|
|
|
|
|
|
FilteringConfig
|
|
|
|
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
|
|
|
|
TLSCiphers []uint16 // list of TLS ciphers to use
|
|
|
|
|
|
|
|
// Called when the configuration is changed by HTTP request
|
|
|
|
ConfigModified func()
|
|
|
|
|
|
|
|
// Register an HTTP handler
|
|
|
|
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request))
|
2021-04-07 20:16:06 +03:00
|
|
|
|
|
|
|
// ResolveClients signals if the RDNS should resolve clients' addresses.
|
|
|
|
ResolveClients bool
|
|
|
|
|
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
|
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
// LocalPTRResolvers is a slice of addresses to be used as upstreams for
|
|
|
|
// resolving PTR queries for local addresses.
|
|
|
|
LocalPTRResolvers []string
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// if any of ServerConfig values are zero, then default values from below are used
|
|
|
|
var defaultValues = ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{Port: 53}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{Port: 53}},
|
2020-05-08 18:39:37 +03:00
|
|
|
FilteringConfig: FilteringConfig{BlockedResponseTTL: 3600},
|
|
|
|
}
|
|
|
|
|
|
|
|
// createProxyConfig creates and validates configuration for the main proxy
|
|
|
|
func (s *Server) createProxyConfig() (proxy.Config, error) {
|
|
|
|
proxyConfig := proxy.Config{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddr: s.conf.UDPListenAddrs,
|
|
|
|
TCPListenAddr: s.conf.TCPListenAddrs,
|
2020-05-13 20:31:43 +03:00
|
|
|
Ratelimit: int(s.conf.Ratelimit),
|
|
|
|
RatelimitWhitelist: s.conf.RatelimitWhitelist,
|
|
|
|
RefuseAny: s.conf.RefuseAny,
|
2021-07-26 19:30:35 +03:00
|
|
|
TrustedProxies: s.conf.TrustedProxies,
|
2020-05-13 20:31:43 +03:00
|
|
|
CacheMinTTL: s.conf.CacheMinTTL,
|
|
|
|
CacheMaxTTL: s.conf.CacheMaxTTL,
|
2021-07-14 21:03:56 +03:00
|
|
|
CacheOptimistic: s.conf.CacheOptimistic,
|
2020-05-13 20:31:43 +03:00
|
|
|
UpstreamConfig: s.conf.UpstreamConfig,
|
|
|
|
BeforeRequestHandler: s.beforeRequestHandler,
|
|
|
|
RequestHandler: s.handleDNSRequest,
|
|
|
|
EnableEDNSClientSubnet: s.conf.EnableEDNSClientSubnet,
|
2020-08-14 19:27:36 +03:00
|
|
|
MaxGoroutines: int(s.conf.MaxGoroutines),
|
2020-06-10 15:33:23 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 15:13:38 +03:00
|
|
|
if s.conf.CacheSize != 0 {
|
|
|
|
proxyConfig.CacheEnabled = true
|
|
|
|
proxyConfig.CacheSizeBytes = int(s.conf.CacheSize)
|
|
|
|
}
|
|
|
|
|
2020-06-10 15:33:23 +03:00
|
|
|
proxyConfig.UpstreamMode = proxy.UModeLoadBalance
|
|
|
|
if s.conf.AllServers {
|
|
|
|
proxyConfig.UpstreamMode = proxy.UModeParallel
|
|
|
|
} else if s.conf.FastestAddr {
|
|
|
|
proxyConfig.UpstreamMode = proxy.UModeFastestAddr
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.conf.BogusNXDomain) > 0 {
|
|
|
|
for _, s := range s.conf.BogusNXDomain {
|
|
|
|
ip := net.ParseIP(s)
|
|
|
|
if ip == nil {
|
|
|
|
log.Error("Invalid bogus IP: %s", s)
|
|
|
|
} else {
|
|
|
|
proxyConfig.BogusNXDomain = append(proxyConfig.BogusNXDomain, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLS settings
|
|
|
|
err := s.prepareTLS(&proxyConfig)
|
|
|
|
if err != nil {
|
|
|
|
return proxyConfig, err
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:58:33 +03:00
|
|
|
if s.conf.DNSCryptConfig.Enabled {
|
2021-03-23 12:32:07 +03:00
|
|
|
proxyConfig.DNSCryptUDPListenAddr = s.conf.DNSCryptConfig.UDPListenAddrs
|
|
|
|
proxyConfig.DNSCryptTCPListenAddr = s.conf.DNSCryptConfig.TCPListenAddrs
|
2020-12-07 17:58:33 +03:00
|
|
|
proxyConfig.DNSCryptProviderName = s.conf.DNSCryptConfig.ProviderName
|
|
|
|
proxyConfig.DNSCryptResolverCert = s.conf.DNSCryptConfig.ResolverCert
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
// Validate proxy config
|
2020-05-13 20:31:43 +03:00
|
|
|
if proxyConfig.UpstreamConfig == nil || len(proxyConfig.UpstreamConfig.Upstreams) == 0 {
|
2021-05-24 17:28:11 +03:00
|
|
|
return proxyConfig, errors.Error("no default upstream servers configured")
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return proxyConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
if len(s.conf.ParentalBlockHost) == 0 {
|
|
|
|
s.conf.ParentalBlockHost = parentalBlockHost
|
|
|
|
}
|
2021-03-23 12:32:07 +03:00
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
if len(s.conf.SafeBrowsingBlockHost) == 0 {
|
|
|
|
s.conf.SafeBrowsingBlockHost = safeBrowsingBlockHost
|
|
|
|
}
|
2021-03-23 12:32:07 +03:00
|
|
|
|
|
|
|
if s.conf.UDPListenAddrs == nil {
|
|
|
|
s.conf.UDPListenAddrs = defaultValues.UDPListenAddrs
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2021-03-23 12:32:07 +03:00
|
|
|
|
|
|
|
if s.conf.TCPListenAddrs == nil {
|
|
|
|
s.conf.TCPListenAddrs = defaultValues.TCPListenAddrs
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// prepareUpstreamSettings - prepares upstream DNS server settings
|
|
|
|
func (s *Server) prepareUpstreamSettings() error {
|
2020-09-08 17:28:01 +03: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 "util.LoadSystemRootCAs"
|
|
|
|
upstream.RootCAs = s.conf.TLSv12Roots
|
|
|
|
|
|
|
|
// See util.InitTLSCiphers -- removed unsafe ciphers
|
|
|
|
if len(s.conf.TLSCiphers) > 0 {
|
|
|
|
upstream.CipherSuites = s.conf.TLSCiphers
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:11 +03:00
|
|
|
// Load upstreams either from the file, or from the settings
|
2020-09-01 14:33:35 +03:00
|
|
|
var upstreams []string
|
|
|
|
if s.conf.UpstreamDNSFileName != "" {
|
2021-05-21 14:55:42 +03:00
|
|
|
data, err := os.ReadFile(s.conf.UpstreamDNSFileName)
|
2020-09-01 14:33:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-29 17:40:31 +03:00
|
|
|
|
|
|
|
upstreams = stringutil.SplitTrimmed(string(data), "\n")
|
|
|
|
|
2021-03-25 16:00:27 +03:00
|
|
|
log.Debug("dns: using %d upstream servers from file %s", len(upstreams), s.conf.UpstreamDNSFileName)
|
2020-09-01 14:33:35 +03:00
|
|
|
} else {
|
|
|
|
upstreams = s.conf.UpstreamDNS
|
|
|
|
}
|
2021-03-11 12:17:54 +03:00
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
upstreams = stringutil.FilterOut(upstreams, IsCommentOrEmpty)
|
2021-03-11 12:17:54 +03:00
|
|
|
upstreamConfig, err := proxy.ParseUpstreamsConfig(
|
|
|
|
upstreams,
|
2021-06-29 15:53:28 +03:00
|
|
|
&upstream.Options{
|
2021-03-03 15:27:25 +03:00
|
|
|
Bootstrap: s.conf.BootstrapDNS,
|
2021-06-15 17:36:49 +03:00
|
|
|
Timeout: s.conf.UpstreamTimeout,
|
2021-03-03 15:27:25 +03:00
|
|
|
},
|
|
|
|
)
|
2020-05-08 18:39:37 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("dns: proxy.ParseUpstreamsConfig: %w", err)
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2020-09-09 18:12:11 +03:00
|
|
|
|
|
|
|
if len(upstreamConfig.Upstreams) == 0 {
|
2021-02-16 18:46:49 +03:00
|
|
|
log.Info("warning: no default upstream servers specified, using %v", defaultDNS)
|
2021-06-29 15:53:28 +03:00
|
|
|
var uc *proxy.UpstreamConfig
|
2021-03-11 21:30:52 +03:00
|
|
|
uc, err = proxy.ParseUpstreamsConfig(
|
2021-03-11 12:17:54 +03:00
|
|
|
defaultDNS,
|
2021-06-29 15:53:28 +03:00
|
|
|
&upstream.Options{
|
2021-03-03 15:27:25 +03:00
|
|
|
Bootstrap: s.conf.BootstrapDNS,
|
2021-06-15 17:36:49 +03:00
|
|
|
Timeout: s.conf.UpstreamTimeout,
|
2021-03-03 15:27:25 +03:00
|
|
|
},
|
|
|
|
)
|
2020-09-09 18:12:11 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("dns: failed to parse default upstreams: %v", err)
|
2020-09-09 18:12:11 +03:00
|
|
|
}
|
|
|
|
upstreamConfig.Upstreams = uc.Upstreams
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
s.conf.UpstreamConfig = upstreamConfig
|
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareIntlProxy - initializes DNS proxy that we use for internal DNS queries
|
|
|
|
func (s *Server) prepareIntlProxy() {
|
2021-02-04 20:35:13 +03:00
|
|
|
s.internalProxy = &proxy.Proxy{
|
|
|
|
Config: proxy.Config{
|
|
|
|
CacheEnabled: true,
|
|
|
|
CacheSizeBytes: 4096,
|
|
|
|
UpstreamConfig: s.conf.UpstreamConfig,
|
|
|
|
},
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareTLS - prepares TLS configuration for the DNS proxy
|
|
|
|
func (s *Server) prepareTLS(proxyConfig *proxy.Config) 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
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:32:07 +03:00
|
|
|
if s.conf.TLSListenAddrs != nil {
|
|
|
|
proxyConfig.TLSListenAddr = s.conf.TLSListenAddrs
|
2020-09-08 17:20:24 +03:00
|
|
|
}
|
|
|
|
|
2021-03-23 12:32:07 +03:00
|
|
|
if s.conf.QUICListenAddrs != nil {
|
|
|
|
proxyConfig.QUICListenAddr = s.conf.QUICListenAddrs
|
2020-09-08 17:20:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if s.conf.StrictSNICheck {
|
2021-03-11 21:30:52 +03:00
|
|
|
var x *x509.Certificate
|
|
|
|
x, err = x509.ParseCertificate(s.conf.cert.Certificate[0])
|
2020-05-08 18:39:37 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("x509.ParseCertificate(): %w", err)
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2020-09-08 17:20:24 +03:00
|
|
|
if len(x.DNSNames) != 0 {
|
|
|
|
s.conf.dnsNames = x.DNSNames
|
2021-03-25 16:00:27 +03:00
|
|
|
log.Debug("dns: using DNS names from certificate's SAN: %v", x.DNSNames)
|
2020-09-08 17:20:24 +03:00
|
|
|
sort.Strings(s.conf.dnsNames)
|
|
|
|
} else {
|
|
|
|
s.conf.dnsNames = append(s.conf.dnsNames, x.Subject.CommonName)
|
2021-03-25 16:00:27 +03:00
|
|
|
log.Debug("dns: using DNS name from certificate's CN: %s", x.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,
|
|
|
|
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
|
|
|
// isInSorted returns true if s is in the sorted slice strs.
|
|
|
|
func isInSorted(strs []string, s string) (ok bool) {
|
|
|
|
i := sort.SearchStrings(strs, s)
|
|
|
|
if i == len(strs) || strs[i] != s {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if aghnet.ValidateDomainName(sni) != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if isInSorted(dnsNames, sni) {
|
|
|
|
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
|
|
|
|
}
|