2019-08-07 12:25:19 +00:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2019-10-22 10:11:22 +00:00
|
|
|
"encoding/binary"
|
2021-03-31 12:00:47 +00:00
|
|
|
"fmt"
|
2021-01-20 14:27:53 +00:00
|
|
|
"net"
|
2019-08-07 12:25:19 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
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"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
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-03-31 12:00:47 +00:00
|
|
|
dnsServer *dnsforward.Server
|
|
|
|
clients *clientsContainer
|
|
|
|
subnetDetector *aghnet.SubnetDetector
|
|
|
|
localResolvers aghnet.Exchanger
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// 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.
|
|
|
|
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(
|
|
|
|
dnsServer *dnsforward.Server,
|
|
|
|
clients *clientsContainer,
|
|
|
|
snd *aghnet.SubnetDetector,
|
|
|
|
lr aghnet.Exchanger,
|
|
|
|
) (rDNS *RDNS) {
|
|
|
|
rDNS = &RDNS{
|
|
|
|
dnsServer: dnsServer,
|
|
|
|
clients: clients,
|
|
|
|
subnetDetector: snd,
|
|
|
|
localResolvers: lr,
|
|
|
|
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
|
|
|
}
|
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-03-31 12:00:47 +00:00
|
|
|
// Begin adds the ip to the resolving queue if it is not cached or already
|
|
|
|
// resolved.
|
2021-01-20 14:27:53 +00:00
|
|
|
func (r *RDNS) Begin(ip net.IP) {
|
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 {
|
2019-10-22 10:11:22 +00:00
|
|
|
return
|
|
|
|
}
|
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-01-27 15:32:13 +00:00
|
|
|
id := ip.String()
|
|
|
|
if r.clients.Exists(id, 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
|
|
|
const (
|
|
|
|
// rDNSEmptyAnswerErr is returned by RDNS resolve method when the answer
|
|
|
|
// section of respond is empty.
|
|
|
|
rDNSEmptyAnswerErr agherr.Error = "the answer section is empty"
|
2019-08-07 12:25:19 +00:00
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
// rDNSNotPTRErr is returned by RDNS resolve method when the response is
|
|
|
|
// not of PTR type.
|
|
|
|
rDNSNotPTRErr agherr.Error = "the response is not a ptr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// resolve tries to resolve the ip in a suitable way.
|
|
|
|
func (r *RDNS) resolve(ip net.IP) (host string, err error) {
|
|
|
|
log.Tracef("rdns: resolving host for %q", ip)
|
2019-08-07 12:25:19 +00:00
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
arpa := dns.Fqdn(aghnet.ReverseAddr(ip))
|
|
|
|
msg := &dns.Msg{
|
2021-02-04 17:35:13 +00:00
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: dns.Id(),
|
|
|
|
RecursionDesired: true,
|
|
|
|
},
|
2021-03-31 12:00:47 +00:00
|
|
|
Compress: true,
|
2021-02-04 17:35:13 +00:00
|
|
|
Question: []dns.Question{{
|
2021-03-31 12:00:47 +00:00
|
|
|
Name: arpa,
|
2021-02-04 17:35:13 +00:00
|
|
|
Qtype: dns.TypePTR,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}},
|
2021-03-31 12:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resp *dns.Msg
|
|
|
|
if r.subnetDetector.IsLocallyServedNetwork(ip) {
|
|
|
|
resp, err = r.localResolvers.Exchange(msg)
|
|
|
|
} else {
|
|
|
|
resp, err = r.dnsServer.Exchange(msg)
|
|
|
|
}
|
2019-08-07 12:25:19 +00:00
|
|
|
if err != nil {
|
2021-03-31 12:00:47 +00:00
|
|
|
return "", fmt.Errorf("performing lookup for %q: %w", arpa, err)
|
2019-08-07 12:25:19 +00:00
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
2019-12-05 10:06:52 +00:00
|
|
|
if len(resp.Answer) == 0 {
|
2021-03-31 12:00:47 +00:00
|
|
|
return "", fmt.Errorf("lookup for %q: %w", arpa, rDNSEmptyAnswerErr)
|
2019-08-07 12:25:19 +00:00
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
2019-08-07 12:25:19 +00:00
|
|
|
ptr, ok := resp.Answer[0].(*dns.PTR)
|
|
|
|
if !ok {
|
2021-03-31 12:00:47 +00:00
|
|
|
return "", fmt.Errorf("type checking: %w", rDNSNotPTRErr)
|
2019-08-07 12:25:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
log.Tracef("rdns: ptr response for %q: %s", ip, ptr.String())
|
2019-08-07 12:25:19 +00:00
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
return strings.TrimSuffix(ptr.Ptr, "."), nil
|
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-03-31 12:00:47 +00:00
|
|
|
defer agherr.LogPanic("rdns")
|
|
|
|
|
|
|
|
for ip := range r.ipCh {
|
|
|
|
host, err := r.resolve(ip)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("rdns: resolving %q: %s", ip, err)
|
2019-08-07 12:25:19 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
// Don't handle any errors since AddHost doesn't return non-nil
|
|
|
|
// errors for now.
|
2021-01-20 14:27:53 +00:00
|
|
|
_, _ = r.clients.AddHost(ip.String(), host, ClientSourceRDNS)
|
2019-08-07 12:25:19 +00:00
|
|
|
}
|
|
|
|
}
|