2020-11-03 05:39:55 -07:00
|
|
|
// Package dnsforward contains a DNS forwarding server.
|
2018-11-28 05:40:56 -07:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2019-10-30 01:52:58 -07:00
|
|
|
"fmt"
|
2018-11-28 05:40:56 -07:00
|
|
|
"net"
|
2019-02-22 05:52:12 -07:00
|
|
|
"net/http"
|
2021-02-01 07:06:09 -07:00
|
|
|
"os"
|
2019-10-21 05:58:14 -07:00
|
|
|
"runtime"
|
2021-04-07 10:16:06 -07:00
|
|
|
"strings"
|
2018-11-28 05:40:56 -07:00
|
|
|
"sync"
|
2018-12-05 04:03:41 -07:00
|
|
|
"time"
|
2018-11-28 05:40:56 -07:00
|
|
|
|
2021-03-31 05:00:47 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-04-07 10:16:06 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghstrings"
|
2020-10-30 03:32:02 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
2021-05-21 06:15:47 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-10-30 03:32:02 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
2018-12-24 06:58:48 -07:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-04-09 11:01:21 -07:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2021-05-24 07:28:11 -07:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-25 06:44:22 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2018-11-28 05:40:56 -07:00
|
|
|
"github.com/miekg/dns"
|
2018-12-24 05:19:52 -07:00
|
|
|
)
|
|
|
|
|
2018-12-24 15:59:38 -07:00
|
|
|
// DefaultTimeout is the default upstream timeout
|
|
|
|
const DefaultTimeout = 10 * time.Second
|
|
|
|
|
2018-12-24 05:19:52 -07:00
|
|
|
const (
|
|
|
|
safeBrowsingBlockHost = "standard-block.dns.adguard.com"
|
|
|
|
parentalBlockHost = "family-block.dns.adguard.com"
|
2018-11-28 05:40:56 -07:00
|
|
|
)
|
|
|
|
|
2019-10-31 02:32:14 -07:00
|
|
|
var defaultDNS = []string{
|
2020-03-05 03:12:21 -07:00
|
|
|
"https://dns10.quad9.net/dns-query",
|
2019-10-31 02:32:14 -07:00
|
|
|
}
|
2020-03-05 03:12:21 -07:00
|
|
|
var defaultBootstrap = []string{"9.9.9.10", "149.112.112.10", "2620:fe::10", "2620:fe::fe:10"}
|
2019-10-31 02:32:14 -07:00
|
|
|
|
2020-09-10 02:32:36 -07:00
|
|
|
// Often requested by all kinds of DNS probes
|
|
|
|
var defaultBlockedHosts = []string{"version.bind", "id.server", "hostname.bind"}
|
|
|
|
|
2020-01-16 04:25:40 -07:00
|
|
|
var webRegistered bool
|
|
|
|
|
2021-04-15 07:52:53 -07:00
|
|
|
// hostToIPTable is an alias for the type of Server.tableHostToIP.
|
|
|
|
type hostToIPTable = map[string]net.IP
|
|
|
|
|
|
|
|
// ipToHostTable is an alias for the type of Server.tableIPToHost.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Define an IPMap type in aghnet and use here and in other
|
|
|
|
// places?
|
|
|
|
type ipToHostTable = map[string]string
|
|
|
|
|
2018-12-05 02:52:23 -07:00
|
|
|
// Server is the main way to start a DNS server.
|
|
|
|
//
|
2018-11-28 05:40:56 -07:00
|
|
|
// Example:
|
2018-12-05 02:52:23 -07: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 05:40:56 -07:00
|
|
|
//
|
|
|
|
// The zero Server is empty and ready for use.
|
|
|
|
type Server struct {
|
2020-07-03 08:20:01 -07:00
|
|
|
dnsProxy *proxy.Proxy // DNS proxy instance
|
2021-05-21 06:15:47 -07:00
|
|
|
dnsFilter *filtering.DNSFilter // DNS filter instance
|
2020-07-03 08:20:01 -07:00
|
|
|
dhcpServer dhcpd.ServerInterface // DHCP server instance (optional)
|
|
|
|
queryLog querylog.QueryLog // Query log instance
|
2020-06-23 02:13:13 -07:00
|
|
|
stats stats.Stats
|
|
|
|
access *accessCtx
|
|
|
|
|
2021-04-15 09:00:31 -07: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 06:00:27 -07:00
|
|
|
|
2021-03-31 05:00:47 -07:00
|
|
|
ipset ipsetCtx
|
|
|
|
subnetDetector *aghnet.SubnetDetector
|
2021-04-09 11:01:21 -07:00
|
|
|
localResolvers *proxy.Proxy
|
2021-05-27 09:19:19 -07:00
|
|
|
recDetector *recursionDetector
|
2020-09-02 04:13:45 -07:00
|
|
|
|
2021-04-15 07:52:53 -07:00
|
|
|
tableHostToIP hostToIPTable
|
2020-08-18 02:36:52 -07:00
|
|
|
tableHostToIPLock sync.Mutex
|
|
|
|
|
2021-04-15 07:52:53 -07:00
|
|
|
tableIPToHost ipToHostTable
|
|
|
|
tableIPToHostLock sync.Mutex
|
2019-05-24 04:49:26 -07:00
|
|
|
|
2019-12-11 02:38:58 -07: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 04:25:40 -07:00
|
|
|
isRunning bool
|
2019-10-30 01:52:58 -07:00
|
|
|
|
2019-05-15 05:08:15 -07:00
|
|
|
conf ServerConfig
|
2021-05-26 07:55:19 -07:00
|
|
|
// serverLock protects Server.
|
|
|
|
serverLock sync.RWMutex
|
2018-11-28 05:40:56 -07:00
|
|
|
}
|
|
|
|
|
2021-04-15 09:00:31 -07:00
|
|
|
// defaultLocalDomainSuffix is the default suffix used to detect internal hosts
|
|
|
|
// when no suffix is provided.
|
|
|
|
//
|
|
|
|
// See the documentation for Server.localDomainSuffix.
|
|
|
|
const defaultLocalDomainSuffix = ".lan."
|
2021-03-25 06:00:27 -07:00
|
|
|
|
|
|
|
// DNSCreateParams are parameters to create a new server.
|
2020-06-23 02:13:13 -07:00
|
|
|
type DNSCreateParams struct {
|
2021-05-21 06:15:47 -07:00
|
|
|
DNSFilter *filtering.DNSFilter
|
2021-03-31 05:00:47 -07:00
|
|
|
Stats stats.Stats
|
|
|
|
QueryLog querylog.QueryLog
|
|
|
|
DHCPServer dhcpd.ServerInterface
|
|
|
|
SubnetDetector *aghnet.SubnetDetector
|
2021-04-15 09:00:31 -07:00
|
|
|
LocalDomain string
|
2021-03-25 06:00:27 -07:00
|
|
|
}
|
|
|
|
|
2021-04-15 09:00:31 -07:00
|
|
|
// domainNameToSuffix converts a domain name into a local domain suffix.
|
|
|
|
func domainNameToSuffix(tld string) (suffix string) {
|
2021-03-25 06:00:27 -07:00
|
|
|
l := len(tld) + 2
|
|
|
|
b := make([]byte, l)
|
|
|
|
b[0] = '.'
|
|
|
|
copy(b[1:], tld)
|
|
|
|
b[l-1] = '.'
|
|
|
|
|
|
|
|
return string(b)
|
2020-06-23 02:13:13 -07:00
|
|
|
}
|
|
|
|
|
2021-05-27 09:19:19 -07:00
|
|
|
const (
|
|
|
|
// recursionTTL is the time recursive request is cached for.
|
|
|
|
recursionTTL = 5 * time.Second
|
|
|
|
// cachedRecurrentReqNum is the maximum number of cached recurrent
|
|
|
|
// requests.
|
|
|
|
cachedRecurrentReqNum = 1000
|
|
|
|
)
|
|
|
|
|
2019-02-10 10:47:43 -07:00
|
|
|
// NewServer creates a new instance of the dnsforward.Server
|
2019-07-10 01:56:54 -07:00
|
|
|
// Note: this function must be called only once
|
2021-03-25 06:00:27 -07:00
|
|
|
func NewServer(p DNSCreateParams) (s *Server, err error) {
|
2021-04-15 09:00:31 -07:00
|
|
|
var localDomainSuffix string
|
|
|
|
if p.LocalDomain == "" {
|
|
|
|
localDomainSuffix = defaultLocalDomainSuffix
|
2021-03-25 06:00:27 -07:00
|
|
|
} else {
|
2021-04-15 09:00:31 -07:00
|
|
|
err = aghnet.ValidateDomainName(p.LocalDomain)
|
2021-03-25 06:00:27 -07:00
|
|
|
if err != nil {
|
2021-04-15 09:00:31 -07:00
|
|
|
return nil, fmt.Errorf("local domain: %w", err)
|
2021-03-25 06:00:27 -07:00
|
|
|
}
|
|
|
|
|
2021-04-15 09:00:31 -07:00
|
|
|
localDomainSuffix = domainNameToSuffix(p.LocalDomain)
|
2021-03-25 06:00:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
s = &Server{
|
2021-04-15 09:00:31 -07:00
|
|
|
dnsFilter: p.DNSFilter,
|
|
|
|
stats: p.Stats,
|
|
|
|
queryLog: p.QueryLog,
|
|
|
|
subnetDetector: p.SubnetDetector,
|
|
|
|
localDomainSuffix: localDomainSuffix,
|
2021-05-27 09:19:19 -07:00
|
|
|
recDetector: newRecursionDetector(recursionTTL, cachedRecurrentReqNum),
|
2021-02-04 10:35:13 -07:00
|
|
|
}
|
2020-06-23 02:13:13 -07:00
|
|
|
|
2020-08-24 10:06:53 -07:00
|
|
|
if p.DHCPServer != nil {
|
|
|
|
s.dhcpServer = p.DHCPServer
|
2020-06-23 02:13:13 -07:00
|
|
|
s.dhcpServer.SetOnLeaseChanged(s.onDHCPLeaseChanged)
|
|
|
|
s.onDHCPLeaseChanged(dhcpd.LeaseChangedAdded)
|
|
|
|
}
|
2019-10-31 02:32:14 -07:00
|
|
|
|
|
|
|
if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" {
|
|
|
|
// Use plain DNS on MIPS, encryption is too slow
|
2019-10-31 02:43:33 -07:00
|
|
|
defaultDNS = defaultBootstrap
|
2019-10-31 02:32:14 -07:00
|
|
|
}
|
2021-03-25 06:00:27 -07:00
|
|
|
|
|
|
|
return s, nil
|
2019-02-10 10:47:43 -07:00
|
|
|
}
|
|
|
|
|
2021-02-04 10:35:13 -07:00
|
|
|
// NewCustomServer creates a new instance of *Server with custom internal proxy.
|
|
|
|
func NewCustomServer(internalProxy *proxy.Proxy) *Server {
|
2021-05-27 09:19:19 -07:00
|
|
|
s := &Server{
|
|
|
|
recDetector: newRecursionDetector(0, 1),
|
|
|
|
}
|
2021-02-04 10:35:13 -07:00
|
|
|
if internalProxy != nil {
|
|
|
|
s.internalProxy = internalProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-05-26 07:55:19 -07: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 05:54:41 -07:00
|
|
|
func (s *Server) Close() {
|
2021-05-26 07:55:19 -07:00
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
|
|
|
|
2019-10-09 09:51:26 -07:00
|
|
|
s.dnsFilter = nil
|
2019-09-16 05:54:41 -07:00
|
|
|
s.stats = nil
|
|
|
|
s.queryLog = nil
|
2019-12-11 02:38:58 -07:00
|
|
|
s.dnsProxy = nil
|
2021-01-29 08:53:39 -07:00
|
|
|
|
2021-05-26 07:55:19 -07:00
|
|
|
if err := s.ipset.Close(); err != nil {
|
2021-01-29 08:53:39 -07:00
|
|
|
log.Error("closing ipset: %s", err)
|
|
|
|
}
|
2019-09-16 05:54:41 -07:00
|
|
|
}
|
|
|
|
|
2019-10-30 01:52:58 -07:00
|
|
|
// WriteDiskConfig - write configuration
|
|
|
|
func (s *Server) WriteDiskConfig(c *FilteringConfig) {
|
2021-05-26 07:55:19 -07:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2019-12-12 05:04:29 -07:00
|
|
|
sc := s.conf.FilteringConfig
|
|
|
|
*c = sc
|
2021-04-07 10:16:06 -07:00
|
|
|
c.RatelimitWhitelist = aghstrings.CloneSlice(sc.RatelimitWhitelist)
|
|
|
|
c.BootstrapDNS = aghstrings.CloneSlice(sc.BootstrapDNS)
|
|
|
|
c.AllowedClients = aghstrings.CloneSlice(sc.AllowedClients)
|
|
|
|
c.DisallowedClients = aghstrings.CloneSlice(sc.DisallowedClients)
|
|
|
|
c.BlockedHosts = aghstrings.CloneSlice(sc.BlockedHosts)
|
|
|
|
c.UpstreamDNS = aghstrings.CloneSlice(sc.UpstreamDNS)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
|
2021-04-07 10:16:06 -07:00
|
|
|
// RDNSSettings returns the copy of actual RDNS configuration.
|
2021-05-26 07:55:19 -07:00
|
|
|
func (s *Server) RDNSSettings() (localPTRResolvers []string, resolveClients, resolvePTR bool) {
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
2021-04-07 10:16:06 -07:00
|
|
|
|
2021-05-26 07:55:19 -07:00
|
|
|
return aghstrings.CloneSlice(s.conf.LocalPTRResolvers),
|
|
|
|
s.conf.ResolveClients,
|
|
|
|
s.conf.UsePrivateRDNS
|
2021-04-07 10:16:06 -07:00
|
|
|
}
|
|
|
|
|
2019-12-11 02:38:58 -07: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 07:55:19 -07:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2019-12-11 02:38:58 -07:00
|
|
|
return s.internalProxy.LookupIPAddr(host)
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:16:06 -07: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 07:55:19 -07:00
|
|
|
// ResolvesPrivatePTR returns true if the RDNSExchanger is able to
|
|
|
|
// resolve PTR requests for locally-served addresses.
|
|
|
|
ResolvesPrivatePTR() (ok bool)
|
2021-04-07 10:16:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// rDNSEmptyAnswerErr is returned by Exchange method when the answer
|
|
|
|
// section of respond is empty.
|
2021-05-24 07:28:11 -07:00
|
|
|
rDNSEmptyAnswerErr errors.Error = "the answer section is empty"
|
2021-04-07 10:16:06 -07:00
|
|
|
|
|
|
|
// rDNSNotPTRErr is returned by Exchange method when the response is not
|
|
|
|
// of PTR type.
|
2021-05-24 07:28:11 -07:00
|
|
|
rDNSNotPTRErr errors.Error = "the response is not a ptr"
|
2021-04-07 10:16:06 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Exchange implements the RDNSExchanger interface for *Server.
|
|
|
|
func (s *Server) Exchange(ip net.IP) (host string, err error) {
|
2021-05-26 07:55:19 -07:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
2019-12-11 02:38:58 -07:00
|
|
|
|
2021-04-07 10:16:06 -07:00
|
|
|
if !s.conf.ResolveClients {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 07:55:19 -07:00
|
|
|
var resolver *proxy.Proxy = s.localResolvers
|
|
|
|
if !s.subnetDetector.IsLocallyServedNetwork(ip) {
|
|
|
|
resolver = s.internalProxy
|
|
|
|
} else if !s.conf.UsePrivateRDNS {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:16:06 -07:00
|
|
|
arpa := dns.Fqdn(aghnet.ReverseAddr(ip))
|
|
|
|
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 11:01:21 -07:00
|
|
|
ctx := &proxy.DNSContext{
|
|
|
|
Proto: "udp",
|
|
|
|
Req: req,
|
|
|
|
StartTime: time.Now(),
|
|
|
|
}
|
2021-05-27 09:19:19 -07:00
|
|
|
|
|
|
|
s.recDetector.add(*req)
|
|
|
|
if err = resolver.Resolve(ctx); err != nil {
|
2021-04-07 10:16:06 -07:00
|
|
|
return "", err
|
2019-12-11 02:38:58 -07:00
|
|
|
}
|
2021-04-07 10:16:06 -07:00
|
|
|
|
2021-05-27 09:19:19 -07:00
|
|
|
resp := ctx.Res
|
2021-04-07 10:16:06 -07: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 02:38:58 -07:00
|
|
|
}
|
|
|
|
|
2021-05-26 07:55:19 -07: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 05:13:12 -07:00
|
|
|
// Start starts the DNS server.
|
2019-12-11 02:38:58 -07:00
|
|
|
func (s *Server) Start() error {
|
2021-05-26 07:55:19 -07:00
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
|
|
|
|
2021-02-02 05:13:12 -07:00
|
|
|
return s.startLocked()
|
2018-12-24 05:19:52 -07:00
|
|
|
}
|
|
|
|
|
2021-02-02 05:13:12 -07:00
|
|
|
// startLocked starts the DNS server without locking. For internal use only.
|
|
|
|
func (s *Server) startLocked() error {
|
2019-12-11 02:38:58 -07:00
|
|
|
err := s.dnsProxy.Start()
|
|
|
|
if err == nil {
|
|
|
|
s.isRunning = true
|
2019-11-21 06:13:19 -07:00
|
|
|
}
|
2019-12-11 02:38:58 -07:00
|
|
|
return err
|
2019-11-21 06:13:19 -07:00
|
|
|
}
|
|
|
|
|
2021-04-07 10:16:06 -07: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 03:44:29 -07:00
|
|
|
// collectDNSIPAddrs returns IP addresses the server is listening on without
|
2021-05-26 07:55:19 -07:00
|
|
|
// port numbersю For internal use only.
|
2021-04-07 10:16:06 -07: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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
var sysRes aghnet.SystemResolvers
|
|
|
|
// TODO(e.burkov): Enable the refresher after the actual
|
|
|
|
// implementation passes the public testing.
|
|
|
|
sysRes, err = aghnet.NewSystemResolvers(0, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
localAddrs = sysRes.Get()
|
|
|
|
bootstraps = nil
|
|
|
|
}
|
|
|
|
log.Debug("upstreams to resolve PTR for local addresses: %v", localAddrs)
|
|
|
|
|
|
|
|
var ourAddrs []string
|
|
|
|
ourAddrs, err = s.collectDNSIPAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 06:26:19 -07:00
|
|
|
ourAddrsSet := aghstrings.NewSet(ourAddrs...)
|
2021-04-13 03:44:29 -07:00
|
|
|
|
2021-04-09 11:01:21 -07: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-05-12 10:04:50 -07:00
|
|
|
localAddrs = aghstrings.FilterOut(localAddrs, ourAddrsSet.Has)
|
2021-04-07 10:16:06 -07:00
|
|
|
|
2021-04-09 11:01:21 -07:00
|
|
|
var upsConfig proxy.UpstreamConfig
|
|
|
|
upsConfig, err = proxy.ParseUpstreamsConfig(localAddrs, upstream.Options{
|
|
|
|
Bootstrap: bootstraps,
|
|
|
|
Timeout: defaultLocalTimeout,
|
|
|
|
// TODO(e.burkov): Should we verify server's ceritificates?
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing upstreams: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.localResolvers = &proxy.Proxy{
|
|
|
|
Config: proxy.Config{
|
|
|
|
UpstreamConfig: &upsConfig,
|
|
|
|
},
|
2021-04-07 10:16:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 06:13:19 -07:00
|
|
|
// Prepare the object
|
2019-12-11 02:38:58 -07:00
|
|
|
func (s *Server) Prepare(config *ServerConfig) error {
|
2020-09-02 04:13:45 -07:00
|
|
|
// Initialize the server configuration
|
2020-05-08 08:39:37 -07:00
|
|
|
// --
|
2019-10-09 09:51:26 -07:00
|
|
|
if config != nil {
|
|
|
|
s.conf = *config
|
2019-12-16 07:04:30 -07:00
|
|
|
if s.conf.BlockingMode == "custom_ip" {
|
2021-01-13 06:56:05 -07:00
|
|
|
if s.conf.BlockingIPv4 == nil || s.conf.BlockingIPv6 == nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("dns: invalid custom blocking IP address specified")
|
2019-12-16 07:04:30 -07:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 02:32:14 -07:00
|
|
|
}
|
2019-10-30 01:52:58 -07:00
|
|
|
|
2020-09-02 04:13:45 -07:00
|
|
|
// Set default values in the case if nothing is configured
|
2020-05-08 08:39:37 -07:00
|
|
|
// --
|
|
|
|
s.initDefaultSettings()
|
2019-10-31 02:32:14 -07:00
|
|
|
|
2020-09-02 04:13:45 -07:00
|
|
|
// Initialize IPSET configuration
|
|
|
|
// --
|
2021-01-29 08:53:39 -07:00
|
|
|
err := s.ipset.init(s.conf.IPSETList)
|
|
|
|
if err != nil {
|
2021-02-01 10:44:15 -07:00
|
|
|
if !errors.Is(err, os.ErrInvalid) && !errors.Is(err, os.ErrPermission) {
|
2021-02-01 07:06:09 -07:00
|
|
|
return fmt.Errorf("cannot initialize ipset: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ipset cannot currently be initialized if the server was
|
|
|
|
// installed from Snap or when the user or the binary doesn't
|
2021-02-01 10:44:15 -07:00
|
|
|
// have the required permissions, or when the kernel doesn't
|
|
|
|
// support netfilter.
|
2021-02-01 07:06:09 -07:00
|
|
|
//
|
|
|
|
// Log and go on.
|
2021-02-01 10:44:15 -07:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): The Snap problem can probably be solved if
|
2021-02-02 03:35:23 -07:00
|
|
|
// we add the netlink-connector interface plug.
|
2021-04-26 06:41:50 -07:00
|
|
|
log.Info("warning: cannot initialize ipset: %s", err)
|
2021-01-29 08:53:39 -07:00
|
|
|
}
|
2020-09-02 04:13:45 -07:00
|
|
|
|
|
|
|
// Prepare DNS servers settings
|
2020-05-08 08:39:37 -07:00
|
|
|
// --
|
2021-01-29 08:53:39 -07:00
|
|
|
err = s.prepareUpstreamSettings()
|
2019-10-31 02:32:14 -07:00
|
|
|
if err != nil {
|
2020-05-08 08:39:37 -07:00
|
|
|
return err
|
2019-10-31 02:32:14 -07:00
|
|
|
}
|
2018-12-05 04:03:41 -07:00
|
|
|
|
2020-09-02 04:13:45 -07:00
|
|
|
// Create DNS proxy configuration
|
2020-05-08 08:39:37 -07:00
|
|
|
// --
|
|
|
|
var proxyConfig proxy.Config
|
|
|
|
proxyConfig, err = s.createProxyConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-12-24 05:19:52 -07:00
|
|
|
}
|
2018-11-28 05:40:56 -07:00
|
|
|
|
2020-09-02 04:13:45 -07:00
|
|
|
// Prepare a DNS proxy instance that we use for internal DNS queries
|
2020-05-08 08:39:37 -07:00
|
|
|
// --
|
|
|
|
s.prepareIntlProxy()
|
2019-12-11 02:38:58 -07:00
|
|
|
|
2021-04-20 06:26:19 -07:00
|
|
|
s.access, err = newAccessCtx(s.conf.AllowedClients, s.conf.DisallowedClients, s.conf.BlockedHosts)
|
2019-05-24 04:49:26 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-02 04:13:45 -07:00
|
|
|
// Register web handlers if necessary
|
2020-05-08 08:39:37 -07:00
|
|
|
// --
|
2020-01-16 04:25:40 -07:00
|
|
|
if !webRegistered && s.conf.HTTPRegister != nil {
|
|
|
|
webRegistered = true
|
2019-10-30 01:52:58 -07:00
|
|
|
s.registerHandlers()
|
|
|
|
}
|
|
|
|
|
2020-09-02 04:13:45 -07:00
|
|
|
// Create the main DNS proxy instance
|
2020-05-08 08:39:37 -07:00
|
|
|
// --
|
2018-12-24 05:19:52 -07:00
|
|
|
s.dnsProxy = &proxy.Proxy{Config: proxyConfig}
|
2021-04-07 10:16:06 -07:00
|
|
|
|
|
|
|
err = s.setupResolvers(s.conf.LocalPTRResolvers)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting up resolvers: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-27 09:19:19 -07:00
|
|
|
s.recDetector.clear()
|
|
|
|
|
2019-11-21 06:13:19 -07:00
|
|
|
return nil
|
2018-12-24 05:27:14 -07:00
|
|
|
}
|
2018-12-24 05:19:52 -07:00
|
|
|
|
2021-02-02 05:13:12 -07:00
|
|
|
// Stop stops the DNS server.
|
2018-11-28 05:40:56 -07:00
|
|
|
func (s *Server) Stop() error {
|
2021-05-26 07:55:19 -07:00
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
|
|
|
|
2021-02-02 05:13:12 -07:00
|
|
|
return s.stopLocked()
|
2018-12-24 05:19:52 -07:00
|
|
|
}
|
|
|
|
|
2021-02-02 05:13:12 -07:00
|
|
|
// stopLocked stops the DNS server without locking. For internal use only.
|
|
|
|
func (s *Server) stopLocked() error {
|
2018-12-24 05:19:52 -07:00
|
|
|
if s.dnsProxy != nil {
|
|
|
|
err := s.dnsProxy.Stop()
|
2018-11-28 05:40:56 -07:00
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("could not stop the DNS server properly: %w", err)
|
2018-11-28 05:40:56 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-05 04:21:48 -07:00
|
|
|
|
2019-12-11 02:38:58 -07:00
|
|
|
s.isRunning = false
|
2019-08-26 01:54:38 -07:00
|
|
|
return nil
|
2018-11-28 05:40:56 -07:00
|
|
|
}
|
|
|
|
|
2021-05-26 07:55:19 -07:00
|
|
|
// IsRunning returns true if the DNS server is running.
|
2018-11-28 06:43:50 -07:00
|
|
|
func (s *Server) IsRunning() bool {
|
2021-05-26 07:55:19 -07:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2019-12-11 02:38:58 -07:00
|
|
|
return s.isRunning
|
2018-11-28 06:43:50 -07:00
|
|
|
}
|
|
|
|
|
2021-05-26 07:55:19 -07:00
|
|
|
// Reconfigure applies the new configuration to the DNS server.
|
2018-12-24 05:19:52 -07:00
|
|
|
func (s *Server) Reconfigure(config *ServerConfig) error {
|
2021-05-26 07:55:19 -07:00
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
2018-11-28 05:40:56 -07:00
|
|
|
|
2018-12-24 05:19:52 -07:00
|
|
|
log.Print("Start reconfiguring the server")
|
2021-02-02 05:13:12 -07:00
|
|
|
err := s.stopLocked()
|
2018-12-24 05:19:52 -07:00
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("could not reconfigure the server: %w", err)
|
2018-11-28 05:40:56 -07:00
|
|
|
}
|
2019-10-21 05:58:14 -07:00
|
|
|
|
2019-12-11 07:54:34 -07: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 05:58:14 -07:00
|
|
|
|
2019-12-11 02:38:58 -07:00
|
|
|
err = s.Prepare(config)
|
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("could not reconfigure the server: %w", err)
|
2019-12-11 02:38:58 -07:00
|
|
|
}
|
|
|
|
|
2021-02-02 05:13:12 -07:00
|
|
|
err = s.startLocked()
|
2018-12-24 05:19:52 -07:00
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("could not reconfigure the server: %w", err)
|
2018-11-28 05:40:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 07:55:19 -07:00
|
|
|
// ServeHTTP is a HTTP handler method we use to provide DNS-over-HTTPS.
|
2019-02-22 05:52:12 -07:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-05-26 07:55:19 -07:00
|
|
|
var p *proxy.Proxy
|
|
|
|
|
|
|
|
func() {
|
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
|
|
|
p = s.dnsProxy
|
|
|
|
}()
|
|
|
|
|
|
|
|
if p != nil {
|
2019-12-12 05:00:10 -07:00
|
|
|
p.ServeHTTP(w, r)
|
|
|
|
}
|
2019-02-22 05:52:12 -07:00
|
|
|
}
|
2020-07-24 04:30:29 -07:00
|
|
|
|
|
|
|
// IsBlockedIP - return TRUE if this client should be blocked
|
2021-01-20 07:27:53 -07:00
|
|
|
func (s *Server) IsBlockedIP(ip net.IP) (bool, string) {
|
2021-04-02 07:30:39 -07:00
|
|
|
if ip == nil {
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
2020-07-24 04:30:29 -07:00
|
|
|
return s.access.IsBlockedIP(ip)
|
|
|
|
}
|