mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 13:05:36 +03:00
c4a13b92d2
Merge in DNS/adguard-home from 3157-excessive-ptrs to master Updates #3157. Squashed commit of the following: commit 6803988240dca2f147bb80a5b3f78d7749d2fa14 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:50:01 2022 +0300 aghnet: and again commit 1a7f4d1dbc8fd4d3ae620349917526a75fa71b47 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:49:20 2022 +0300 aghnet: docs again commit d88da1fc7135f3cd03aff10b02d9957c8ffdfd30 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:47:36 2022 +0300 aghnet: imp docs commit c45dbc7800e882c6c4110aab640c32b03046f89a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:41:19 2022 +0300 aghnet: keep alphabetical order commit b61781785d096ef43f60fb4f1905a4ed3cdf7c68 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 13:50:56 2022 +0300 aghnet: imp code quality commit 578dbd71ed2f2089c69343d7d4bf8bbc29150ace Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 12 17:02:38 2022 +0300 aghnet: imp arp container
147 lines
3.9 KiB
Go
147 lines
3.9 KiB
Go
//go:build !windows
|
|
// +build !windows
|
|
|
|
package aghnet
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
|
)
|
|
|
|
// defaultHostGen is the default method of generating host for Refresh.
|
|
func defaultHostGen() (host string) {
|
|
// TODO(e.burkov): Use strings.Builder.
|
|
return fmt.Sprintf("test%d.org", time.Now().UnixNano())
|
|
}
|
|
|
|
// systemResolvers is a default implementation of SystemResolvers interface.
|
|
type systemResolvers struct {
|
|
// addrsLock protects addrs.
|
|
addrsLock sync.RWMutex
|
|
// addrs is the set that contains cached local resolvers' addresses.
|
|
addrs *stringutil.Set
|
|
|
|
// resolver is used to fetch the resolvers' addresses.
|
|
resolver *net.Resolver
|
|
// hostGenFunc generates hosts to resolve.
|
|
hostGenFunc HostGenFunc
|
|
}
|
|
|
|
const (
|
|
// errBadAddrPassed is returned when dialFunc can't parse an IP address.
|
|
errBadAddrPassed errors.Error = "the passed string is not a valid IP address"
|
|
|
|
// errFakeDial is an error which dialFunc is expected to return.
|
|
errFakeDial errors.Error = "this error signals the successful dialFunc work"
|
|
|
|
// errUnexpectedHostFormat is returned by validateDialedHost when the host has
|
|
// more than one percent sign.
|
|
errUnexpectedHostFormat errors.Error = "unexpected host format"
|
|
)
|
|
|
|
// refresh implements the SystemResolvers interface for *systemResolvers.
|
|
func (sr *systemResolvers) refresh() (err error) {
|
|
defer func() { err = errors.Annotate(err, "systemResolvers: %w") }()
|
|
|
|
_, err = sr.resolver.LookupHost(context.Background(), sr.hostGenFunc())
|
|
dnserr := &net.DNSError{}
|
|
if errors.As(err, &dnserr) && dnserr.Err == errFakeDial.Error() {
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func newSystemResolvers(hostGenFunc HostGenFunc) (sr SystemResolvers) {
|
|
if hostGenFunc == nil {
|
|
hostGenFunc = defaultHostGen
|
|
}
|
|
s := &systemResolvers{
|
|
resolver: &net.Resolver{
|
|
PreferGo: true,
|
|
},
|
|
hostGenFunc: hostGenFunc,
|
|
addrs: stringutil.NewSet(),
|
|
}
|
|
s.resolver.Dial = s.dialFunc
|
|
|
|
return s
|
|
}
|
|
|
|
// validateDialedHost validated the host used by resolvers in dialFunc.
|
|
func validateDialedHost(host string) (err error) {
|
|
defer func() { err = errors.Annotate(err, "parsing %q: %w", host) }()
|
|
|
|
parts := strings.Split(host, "%")
|
|
switch len(parts) {
|
|
case 1:
|
|
// host
|
|
case 2:
|
|
// Remove the zone and check the IP address part.
|
|
host = parts[0]
|
|
default:
|
|
return errUnexpectedHostFormat
|
|
}
|
|
|
|
if _, err = netutil.ParseIP(host); err != nil {
|
|
return errBadAddrPassed
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// dockerEmbeddedDNS is the address of Docker's embedded DNS server.
|
|
//
|
|
// See
|
|
// https://github.com/moby/moby/blob/v1.12.0/docs/userguide/networking/dockernetworks.md.
|
|
const dockerEmbeddedDNS = "127.0.0.11"
|
|
|
|
// dialFunc gets the resolver's address and puts it into internal cache.
|
|
func (sr *systemResolvers) dialFunc(_ context.Context, _, address string) (_ net.Conn, err error) {
|
|
// Just validate the passed address is a valid IP.
|
|
var host string
|
|
host, err = netutil.SplitHost(address)
|
|
if err != nil {
|
|
// TODO(e.burkov): Maybe use a structured errBadAddrPassed to
|
|
// allow unwrapping of the real error.
|
|
return nil, fmt.Errorf("%s: %w", err, errBadAddrPassed)
|
|
}
|
|
|
|
// Exclude Docker's embedded DNS server, as it may cause recursion if
|
|
// the container is set as the host system's default DNS server.
|
|
//
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3064.
|
|
//
|
|
// TODO(a.garipov): Perhaps only do this when we are in the container?
|
|
// Maybe use an environment variable?
|
|
if host == dockerEmbeddedDNS {
|
|
return nil, errFakeDial
|
|
}
|
|
|
|
err = validateDialedHost(host)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("validating dialed host: %w", err)
|
|
}
|
|
|
|
sr.addrsLock.Lock()
|
|
defer sr.addrsLock.Unlock()
|
|
|
|
sr.addrs.Add(host)
|
|
|
|
return nil, errFakeDial
|
|
}
|
|
|
|
func (sr *systemResolvers) Get() (rs []string) {
|
|
sr.addrsLock.RLock()
|
|
defer sr.addrsLock.RUnlock()
|
|
|
|
return sr.addrs.Values()
|
|
}
|