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 (
|
2024-05-15 13:34:12 +03:00
|
|
|
"cmp"
|
2023-12-07 17:23:00 +03:00
|
|
|
"context"
|
2019-10-30 11:52:58 +03:00
|
|
|
"fmt"
|
2023-12-07 17:23:00 +03:00
|
|
|
"io"
|
2018-11-28 15:40:56 +03:00
|
|
|
"net"
|
2019-02-22 15:52:12 +03:00
|
|
|
"net/http"
|
2022-11-02 16:18:02 +03:00
|
|
|
"net/netip"
|
2019-10-21 15:58:14 +03:00
|
|
|
"runtime"
|
2024-03-12 17:45:11 +03:00
|
|
|
"slices"
|
2021-04-07 20:16:06 +03:00
|
|
|
"strings"
|
2018-11-28 15:40:56 +03:00
|
|
|
"sync"
|
2023-04-12 14:48:42 +03:00
|
|
|
"sync/atomic"
|
2018-12-05 14:03:41 +03:00
|
|
|
"time"
|
2018-11-28 15:40:56 +03:00
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
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-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
2023-07-12 15:13:31 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/rdns"
|
2020-10-30 13:32:02 +03:00
|
|
|
"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"
|
2023-10-11 17:31:41 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil/sysresolv"
|
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
|
|
|
)
|
|
|
|
|
2018-12-25 01:59:38 +03:00
|
|
|
// DefaultTimeout is the default upstream timeout
|
|
|
|
const DefaultTimeout = 10 * time.Second
|
|
|
|
|
2023-12-07 17:23:00 +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
|
|
|
|
|
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"}
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
var (
|
|
|
|
// defaultUDPListenAddrs are the default UDP addresses for the server.
|
|
|
|
defaultUDPListenAddrs = []*net.UDPAddr{{Port: 53}}
|
2020-01-16 14:25:40 +03:00
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
// defaultTCPListenAddrs are the default TCP addresses for the server.
|
|
|
|
defaultTCPListenAddrs = []*net.TCPAddr{{Port: 53}}
|
|
|
|
)
|
2022-11-02 16:18:02 +03:00
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
var webRegistered bool
|
2021-04-15 17:52:53 +03:00
|
|
|
|
2023-07-03 14:10:40 +03:00
|
|
|
// DHCP is an interface for accessing DHCP lease data needed in this package.
|
|
|
|
type DHCP interface {
|
|
|
|
// HostByIP returns the hostname of the DHCP client with the given IP
|
|
|
|
// address. The address will be netip.Addr{} if there is no such client,
|
|
|
|
// due to an assumption that a DHCP client must always have an IP address.
|
|
|
|
HostByIP(ip netip.Addr) (host string)
|
|
|
|
|
|
|
|
// IPByHost returns the IP address of the DHCP client with the given
|
|
|
|
// hostname. The hostname will be an empty string if there is no such
|
|
|
|
// client, due to an assumption that a DHCP client must always have a
|
|
|
|
// hostname, either set by the client or assigned automatically.
|
|
|
|
IPByHost(host string) (ip netip.Addr)
|
|
|
|
|
|
|
|
// Enabled returns true if DHCP provides information about clients.
|
|
|
|
Enabled() (ok bool)
|
|
|
|
}
|
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
// SystemResolvers is an interface for accessing the OS-provided resolvers.
|
2023-10-11 17:31:41 +03:00
|
|
|
type SystemResolvers interface {
|
2024-02-01 15:20:53 +03:00
|
|
|
// Addrs returns the list of system resolvers' addresses. Callers must
|
|
|
|
// clone the returned slice before modifying it. Implementations of Addrs
|
|
|
|
// must be safe for concurrent use.
|
2023-10-11 17:31:41 +03:00
|
|
|
Addrs() (addrs []netip.AddrPort)
|
|
|
|
}
|
|
|
|
|
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-09-07 18:03:18 +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 {
|
2023-09-07 17:13:48 +03:00
|
|
|
// dnsProxy is the DNS proxy for forwarding client's DNS requests.
|
|
|
|
dnsProxy *proxy.Proxy
|
|
|
|
|
|
|
|
// dnsFilter is the DNS filter for filtering client's DNS requests and
|
|
|
|
// responses.
|
|
|
|
dnsFilter *filtering.DNSFilter
|
|
|
|
|
|
|
|
// dhcpServer is the DHCP server for accessing lease data.
|
|
|
|
dhcpServer DHCP
|
|
|
|
|
|
|
|
// queryLog is the query log for client's DNS requests, responses and
|
|
|
|
// filtering results.
|
|
|
|
queryLog querylog.QueryLog
|
|
|
|
|
|
|
|
// stats is the statistics collector for client's DNS usage data.
|
|
|
|
stats stats.Interface
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// access drops disallowed clients.
|
2023-09-07 17:13:48 +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
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
// ipset processes DNS requests using ipset data.
|
|
|
|
ipset ipsetCtx
|
|
|
|
|
|
|
|
// privateNets is the configured set of IP networks considered private.
|
2023-07-26 13:18:44 +03:00
|
|
|
privateNets netutil.SubnetSet
|
|
|
|
|
|
|
|
// addrProc, if not nil, is used to process clients' IP addresses with rDNS,
|
|
|
|
// WHOIS, etc.
|
|
|
|
addrProc client.AddressProcessor
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
// sysResolvers used to fetch system resolvers to use by default for private
|
|
|
|
// PTR resolving.
|
2023-10-11 17:31:41 +03:00
|
|
|
sysResolvers SystemResolvers
|
2020-09-02 14:13:45 +03:00
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
// etcHosts contains the current data from the system's hosts files.
|
2023-12-07 17:23:00 +03:00
|
|
|
etcHosts upstream.Resolver
|
|
|
|
|
|
|
|
// bootstrap is the resolver for upstreams' hostnames.
|
|
|
|
bootstrap upstream.Resolver
|
|
|
|
|
|
|
|
// bootResolvers are the resolvers that should be used for
|
|
|
|
// bootstrapping along with [etcHosts].
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Use [proxy.UpstreamConfig] when it will implement the
|
|
|
|
// [upstream.Resolver] interface.
|
|
|
|
bootResolvers []*upstream.UpstreamResolver
|
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
// dns64Pref is the NAT64 prefix used for DNS64 response mapping. The major
|
|
|
|
// part of DNS64 happens inside the [proxy] package, but there still are
|
|
|
|
// some places where response mapping is needed (e.g. DHCP).
|
|
|
|
dns64Pref netip.Prefix
|
2023-02-01 15:41:34 +03:00
|
|
|
|
2021-12-06 17:26:43 +03:00
|
|
|
// anonymizer masks the client's IP addresses if needed.
|
|
|
|
anonymizer *aghnet.IPMut
|
|
|
|
|
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
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// internalProxy resolves internal requests from the application itself. It
|
|
|
|
// isn't started and so no listen ports are required.
|
2019-12-11 12:38:58 +03:00
|
|
|
internalProxy *proxy.Proxy
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
// isRunning is true if the DNS server is running.
|
2020-01-16 14:25:40 +03:00
|
|
|
isRunning bool
|
2019-10-30 11:52:58 +03:00
|
|
|
|
2023-04-12 14:48:42 +03:00
|
|
|
// protectionUpdateInProgress is used to make sure that only one goroutine
|
|
|
|
// updating the protection configuration after a pause is running at a time.
|
|
|
|
protectionUpdateInProgress atomic.Bool
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
// conf is the current configuration of the server.
|
2019-05-15 15:08:15 +03:00
|
|
|
conf ServerConfig
|
2023-09-07 17:13:48 +03:00
|
|
|
|
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-06-02 17:55:48 +03:00
|
|
|
DNSFilter *filtering.DNSFilter
|
2022-08-04 19:05:28 +03:00
|
|
|
Stats stats.Interface
|
2022-06-02 17:55:48 +03:00
|
|
|
QueryLog querylog.QueryLog
|
2023-09-07 17:13:48 +03:00
|
|
|
DHCPServer DHCP
|
2022-06-02 17:55:48 +03:00
|
|
|
PrivateNets netutil.SubnetSet
|
|
|
|
Anonymizer *aghnet.IPMut
|
2023-12-07 17:23:00 +03:00
|
|
|
EtcHosts *aghnet.HostsContainer
|
2022-06-02 17:55:48 +03:00
|
|
|
LocalDomain string
|
2021-03-25 16:00:27 +03:00
|
|
|
}
|
|
|
|
|
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
|
2023-07-26 13:18:44 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): How many constructors and initializers does this thing have?
|
|
|
|
// Refactor!
|
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)
|
|
|
|
}
|
2023-12-07 17:23:00 +03:00
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
var etcHosts upstream.Resolver
|
|
|
|
if p.EtcHosts != nil {
|
|
|
|
etcHosts = upstream.NewHostsResolver(p.EtcHosts)
|
|
|
|
}
|
|
|
|
|
2021-03-25 16:00:27 +03:00
|
|
|
s = &Server{
|
2023-09-07 17:13:48 +03:00
|
|
|
dnsFilter: p.DNSFilter,
|
2023-12-07 17:23:00 +03:00
|
|
|
dhcpServer: p.DHCPServer,
|
2023-09-07 17:13:48 +03:00
|
|
|
stats: p.Stats,
|
|
|
|
queryLog: p.QueryLog,
|
|
|
|
privateNets: p.PrivateNets,
|
|
|
|
// TODO(e.burkov): Use some case-insensitive string comparison.
|
|
|
|
localDomainSuffix: strings.ToLower(localDomainSuffix),
|
2024-01-30 18:43:51 +03:00
|
|
|
etcHosts: etcHosts,
|
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,
|
2023-12-07 17:23:00 +03:00
|
|
|
conf: ServerConfig{
|
|
|
|
ServePlainDNS: true,
|
|
|
|
},
|
|
|
|
}
|
2020-06-23 12:13:13 +03:00
|
|
|
|
2023-10-11 17:31:41 +03:00
|
|
|
s.sysResolvers, err = sysresolv.NewSystemResolvers(nil, defaultPlainDNSPort)
|
2021-06-01 14:28:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("initializing system resolvers: %w", err)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
// TODO(s.chzhen): Remove it.
|
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 {
|
2023-07-03 14:10:40 +03:00
|
|
|
log.Error("dnsforward: closing ipset: %s", err)
|
2021-01-29 18:53:39 +03:00
|
|
|
}
|
2019-09-16 15:54:41 +03:00
|
|
|
}
|
|
|
|
|
2019-10-30 11:52:58 +03:00
|
|
|
// WriteDiskConfig - write configuration
|
2023-09-07 17:13:48 +03:00
|
|
|
func (s *Server) WriteDiskConfig(c *Config) {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
sc := s.conf.Config
|
2019-12-12 15:04:29 +03:00
|
|
|
*c = sc
|
2023-12-07 17:23:00 +03:00
|
|
|
c.RatelimitWhitelist = slices.Clone(sc.RatelimitWhitelist)
|
2024-04-02 20:22:19 +03:00
|
|
|
c.BootstrapDNS = slices.Clone(sc.BootstrapDNS)
|
|
|
|
c.FallbackDNS = slices.Clone(sc.FallbackDNS)
|
|
|
|
c.AllowedClients = slices.Clone(sc.AllowedClients)
|
|
|
|
c.DisallowedClients = slices.Clone(sc.DisallowedClients)
|
|
|
|
c.BlockedHosts = slices.Clone(sc.BlockedHosts)
|
2024-01-30 18:43:51 +03:00
|
|
|
c.TrustedProxies = slices.Clone(sc.TrustedProxies)
|
2024-04-02 20:22:19 +03:00
|
|
|
c.UpstreamDNS = slices.Clone(sc.UpstreamDNS)
|
2019-10-30 11:52:58 +03:00
|
|
|
}
|
|
|
|
|
2023-07-26 13:18:44 +03:00
|
|
|
// LocalPTRResolvers returns the current local PTR resolver configuration.
|
|
|
|
func (s *Server) LocalPTRResolvers() (localPTRResolvers []string) {
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2024-04-02 20:22:19 +03:00
|
|
|
return slices.Clone(s.conf.LocalPTRResolvers)
|
2023-07-26 13:18:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddrProcConfig returns the current address processing configuration. Only
|
|
|
|
// fields c.UsePrivateRDNS, c.UseRDNS, and c.UseWHOIS are filled.
|
|
|
|
func (s *Server) AddrProcConfig() (c *client.DefaultAddrProcConfig) {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2023-07-26 13:18:44 +03:00
|
|
|
return &client.DefaultAddrProcConfig{
|
|
|
|
UsePrivateRDNS: s.conf.UsePrivateRDNS,
|
|
|
|
UseRDNS: s.conf.AddrProcConf.UseRDNS,
|
|
|
|
UseWHOIS: s.conf.AddrProcConf.UseWHOIS,
|
|
|
|
}
|
2021-04-07 20:16:06 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// Resolve gets 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 [Server.Start].
|
|
|
|
func (s *Server) Resolve(ctx context.Context, net, host string) (addr []netip.Addr, err error) {
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
return s.internalProxy.LookupNetIP(ctx, net, host)
|
2019-12-11 12:38:58 +03:00
|
|
|
}
|
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
const (
|
2023-02-15 16:53:29 +03:00
|
|
|
// ErrRDNSNoData is returned by [RDNSExchanger.Exchange] when the answer
|
|
|
|
// section of response is either NODATA or has no PTR records.
|
|
|
|
ErrRDNSNoData errors.Error = "no ptr data in response"
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
// ErrRDNSFailed is returned by [RDNSExchanger.Exchange] if the received
|
|
|
|
// response is not a NOERROR or NXDOMAIN.
|
|
|
|
ErrRDNSFailed errors.Error = "failed to resolve ptr"
|
2021-04-07 20:16:06 +03:00
|
|
|
)
|
|
|
|
|
2022-11-23 16:52:05 +03:00
|
|
|
// type check
|
2023-07-12 15:13:31 +03:00
|
|
|
var _ rdns.Exchanger = (*Server)(nil)
|
2022-11-23 16:52:05 +03:00
|
|
|
|
2023-07-12 15:13:31 +03:00
|
|
|
// Exchange implements the [rdns.Exchanger] interface for *Server.
|
2023-07-26 13:18:44 +03:00
|
|
|
func (s *Server) Exchange(ip netip.Addr) (host string, ttl time.Duration, 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
|
|
|
|
2024-05-15 13:34:12 +03:00
|
|
|
// TODO(e.burkov): Migrate to [netip.Addr] already.
|
2023-07-12 15:13:31 +03:00
|
|
|
arpa, err := netutil.IPToReversedAddr(ip.AsSlice())
|
2021-08-09 16:03:37 +03:00
|
|
|
if err != nil {
|
2023-07-26 13:18:44 +03:00
|
|
|
return "", 0, fmt.Errorf("reversing ip: %w", err)
|
2021-08-09 16:03:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}},
|
|
|
|
}
|
2023-07-26 13:18:44 +03:00
|
|
|
|
|
|
|
dctx := &proxy.DNSContext{
|
2024-05-15 13:34:12 +03:00
|
|
|
Proto: proxy.ProtoUDP,
|
|
|
|
Req: req,
|
|
|
|
IsPrivateClient: true,
|
2021-04-09 21:01:21 +03:00
|
|
|
}
|
2021-05-27 19:19:19 +03:00
|
|
|
|
2023-08-02 16:26:34 +03:00
|
|
|
var errMsg string
|
2024-01-30 18:43:51 +03:00
|
|
|
if s.privateNets.Contains(ip) {
|
2021-05-31 13:25:40 +03:00
|
|
|
if !s.conf.UsePrivateRDNS {
|
2023-07-26 13:18:44 +03:00
|
|
|
return "", 0, nil
|
2021-05-31 13:25:40 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:26:34 +03:00
|
|
|
errMsg = "resolving a private address: %w"
|
2024-05-15 13:34:12 +03:00
|
|
|
dctx.RequestedPrivateRDNS = netip.PrefixFrom(ip, ip.BitLen())
|
2022-03-22 15:21:03 +03:00
|
|
|
} else {
|
2023-08-02 16:26:34 +03:00
|
|
|
errMsg = "resolving an address: %w"
|
2021-05-31 13:25:40 +03:00
|
|
|
}
|
2024-05-15 13:34:12 +03:00
|
|
|
if err = s.internalProxy.Resolve(dctx); err != nil {
|
2023-08-02 16:26:34 +03:00
|
|
|
return "", 0, fmt.Errorf(errMsg, err)
|
2019-12-11 12:38:58 +03:00
|
|
|
}
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2023-07-26 13:18:44 +03:00
|
|
|
return hostFromPTR(dctx.Res)
|
2023-07-12 15:13:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// hostFromPTR returns domain name from the PTR response or error.
|
2023-07-26 13:18:44 +03:00
|
|
|
func hostFromPTR(resp *dns.Msg) (host string, ttl time.Duration, err error) {
|
2023-02-15 16:53:29 +03:00
|
|
|
// Distinguish between NODATA response and a failed request.
|
|
|
|
if resp.Rcode != dns.RcodeSuccess && resp.Rcode != dns.RcodeNameError {
|
2023-07-26 13:18:44 +03:00
|
|
|
return "", 0, fmt.Errorf(
|
2023-02-15 16:53:29 +03:00
|
|
|
"received %s response: %w",
|
|
|
|
dns.RcodeToString[resp.Rcode],
|
|
|
|
ErrRDNSFailed,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-26 13:18:44 +03:00
|
|
|
var ttlSec uint32
|
|
|
|
|
2023-08-02 16:26:34 +03:00
|
|
|
log.Debug("dnsforward: resolving ptr, received %d answers", len(resp.Answer))
|
2023-02-15 16:53:29 +03:00
|
|
|
for _, ans := range resp.Answer {
|
|
|
|
ptr, ok := ans.(*dns.PTR)
|
2023-07-26 13:18:44 +03:00
|
|
|
if !ok {
|
|
|
|
continue
|
2023-02-15 16:53:29 +03:00
|
|
|
}
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2023-08-02 16:26:34 +03:00
|
|
|
// Respect zero TTL records since some DNS servers use it to
|
|
|
|
// locally-resolved addresses.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/6046.
|
|
|
|
if ptr.Hdr.Ttl >= ttlSec {
|
2023-07-26 13:18:44 +03:00
|
|
|
host = ptr.Ptr
|
|
|
|
ttlSec = ptr.Hdr.Ttl
|
|
|
|
}
|
2023-07-12 15:13:31 +03:00
|
|
|
}
|
|
|
|
|
2023-07-26 13:18:44 +03:00
|
|
|
if host != "" {
|
|
|
|
// NOTE: Don't use [aghnet.NormalizeDomain] to retain original letter
|
|
|
|
// case.
|
|
|
|
host = strings.TrimSuffix(host, ".")
|
|
|
|
ttl = time.Duration(ttlSec) * time.Second
|
2023-07-12 15:13:31 +03:00
|
|
|
|
2023-07-26 13:18:44 +03:00
|
|
|
return host, ttl, nil
|
|
|
|
}
|
2021-05-26 17:55:19 +03:00
|
|
|
|
2023-07-26 13:18:44 +03:00
|
|
|
return "", 0, ErrRDNSNoData
|
2021-05-26 17:55:19 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// Start starts the DNS server. It must only be called after [Server.Prepare].
|
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
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// startLocked starts the DNS server without locking. s.serverLock is expected
|
|
|
|
// to be locked.
|
2021-02-02 15:13:12 +03:00
|
|
|
func (s *Server) startLocked() error {
|
2024-03-12 17:45:11 +03:00
|
|
|
// TODO(e.burkov): Use context properly.
|
|
|
|
err := s.dnsProxy.Start(context.Background())
|
2019-12-11 12:38:58 +03:00
|
|
|
if err == nil {
|
|
|
|
s.isRunning = true
|
2019-11-21 16:13:19 +03:00
|
|
|
}
|
2023-12-07 17:23:00 +03:00
|
|
|
|
2019-12-11 12:38:58 +03:00
|
|
|
return err
|
2019-11-21 16:13:19 +03:00
|
|
|
}
|
|
|
|
|
2022-09-07 18:03:18 +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
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
// dnsFilter can be nil during application update.
|
|
|
|
if s.dnsFilter != nil {
|
|
|
|
mode, bIPv4, bIPv6 := s.dnsFilter.BlockingMode()
|
|
|
|
err = validateBlockingMode(mode, bIPv4, bIPv6)
|
|
|
|
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
|
|
|
|
2024-05-15 13:34:12 +03:00
|
|
|
err = s.prepareInternalDNS()
|
2019-10-31 12:32:14 +03:00
|
|
|
if err != nil {
|
2023-07-03 14:10:40 +03:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
2019-10-31 12:32:14 +03:00
|
|
|
}
|
2018-12-05 14:03:41 +03:00
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
proxyConfig, err := s.newProxyConfig()
|
2020-05-08 18:39:37 +03:00
|
|
|
if err != nil {
|
2022-09-07 18:03:18 +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
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
s.setupDNS64()
|
|
|
|
|
2022-09-07 18:03:18 +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-07 18:03:18 +03:00
|
|
|
return fmt.Errorf("preparing access: %w", err)
|
2019-05-24 14:49:26 +03:00
|
|
|
}
|
|
|
|
|
2024-03-12 17:45:11 +03:00
|
|
|
proxyConfig.Fallbacks, err = s.setupFallbackDNS()
|
2023-09-07 17:13:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting up fallback dns servers: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-03-19 16:20:32 +03:00
|
|
|
dnsProxy, err := proxy.New(proxyConfig)
|
2024-03-12 17:45:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating proxy: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-03-19 16:20:32 +03:00
|
|
|
s.dnsProxy = dnsProxy
|
|
|
|
|
2023-07-26 13:18:44 +03:00
|
|
|
s.setupAddrProc()
|
2023-02-15 16:53:29 +03:00
|
|
|
|
2023-07-26 13:18:44 +03:00
|
|
|
s.registerHandlers()
|
2021-05-27 19:19:19 +03:00
|
|
|
|
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
|
|
|
|
2024-05-15 13:34:12 +03:00
|
|
|
// prepareUpstreamSettings sets upstream DNS server settings.
|
|
|
|
func (s *Server) prepareUpstreamSettings(boot upstream.Resolver) (err error) {
|
|
|
|
// Load upstreams either from the file, or from the settings
|
|
|
|
var upstreams []string
|
|
|
|
upstreams, err = s.conf.loadUpstreams()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading upstreams: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uc, err := newUpstreamConfig(upstreams, defaultDNS, &upstream.Options{
|
|
|
|
Bootstrap: boot,
|
|
|
|
Timeout: s.conf.UpstreamTimeout,
|
|
|
|
HTTPVersions: UpstreamHTTPVersions(s.conf.UseHTTP3Upstreams),
|
|
|
|
PreferIPv6: s.conf.BootstrapPreferIPv6,
|
|
|
|
// Use a customized set of RootCAs, because Go's 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].
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Investigate if that's true.
|
|
|
|
RootCAs: s.conf.TLSv12Roots,
|
|
|
|
CipherSuites: s.conf.TLSCiphers,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("preparing upstream config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.conf.UpstreamConfig = uc
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrivateRDNSError is returned when the private rDNS upstreams are
|
|
|
|
// invalid but enabled.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Consider allowing to use incomplete private rDNS upstreams
|
|
|
|
// configuration in proxy when the private rDNS function is enabled. In theory,
|
|
|
|
// proxy supports the case when no upstreams provided to resolve the private
|
|
|
|
// request, since it already supports this for DNS64-prefixed PTR requests.
|
|
|
|
type PrivateRDNSError struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements the [errors.Error] interface.
|
|
|
|
func (e *PrivateRDNSError) Error() (s string) {
|
|
|
|
return e.err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PrivateRDNSError) Unwrap() (err error) {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareLocalResolvers initializes the private RDNS upstream configuration
|
|
|
|
// according to the server's settings. It assumes s.serverLock is locked or the
|
|
|
|
// Server not running.
|
|
|
|
func (s *Server) prepareLocalResolvers() (uc *proxy.UpstreamConfig, err error) {
|
|
|
|
if !s.conf.UsePrivateRDNS {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ownAddrs addrPortSet
|
|
|
|
ownAddrs, err = s.conf.ourAddrsSet()
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &upstream.Options{
|
|
|
|
Bootstrap: s.bootstrap,
|
|
|
|
Timeout: defaultLocalTimeout,
|
|
|
|
// TODO(e.burkov): Should we verify server's certificates?
|
|
|
|
PreferIPv6: s.conf.BootstrapPreferIPv6,
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs := s.conf.LocalPTRResolvers
|
|
|
|
uc, err = newPrivateConfig(addrs, ownAddrs, s.sysResolvers, s.privateNets, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("preparing resolvers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uc, nil
|
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// prepareInternalDNS initializes the internal state of s before initializing
|
|
|
|
// the primary DNS proxy instance. It assumes s.serverLock is locked or the
|
|
|
|
// Server not running.
|
2024-05-15 13:34:12 +03:00
|
|
|
func (s *Server) prepareInternalDNS() (err error) {
|
2023-12-07 17:23:00 +03:00
|
|
|
err = s.prepareIpsetListSettings()
|
|
|
|
if err != nil {
|
2024-05-15 13:34:12 +03:00
|
|
|
return fmt.Errorf("preparing ipset settings: %w", err)
|
2023-12-07 17:23:00 +03:00
|
|
|
}
|
|
|
|
|
2024-05-15 13:34:12 +03:00
|
|
|
bootOpts := &upstream.Options{
|
2023-12-07 17:23:00 +03:00
|
|
|
Timeout: DefaultTimeout,
|
|
|
|
HTTPVersions: UpstreamHTTPVersions(s.conf.UseHTTP3Upstreams),
|
2024-05-15 13:34:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.bootstrap, s.bootResolvers, err = newBootstrap(s.conf.BootstrapDNS, s.etcHosts, bootOpts)
|
2023-12-07 17:23:00 +03:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
2024-05-15 13:34:12 +03:00
|
|
|
return err
|
2023-12-07 17:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.prepareUpstreamSettings(s.bootstrap)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
2024-05-15 13:34:12 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.conf.PrivateRDNSUpstreamConfig, err = s.prepareLocalResolvers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-12-07 17:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.prepareInternalProxy()
|
|
|
|
if err != nil {
|
2024-05-15 13:34:12 +03:00
|
|
|
return fmt.Errorf("preparing internal proxy: %w", err)
|
2023-12-07 17:23:00 +03:00
|
|
|
}
|
|
|
|
|
2024-05-15 13:34:12 +03:00
|
|
|
return nil
|
2023-12-07 17:23:00 +03:00
|
|
|
}
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
// setupFallbackDNS initializes the fallback DNS servers.
|
2024-03-12 17:45:11 +03:00
|
|
|
func (s *Server) setupFallbackDNS() (uc *proxy.UpstreamConfig, err error) {
|
2023-09-07 17:13:48 +03:00
|
|
|
fallbacks := s.conf.FallbackDNS
|
2023-09-11 17:51:50 +03:00
|
|
|
fallbacks = stringutil.FilterOut(fallbacks, IsCommentOrEmpty)
|
2023-09-07 17:13:48 +03:00
|
|
|
if len(fallbacks) == 0 {
|
2024-03-12 17:45:11 +03:00
|
|
|
return nil, nil
|
2023-09-07 17:13:48 +03:00
|
|
|
}
|
|
|
|
|
2024-03-12 17:45:11 +03:00
|
|
|
uc, err = proxy.ParseUpstreamsConfig(fallbacks, &upstream.Options{
|
2023-09-07 17:13:48 +03:00
|
|
|
// TODO(s.chzhen): Investigate if other options are needed.
|
|
|
|
Timeout: s.conf.UpstreamTimeout,
|
|
|
|
PreferIPv6: s.conf.BootstrapPreferIPv6,
|
2024-03-12 17:45:11 +03:00
|
|
|
// TODO(e.burkov): Use bootstrap.
|
2023-09-07 17:13:48 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// Do not wrap the error because it's informative enough as is.
|
2024-03-12 17:45:11 +03:00
|
|
|
return nil, err
|
2023-09-07 17:13:48 +03:00
|
|
|
}
|
|
|
|
|
2024-03-12 17:45:11 +03:00
|
|
|
return uc, nil
|
2023-09-07 17:13:48 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// setupAddrProc initializes the address processor. It assumes s.serverLock is
|
|
|
|
// locked or the Server not running.
|
2023-07-26 13:18:44 +03:00
|
|
|
func (s *Server) setupAddrProc() {
|
|
|
|
// TODO(a.garipov): This is a crutch for tests; remove.
|
|
|
|
if s.conf.AddrProcConf == nil {
|
|
|
|
s.conf.AddrProcConf = &client.DefaultAddrProcConfig{}
|
|
|
|
}
|
|
|
|
if s.conf.AddrProcConf.AddressUpdater == nil {
|
|
|
|
s.addrProc = client.EmptyAddrProc{}
|
|
|
|
} else {
|
|
|
|
c := s.conf.AddrProcConf
|
|
|
|
c.DialContext = s.DialContext
|
|
|
|
c.PrivateSubnets = s.privateNets
|
|
|
|
c.UsePrivateRDNS = s.conf.UsePrivateRDNS
|
|
|
|
s.addrProc = client.NewDefaultAddrProc(s.conf.AddrProcConf)
|
|
|
|
|
|
|
|
// Clear the initial addresses to not resolve them again.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Consider ways of removing this once more client
|
|
|
|
// logic is moved to package client.
|
|
|
|
c.InitialAddresses = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 18:03:18 +03:00
|
|
|
// validateBlockingMode returns an error if the blocking mode data aren't valid.
|
2023-09-07 17:13:48 +03:00
|
|
|
func validateBlockingMode(
|
|
|
|
mode filtering.BlockingMode,
|
|
|
|
blockingIPv4, blockingIPv6 netip.Addr,
|
|
|
|
) (err error) {
|
2022-09-07 18:03:18 +03:00
|
|
|
switch mode {
|
|
|
|
case
|
2023-09-07 17:13:48 +03:00
|
|
|
filtering.BlockingModeDefault,
|
|
|
|
filtering.BlockingModeNXDOMAIN,
|
|
|
|
filtering.BlockingModeREFUSED,
|
|
|
|
filtering.BlockingModeNullIP:
|
2022-09-07 18:03:18 +03:00
|
|
|
return nil
|
2023-09-07 17:13:48 +03:00
|
|
|
case filtering.BlockingModeCustomIP:
|
|
|
|
if !blockingIPv4.Is4() {
|
|
|
|
return fmt.Errorf("blocking_ipv4 must be valid ipv4 on custom_ip blocking_mode")
|
|
|
|
} else if !blockingIPv6.Is6() {
|
|
|
|
return fmt.Errorf("blocking_ipv6 must be valid ipv6 on custom_ip blocking_mode")
|
2022-09-07 18:03:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("bad blocking mode %q", mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareInternalProxy initializes the DNS proxy that is used for internal DNS
|
2022-11-02 16:18:02 +03:00
|
|
|
// queries, such as public clients PTR resolving and updater hostname resolving.
|
2022-09-07 18:03:18 +03:00
|
|
|
func (s *Server) prepareInternalProxy() (err error) {
|
2023-01-19 15:04:46 +03:00
|
|
|
srvConf := s.conf
|
2022-09-07 18:03:18 +03:00
|
|
|
conf := &proxy.Config{
|
2024-05-15 13:34:12 +03:00
|
|
|
CacheEnabled: true,
|
|
|
|
CacheSizeBytes: 4096,
|
|
|
|
PrivateRDNSUpstreamConfig: srvConf.PrivateRDNSUpstreamConfig,
|
|
|
|
UpstreamConfig: srvConf.UpstreamConfig,
|
|
|
|
MaxGoroutines: srvConf.MaxGoroutines,
|
|
|
|
UseDNS64: srvConf.UseDNS64,
|
|
|
|
DNS64Prefs: srvConf.DNS64Prefixes,
|
|
|
|
UsePrivateRDNS: srvConf.UsePrivateRDNS,
|
|
|
|
PrivateSubnets: s.privateNets,
|
|
|
|
MessageConstructor: s,
|
2022-09-07 18:03:18 +03:00
|
|
|
}
|
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
err = setProxyUpstreamMode(conf, srvConf.UpstreamMode, srvConf.FastestTimeout.Duration)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid upstream mode: %w", err)
|
|
|
|
}
|
2022-09-07 18:03:18 +03:00
|
|
|
|
2024-03-12 17:45:11 +03:00
|
|
|
s.internalProxy, err = proxy.New(conf)
|
2022-09-07 18:03:18 +03:00
|
|
|
|
2024-03-12 17:45:11 +03:00
|
|
|
return err
|
2022-09-07 18:03:18 +03:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2024-06-05 19:00:28 +03:00
|
|
|
s.stopLocked()
|
|
|
|
|
|
|
|
return nil
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// stopLocked stops the DNS server without locking. s.serverLock is expected to
|
|
|
|
// be locked.
|
2024-06-05 19:00:28 +03:00
|
|
|
func (s *Server) stopLocked() {
|
2022-12-15 17:50:08 +03:00
|
|
|
// TODO(e.burkov, a.garipov): Return critical errors, not just log them.
|
|
|
|
// This will require filtering all the non-critical errors in
|
|
|
|
// [upstream.Upstream] implementations.
|
|
|
|
|
2018-12-24 15:19:52 +03:00
|
|
|
if s.dnsProxy != nil {
|
2024-03-12 17:45:11 +03:00
|
|
|
// TODO(e.burkov): Use context properly.
|
2024-06-05 19:00:28 +03:00
|
|
|
err := s.dnsProxy.Shutdown(context.Background())
|
2022-11-02 16:18:02 +03:00
|
|
|
if err != nil {
|
2022-12-15 17:50:08 +03:00
|
|
|
log.Error("dnsforward: closing primary resolvers: %s", err)
|
2022-11-02 16:18:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
for _, b := range s.bootResolvers {
|
|
|
|
logCloserErr(b, "dnsforward: closing bootstrap %s: %s", b.Address())
|
2018-11-28 15:40:56 +03:00
|
|
|
}
|
2018-12-05 14:21:48 +03:00
|
|
|
|
2022-12-15 17:50:08 +03:00
|
|
|
s.isRunning = false
|
2018-11-28 15:40:56 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:23:00 +03:00
|
|
|
// logCloserErr logs the error returned by c, if any.
|
|
|
|
func logCloserErr(c io.Closer, format string, args ...any) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(format, append(args, err)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07 18:03:18 +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
|
|
|
|
2023-07-03 14:10:40 +03:00
|
|
|
log.Info("dnsforward: starting reconfiguring server")
|
|
|
|
defer log.Info("dnsforward: finished reconfiguring server")
|
|
|
|
|
2024-06-05 19:00:28 +03:00
|
|
|
s.stopLocked()
|
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-07 18:03:18 +03:00
|
|
|
// TODO(a.garipov): This whole piece of API is weird and needs to be remade.
|
|
|
|
if conf == nil {
|
|
|
|
conf = &s.conf
|
2023-07-26 13:18:44 +03:00
|
|
|
} else {
|
|
|
|
closeErr := s.addrProc.Close()
|
|
|
|
if closeErr != nil {
|
|
|
|
log.Error("dnsforward: closing address processor: %s", closeErr)
|
|
|
|
}
|
2022-09-07 18:03:18 +03:00
|
|
|
}
|
|
|
|
|
2024-03-19 16:20:32 +03:00
|
|
|
// TODO(e.burkov): It seems an error here brings the server down, which is
|
|
|
|
// not reliable enough.
|
2024-06-05 19:00:28 +03:00
|
|
|
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.
|
2022-11-23 16:52:05 +03:00
|
|
|
func (s *Server) IsBlockedClient(ip netip.Addr, clientID string) (blocked bool, rule string) {
|
2021-06-29 15:53:28 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
blockedByIP := false
|
2022-11-23 16:52:05 +03:00
|
|
|
if ip != (netip.Addr{}) {
|
|
|
|
blockedByIP, rule = s.access.isBlockedIP(ip)
|
2022-11-02 16:18:02 +03:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
allowlistMode := s.access.allowlistMode()
|
|
|
|
blockedByClientID := s.access.isBlockedClientID(clientID)
|
|
|
|
|
2022-11-02 16:18:02 +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 {
|
2023-07-03 14:10:40 +03:00
|
|
|
log.Debug("dnsforward: 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) {
|
2023-07-03 14:10:40 +03:00
|
|
|
log.Debug("dnsforward: client %v (id %q) is in access blocklist", ip, clientID)
|
2021-06-29 15:53:28 +03:00
|
|
|
|
|
|
|
blocked = true
|
|
|
|
}
|
|
|
|
|
2024-05-15 13:34:12 +03:00
|
|
|
return blocked, cmp.Or(rule, clientID)
|
2020-07-24 14:30:29 +03:00
|
|
|
}
|