mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-19 14:21:58 +03:00
c6888326b0
Closes #2576. Updates #2275. Updates #2419. Updates #2443. Squashed commit of the following: commit b1a4809ada298d675de12740051ba26fb9945957 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri May 21 14:01:40 2021 +0300 all: add --local-frontend, upd docker commit 619ee7c82f27e3405753003dbec556ffb056d025 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu May 20 15:02:33 2021 +0300 bamboo-specs: bump docker version commit 5c2b2fbce80afdcc81fd0cb83674dc3d64facbf1 Merge: 6536b32d9c60aef6
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu May 20 15:01:47 2021 +0300 Merge branch 'master' into 2275-upd-go commit 6536b32dd4580425f7dedde6765463a79b9bd699 Merge: 9bb32bc46f7fd33a
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 20:38:48 2021 +0300 Merge branch 'master' into 2275-upd-go commit 9bb32bc4c0ac0f3a97195adc75359e48c9c58897 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 18:48:50 2021 +0300 all: fix build, imp err handling commit 6868eac7f7d2980fb706881f53e72afe5f7c3447 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 18:09:32 2021 +0300 all: fix github lint commit ebbb9c55f32fbd57e34e8b161016aa6b291c097c Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 17:36:56 2021 +0300 all: update go and backend tools
134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
// +build !windows
|
|
|
|
//go:build !windows
|
|
|
|
package aghnet
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghstrings"
|
|
)
|
|
|
|
// 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
|
|
|
|
// addrs is the set that contains cached local resolvers' addresses.
|
|
addrs *aghstrings.Set
|
|
addrsLock sync.RWMutex
|
|
}
|
|
|
|
func (sr *systemResolvers) refresh() (err error) {
|
|
defer agherr.Annotate("systemResolvers: %w", &err)
|
|
|
|
_, 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(refreshIvl time.Duration, hostGenFunc HostGenFunc) (sr SystemResolvers) {
|
|
if hostGenFunc == nil {
|
|
hostGenFunc = defaultHostGen
|
|
}
|
|
s := &systemResolvers{
|
|
resolver: &net.Resolver{
|
|
PreferGo: true,
|
|
},
|
|
hostGenFunc: hostGenFunc,
|
|
addrs: aghstrings.NewSet(),
|
|
}
|
|
s.resolver.Dial = s.dialFunc
|
|
|
|
return s
|
|
}
|
|
|
|
// validateDialedHost validated the host used by resolvers in dialFunc.
|
|
func validateDialedHost(host string) (err error) {
|
|
defer agherr.Annotate("parsing %q: %w", &err, host)
|
|
|
|
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
|
|
}
|
|
|
|
// 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 = 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()
|
|
}
|