2019-08-07 15:25:19 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2019-10-22 13:11:22 +03:00
|
|
|
"encoding/binary"
|
2022-11-09 14:37:07 +03:00
|
|
|
"net/netip"
|
2021-05-26 17:55:19 +03:00
|
|
|
"sync/atomic"
|
2019-08-07 15:25:19 +03:00
|
|
|
"time"
|
|
|
|
|
2020-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2019-10-22 13:11:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/cache"
|
2023-02-10 16:40:36 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-08-07 15:25:19 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
// RDNS resolves clients' addresses to enrich their metadata.
|
2019-09-19 18:27:13 +03:00
|
|
|
type RDNS struct {
|
2021-04-07 20:16:06 +03:00
|
|
|
exchanger dnsforward.RDNSExchanger
|
|
|
|
clients *clientsContainer
|
2021-03-31 15:00:47 +03:00
|
|
|
|
|
|
|
// ipCh used to pass client's IP to rDNS workerLoop.
|
2022-11-09 14:37:07 +03:00
|
|
|
ipCh chan netip.Addr
|
2021-03-31 15:00:47 +03:00
|
|
|
|
|
|
|
// ipCache caches the IP addresses to be resolved by rDNS. The resolved
|
2022-04-19 15:01:49 +03:00
|
|
|
// address stays here while it's inside clients. After leaving clients the
|
|
|
|
// address will be resolved once again. If the address couldn't be
|
|
|
|
// resolved, cache prevents further attempts to resolve it for some time.
|
2021-03-31 15:00:47 +03:00
|
|
|
ipCache cache.Cache
|
2023-02-08 13:39:04 +03:00
|
|
|
|
|
|
|
// usePrivate stores the state of current private reverse-DNS resolving
|
|
|
|
// settings.
|
|
|
|
usePrivate atomic.Bool
|
2019-09-19 18:27:13 +03:00
|
|
|
}
|
|
|
|
|
2023-02-10 16:40:36 +03:00
|
|
|
// Default AdGuard Home reverse DNS values.
|
2021-03-31 15:00:47 +03:00
|
|
|
const (
|
2023-02-10 16:40:36 +03:00
|
|
|
revDNSCacheSize = 10000
|
|
|
|
|
|
|
|
// TODO(e.burkov): Make these values configurable.
|
|
|
|
revDNSCacheTTL = 24 * 60 * 60
|
|
|
|
revDNSFailureCacheTTL = 1 * 60 * 60
|
|
|
|
|
|
|
|
revDNSQueueSize = 256
|
2021-03-31 15:00:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewRDNS creates and returns initialized RDNS.
|
|
|
|
func NewRDNS(
|
2021-04-07 20:16:06 +03:00
|
|
|
exchanger dnsforward.RDNSExchanger,
|
2021-03-31 15:00:47 +03:00
|
|
|
clients *clientsContainer,
|
2021-05-26 17:55:19 +03:00
|
|
|
usePrivate bool,
|
2021-03-31 15:00:47 +03:00
|
|
|
) (rDNS *RDNS) {
|
|
|
|
rDNS = &RDNS{
|
2021-04-07 20:16:06 +03:00
|
|
|
exchanger: exchanger,
|
|
|
|
clients: clients,
|
2021-03-31 15:00:47 +03:00
|
|
|
ipCache: cache.New(cache.Config{
|
2021-02-04 20:35:13 +03:00
|
|
|
EnableLRU: true,
|
2023-02-10 16:40:36 +03:00
|
|
|
MaxCount: revDNSCacheSize,
|
2021-02-04 20:35:13 +03:00
|
|
|
}),
|
2023-02-10 16:40:36 +03:00
|
|
|
ipCh: make(chan netip.Addr, revDNSQueueSize),
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
2023-02-08 13:39:04 +03:00
|
|
|
|
|
|
|
rDNS.usePrivate.Store(usePrivate)
|
2019-10-22 13:11:22 +03:00
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
go rDNS.workerLoop()
|
|
|
|
|
|
|
|
return rDNS
|
2019-08-07 15:25:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
// ensurePrivateCache ensures that the state of the RDNS cache is consistent
|
|
|
|
// with the current private client RDNS resolving settings.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Clearing cache each time this value changed is not a perfect
|
|
|
|
// approach since only unresolved locally-served addresses should be removed.
|
|
|
|
// Implement when improving the cache.
|
|
|
|
func (r *RDNS) ensurePrivateCache() {
|
2023-02-08 13:39:04 +03:00
|
|
|
usePrivate := r.exchanger.ResolvesPrivatePTR()
|
|
|
|
if r.usePrivate.CompareAndSwap(!usePrivate, usePrivate) {
|
2021-05-26 17:55:19 +03:00
|
|
|
r.ipCache.Clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isCached returns true if ip is already cached and not expired yet. It also
|
|
|
|
// caches it otherwise.
|
2022-11-09 14:37:07 +03:00
|
|
|
func (r *RDNS) isCached(ip netip.Addr) (ok bool) {
|
|
|
|
ipBytes := ip.AsSlice()
|
2019-10-22 13:11:22 +03:00
|
|
|
now := uint64(time.Now().Unix())
|
2022-11-09 14:37:07 +03:00
|
|
|
if expire := r.ipCache.Get(ipBytes); len(expire) != 0 {
|
2023-02-10 16:40:36 +03:00
|
|
|
return binary.BigEndian.Uint64(expire) > now
|
2019-08-07 15:25:19 +03:00
|
|
|
}
|
2021-03-31 15:00:47 +03:00
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-02-10 16:40:36 +03:00
|
|
|
// cache caches the ip address for ttl seconds.
|
|
|
|
func (r *RDNS) cache(ip netip.Addr, ttl uint64) {
|
|
|
|
ipData := ip.AsSlice()
|
|
|
|
|
|
|
|
ttlData := [8]byte{}
|
|
|
|
binary.BigEndian.PutUint64(ttlData[:], uint64(time.Now().Unix())+ttl)
|
|
|
|
|
|
|
|
r.ipCache.Set(ipData, ttlData[:])
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
// Begin adds the ip to the resolving queue if it is not cached or already
|
|
|
|
// resolved.
|
2022-11-09 14:37:07 +03:00
|
|
|
func (r *RDNS) Begin(ip netip.Addr) {
|
2021-05-26 17:55:19 +03:00
|
|
|
r.ensurePrivateCache()
|
|
|
|
|
2023-02-10 16:40:36 +03:00
|
|
|
if r.isCached(ip) || r.clients.clientSource(ip) > ClientSourceRDNS {
|
2019-08-07 15:25:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2021-03-31 15:00:47 +03:00
|
|
|
case r.ipCh <- ip:
|
2022-11-09 14:37:07 +03:00
|
|
|
log.Debug("rdns: %q added to queue", ip)
|
2019-08-07 15:25:19 +03:00
|
|
|
default:
|
2022-11-09 14:37:07 +03:00
|
|
|
log.Debug("rdns: queue is full")
|
2019-08-07 15:25:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
// workerLoop handles incoming IP addresses from ipChan and adds it into
|
|
|
|
// clients.
|
2019-09-19 18:27:13 +03:00
|
|
|
func (r *RDNS) workerLoop() {
|
2021-05-24 17:28:11 +03:00
|
|
|
defer log.OnPanic("rdns")
|
2021-03-31 15:00:47 +03:00
|
|
|
|
|
|
|
for ip := range r.ipCh {
|
2023-02-10 16:40:36 +03:00
|
|
|
ttl := uint64(revDNSCacheTTL)
|
|
|
|
|
2022-11-09 14:37:07 +03:00
|
|
|
host, err := r.exchanger.Exchange(ip.AsSlice())
|
2021-03-31 15:00:47 +03:00
|
|
|
if err != nil {
|
2021-04-16 10:32:41 +03:00
|
|
|
log.Debug("rdns: resolving %q: %s", ip, err)
|
2023-02-10 16:40:36 +03:00
|
|
|
if errors.Is(err, dnsforward.ErrRDNSFailed) {
|
|
|
|
// Cache failure for a less time.
|
|
|
|
ttl = revDNSFailureCacheTTL
|
|
|
|
}
|
2021-04-07 20:16:06 +03:00
|
|
|
}
|
2021-04-13 16:23:36 +03:00
|
|
|
|
2023-02-10 16:40:36 +03:00
|
|
|
r.cache(ip, ttl)
|
|
|
|
|
|
|
|
if host != "" {
|
|
|
|
_ = r.clients.AddHost(ip, host, ClientSourceRDNS)
|
|
|
|
}
|
2019-08-07 15:25:19 +03:00
|
|
|
}
|
|
|
|
}
|