Merge: * rdns,whois: get client info for all question types (not just A/AAAA)
Close #1103 * commit '235b198ef97d7a46ab6d76a4074ec589fc0148eb': * rdns,whois: recheck IP addresses after some time * rdns,whois: get client info for all question types (not just A/AAAA)
This commit is contained in:
commit
a52c4b4362
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/AdguardTeam/dnsproxy/proxy"
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/joomcode/errorx"
|
"github.com/joomcode/errorx"
|
||||||
"github.com/miekg/dns"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type dnsContext struct {
|
type dnsContext struct {
|
||||||
@ -137,11 +136,6 @@ func isPublicIP(ip net.IP) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func onDNSRequest(d *proxy.DNSContext) {
|
func onDNSRequest(d *proxy.DNSContext) {
|
||||||
qType := d.Req.Question[0].Qtype
|
|
||||||
if qType != dns.TypeA && qType != dns.TypeAAAA {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ip := dnsforward.GetIPString(d.Addr)
|
ip := dnsforward.GetIPString(d.Addr)
|
||||||
if ip == "" {
|
if ip == "" {
|
||||||
// This would be quite weird if we get here
|
// This would be quite weird if we get here
|
||||||
|
54
home/rdns.go
54
home/rdns.go
@ -1,12 +1,13 @@
|
|||||||
package home
|
package home
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/AdguardTeam/dnsproxy/upstream"
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
||||||
|
"github.com/AdguardTeam/golibs/cache"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
@ -18,12 +19,14 @@ const (
|
|||||||
// RDNS - module context
|
// RDNS - module context
|
||||||
type RDNS struct {
|
type RDNS struct {
|
||||||
clients *clientsContainer
|
clients *clientsContainer
|
||||||
ipChannel chan string // pass data from DNS request handling thread to rDNS thread
|
ipChannel chan string // pass data from DNS request handling thread to rDNS thread
|
||||||
// contains IP addresses of clients to be resolved by rDNS
|
upstream upstream.Upstream // Upstream object for our own DNS server
|
||||||
// if IP address couldn't be resolved, it stays here forever to prevent further attempts to resolve the same IP
|
|
||||||
ips map[string]bool
|
// Contains IP addresses of clients to be resolved by rDNS
|
||||||
lock sync.Mutex // synchronize access to 'ips'
|
// If IP address is resolved, it stays here while it's inside Clients.
|
||||||
upstream upstream.Upstream // Upstream object for our own DNS server
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitRDNS - create module context
|
// InitRDNS - create module context
|
||||||
@ -47,7 +50,11 @@ func InitRDNS(clients *clientsContainer) *RDNS {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
r.ips = make(map[string]bool)
|
cconf := cache.Config{}
|
||||||
|
cconf.EnableLRU = true
|
||||||
|
cconf.MaxCount = 10000
|
||||||
|
r.ipAddrs = cache.New(cconf)
|
||||||
|
|
||||||
r.ipChannel = make(chan string, 256)
|
r.ipChannel = make(chan string, 256)
|
||||||
go r.workerLoop()
|
go r.workerLoop()
|
||||||
return &r
|
return &r
|
||||||
@ -55,25 +62,30 @@ func InitRDNS(clients *clientsContainer) *RDNS {
|
|||||||
|
|
||||||
// Begin - add IP address to rDNS queue
|
// Begin - add IP address to rDNS queue
|
||||||
func (r *RDNS) Begin(ip string) {
|
func (r *RDNS) Begin(ip string) {
|
||||||
|
now := uint64(time.Now().Unix())
|
||||||
|
expire := r.ipAddrs.Get([]byte(ip))
|
||||||
|
if len(expire) != 0 {
|
||||||
|
exp := binary.BigEndian.Uint64(expire)
|
||||||
|
if exp > now {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// TTL expired
|
||||||
|
}
|
||||||
|
expire = make([]byte, 8)
|
||||||
|
const ttl = 12 * 60 * 60
|
||||||
|
binary.BigEndian.PutUint64(expire, now+ttl)
|
||||||
|
_ = r.ipAddrs.Set([]byte(ip), expire)
|
||||||
|
|
||||||
if r.clients.Exists(ip, ClientSourceRDNS) {
|
if r.clients.Exists(ip, ClientSourceRDNS) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// add IP to ips, if not exists
|
log.Tracef("rDNS: adding %s", ip)
|
||||||
r.lock.Lock()
|
|
||||||
defer r.lock.Unlock()
|
|
||||||
_, ok := r.ips[ip]
|
|
||||||
if ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
r.ips[ip] = true
|
|
||||||
|
|
||||||
log.Tracef("Adding %s for rDNS resolve", ip)
|
|
||||||
select {
|
select {
|
||||||
case r.ipChannel <- ip:
|
case r.ipChannel <- ip:
|
||||||
//
|
//
|
||||||
default:
|
default:
|
||||||
log.Tracef("rDNS queue is full")
|
log.Tracef("rDNS: queue is full")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,10 +144,6 @@ func (r *RDNS) workerLoop() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
r.lock.Lock()
|
|
||||||
delete(r.ips, ip)
|
|
||||||
r.lock.Unlock()
|
|
||||||
|
|
||||||
_, _ = config.clients.AddHost(ip, host, ClientSourceRDNS)
|
_, _ = config.clients.AddHost(ip, host, ClientSourceRDNS)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package home
|
package home
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/golibs/cache"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,10 +21,13 @@ const (
|
|||||||
// Whois - module context
|
// Whois - module context
|
||||||
type Whois struct {
|
type Whois struct {
|
||||||
clients *clientsContainer
|
clients *clientsContainer
|
||||||
ips map[string]bool
|
|
||||||
lock sync.Mutex
|
|
||||||
ipChan chan string
|
ipChan chan string
|
||||||
timeoutMsec uint
|
timeoutMsec uint
|
||||||
|
|
||||||
|
// Contains IP addresses of clients
|
||||||
|
// An active IP address is resolved once again after it expires.
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create module context
|
// Create module context
|
||||||
@ -31,7 +35,12 @@ func initWhois(clients *clientsContainer) *Whois {
|
|||||||
w := Whois{}
|
w := Whois{}
|
||||||
w.timeoutMsec = 5000
|
w.timeoutMsec = 5000
|
||||||
w.clients = clients
|
w.clients = clients
|
||||||
w.ips = make(map[string]bool)
|
|
||||||
|
cconf := cache.Config{}
|
||||||
|
cconf.EnableLRU = true
|
||||||
|
cconf.MaxCount = 10000
|
||||||
|
w.ipAddrs = cache.New(cconf)
|
||||||
|
|
||||||
w.ipChan = make(chan string, 255)
|
w.ipChan = make(chan string, 255)
|
||||||
go w.workerLoop()
|
go w.workerLoop()
|
||||||
return &w
|
return &w
|
||||||
@ -186,14 +195,19 @@ func (w *Whois) process(ip string) [][]string {
|
|||||||
|
|
||||||
// Begin - begin requesting WHOIS info
|
// Begin - begin requesting WHOIS info
|
||||||
func (w *Whois) Begin(ip string) {
|
func (w *Whois) Begin(ip string) {
|
||||||
w.lock.Lock()
|
now := uint64(time.Now().Unix())
|
||||||
_, found := w.ips[ip]
|
expire := w.ipAddrs.Get([]byte(ip))
|
||||||
if found {
|
if len(expire) != 0 {
|
||||||
w.lock.Unlock()
|
exp := binary.BigEndian.Uint64(expire)
|
||||||
return
|
if exp > now {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// TTL expired
|
||||||
}
|
}
|
||||||
w.ips[ip] = true
|
expire = make([]byte, 8)
|
||||||
w.lock.Unlock()
|
const ttl = 12 * 60 * 60
|
||||||
|
binary.BigEndian.PutUint64(expire, now+ttl)
|
||||||
|
_ = w.ipAddrs.Set([]byte(ip), expire)
|
||||||
|
|
||||||
log.Debug("Whois: adding %s", ip)
|
log.Debug("Whois: adding %s", ip)
|
||||||
select {
|
select {
|
||||||
|
Loading…
Reference in New Issue
Block a user