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