2020-11-03 15:39:55 +03:00
|
|
|
// Package dnsforward contains a DNS forwarding server.
|
2018-11-28 15:40:56 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2019-10-30 11:52:58 +03:00
|
|
|
"fmt"
|
2018-11-28 15:40:56 +03:00
|
|
|
"net"
|
2019-02-22 15:52:12 +03:00
|
|
|
"net/http"
|
2019-10-21 15:58:14 +03:00
|
|
|
"runtime"
|
2021-04-07 20:16:06 +03:00
|
|
|
"strings"
|
2018-11-28 15:40:56 +03:00
|
|
|
"sync"
|
2018-12-05 14:03:41 +03:00
|
|
|
"time"
|
2018-11-28 15:40:56 +03:00
|
|
|
|
2022-10-24 16:29:44 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2021-03-31 15:00:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2020-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
2018-12-24 16:58:48 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-04-09 21:01:21 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2021-06-29 15:53:28 +03:00
|
|
|
"github.com/AdguardTeam/golibs/cache"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-25 16:44:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
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"
|
2018-11-28 15:40:56 +03:00
|
|
|
"github.com/miekg/dns"
|
2018-12-24 15:19:52 +03:00
|
|
|
)
|
|
|
|
|
2022-10-24 16:29:44 +03:00
|
|
|
//lint:file-ignore SA1019 TODO(a.garipov): Replace [*netutil.IPMap].
|
|
|
|
|
2018-12-25 01:59:38 +03:00
|
|
|
// DefaultTimeout is the default upstream timeout
|
|
|
|
const DefaultTimeout = 10 * time.Second
|
|
|
|
|
2022-02-10 15:42:59 +03:00
|
|
|
// defaultClientIDCacheCount is the default count of items in the LRU ClientID
|
2021-06-29 15:53:28 +03:00
|
|
|
// cache. The assumption here is that there won't be more than this many
|
|
|
|
// requests between the BeforeRequestHandler stage and the actual processing.
|
|
|
|
const defaultClientIDCacheCount = 1024
|
|
|
|
|
2019-10-31 12:32:14 +03:00
|
|
|
var defaultDNS = []string{
|
2020-03-05 13:12:21 +03:00
|
|
|
"https://dns10.quad9.net/dns-query",
|
2019-10-31 12:32:14 +03:00
|
|
|
}
|
2020-03-05 13:12:21 +03:00
|
|
|
var defaultBootstrap = []string{"9.9.9.10", "149.112.112.10", "2620:fe::10", "2620:fe::fe:10"}
|
2019-10-31 12:32:14 +03:00
|
|
|
|
2020-09-10 12:32:36 +03:00
|
|
|
// Often requested by all kinds of DNS probes
|
|
|
|
var defaultBlockedHosts = []string{"version.bind", "id.server", "hostname.bind"}
|
|
|
|
|
2020-01-16 14:25:40 +03:00
|
|
|
var webRegistered bool
|
|
|
|
|
2021-04-15 17:52:53 +03:00
|
|
|
// hostToIPTable is an alias for the type of Server.tableHostToIP.
|
|
|
|
type hostToIPTable = map[string]net.IP
|
|
|
|
|
2018-12-05 12:52:23 +03:00
|
|
|
// Server is the main way to start a DNS server.
|
|
|
|
//
|
2018-11-28 15:40:56 +03:00
|
|
|
// Example:
|
2022-08-29 15:54:41 +03:00
|
|
|
//
|
|
|
|
// s := dnsforward.Server{}
|
|
|
|
// err := s.Start(nil) // will start a DNS server listening on default port 53, in a goroutine
|
|
|
|
// err := s.Reconfigure(ServerConfig{UDPListenAddr: &net.UDPAddr{Port: 53535}}) // will reconfigure running DNS server to listen on UDP port 53535
|
|
|
|
// err := s.Stop() // will stop listening on port 53535 and cancel all goroutines
|
|
|
|
// err := s.Start(nil) // will start listening again, on port 53535, in a goroutine
|
2018-11-28 15:40:56 +03:00
|
|
|
//
|
|
|
|
// The zero Server is empty and ready for use.
|
|
|
|
type Server struct {
|
2022-09-13 23:45:35 +03:00
|
|
|
dnsProxy *proxy.Proxy // DNS proxy instance
|
|
|
|
dnsFilter *filtering.DNSFilter // DNS filter instance
|
|
|
|
dhcpServer dhcpd.Interface // DHCP server instance (optional)
|
|
|
|
queryLog querylog.QueryLog // Query log instance
|
2022-08-04 19:05:28 +03:00
|
|
|
stats stats.Interface
|
2022-10-24 16:29:44 +03:00
|
|
|
access *accessManager
|
2020-06-23 12:13:13 +03:00
|
|
|
|
2021-04-15 19:00:31 +03:00
|
|
|
// localDomainSuffix is the suffix used to detect internal hosts. It
|
|
|
|
// must be a valid domain name plus dots on each side.
|
|
|
|
localDomainSuffix string
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
ipset ipsetCtx
|
2022-03-18 13:37:27 +03:00
|
|
|
privateNets netutil.SubnetSet
|
2021-04-09 21:01:21 +03:00
|
|
|
localResolvers *proxy.Proxy
|
2021-06-01 14:28:34 +03:00
|
|
|
sysResolvers aghnet.SystemResolvers
|
2021-05-27 19:19:19 +03:00
|
|
|
recDetector *recursionDetector
|
2020-09-02 14:13:45 +03:00
|
|
|
|
2021-12-06 17:26:43 +03:00
|
|
|
// anonymizer masks the client's IP addresses if needed.
|
|
|
|
anonymizer *aghnet.IPMut
|
|
|
|
|
2021-04-15 17:52:53 +03:00
|
|
|
tableHostToIP hostToIPTable
|
2020-08-18 12:36:52 +03:00
|
|
|
tableHostToIPLock sync.Mutex
|
|
|
|
|
2022-10-14 15:29:44 +03:00
|
|
|
// TODO(e.burkov): Use map[netip.Addr]struct{} instead.
|
2021-08-09 16:03:37 +03:00
|
|
|
tableIPToHost *netutil.IPMap
|
2021-04-15 17:52:53 +03:00
|
|
|
tableIPToHostLock sync.Mutex
|
2019-05-24 14:49:26 +03:00
|
|
|
|
2022-02-10 15:42:59 +03:00
|
|
|
// clientIDCache is a temporary storage for ClientIDs that were extracted
|
|
|
|
// during the BeforeRequestHandler stage.
|
2021-06-29 15:53:28 +03:00
|
|
|
clientIDCache cache.Cache
|
|
|
|
|
2019-12-11 12:38:58 +03:00
|
|
|
// DNS proxy instance for internal usage
|
|
|
|
// We don't Start() it and so no listen port is required.
|
|
|
|
internalProxy *proxy.Proxy
|
|
|
|
|
2020-01-16 14:25:40 +03:00
|
|
|
isRunning bool
|
2019-10-30 11:52:58 +03:00
|
|
|
|
2019-05-15 15:08:15 +03:00
|
|
|
conf ServerConfig
|
2021-05-26 17:55:19 +03:00
|
|
|
// serverLock protects Server.
|
|
|
|
serverLock sync.RWMutex
|
2018-11-28 15:40:56 +03:00
|
|
|
}
|
|
|
|
|
2021-04-15 19:00:31 +03:00
|
|
|
// defaultLocalDomainSuffix is the default suffix used to detect internal hosts
|
|
|
|
// when no suffix is provided.
|
|
|
|
//
|
|
|
|
// See the documentation for Server.localDomainSuffix.
|
2022-06-28 19:09:26 +03:00
|
|
|
const defaultLocalDomainSuffix = "lan"
|
2021-03-25 16:00:27 +03:00
|
|
|
|
|
|
|
// DNSCreateParams are parameters to create a new server.
|
2020-06-23 12:13:13 +03:00
|
|
|
type DNSCreateParams struct {
|
2022-03-18 13:37:27 +03:00
|
|
|
DNSFilter *filtering.DNSFilter
|
2022-08-04 19:05:28 +03:00
|
|
|
Stats stats.Interface
|
2022-03-18 13:37:27 +03:00
|
|
|
QueryLog querylog.QueryLog
|
2022-09-13 23:45:35 +03:00
|
|
|
DHCPServer dhcpd.Interface
|
2022-03-18 13:37:27 +03:00
|
|
|
PrivateNets netutil.SubnetSet
|
|
|
|
Anonymizer *aghnet.IPMut
|
|
|
|
LocalDomain string
|
2021-03-25 16:00:27 +03:00
|
|
|
}
|
|
|
|
|
2021-05-27 19:19:19 +03:00
|
|
|
const (
|
|
|
|
// recursionTTL is the time recursive request is cached for.
|
2021-05-31 13:25:40 +03:00
|
|
|
recursionTTL = 1 * time.Second
|
2021-05-27 19:19:19 +03:00
|
|
|
// cachedRecurrentReqNum is the maximum number of cached recurrent
|
|
|
|
// requests.
|
|
|
|
cachedRecurrentReqNum = 1000
|
|
|
|
)
|
|
|
|
|
2019-02-10 20:47:43 +03:00
|
|
|
// NewServer creates a new instance of the dnsforward.Server
|
2019-07-10 11:56:54 +03:00
|
|
|
// Note: this function must be called only once
|
2021-03-25 16:00:27 +03:00
|
|
|
func NewServer(p DNSCreateParams) (s *Server, err error) {
|
2021-04-15 19:00:31 +03:00
|
|
|
var localDomainSuffix string
|
|
|
|
if p.LocalDomain == "" {
|
|
|
|
localDomainSuffix = defaultLocalDomainSuffix
|
2021-03-25 16:00:27 +03:00
|
|
|
} else {
|
2021-08-09 16:03:37 +03:00
|
|
|
err = netutil.ValidateDomainName(p.LocalDomain)
|
2021-03-25 16:00:27 +03:00
|
|
|
if err != nil {
|
2021-04-15 19:00:31 +03:00
|
|
|
return nil, fmt.Errorf("local domain: %w", err)
|
2021-03-25 16:00:27 +03:00
|
|
|
}
|
|
|
|
|
2022-06-28 19:09:26 +03:00
|
|
|
localDomainSuffix = p.LocalDomain
|
2021-03-25 16:00:27 +03:00
|
|
|
}
|
|
|
|
|
2021-12-06 17:26:43 +03:00
|
|
|
if p.Anonymizer == nil {
|
|
|
|
p.Anonymizer = aghnet.NewIPMut(nil)
|
|
|
|
}
|
2021-03-25 16:00:27 +03:00
|
|
|
s = &Server{
|
2021-04-15 19:00:31 +03:00
|
|
|
dnsFilter: p.DNSFilter,
|
|
|
|
stats: p.Stats,
|
|
|
|
queryLog: p.QueryLog,
|
2022-03-18 13:37:27 +03:00
|
|
|
privateNets: p.PrivateNets,
|
2021-04-15 19:00:31 +03:00
|
|
|
localDomainSuffix: localDomainSuffix,
|
2021-05-27 19:19:19 +03:00
|
|
|
recDetector: newRecursionDetector(recursionTTL, cachedRecurrentReqNum),
|
2021-06-29 15:53:28 +03:00
|
|
|
clientIDCache: cache.New(cache.Config{
|
|
|
|
EnableLRU: true,
|
|
|
|
MaxCount: defaultClientIDCacheCount,
|
|
|
|
}),
|
2021-12-06 17:26:43 +03:00
|
|
|
anonymizer: p.Anonymizer,
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
2020-06-23 12:13:13 +03:00
|
|
|
|
2021-06-01 14:28:34 +03:00
|
|
|
// TODO(e.burkov): Enable the refresher after the actual implementation
|
|
|
|
// passes the public testing.
|
2022-03-23 20:47:45 +03:00
|
|
|
s.sysResolvers, err = aghnet.NewSystemResolvers(nil)
|
2021-06-01 14:28:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("initializing system resolvers: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-24 20:06:53 +03:00
|
|
|
if p.DHCPServer != nil {
|
|
|
|
s.dhcpServer = p.DHCPServer
|
2020-06-23 12:13:13 +03:00
|
|
|
s.dhcpServer.SetOnLeaseChanged(s.onDHCPLeaseChanged)
|
|
|
|
s.onDHCPLeaseChanged(dhcpd.LeaseChangedAdded)
|
|
|
|
}
|
2019-10-31 12:32:14 +03:00
|
|
|
|
|
|
|
if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" {
|
|
|
|
// Use plain DNS on MIPS, encryption is too slow
|
2019-10-31 12:43:33 +03:00
|
|
|
defaultDNS = defaultBootstrap
|
2019-10-31 12:32:14 +03:00
|
|
|
}
|
2021-03-25 16:00:27 +03:00
|
|
|
|
|
|
|
return s, nil
|
2019-02-10 20:47:43 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
// Close gracefully closes the server. It is safe for concurrent use.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): A better approach would be making Stop method waiting for all
|
|
|
|
// its workers finished. But it would require the upstream.Upstream to have the
|
|
|
|
// Close method to prevent from hanging while waiting for unresponsive server to
|
|
|
|
// respond.
|
2019-09-16 15:54:41 +03:00
|
|
|
func (s *Server) Close() {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
|
|
|
|
2019-10-09 19:51:26 +03:00
|
|
|
s.dnsFilter = nil
|
2019-09-16 15:54:41 +03:00
|
|
|
s.stats = nil
|
|
|
|
s.queryLog = nil
|
2019-12-11 12:38:58 +03:00
|
|
|
s.dnsProxy = nil
|
2021-01-29 18:53:39 +03:00
|
|
|
|
2021-06-18 17:55:01 +03:00
|
|
|
if err := s.ipset.close(); err != nil {
|
2021-01-29 18:53:39 +03:00
|
|
|
log.Error("closing ipset: %s", err)
|
|
|
|
}
|
2019-09-16 15:54:41 +03:00
|
|
|
}
|
|
|
|
|
2019-10-30 11:52:58 +03:00
|
|
|
// WriteDiskConfig - write configuration
|
|
|
|
func (s *Server) WriteDiskConfig(c *FilteringConfig) {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2019-12-12 15:04:29 +03:00
|
|
|
sc := s.conf.FilteringConfig
|
|
|
|
*c = sc
|
2021-07-29 17:40:31 +03:00
|
|
|
c.RatelimitWhitelist = stringutil.CloneSlice(sc.RatelimitWhitelist)
|
|
|
|
c.BootstrapDNS = stringutil.CloneSlice(sc.BootstrapDNS)
|
|
|
|
c.AllowedClients = stringutil.CloneSlice(sc.AllowedClients)
|
|
|
|
c.DisallowedClients = stringutil.CloneSlice(sc.DisallowedClients)
|
|
|
|
c.BlockedHosts = stringutil.CloneSlice(sc.BlockedHosts)
|
|
|
|
c.TrustedProxies = stringutil.CloneSlice(sc.TrustedProxies)
|
|
|
|
c.UpstreamDNS = stringutil.CloneSlice(sc.UpstreamDNS)
|
2019-10-30 11:52:58 +03:00
|
|
|
}
|
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
// RDNSSettings returns the copy of actual RDNS configuration.
|
2021-05-26 17:55:19 +03:00
|
|
|
func (s *Server) RDNSSettings() (localPTRResolvers []string, resolveClients, resolvePTR bool) {
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
return stringutil.CloneSlice(s.conf.LocalPTRResolvers),
|
2021-05-26 17:55:19 +03:00
|
|
|
s.conf.ResolveClients,
|
|
|
|
s.conf.UsePrivateRDNS
|
2021-04-07 20:16:06 +03:00
|
|
|
}
|
|
|
|
|
2019-12-11 12:38:58 +03:00
|
|
|
// Resolve - get IP addresses by host name from an upstream server.
|
|
|
|
// No request/response filtering is performed.
|
|
|
|
// Query log and Stats are not updated.
|
|
|
|
// This method may be called before Start().
|
|
|
|
func (s *Server) Resolve(host string) ([]net.IPAddr, error) {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2019-12-11 12:38:58 +03:00
|
|
|
return s.internalProxy.LookupIPAddr(host)
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
// RDNSExchanger is a resolver for clients' addresses.
|
|
|
|
type RDNSExchanger interface {
|
|
|
|
// Exchange tries to resolve the ip in a suitable way, e.g. either as
|
|
|
|
// local or as external.
|
|
|
|
Exchange(ip net.IP) (host string, err error)
|
2021-05-26 17:55:19 +03:00
|
|
|
// ResolvesPrivatePTR returns true if the RDNSExchanger is able to
|
|
|
|
// resolve PTR requests for locally-served addresses.
|
|
|
|
ResolvesPrivatePTR() (ok bool)
|
2021-04-07 20:16:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// rDNSEmptyAnswerErr is returned by Exchange method when the answer
|
|
|
|
// section of respond is empty.
|
2021-05-24 17:28:11 +03:00
|
|
|
rDNSEmptyAnswerErr errors.Error = "the answer section is empty"
|
2021-04-07 20:16:06 +03:00
|
|
|
|
|
|
|
// rDNSNotPTRErr is returned by Exchange method when the response is not
|
|
|
|
// of PTR type.
|
2021-05-24 17:28:11 +03:00
|
|
|
rDNSNotPTRErr errors.Error = "the response is not a ptr"
|
2021-04-07 20:16:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Exchange implements the RDNSExchanger interface for *Server.
|
|
|
|
func (s *Server) Exchange(ip net.IP) (host string, err error) {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
2019-12-11 12:38:58 +03:00
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
if !s.conf.ResolveClients {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-08-09 16:03:37 +03:00
|
|
|
arpa, err := netutil.IPToReversedAddr(ip)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("reversing ip: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arpa = dns.Fqdn(arpa)
|
2021-04-07 20:16:06 +03:00
|
|
|
req := &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: dns.Id(),
|
|
|
|
RecursionDesired: true,
|
|
|
|
},
|
|
|
|
Compress: true,
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Name: arpa,
|
|
|
|
Qtype: dns.TypePTR,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}},
|
|
|
|
}
|
2021-04-09 21:01:21 +03:00
|
|
|
ctx := &proxy.DNSContext{
|
|
|
|
Proto: "udp",
|
|
|
|
Req: req,
|
|
|
|
StartTime: time.Now(),
|
|
|
|
}
|
2021-05-27 19:19:19 +03:00
|
|
|
|
2022-03-22 15:21:03 +03:00
|
|
|
var resolver *proxy.Proxy
|
2022-03-18 13:37:27 +03:00
|
|
|
if s.privateNets.Contains(ip) {
|
2021-05-31 13:25:40 +03:00
|
|
|
if !s.conf.UsePrivateRDNS {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resolver = s.localResolvers
|
|
|
|
s.recDetector.add(*req)
|
2022-03-22 15:21:03 +03:00
|
|
|
} else {
|
|
|
|
resolver = s.internalProxy
|
2021-05-31 13:25:40 +03:00
|
|
|
}
|
|
|
|
|
2021-05-27 19:19:19 +03:00
|
|
|
if err = resolver.Resolve(ctx); err != nil {
|
2021-04-07 20:16:06 +03:00
|
|
|
return "", err
|
2019-12-11 12:38:58 +03:00
|
|
|
}
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2021-05-27 19:19:19 +03:00
|
|
|
resp := ctx.Res
|
2021-04-07 20:16:06 +03:00
|
|
|
if len(resp.Answer) == 0 {
|
|
|
|
return "", fmt.Errorf("lookup for %q: %w", arpa, rDNSEmptyAnswerErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr, ok := resp.Answer[0].(*dns.PTR)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("type checking: %w", rDNSNotPTRErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSuffix(ptr.Ptr, "."), nil
|
2019-12-11 12:38:58 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
// ResolvesPrivatePTR implements the RDNSExchanger interface for *Server.
|
|
|
|
func (s *Server) ResolvesPrivatePTR() (ok bool) {
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
|
|
|
return s.conf.UsePrivateRDNS
|
|
|
|
}
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Start starts the DNS server.
|
2019-12-11 12:38:58 +03:00
|
|
|
func (s *Server) Start() error {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
return s.startLocked()
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// startLocked starts the DNS server without locking. For internal use only.
|
|
|
|
func (s *Server) startLocked() error {
|
2019-12-11 12:38:58 +03:00
|
|
|
err := s.dnsProxy.Start()
|
|
|
|
if err == nil {
|
|
|
|
s.isRunning = true
|
2019-11-21 16:13:19 +03:00
|
|
|
}
|
2019-12-11 12:38:58 +03:00
|
|
|
return err
|
2019-11-21 16:13:19 +03:00
|
|
|
}
|
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
// defaultLocalTimeout is the default timeout for resolving addresses from
|
|
|
|
// locally-served networks. It is assumed that local resolvers should work much
|
|
|
|
// faster than ordinary upstreams.
|
|
|
|
const defaultLocalTimeout = 1 * time.Second
|
|
|
|
|
2021-04-13 13:44:29 +03:00
|
|
|
// collectDNSIPAddrs returns IP addresses the server is listening on without
|
2021-09-13 16:00:36 +03:00
|
|
|
// port numbers. For internal use only.
|
2021-04-07 20:16:06 +03:00
|
|
|
func (s *Server) collectDNSIPAddrs() (addrs []string, err error) {
|
|
|
|
addrs = make([]string, len(s.conf.TCPListenAddrs)+len(s.conf.UDPListenAddrs))
|
|
|
|
var i int
|
|
|
|
var ip net.IP
|
|
|
|
for _, addr := range s.conf.TCPListenAddrs {
|
|
|
|
if addr == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip = addr.IP; ip.IsUnspecified() {
|
|
|
|
return aghnet.CollectAllIfacesAddrs()
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs[i] = ip.String()
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
for _, addr := range s.conf.UDPListenAddrs {
|
|
|
|
if addr == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip = addr.IP; ip.IsUnspecified() {
|
|
|
|
return aghnet.CollectAllIfacesAddrs()
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs[i] = ip.String()
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs[:i], nil
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:28:34 +03:00
|
|
|
func (s *Server) filterOurDNSAddrs(addrs []string) (filtered []string, err error) {
|
|
|
|
var ourAddrs []string
|
|
|
|
ourAddrs, err = s.collectDNSIPAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
ourAddrsSet := stringutil.NewSet(ourAddrs...)
|
2021-06-01 14:28:34 +03:00
|
|
|
|
|
|
|
// TODO(e.burkov): The approach of subtracting sets of strings is not
|
|
|
|
// really applicable here since in case of listening on all network
|
|
|
|
// interfaces we should check the whole interface's network to cut off
|
|
|
|
// all the loopback addresses as well.
|
2021-07-29 17:40:31 +03:00
|
|
|
return stringutil.FilterOut(addrs, ourAddrsSet.Has), nil
|
2021-06-01 14:28:34 +03:00
|
|
|
}
|
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
// setupResolvers initializes the resolvers for local addresses. For internal
|
|
|
|
// use only.
|
|
|
|
func (s *Server) setupResolvers(localAddrs []string) (err error) {
|
|
|
|
bootstraps := s.conf.BootstrapDNS
|
|
|
|
if len(localAddrs) == 0 {
|
2021-06-01 14:28:34 +03:00
|
|
|
localAddrs = s.sysResolvers.Get()
|
2021-04-07 20:16:06 +03:00
|
|
|
bootstraps = nil
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:28:34 +03:00
|
|
|
localAddrs, err = s.filterOurDNSAddrs(localAddrs)
|
2021-04-07 20:16:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:28:34 +03:00
|
|
|
log.Debug("upstreams to resolve PTR for local addresses: %v", localAddrs)
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
var upsConfig *proxy.UpstreamConfig
|
|
|
|
upsConfig, err = proxy.ParseUpstreamsConfig(
|
|
|
|
localAddrs,
|
|
|
|
&upstream.Options{
|
|
|
|
Bootstrap: bootstraps,
|
|
|
|
Timeout: defaultLocalTimeout,
|
2021-12-27 19:40:39 +03:00
|
|
|
// TODO(e.burkov): Should we verify server's certificates?
|
2021-06-29 15:53:28 +03:00
|
|
|
},
|
|
|
|
)
|
2021-04-09 21:01:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing upstreams: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.localResolvers = &proxy.Proxy{
|
|
|
|
Config: proxy.Config{
|
2021-06-29 15:53:28 +03:00
|
|
|
UpstreamConfig: upsConfig,
|
2021-04-09 21:01:21 +03:00
|
|
|
},
|
2021-04-07 20:16:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:09:54 +03:00
|
|
|
// Prepare initializes parameters of s using data from conf. conf must not be
|
|
|
|
// nil.
|
|
|
|
func (s *Server) Prepare(conf *ServerConfig) (err error) {
|
|
|
|
s.conf = *conf
|
|
|
|
|
|
|
|
err = validateBlockingMode(s.conf.BlockingMode, s.conf.BlockingIPv4, s.conf.BlockingIPv6)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("checking blocking mode: %w", err)
|
2019-10-31 12:32:14 +03:00
|
|
|
}
|
2019-10-30 11:52:58 +03:00
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
s.initDefaultSettings()
|
2019-10-31 12:32:14 +03:00
|
|
|
|
2022-09-09 14:51:10 +03:00
|
|
|
err = s.prepareIpsetListSettings()
|
2021-01-29 18:53:39 +03:00
|
|
|
if err != nil {
|
2022-09-06 17:09:54 +03:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
2022-09-12 16:11:32 +03:00
|
|
|
return fmt.Errorf("preparing ipset settings: %w", err)
|
2021-01-29 18:53:39 +03:00
|
|
|
}
|
2020-09-02 14:13:45 +03:00
|
|
|
|
2021-01-29 18:53:39 +03:00
|
|
|
err = s.prepareUpstreamSettings()
|
2019-10-31 12:32:14 +03:00
|
|
|
if err != nil {
|
2022-09-06 17:09:54 +03:00
|
|
|
return fmt.Errorf("preparing upstream settings: %w", err)
|
2019-10-31 12:32:14 +03:00
|
|
|
}
|
2018-12-05 14:03:41 +03:00
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
var proxyConfig proxy.Config
|
|
|
|
proxyConfig, err = s.createProxyConfig()
|
|
|
|
if err != nil {
|
2022-09-06 17:09:54 +03:00
|
|
|
return fmt.Errorf("preparing proxy: %w", err)
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
2018-11-28 15:40:56 +03:00
|
|
|
|
2022-09-06 17:09:54 +03:00
|
|
|
err = s.prepareInternalProxy()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("preparing internal proxy: %w", err)
|
|
|
|
}
|
2019-12-11 12:38:58 +03:00
|
|
|
|
2022-09-06 17:09:54 +03:00
|
|
|
s.access, err = newAccessCtx(
|
|
|
|
s.conf.AllowedClients,
|
|
|
|
s.conf.DisallowedClients,
|
|
|
|
s.conf.BlockedHosts,
|
|
|
|
)
|
2019-05-24 14:49:26 +03:00
|
|
|
if err != nil {
|
2022-09-06 17:09:54 +03:00
|
|
|
return fmt.Errorf("preparing access: %w", err)
|
2019-05-24 14:49:26 +03:00
|
|
|
}
|
|
|
|
|
2020-01-16 14:25:40 +03:00
|
|
|
if !webRegistered && s.conf.HTTPRegister != nil {
|
|
|
|
webRegistered = true
|
2019-10-30 11:52:58 +03:00
|
|
|
s.registerHandlers()
|
|
|
|
}
|
|
|
|
|
2018-12-24 15:19:52 +03:00
|
|
|
s.dnsProxy = &proxy.Proxy{Config: proxyConfig}
|
2021-04-07 20:16:06 +03:00
|
|
|
|
|
|
|
err = s.setupResolvers(s.conf.LocalPTRResolvers)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting up resolvers: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-27 19:19:19 +03:00
|
|
|
s.recDetector.clear()
|
|
|
|
|
2019-11-21 16:13:19 +03:00
|
|
|
return nil
|
2018-12-24 15:27:14 +03:00
|
|
|
}
|
2018-12-24 15:19:52 +03:00
|
|
|
|
2022-09-06 17:09:54 +03:00
|
|
|
// validateBlockingMode returns an error if the blocking mode data aren't valid.
|
|
|
|
func validateBlockingMode(mode BlockingMode, blockingIPv4, blockingIPv6 net.IP) (err error) {
|
|
|
|
switch mode {
|
|
|
|
case
|
|
|
|
BlockingModeDefault,
|
|
|
|
BlockingModeNXDOMAIN,
|
|
|
|
BlockingModeREFUSED,
|
|
|
|
BlockingModeNullIP:
|
|
|
|
return nil
|
|
|
|
case BlockingModeCustomIP:
|
|
|
|
if blockingIPv4 == nil {
|
|
|
|
return fmt.Errorf("blocking_ipv4 must be set when blocking_mode is custom_ip")
|
|
|
|
} else if blockingIPv6 == nil {
|
|
|
|
return fmt.Errorf("blocking_ipv6 must be set when blocking_mode is custom_ip")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("bad blocking mode %q", mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareInternalProxy initializes the DNS proxy that is used for internal DNS
|
2022-10-19 16:13:05 +03:00
|
|
|
// queries, such as public clients PTR resolving and updater hostname resolving.
|
2022-09-06 17:09:54 +03:00
|
|
|
func (s *Server) prepareInternalProxy() (err error) {
|
|
|
|
conf := &proxy.Config{
|
|
|
|
CacheEnabled: true,
|
|
|
|
CacheSizeBytes: 4096,
|
|
|
|
UpstreamConfig: s.conf.UpstreamConfig,
|
|
|
|
MaxGoroutines: int(s.conf.MaxGoroutines),
|
|
|
|
}
|
|
|
|
|
|
|
|
srvConf := s.conf
|
|
|
|
setProxyUpstreamMode(
|
|
|
|
conf,
|
|
|
|
srvConf.AllServers,
|
|
|
|
srvConf.FastestAddr,
|
|
|
|
srvConf.FastestTimeout.Duration,
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(a.garipov): Make a proper constructor for proxy.Proxy.
|
|
|
|
p := &proxy.Proxy{
|
|
|
|
Config: *conf,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.internalProxy = p
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Stop stops the DNS server.
|
2018-11-28 15:40:56 +03:00
|
|
|
func (s *Server) Stop() error {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
return s.stopLocked()
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
|
|
|
|
2022-10-19 16:13:05 +03:00
|
|
|
// stopLocked stops the DNS server without locking. For internal use only.
|
|
|
|
func (s *Server) stopLocked() (err error) {
|
2018-12-24 15:19:52 +03:00
|
|
|
if s.dnsProxy != nil {
|
2022-10-19 16:13:05 +03:00
|
|
|
err = s.dnsProxy.Stop()
|
|
|
|
if err != nil {
|
2022-10-20 18:22:37 +03:00
|
|
|
return fmt.Errorf("closing primary resolvers: %w", err)
|
2022-10-19 16:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 18:22:37 +03:00
|
|
|
var errs []error
|
|
|
|
|
|
|
|
if upsConf := s.internalProxy.UpstreamConfig; upsConf != nil {
|
|
|
|
const action = "closing internal resolvers"
|
|
|
|
|
|
|
|
err = upsConf.Close()
|
2022-10-19 16:13:05 +03:00
|
|
|
if err != nil {
|
2022-10-20 18:22:37 +03:00
|
|
|
if errors.Is(err, net.ErrClosed) {
|
|
|
|
log.Debug("dnsforward: %s: %s", action, err)
|
|
|
|
} else {
|
|
|
|
errs = append(errs, fmt.Errorf("%s: %w", action, err))
|
|
|
|
}
|
2022-10-19 16:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 18:22:37 +03:00
|
|
|
if upsConf := s.localResolvers.UpstreamConfig; upsConf != nil {
|
|
|
|
const action = "closing local resolvers"
|
|
|
|
|
|
|
|
err = upsConf.Close()
|
2018-11-28 15:40:56 +03:00
|
|
|
if err != nil {
|
2022-10-20 18:22:37 +03:00
|
|
|
if errors.Is(err, net.ErrClosed) {
|
|
|
|
log.Debug("dnsforward: %s: %s", action, err)
|
|
|
|
} else {
|
|
|
|
errs = append(errs, fmt.Errorf("%s: %w", action, err))
|
|
|
|
}
|
2018-11-28 15:40:56 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-05 14:21:48 +03:00
|
|
|
|
2022-10-19 16:13:05 +03:00
|
|
|
if len(errs) > 0 {
|
2022-10-20 18:22:37 +03:00
|
|
|
return errors.List("stopping dns server", errs...)
|
2022-10-19 16:13:05 +03:00
|
|
|
} else {
|
|
|
|
s.isRunning = false
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:54:38 +03:00
|
|
|
return nil
|
2018-11-28 15:40:56 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
// IsRunning returns true if the DNS server is running.
|
2018-11-28 16:43:50 +03:00
|
|
|
func (s *Server) IsRunning() bool {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2019-12-11 12:38:58 +03:00
|
|
|
return s.isRunning
|
2018-11-28 16:43:50 +03:00
|
|
|
}
|
|
|
|
|
2021-12-27 19:40:39 +03:00
|
|
|
// srvClosedErr is returned when the method can't complete without inaccessible
|
2021-10-06 13:14:41 +03:00
|
|
|
// data from the closing server.
|
|
|
|
const srvClosedErr errors.Error = "server is closed"
|
|
|
|
|
|
|
|
// proxy returns a pointer to the current DNS proxy instance. If p is nil, the
|
|
|
|
// server is closing.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3655.
|
|
|
|
func (s *Server) proxy() (p *proxy.Proxy) {
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
|
|
|
return s.dnsProxy
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
// Reconfigure applies the new configuration to the DNS server.
|
2022-09-06 17:09:54 +03:00
|
|
|
func (s *Server) Reconfigure(conf *ServerConfig) error {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
2018-11-28 15:40:56 +03:00
|
|
|
|
2018-12-24 15:19:52 +03:00
|
|
|
log.Print("Start reconfiguring the server")
|
2021-02-02 15:13:12 +03:00
|
|
|
err := s.stopLocked()
|
2018-12-24 15:19:52 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("could not reconfigure the server: %w", err)
|
2018-11-28 15:40:56 +03:00
|
|
|
}
|
2019-10-21 15:58:14 +03:00
|
|
|
|
2019-12-11 17:54:34 +03:00
|
|
|
// It seems that net.Listener.Close() doesn't close file descriptors right away.
|
|
|
|
// We wait for some time and hope that this fd will be closed.
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2019-10-21 15:58:14 +03:00
|
|
|
|
2022-09-06 17:09:54 +03:00
|
|
|
// TODO(a.garipov): This whole piece of API is weird and needs to be remade.
|
|
|
|
if conf == nil {
|
|
|
|
conf = &s.conf
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Prepare(conf)
|
2019-12-11 12:38:58 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("could not reconfigure the server: %w", err)
|
2019-12-11 12:38:58 +03:00
|
|
|
}
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
err = s.startLocked()
|
2018-12-24 15:19:52 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("could not reconfigure the server: %w", err)
|
2018-11-28 15:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
// ServeHTTP is a HTTP handler method we use to provide DNS-over-HTTPS.
|
2019-02-22 15:52:12 +03:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-10-06 13:14:41 +03:00
|
|
|
if prx := s.proxy(); prx != nil {
|
|
|
|
prx.ServeHTTP(w, r)
|
2019-12-12 15:00:10 +03:00
|
|
|
}
|
2019-02-22 15:52:12 +03:00
|
|
|
}
|
2020-07-24 14:30:29 +03:00
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
// IsBlockedClient returns true if the client is blocked by the current access
|
|
|
|
// settings.
|
|
|
|
func (s *Server) IsBlockedClient(ip net.IP, clientID string) (blocked bool, rule string) {
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2022-10-24 16:29:44 +03:00
|
|
|
blockedByIP := false
|
|
|
|
if ip != nil {
|
|
|
|
// TODO(a.garipov): Remove once we switch to netip.Addr more fully.
|
|
|
|
ipAddr, err := netutil.IPToAddrNoMapped(ip)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("dnsforward: bad client ip %v: %s", ip, err)
|
|
|
|
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
blockedByIP, rule = s.access.isBlockedIP(ipAddr)
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
allowlistMode := s.access.allowlistMode()
|
|
|
|
blockedByClientID := s.access.isBlockedClientID(clientID)
|
|
|
|
|
2022-10-24 16:29:44 +03:00
|
|
|
// Allow if at least one of the checks allows in allowlist mode, but block
|
|
|
|
// if at least one of the checks blocks in blocklist mode.
|
2021-06-29 15:53:28 +03:00
|
|
|
if allowlistMode && blockedByIP && blockedByClientID {
|
2022-10-24 16:29:44 +03:00
|
|
|
log.Debug("client %v (id %q) is not in access allowlist", ip, clientID)
|
2021-06-29 15:53:28 +03:00
|
|
|
|
|
|
|
// Return now without substituting the empty rule for the
|
|
|
|
// clientID because the rule can't be empty here.
|
|
|
|
return true, rule
|
|
|
|
} else if !allowlistMode && (blockedByIP || blockedByClientID) {
|
2022-10-24 16:29:44 +03:00
|
|
|
log.Debug("client %v (id %q) is in access blocklist", ip, clientID)
|
2021-06-29 15:53:28 +03:00
|
|
|
|
|
|
|
blocked = true
|
|
|
|
}
|
|
|
|
|
2022-10-24 17:49:52 +03:00
|
|
|
return blocked, aghalg.Coalesce(rule, clientID)
|
2020-07-24 14:30:29 +03:00
|
|
|
}
|