mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-05-03 14:32:56 +03:00
Pull request 1878: AG-22597-imp-rdns
Squashed commit of the following: commit ccad155c34989943d88a0a260c50845d1f4ece6b Merge: 0cd889f6a5a195b441
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 6 17:00:58 2023 +0300 Merge branch 'master' into AG-22597-imp-rdns commit 0cd889f6a500f5616af0f8d8fdcde0403b87ad4f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 6 12:20:49 2023 +0300 dnsforward: imp code commit 1aaa1998b914b0d53142c21fa3bdcae502e4f3f6 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 4 20:11:55 2023 +0300 home: add todo commit aed232fcf70ef546f373d5235b73abcb4fbb4b6c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 4 13:25:28 2023 +0300 all: imp code, tests commit 5c028c2766ffb8ebdc358a245a249c6a55d9ad81 Merge: 83d6ae7f697af062f7
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jul 3 18:54:42 2023 +0300 Merge branch 'master' into AG-22597-imp-rdns commit 83d6ae7f61a7b81a8d73cd6d747035278c64fb70 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jul 3 18:53:05 2023 +0300 home: imp code commit 8153988dece0406e51a90a43eaffae59dba30a36 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jun 30 18:06:09 2023 +0300 all: imp code commit 00d3cc11a9378318f176aae00ddf972f255d575c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jun 30 13:05:04 2023 +0300 all: add tests commit ffdc95f237bfdb780922b4390d82cdc0154b0621 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jun 29 15:20:00 2023 +0300 all: imp code, docs commit 0dc60e2b355750ca701558927d22fb9ad187ea7e Merge: 69dd56bdbd4a4bda64
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jun 29 15:13:19 2023 +0300 Merge branch 'master' into AG-22597-imp-rdns commit 69dd56bdb75056b0fa6bcf6538af7fff93383323 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jun 23 14:36:29 2023 +0300 rdns: add tests commit 16909b51adbe3a3f230291834cc9486dd8a0e8f8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jun 19 16:28:26 2023 +0300 rdns: extract rdns
This commit is contained in:
parent
5a195b441c
commit
c21f958eaf
9 changed files with 389 additions and 458 deletions
internal/home
|
@ -17,6 +17,7 @@ import (
|
|||
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/rdns"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
||||
"github.com/AdguardTeam/dnsproxy/proxy"
|
||||
|
@ -167,30 +168,77 @@ func initDNSServer(
|
|||
return fmt.Errorf("dnsServer.Prepare: %w", err)
|
||||
}
|
||||
|
||||
if config.Clients.Sources.RDNS {
|
||||
Context.rdns = NewRDNS(Context.dnsServer, &Context.clients, config.DNS.UsePrivateRDNS)
|
||||
}
|
||||
|
||||
initRDNS()
|
||||
initWHOIS()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
const (
|
||||
// defaultQueueSize is the size of queue of IPs for rDNS and WHOIS
|
||||
// processing.
|
||||
defaultQueueSize = 255
|
||||
|
||||
// defaultCacheSize is the maximum size of the cache for rDNS and WHOIS
|
||||
// processing. It must be greater than zero.
|
||||
defaultCacheSize = 10_000
|
||||
|
||||
// defaultIPTTL is the Time to Live duration for IP addresses cached by
|
||||
// rDNS and WHOIS.
|
||||
defaultIPTTL = 1 * time.Hour
|
||||
)
|
||||
|
||||
// initRDNS initializes the rDNS.
|
||||
func initRDNS() {
|
||||
Context.rdnsCh = make(chan netip.Addr, defaultQueueSize)
|
||||
|
||||
// TODO(s.chzhen): Add ability to disable it on dns server configuration
|
||||
// update in [dnsforward] package.
|
||||
r := rdns.New(&rdns.Config{
|
||||
Exchanger: Context.dnsServer,
|
||||
CacheSize: defaultCacheSize,
|
||||
CacheTTL: defaultIPTTL,
|
||||
})
|
||||
|
||||
go processRDNS(r)
|
||||
}
|
||||
|
||||
// processRDNS processes reverse DNS lookup queries. It is intended to be used
|
||||
// as a goroutine.
|
||||
func processRDNS(r rdns.Interface) {
|
||||
defer log.OnPanic("rdns")
|
||||
|
||||
for ip := range Context.rdnsCh {
|
||||
ok := Context.dnsServer.ShouldResolveClient(ip)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
host, changed := r.Process(ip)
|
||||
if host == "" || !changed {
|
||||
continue
|
||||
}
|
||||
|
||||
ok = Context.clients.AddHost(ip, host, ClientSourceRDNS)
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debug(
|
||||
"dns: can't set rdns info for client %q: already set with higher priority source",
|
||||
ip,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// initWHOIS initializes the WHOIS.
|
||||
//
|
||||
// TODO(s.chzhen): Consider making configurable.
|
||||
func initWHOIS() {
|
||||
const (
|
||||
// defaultQueueSize is the size of queue of IPs for WHOIS processing.
|
||||
defaultQueueSize = 255
|
||||
|
||||
// defaultTimeout is the timeout for WHOIS requests.
|
||||
defaultTimeout = 5 * time.Second
|
||||
|
||||
// defaultCacheSize is the maximum size of the cache. If it's zero,
|
||||
// cache size is unlimited.
|
||||
defaultCacheSize = 10_000
|
||||
|
||||
// defaultMaxConnReadSize is an upper limit in bytes for reading from
|
||||
// net.Conn.
|
||||
defaultMaxConnReadSize = 64 * 1024
|
||||
|
@ -200,9 +248,6 @@ func initWHOIS() {
|
|||
|
||||
// defaultMaxInfoLen is the maximum length of whois.Info fields.
|
||||
defaultMaxInfoLen = 250
|
||||
|
||||
// defaultIPTTL is the Time to Live duration for cached IP addresses.
|
||||
defaultIPTTL = 1 * time.Hour
|
||||
)
|
||||
|
||||
Context.whoisCh = make(chan netip.Addr, defaultQueueSize)
|
||||
|
@ -274,11 +319,7 @@ func onDNSRequest(pctx *proxy.DNSContext) {
|
|||
return
|
||||
}
|
||||
|
||||
srcs := config.Clients.Sources
|
||||
if srcs.RDNS && !ip.IsLoopback() {
|
||||
Context.rdns.Begin(ip)
|
||||
}
|
||||
|
||||
Context.rdnsCh <- ip
|
||||
Context.whoisCh <- ip
|
||||
}
|
||||
|
||||
|
@ -517,11 +558,7 @@ func startDNSServer() error {
|
|||
|
||||
const topClientsNumber = 100 // the number of clients to get
|
||||
for _, ip := range Context.stats.TopClientsIP(topClientsNumber) {
|
||||
srcs := config.Clients.Sources
|
||||
if srcs.RDNS && !ip.IsLoopback() {
|
||||
Context.rdns.Begin(ip)
|
||||
}
|
||||
|
||||
Context.rdnsCh <- ip
|
||||
Context.whoisCh <- ip
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue