2021-05-21 14:55:42 +03:00
|
|
|
//go:build !windows
|
2021-06-15 19:42:41 +03:00
|
|
|
// +build !windows
|
2021-05-21 14:55:42 +03:00
|
|
|
|
2021-03-22 16:46:36 +03:00
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-04-28 21:34:18 +03:00
|
|
|
"strings"
|
2021-03-22 16:46:36 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2021-03-22 16:46:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
resolver *net.Resolver
|
|
|
|
hostGenFunc HostGenFunc
|
|
|
|
|
2021-04-20 16:26:19 +03:00
|
|
|
// addrs is the set that contains cached local resolvers' addresses.
|
2021-07-29 17:40:31 +03:00
|
|
|
addrs *stringutil.Set
|
2021-03-22 16:46:36 +03:00
|
|
|
addrsLock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2021-05-31 20:11:06 +03:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
func (sr *systemResolvers) refresh() (err error) {
|
2021-05-24 17:28:11 +03:00
|
|
|
defer func() { err = errors.Annotate(err, "systemResolvers: %w") }()
|
2021-03-22 16:46:36 +03:00
|
|
|
|
|
|
|
_, err = sr.resolver.LookupHost(context.Background(), sr.hostGenFunc())
|
|
|
|
dnserr := &net.DNSError{}
|
2021-04-28 21:34:18 +03:00
|
|
|
if errors.As(err, &dnserr) && dnserr.Err == errFakeDial.Error() {
|
2021-03-22 16:46:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSystemResolvers(refreshIvl time.Duration, hostGenFunc HostGenFunc) (sr SystemResolvers) {
|
|
|
|
if hostGenFunc == nil {
|
|
|
|
hostGenFunc = defaultHostGen
|
|
|
|
}
|
|
|
|
s := &systemResolvers{
|
|
|
|
resolver: &net.Resolver{
|
|
|
|
PreferGo: true,
|
|
|
|
},
|
|
|
|
hostGenFunc: hostGenFunc,
|
2021-07-29 17:40:31 +03:00
|
|
|
addrs: stringutil.NewSet(),
|
2021-03-22 16:46:36 +03:00
|
|
|
}
|
|
|
|
s.resolver.Dial = s.dialFunc
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-04-28 21:34:18 +03:00
|
|
|
// validateDialedHost validated the host used by resolvers in dialFunc.
|
|
|
|
func validateDialedHost(host string) (err error) {
|
2021-05-24 17:28:11 +03:00
|
|
|
defer func() { err = errors.Annotate(err, "parsing %q: %w", host) }()
|
2021-04-28 21:34:18 +03:00
|
|
|
|
|
|
|
var ipStr string
|
|
|
|
parts := strings.Split(host, "%")
|
|
|
|
switch len(parts) {
|
|
|
|
case 1:
|
|
|
|
ipStr = host
|
|
|
|
case 2:
|
|
|
|
// Remove the zone and check the IP address part.
|
|
|
|
ipStr = parts[0]
|
|
|
|
default:
|
|
|
|
return errUnexpectedHostFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
if net.ParseIP(ipStr) == nil {
|
|
|
|
return errBadAddrPassed
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-13 14:30:40 +03:00
|
|
|
// 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"
|
|
|
|
|
2021-03-22 16:46:36 +03:00
|
|
|
// 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 = SplitHost(address)
|
|
|
|
if err != nil {
|
2021-04-28 21:34:18 +03:00
|
|
|
// TODO(e.burkov): Maybe use a structured errBadAddrPassed to
|
2021-03-22 16:46:36 +03:00
|
|
|
// allow unwrapping of the real error.
|
2021-04-28 21:34:18 +03:00
|
|
|
return nil, fmt.Errorf("%s: %w", err, errBadAddrPassed)
|
2021-03-22 16:46:36 +03:00
|
|
|
}
|
|
|
|
|
2021-05-13 14:30:40 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-04-28 21:34:18 +03:00
|
|
|
err = validateDialedHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("validating dialed host: %w", err)
|
2021-03-22 16:46:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sr.addrsLock.Lock()
|
|
|
|
defer sr.addrsLock.Unlock()
|
|
|
|
|
2021-04-20 16:26:19 +03:00
|
|
|
sr.addrs.Add(host)
|
2021-03-22 16:46:36 +03:00
|
|
|
|
2021-04-28 21:34:18 +03:00
|
|
|
return nil, errFakeDial
|
2021-03-22 16:46:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *systemResolvers) Get() (rs []string) {
|
|
|
|
sr.addrsLock.RLock()
|
|
|
|
defer sr.addrsLock.RUnlock()
|
|
|
|
|
2021-04-20 16:26:19 +03:00
|
|
|
return sr.addrs.Values()
|
2021-03-22 16:46:36 +03:00
|
|
|
}
|