2019-08-07 12:25:19 +00:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2019-10-22 10:11:22 +00:00
|
|
|
"encoding/binary"
|
2021-01-20 14:27:53 +00:00
|
|
|
"net"
|
2021-05-26 14:55:19 +00:00
|
|
|
"sync/atomic"
|
2019-08-07 12:25:19 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2019-10-22 10:11:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/cache"
|
2019-08-07 12:25:19 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
// RDNS resolves clients' addresses to enrich their metadata.
|
2019-09-19 15:27:13 +00:00
|
|
|
type RDNS struct {
|
2021-04-07 17:16:06 +00:00
|
|
|
exchanger dnsforward.RDNSExchanger
|
|
|
|
clients *clientsContainer
|
2021-03-31 12:00:47 +00:00
|
|
|
|
2022-04-19 12:01:49 +00:00
|
|
|
// usePrivate is used to store the state of current private RDNS resolving
|
|
|
|
// settings and to react to it's changes.
|
2021-05-26 14:55:19 +00:00
|
|
|
usePrivate uint32
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
// ipCh used to pass client's IP to rDNS workerLoop.
|
|
|
|
ipCh chan net.IP
|
|
|
|
|
|
|
|
// ipCache caches the IP addresses to be resolved by rDNS. The resolved
|
2022-04-19 12:01:49 +00: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 12:00:47 +00:00
|
|
|
ipCache cache.Cache
|
2019-09-19 15:27:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
// Default rDNS values.
|
|
|
|
const (
|
|
|
|
defaultRDNSCacheSize = 10000
|
|
|
|
defaultRDNSCacheTTL = 1 * 60 * 60
|
|
|
|
defaultRDNSIPChSize = 256
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewRDNS creates and returns initialized RDNS.
|
|
|
|
func NewRDNS(
|
2021-04-07 17:16:06 +00:00
|
|
|
exchanger dnsforward.RDNSExchanger,
|
2021-03-31 12:00:47 +00:00
|
|
|
clients *clientsContainer,
|
2021-05-26 14:55:19 +00:00
|
|
|
usePrivate bool,
|
2021-03-31 12:00:47 +00:00
|
|
|
) (rDNS *RDNS) {
|
|
|
|
rDNS = &RDNS{
|
2021-04-07 17:16:06 +00:00
|
|
|
exchanger: exchanger,
|
|
|
|
clients: clients,
|
2021-03-31 12:00:47 +00:00
|
|
|
ipCache: cache.New(cache.Config{
|
2021-02-04 17:35:13 +00:00
|
|
|
EnableLRU: true,
|
2021-03-31 12:00:47 +00:00
|
|
|
MaxCount: defaultRDNSCacheSize,
|
2021-02-04 17:35:13 +00:00
|
|
|
}),
|
2021-03-31 12:00:47 +00:00
|
|
|
ipCh: make(chan net.IP, defaultRDNSIPChSize),
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
2021-05-26 14:55:19 +00:00
|
|
|
if usePrivate {
|
|
|
|
rDNS.usePrivate = 1
|
|
|
|
}
|
2019-10-22 10:11:22 +00:00
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
go rDNS.workerLoop()
|
|
|
|
|
|
|
|
return rDNS
|
2019-08-07 12:25:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 14:55:19 +00: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() {
|
|
|
|
var usePrivate uint32
|
|
|
|
if r.exchanger.ResolvesPrivatePTR() {
|
|
|
|
usePrivate = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if atomic.CompareAndSwapUint32(&r.usePrivate, 1-usePrivate, usePrivate) {
|
|
|
|
r.ipCache.Clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isCached returns true if ip is already cached and not expired yet. It also
|
|
|
|
// caches it otherwise.
|
|
|
|
func (r *RDNS) isCached(ip net.IP) (ok bool) {
|
2019-10-22 10:11:22 +00:00
|
|
|
now := uint64(time.Now().Unix())
|
2021-03-31 12:00:47 +00:00
|
|
|
if expire := r.ipCache.Get(ip); len(expire) != 0 {
|
|
|
|
if binary.BigEndian.Uint64(expire) > now {
|
2021-05-26 14:55:19 +00:00
|
|
|
return true
|
2019-10-22 10:11:22 +00:00
|
|
|
}
|
2019-08-07 12:25:19 +00:00
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
|
|
// The cache entry either expired or doesn't exist.
|
|
|
|
ttl := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(ttl, now+defaultRDNSCacheTTL)
|
|
|
|
r.ipCache.Set(ip, ttl)
|
2019-08-07 12:25:19 +00:00
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin adds the ip to the resolving queue if it is not cached or already
|
|
|
|
// resolved.
|
|
|
|
func (r *RDNS) Begin(ip net.IP) {
|
|
|
|
r.ensurePrivateCache()
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
if r.isCached(ip) || r.clients.Exists(ip, ClientSourceRDNS) {
|
2019-08-07 12:25:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2021-03-31 12:00:47 +00:00
|
|
|
case r.ipCh <- ip:
|
|
|
|
log.Tracef("rdns: %q added to queue", ip)
|
2019-08-07 12:25:19 +00:00
|
|
|
default:
|
2021-03-31 12:00:47 +00:00
|
|
|
log.Tracef("rdns: queue is full")
|
2019-08-07 12:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
// workerLoop handles incoming IP addresses from ipChan and adds it into
|
|
|
|
// clients.
|
2019-09-19 15:27:13 +00:00
|
|
|
func (r *RDNS) workerLoop() {
|
2021-05-24 14:28:11 +00:00
|
|
|
defer log.OnPanic("rdns")
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
|
|
for ip := range r.ipCh {
|
2021-04-07 17:16:06 +00:00
|
|
|
host, err := r.exchanger.Exchange(ip)
|
2021-03-31 12:00:47 +00:00
|
|
|
if err != nil {
|
2021-04-16 07:32:41 +00:00
|
|
|
log.Debug("rdns: resolving %q: %s", ip, err)
|
2019-08-07 12:25:19 +00:00
|
|
|
|
|
|
|
continue
|
2022-03-22 12:21:03 +00:00
|
|
|
} else if host == "" {
|
2021-04-13 13:23:36 +00:00
|
|
|
continue
|
2021-04-07 17:16:06 +00:00
|
|
|
}
|
2021-04-13 13:23:36 +00:00
|
|
|
|
2022-03-22 12:21:03 +00:00
|
|
|
// Don't handle any errors since AddHost doesn't return non-nil errors
|
|
|
|
// for now.
|
2021-06-29 12:53:28 +00:00
|
|
|
_, _ = r.clients.AddHost(ip, host, ClientSourceRDNS)
|
2019-08-07 12:25:19 +00:00
|
|
|
}
|
|
|
|
}
|