2023-06-30 12:41:10 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-02-08 20:39:18 +03:00
|
|
|
"slices"
|
2023-06-30 12:41:10 +03:00
|
|
|
"time"
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2023-06-30 12:41:10 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2023-11-28 16:40:52 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2023-06-30 12:41:10 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2023-11-28 16:40:52 +03:00
|
|
|
)
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
// 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
|
2023-06-30 12:41:10 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
boots, err = aghnet.ParseBootstraps(addrs, opts)
|
2023-06-30 12:41:10 +03:00
|
|
|
if err != nil {
|
2024-04-11 14:03:37 +03:00
|
|
|
// Don't wrap the error, since it's informative enough as is.
|
|
|
|
return nil, nil, err
|
2023-06-30 12:41:10 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
var parallel upstream.ParallelResolver
|
|
|
|
for _, b := range boots {
|
|
|
|
parallel = append(parallel, upstream.NewCachingResolver(b))
|
2023-06-30 12:41:10 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
if etcHosts != nil {
|
|
|
|
r = upstream.ConsequentResolver{etcHosts, parallel}
|
|
|
|
} else {
|
|
|
|
r = parallel
|
2023-06-30 12:41:10 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
return r, boots, nil
|
2023-06-30 12:41:10 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
// 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(
|
2023-06-30 12:41:10 +03:00
|
|
|
upstreams []string,
|
|
|
|
defaultUpstreams []string,
|
|
|
|
opts *upstream.Options,
|
|
|
|
) (uc *proxy.UpstreamConfig, err error) {
|
|
|
|
uc, err = proxy.ParseUpstreamsConfig(upstreams, opts)
|
|
|
|
if err != nil {
|
2024-04-11 14:03:37 +03:00
|
|
|
return uc, fmt.Errorf("parsing upstreams: %w", err)
|
2023-06-30 12:41:10 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
if len(uc.Upstreams) == 0 && len(defaultUpstreams) > 0 {
|
2023-06-30 12:41:10 +03:00
|
|
|
log.Info("dnsforward: warning: no default upstreams specified, using %v", defaultUpstreams)
|
2024-04-11 14:03:37 +03:00
|
|
|
|
2023-06-30 12:41:10 +03:00
|
|
|
var defaultUpstreamConfig *proxy.UpstreamConfig
|
|
|
|
defaultUpstreamConfig, err = proxy.ParseUpstreamsConfig(defaultUpstreams, opts)
|
|
|
|
if err != nil {
|
2024-04-11 14:03:37 +03:00
|
|
|
return uc, fmt.Errorf("parsing default upstreams: %w", err)
|
2023-06-30 12:41:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uc.Upstreams = defaultUpstreamConfig.Upstreams
|
|
|
|
}
|
|
|
|
|
|
|
|
return uc, nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-06-30 12:41:10 +03:00
|
|
|
// 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,
|
2023-12-25 14:16:48 +03:00
|
|
|
upstreamMode UpstreamMode,
|
2023-06-30 12:41:10 +03:00
|
|
|
fastestTimeout time.Duration,
|
2023-12-25 14:16:48 +03:00
|
|
|
) (err error) {
|
|
|
|
switch upstreamMode {
|
|
|
|
case UpstreamModeParallel:
|
2023-06-30 12:41:10 +03:00
|
|
|
conf.UpstreamMode = proxy.UModeParallel
|
2023-12-25 14:16:48 +03:00
|
|
|
case UpstreamModeFastestAddr:
|
2023-06-30 12:41:10 +03:00
|
|
|
conf.UpstreamMode = proxy.UModeFastestAddr
|
|
|
|
conf.FastestPingTimeout = fastestTimeout
|
2023-12-25 14:16:48 +03:00
|
|
|
case UpstreamModeLoadBalance:
|
2023-06-30 12:41:10 +03:00
|
|
|
conf.UpstreamMode = proxy.UModeLoadBalance
|
2023-12-25 14:16:48 +03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected value %q", upstreamMode)
|
2023-06-30 12:41:10 +03:00
|
|
|
}
|
2023-12-25 14:16:48 +03:00
|
|
|
|
|
|
|
return nil
|
2023-06-30 12:41:10 +03:00
|
|
|
}
|
2023-11-16 14:05:10 +03:00
|
|
|
|
2023-11-28 16:40:52 +03:00
|
|
|
// 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] == '#'
|
|
|
|
}
|