2021-03-22 13:46:36 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-04-28 18:34:18 +00:00
|
|
|
"strings"
|
2021-03-22 13:46:36 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
|
2021-04-20 13:26:19 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghstrings"
|
2021-03-22 13:46:36 +00: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 13:26:19 +00:00
|
|
|
// addrs is the set that contains cached local resolvers' addresses.
|
|
|
|
addrs *aghstrings.Set
|
2021-03-22 13:46:36 +00:00
|
|
|
addrsLock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
func (sr *systemResolvers) refresh() (err error) {
|
2021-03-22 13:46:36 +00:00
|
|
|
defer agherr.Annotate("systemResolvers: %w", &err)
|
|
|
|
|
|
|
|
_, err = sr.resolver.LookupHost(context.Background(), sr.hostGenFunc())
|
|
|
|
dnserr := &net.DNSError{}
|
2021-04-28 18:34:18 +00:00
|
|
|
if errors.As(err, &dnserr) && dnserr.Err == errFakeDial.Error() {
|
2021-03-22 13:46:36 +00: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-04-20 13:26:19 +00:00
|
|
|
addrs: aghstrings.NewSet(),
|
2021-03-22 13:46:36 +00:00
|
|
|
}
|
|
|
|
s.resolver.Dial = s.dialFunc
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-04-28 18:34:18 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-03-22 13:46:36 +00: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 18:34:18 +00:00
|
|
|
// TODO(e.burkov): Maybe use a structured errBadAddrPassed to
|
2021-03-22 13:46:36 +00:00
|
|
|
// allow unwrapping of the real error.
|
2021-04-28 18:34:18 +00:00
|
|
|
return nil, fmt.Errorf("%s: %w", err, errBadAddrPassed)
|
2021-03-22 13:46:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 18:34:18 +00:00
|
|
|
err = validateDialedHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("validating dialed host: %w", err)
|
2021-03-22 13:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sr.addrsLock.Lock()
|
|
|
|
defer sr.addrsLock.Unlock()
|
|
|
|
|
2021-04-20 13:26:19 +00:00
|
|
|
sr.addrs.Add(host)
|
2021-03-22 13:46:36 +00:00
|
|
|
|
2021-04-28 18:34:18 +00:00
|
|
|
return nil, errFakeDial
|
2021-03-22 13:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *systemResolvers) Get() (rs []string) {
|
|
|
|
sr.addrsLock.RLock()
|
|
|
|
defer sr.addrsLock.RUnlock()
|
|
|
|
|
2021-04-20 13:26:19 +00:00
|
|
|
return sr.addrs.Values()
|
2021-03-22 13:46:36 +00:00
|
|
|
}
|