mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-15 19:31:45 +03:00
ff7c715c5f
Closes #6854.Updates #6875. Squashed commit of the following: commit b98adbc0cc6eeaffb262d57775c487e03b1d5ba5 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Apr 10 19:21:44 2024 +0300 dnsforward: upd proxy, imp code, docs commit 4de1eb2bca1047426e02ba680c212f46782e5616 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Apr 10 16:09:58 2024 +0300 WIP commit afa9d61e8dc129f907dc681cd2f831cb5c3b054a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 19:24:09 2024 +0300 all: log changes commit c8340676a448687a39acd26bc8ce5f94473e441f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 19:06:10 2024 +0300 dnsforward: move code commit 08bb7d43d2a3f689ef2ef2409935dc3946752e94 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 18:09:46 2024 +0300 dnsforward: imp code commit b27547ec806dd9bce502d3c6a7c28f33693ed575 Merge: b7efca7886f36ebc06
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 17:33:19 2024 +0300 Merge branch 'master' into AGDNS-1982-upd-proxy commit b7efca788b66aa672598b088040d4534ce2e55b0 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 9 17:27:14 2024 +0300 all: upd proxy finally commit 3e16fa87befe4c0ef3a3e7a638d7add28627f9b6 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Apr 5 18:20:13 2024 +0300 dnsforward: upd proxy commit f3cdfc86334a182effcd0de22fac5e678fa53ea7 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Apr 4 20:37:32 2024 +0300 all: upd proxy, golibs commit a79298d6d0504521893ee11fdc3a23c098aea911 Merge: 9feeba5c7fd25dcacb
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Apr 4 20:34:01 2024 +0300 Merge branch 'master' into AGDNS-1982-upd-proxy commit 9feeba5c7f24ff1d308a216608d985cb2a7b7588 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Apr 4 20:25:57 2024 +0300 all: imp code, docs commit 6c68d463db64293eb9c5e29ff91879fd68920a77 Merge: d8108e651ee619b2db
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Apr 4 18:46:11 2024 +0300 Merge branch 'master' into AGDNS-1982-upd-proxy commit d8108e65164df8d67aa4e95154a8768a06255b78 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Apr 3 19:25:27 2024 +0300 all: imp code commit 20461565801c9fcd06a652c6066b524b06c80433 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Apr 3 17:10:33 2024 +0300 all: remove private rdns logic
174 lines
5.1 KiB
Go
174 lines
5.1 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
|
)
|
|
|
|
// newBootstrap returns a bootstrap resolver based on the configuration of s.
|
|
// boots are the upstream resolvers that should be closed after use. r is the
|
|
// actual bootstrap resolver, which may include the system hosts.
|
|
//
|
|
// TODO(e.burkov): This function currently returns a resolver and a slice of
|
|
// the upstream resolvers, which are essentially the same. boots are returned
|
|
// for being able to close them afterwards, but it introduces an implicit
|
|
// contract that r could only be used before that. Anyway, this code should
|
|
// improve when the [proxy.UpstreamConfig] will become an [upstream.Resolver]
|
|
// and be used here.
|
|
func newBootstrap(
|
|
addrs []string,
|
|
etcHosts upstream.Resolver,
|
|
opts *upstream.Options,
|
|
) (r upstream.Resolver, boots []*upstream.UpstreamResolver, err error) {
|
|
if len(addrs) == 0 {
|
|
addrs = defaultBootstrap
|
|
}
|
|
|
|
boots, err = aghnet.ParseBootstraps(addrs, opts)
|
|
if err != nil {
|
|
// Don't wrap the error, since it's informative enough as is.
|
|
return nil, nil, err
|
|
}
|
|
|
|
var parallel upstream.ParallelResolver
|
|
for _, b := range boots {
|
|
parallel = append(parallel, upstream.NewCachingResolver(b))
|
|
}
|
|
|
|
if etcHosts != nil {
|
|
r = upstream.ConsequentResolver{etcHosts, parallel}
|
|
} else {
|
|
r = parallel
|
|
}
|
|
|
|
return r, boots, nil
|
|
}
|
|
|
|
// newUpstreamConfig returns the upstream configuration based on upstreams. If
|
|
// upstreams slice specifies no default upstreams, defaultUpstreams are used to
|
|
// create upstreams with no domain specifications. opts are used when creating
|
|
// upstream configuration.
|
|
func newUpstreamConfig(
|
|
upstreams []string,
|
|
defaultUpstreams []string,
|
|
opts *upstream.Options,
|
|
) (uc *proxy.UpstreamConfig, err error) {
|
|
uc, err = proxy.ParseUpstreamsConfig(upstreams, opts)
|
|
if err != nil {
|
|
return uc, fmt.Errorf("parsing upstreams: %w", err)
|
|
}
|
|
|
|
if len(uc.Upstreams) == 0 && len(defaultUpstreams) > 0 {
|
|
log.Info("dnsforward: warning: no default upstreams specified, using %v", defaultUpstreams)
|
|
|
|
var defaultUpstreamConfig *proxy.UpstreamConfig
|
|
defaultUpstreamConfig, err = proxy.ParseUpstreamsConfig(defaultUpstreams, opts)
|
|
if err != nil {
|
|
return uc, fmt.Errorf("parsing default upstreams: %w", err)
|
|
}
|
|
|
|
uc.Upstreams = defaultUpstreamConfig.Upstreams
|
|
}
|
|
|
|
return uc, nil
|
|
}
|
|
|
|
// newPrivateConfig creates an upstream configuration for resolving PTR records
|
|
// for local addresses. The configuration is built either from the provided
|
|
// addresses or from the system resolvers. unwanted filters the resulting
|
|
// upstream configuration.
|
|
func newPrivateConfig(
|
|
addrs []string,
|
|
unwanted addrPortSet,
|
|
sysResolvers SystemResolvers,
|
|
privateNets netutil.SubnetSet,
|
|
opts *upstream.Options,
|
|
) (uc *proxy.UpstreamConfig, err error) {
|
|
confNeedsFiltering := len(addrs) > 0
|
|
if confNeedsFiltering {
|
|
addrs = stringutil.FilterOut(addrs, IsCommentOrEmpty)
|
|
} else {
|
|
sysResolvers := slices.DeleteFunc(slices.Clone(sysResolvers.Addrs()), unwanted.Has)
|
|
addrs = make([]string, 0, len(sysResolvers))
|
|
for _, r := range sysResolvers {
|
|
addrs = append(addrs, r.String())
|
|
}
|
|
}
|
|
|
|
log.Debug("dnsforward: upstreams to resolve ptr for local addresses: %v", addrs)
|
|
|
|
uc, err = proxy.ParseUpstreamsConfig(addrs, opts)
|
|
if err != nil {
|
|
return uc, fmt.Errorf("preparing private upstreams: %w", err)
|
|
}
|
|
|
|
if !confNeedsFiltering {
|
|
return uc, nil
|
|
}
|
|
|
|
err = filterOutAddrs(uc, unwanted)
|
|
if err != nil {
|
|
return uc, fmt.Errorf("filtering private upstreams: %w", err)
|
|
}
|
|
|
|
// Prevalidate the config to catch the exact error before creating proxy.
|
|
// See TODO on [PrivateRDNSError].
|
|
err = proxy.ValidatePrivateConfig(uc, privateNets)
|
|
if err != nil {
|
|
return uc, &PrivateRDNSError{err: err}
|
|
}
|
|
|
|
return uc, nil
|
|
}
|
|
|
|
// UpstreamHTTPVersions returns the HTTP versions for upstream configuration
|
|
// depending on configuration.
|
|
func UpstreamHTTPVersions(http3 bool) (v []upstream.HTTPVersion) {
|
|
if !http3 {
|
|
return upstream.DefaultHTTPVersions
|
|
}
|
|
|
|
return []upstream.HTTPVersion{
|
|
upstream.HTTPVersion3,
|
|
upstream.HTTPVersion2,
|
|
upstream.HTTPVersion11,
|
|
}
|
|
}
|
|
|
|
// setProxyUpstreamMode sets the upstream mode and related settings in conf
|
|
// based on provided parameters.
|
|
func setProxyUpstreamMode(
|
|
conf *proxy.Config,
|
|
upstreamMode UpstreamMode,
|
|
fastestTimeout time.Duration,
|
|
) (err error) {
|
|
switch upstreamMode {
|
|
case UpstreamModeParallel:
|
|
conf.UpstreamMode = proxy.UModeParallel
|
|
case UpstreamModeFastestAddr:
|
|
conf.UpstreamMode = proxy.UModeFastestAddr
|
|
conf.FastestPingTimeout = fastestTimeout
|
|
case UpstreamModeLoadBalance:
|
|
conf.UpstreamMode = proxy.UModeLoadBalance
|
|
default:
|
|
return fmt.Errorf("unexpected value %q", upstreamMode)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// IsCommentOrEmpty returns true if s starts with a "#" character or is empty.
|
|
// This function is useful for filtering out non-upstream lines from upstream
|
|
// configs.
|
|
func IsCommentOrEmpty(s string) (ok bool) {
|
|
return len(s) == 0 || s[0] == '#'
|
|
}
|