2019-08-07 15:25:19 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2019-10-22 13:11:22 +03:00
|
|
|
"encoding/binary"
|
2021-01-20 17:27:53 +03:00
|
|
|
"net"
|
2019-08-07 15:25:19 +03:00
|
|
|
"strings"
|
|
|
|
"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"
|
2019-08-07 15:25:19 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2019-09-19 18:27:13 +03:00
|
|
|
// RDNS - module context
|
|
|
|
type RDNS struct {
|
2019-12-11 12:38:58 +03:00
|
|
|
dnsServer *dnsforward.Server
|
2019-09-19 18:27:13 +03:00
|
|
|
clients *clientsContainer
|
2021-01-20 17:27:53 +03:00
|
|
|
ipChannel chan net.IP // pass data from DNS request handling thread to rDNS thread
|
2019-10-22 13:11:22 +03:00
|
|
|
|
|
|
|
// Contains IP addresses of clients to be resolved by rDNS
|
|
|
|
// If IP address is resolved, it stays here while it's inside Clients.
|
|
|
|
// If it's removed from Clients, this IP address will be resolved once again.
|
|
|
|
// If IP address couldn't be resolved, it stays here for some time to prevent further attempts to resolve the same IP.
|
|
|
|
ipAddrs cache.Cache
|
2019-09-19 18:27:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitRDNS - create module context
|
2019-12-11 12:38:58 +03:00
|
|
|
func InitRDNS(dnsServer *dnsforward.Server, clients *clientsContainer) *RDNS {
|
2021-02-04 20:35:13 +03:00
|
|
|
r := &RDNS{
|
|
|
|
dnsServer: dnsServer,
|
|
|
|
clients: clients,
|
|
|
|
ipAddrs: cache.New(cache.Config{
|
|
|
|
EnableLRU: true,
|
|
|
|
MaxCount: 10000,
|
|
|
|
}),
|
|
|
|
ipChannel: make(chan net.IP, 256),
|
|
|
|
}
|
2019-10-22 13:11:22 +03:00
|
|
|
|
2019-09-19 18:27:13 +03:00
|
|
|
go r.workerLoop()
|
2021-02-04 20:35:13 +03:00
|
|
|
return r
|
2019-08-07 15:25:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 18:27:13 +03:00
|
|
|
// Begin - add IP address to rDNS queue
|
2021-01-20 17:27:53 +03:00
|
|
|
func (r *RDNS) Begin(ip net.IP) {
|
2019-10-22 13:11:22 +03:00
|
|
|
now := uint64(time.Now().Unix())
|
2021-01-20 17:27:53 +03:00
|
|
|
expire := r.ipAddrs.Get(ip)
|
2019-10-22 13:11:22 +03:00
|
|
|
if len(expire) != 0 {
|
|
|
|
exp := binary.BigEndian.Uint64(expire)
|
|
|
|
if exp > now {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TTL expired
|
2019-08-07 15:25:19 +03:00
|
|
|
}
|
2019-10-22 13:11:22 +03:00
|
|
|
expire = make([]byte, 8)
|
2019-11-07 14:02:34 +03:00
|
|
|
const ttl = 1 * 60 * 60
|
2019-10-22 13:11:22 +03:00
|
|
|
binary.BigEndian.PutUint64(expire, now+ttl)
|
2021-01-20 17:27:53 +03:00
|
|
|
_ = r.ipAddrs.Set(ip, expire)
|
2019-08-07 15:25:19 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
id := ip.String()
|
|
|
|
if r.clients.Exists(id, ClientSourceRDNS) {
|
2019-08-07 15:25:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:11:22 +03:00
|
|
|
log.Tracef("rDNS: adding %s", ip)
|
2019-08-07 15:25:19 +03:00
|
|
|
select {
|
2019-09-19 18:27:13 +03:00
|
|
|
case r.ipChannel <- ip:
|
2019-08-07 15:25:19 +03:00
|
|
|
//
|
|
|
|
default:
|
2019-10-22 13:11:22 +03:00
|
|
|
log.Tracef("rDNS: queue is full")
|
2019-08-07 15:25:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use rDNS to get hostname by IP address
|
2021-01-20 17:27:53 +03:00
|
|
|
func (r *RDNS) resolve(ip net.IP) string {
|
2019-08-07 15:25:19 +03:00
|
|
|
log.Tracef("Resolving host for %s", ip)
|
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
name, err := dns.ReverseAddr(ip.String())
|
2019-08-07 15:25:19 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error while calling dns.ReverseAddr(%s): %s", ip, err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
resp, err := r.dnsServer.Exchange(&dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: dns.Id(),
|
|
|
|
RecursionDesired: true,
|
|
|
|
},
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Name: name,
|
|
|
|
Qtype: dns.TypePTR,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}},
|
|
|
|
})
|
2019-08-07 15:25:19 +03:00
|
|
|
if err != nil {
|
2019-08-07 15:35:06 +03:00
|
|
|
log.Debug("Error while making an rDNS lookup for %s: %s", ip, err)
|
2019-08-07 15:25:19 +03:00
|
|
|
return ""
|
|
|
|
}
|
2019-12-05 13:06:52 +03:00
|
|
|
if len(resp.Answer) == 0 {
|
2019-08-07 15:25:19 +03:00
|
|
|
log.Debug("No answer for rDNS lookup of %s", ip)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
ptr, ok := resp.Answer[0].(*dns.PTR)
|
|
|
|
if !ok {
|
2019-08-07 15:35:06 +03:00
|
|
|
log.Debug("not a PTR response for %s", ip)
|
2019-08-07 15:25:19 +03:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("PTR response for %s: %s", ip, ptr.String())
|
|
|
|
if strings.HasSuffix(ptr.Ptr, ".") {
|
|
|
|
ptr.Ptr = ptr.Ptr[:len(ptr.Ptr)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr.Ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for a signal and then synchronously resolve hostname by IP address
|
|
|
|
// Add the hostname:IP pair to "Clients" array
|
2019-09-19 18:27:13 +03:00
|
|
|
func (r *RDNS) workerLoop() {
|
2019-08-07 15:25:19 +03:00
|
|
|
for {
|
2020-11-06 12:15:08 +03:00
|
|
|
ip := <-r.ipChannel
|
2019-08-07 15:25:19 +03:00
|
|
|
|
2019-09-19 18:27:13 +03:00
|
|
|
host := r.resolve(ip)
|
2019-08-07 15:25:19 +03:00
|
|
|
if len(host) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
_, _ = r.clients.AddHost(ip.String(), host, ClientSourceRDNS)
|
2019-08-07 15:25:19 +03:00
|
|
|
}
|
|
|
|
}
|