mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 18:08:51 +03:00
c47509fabc
Updates #1453. Squashed commit of the following: commit f08f68ef5493dad03d3eb120d886f2df1af28be6 Merge: b70b088af54aee2272
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 8 19:04:06 2023 +0300 Merge branch 'master' into 1453-stats-tests commit b70b088af0fdc7d6d048d688160048bad1fceb12 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Aug 3 19:32:04 2023 +0300 stats: imp code commit c341012ba61894c255c1868624be1cac0d26a6fa Merge: a2ac8c34e5eb3cd0f9
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Aug 3 13:36:24 2023 +0300 Merge branch 'master' into 1453-stats-tests commit a2ac8c34ee32606ca5e259c3e2a47db0dd5858de Author: Ildar Kamalov <ik@adguard.com> Date: Thu Aug 3 13:25:12 2023 +0300 client: add top upstreams and average processing time tables commit 11118947f9bf945be0b056f8475cf3b848c6e66e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 1 17:24:57 2023 +0300 stats: imp docs commit 904cf81d02a1f327b9647fa7ad9e181cfabb68a4 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jul 31 17:34:06 2023 +0300 stats: imp code commit 34f0c96dd5865d1470385322a88842dd0b3d996d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jul 31 15:43:46 2023 +0300 all: imp docs commit 2cb2d0d8bef3580f64bc25c414fe9b5ea6b9f997 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 28 17:24:31 2023 +0300 all: imp code commit 5251a899fecc21e50a0ba06042f96f5b404e196a Merge: b6c2b12d4300821a7f
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 27 20:34:39 2023 +0300 Merge branch 'master' into 1453-stats-tests commit b6c2b12d4425012efd73549c3a426735f3a677cd Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 27 20:32:18 2023 +0300 stats: imp code commit 5546b82a78326f9cc6d8c87df5083f8fc66a0178 Merge: 8a3d6b1b45f8fa006c
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 27 14:24:01 2023 +0300 Merge branch 'master' into 1453-stats-tests commit 8a3d6b1b49ce189f95adfa7406a34108e885e676 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 27 14:17:47 2023 +0300 all: imp code commit 2a48001e275e3cdcf70e13e1c9cebd4e502f3259 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 25 18:27:20 2023 +0300 all: imp docs commit 3dd21890175af32a3368378f7e013383f6d040ec Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 25 16:00:39 2023 +0300 all: imp naming commit 6124456fc3149b71f6bd58d35ecf24eb6cf40d5d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 20 16:15:56 2023 +0300 all: add upstreams avg processing time commit 187ad0c77a81c9fd95c24e23141355db2e83e50d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 18 16:42:19 2023 +0300 all: add top upstreams
173 lines
4.5 KiB
Go
173 lines
4.5 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/miekg/dns"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
// Write Stats data and logs
|
|
func (s *Server) processQueryLogsAndStats(dctx *dnsContext) (rc resultCode) {
|
|
log.Debug("dnsforward: started processing querylog and stats")
|
|
defer log.Debug("dnsforward: finished processing querylog and stats")
|
|
|
|
elapsed := time.Since(dctx.startTime)
|
|
pctx := dctx.proxyCtx
|
|
|
|
q := pctx.Req.Question[0]
|
|
host := aghnet.NormalizeDomain(q.Name)
|
|
|
|
ip, _ := netutil.IPAndPortFromAddr(pctx.Addr)
|
|
ip = slices.Clone(ip)
|
|
s.anonymizer.Load()(ip)
|
|
|
|
log.Debug("dnsforward: client ip for stats and querylog: %s", ip)
|
|
|
|
ipStr := ip.String()
|
|
ids := []string{ipStr, dctx.clientID}
|
|
qt, cl := q.Qtype, q.Qclass
|
|
|
|
// Synchronize access to s.queryLog and s.stats so they won't be suddenly
|
|
// uninitialized while in use. This can happen after proxy server has been
|
|
// stopped, but its workers haven't yet exited.
|
|
s.serverLock.RLock()
|
|
defer s.serverLock.RUnlock()
|
|
|
|
if s.shouldLog(host, qt, cl, ids) {
|
|
s.logQuery(dctx, pctx, elapsed, ip)
|
|
} else {
|
|
log.Debug(
|
|
"dnsforward: request %s %s %q from %s ignored; not adding to querylog",
|
|
dns.Class(cl),
|
|
dns.Type(qt),
|
|
host,
|
|
ip,
|
|
)
|
|
}
|
|
|
|
if s.shouldCountStat(host, qt, cl, ids) {
|
|
s.updateStats(dctx, elapsed, *dctx.result, ipStr)
|
|
} else {
|
|
log.Debug(
|
|
"dnsforward: request %s %s %q from %s ignored; not counting in stats",
|
|
dns.Class(cl),
|
|
dns.Type(qt),
|
|
host,
|
|
ip,
|
|
)
|
|
}
|
|
|
|
return resultCodeSuccess
|
|
}
|
|
|
|
// shouldLog returns true if the query with the given data should be logged in
|
|
// the query log. s.serverLock is expected to be locked.
|
|
func (s *Server) shouldLog(host string, qt, cl uint16, ids []string) (ok bool) {
|
|
if qt == dns.TypeANY && s.conf.RefuseAny {
|
|
return false
|
|
}
|
|
|
|
// TODO(s.chzhen): Use dnsforward.dnsContext when it will start containing
|
|
// persistent client.
|
|
return s.queryLog != nil && s.queryLog.ShouldLog(host, qt, cl, ids)
|
|
}
|
|
|
|
// shouldCountStat returns true if the query with the given data should be
|
|
// counted in the statistics. s.serverLock is expected to be locked.
|
|
func (s *Server) shouldCountStat(host string, qt, cl uint16, ids []string) (ok bool) {
|
|
// TODO(s.chzhen): Use dnsforward.dnsContext when it will start containing
|
|
// persistent client.
|
|
return s.stats != nil && s.stats.ShouldCount(host, qt, cl, ids)
|
|
}
|
|
|
|
// logQuery pushes the request details into the query log.
|
|
func (s *Server) logQuery(
|
|
dctx *dnsContext,
|
|
pctx *proxy.DNSContext,
|
|
elapsed time.Duration,
|
|
ip net.IP,
|
|
) {
|
|
p := &querylog.AddParams{
|
|
Question: pctx.Req,
|
|
ReqECS: pctx.ReqECS,
|
|
Answer: pctx.Res,
|
|
OrigAnswer: dctx.origResp,
|
|
Result: dctx.result,
|
|
ClientID: dctx.clientID,
|
|
ClientIP: ip,
|
|
Elapsed: elapsed,
|
|
AuthenticatedData: dctx.responseAD,
|
|
}
|
|
|
|
switch pctx.Proto {
|
|
case proxy.ProtoHTTPS:
|
|
p.ClientProto = querylog.ClientProtoDoH
|
|
case proxy.ProtoQUIC:
|
|
p.ClientProto = querylog.ClientProtoDoQ
|
|
case proxy.ProtoTLS:
|
|
p.ClientProto = querylog.ClientProtoDoT
|
|
case proxy.ProtoDNSCrypt:
|
|
p.ClientProto = querylog.ClientProtoDNSCrypt
|
|
default:
|
|
// Consider this a plain DNS-over-UDP or DNS-over-TCP request.
|
|
}
|
|
|
|
if pctx.Upstream != nil {
|
|
p.Upstream = pctx.Upstream.Address()
|
|
} else if cachedUps := pctx.CachedUpstreamAddr; cachedUps != "" {
|
|
p.Upstream = pctx.CachedUpstreamAddr
|
|
p.Cached = true
|
|
}
|
|
|
|
s.queryLog.Add(p)
|
|
}
|
|
|
|
// updatesStats writes the request into statistics.
|
|
func (s *Server) updateStats(
|
|
ctx *dnsContext,
|
|
elapsed time.Duration,
|
|
res filtering.Result,
|
|
clientIP string,
|
|
) {
|
|
pctx := ctx.proxyCtx
|
|
e := &stats.Entry{
|
|
Domain: aghnet.NormalizeDomain(pctx.Req.Question[0].Name),
|
|
Result: stats.RNotFiltered,
|
|
Time: elapsed,
|
|
}
|
|
|
|
if pctx.Upstream != nil {
|
|
e.Upstream = pctx.Upstream.Address()
|
|
}
|
|
|
|
if clientID := ctx.clientID; clientID != "" {
|
|
e.Client = clientID
|
|
} else {
|
|
e.Client = clientIP
|
|
}
|
|
|
|
switch res.Reason {
|
|
case filtering.FilteredSafeBrowsing:
|
|
e.Result = stats.RSafeBrowsing
|
|
case filtering.FilteredParental:
|
|
e.Result = stats.RParental
|
|
case filtering.FilteredSafeSearch:
|
|
e.Result = stats.RSafeSearch
|
|
case
|
|
filtering.FilteredBlockList,
|
|
filtering.FilteredInvalid,
|
|
filtering.FilteredBlockedService:
|
|
e.Result = stats.RFiltered
|
|
}
|
|
|
|
s.stats.Update(e)
|
|
}
|