mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 13:05:36 +03:00
ff04b2a7d3
Merge in DNS/adguard-home from 4299-stats-ignore to master Updates #1717. Updates #4299. Squashed commit of the following: commit 1d1212d088c944e995deae2fd599eccb0a075033 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Feb 13 17:53:36 2023 +0300 fix changelog commit 5f56852c21d794bd87c13192d3857757be10f9b2 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Feb 13 17:39:02 2023 +0300 add todo; fix data race commit 89b8b16ddf5a43ebf68174cbaf9e8a53365f8cbe Merge: e0a6bb49ec19a85e
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Feb 10 17:21:38 2023 +0300 Merge branch 'master' into 4299-stats-ignore commit e0a6bb490b651d1cf31589a7f17095fff4cb4dbb Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Feb 10 17:21:06 2023 +0300 interval under mutex commit c569c7bc237f11b23fe47c98a20a1c5cb36751cb Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Feb 10 16:19:35 2023 +0300 fix mutex commit 9374cf0c54dccc2fbfc38765b52c64e1c479137c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Feb 10 16:03:17 2023 +0300 fix typo commit 1f4fd1e7ab1b3c2f8e9c3d32ef7e4958f99abb47 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Feb 10 15:55:44 2023 +0300 add mutex commit 2148048ce9ad228381cbb51a806c9b9cc21458fd Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Feb 10 12:27:36 2023 +0300 add key check commit a19350977c463f888aea70d0dace26dff0173a65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 9 18:34:36 2023 +0300 fix changelog commit 23c3b6da162dfd513884b460c265ba4cafeb9727 Merge: 8fccc0b8b89105e3
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 9 13:28:59 2023 +0300 Merge branch 'master' into 4299-stats-ignore commit 8fccc0b8ec670a37e5209d795f35c43dd64afeb3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 9 13:27:42 2023 +0300 add changelog commit 0416c71742795b2fb8adb0173dcd6a99d9d9c676 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 8 14:31:55 2023 +0300 all: stats ignore
143 lines
3.5 KiB
Go
143 lines
3.5 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
"time"
|
|
|
|
"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) {
|
|
elapsed := time.Since(dctx.startTime)
|
|
pctx := dctx.proxyCtx
|
|
|
|
shouldLog := true
|
|
msg := pctx.Req
|
|
q := msg.Question[0]
|
|
host := strings.ToLower(strings.TrimSuffix(q.Name, "."))
|
|
|
|
// don't log ANY request if refuseAny is enabled
|
|
if q.Qtype == dns.TypeANY && s.conf.RefuseAny {
|
|
shouldLog = false
|
|
}
|
|
|
|
ip, _ := netutil.IPAndPortFromAddr(pctx.Addr)
|
|
ip = slices.Clone(ip)
|
|
|
|
s.serverLock.RLock()
|
|
defer s.serverLock.RUnlock()
|
|
|
|
s.anonymizer.Load()(ip)
|
|
|
|
log.Debug("client ip: %s", ip)
|
|
|
|
// 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.
|
|
if shouldLog &&
|
|
s.queryLog != nil &&
|
|
s.queryLog.ShouldLog(host, q.Qtype, q.Qclass) {
|
|
s.logQuery(dctx, pctx, elapsed, ip)
|
|
} else {
|
|
log.Debug(
|
|
"dnsforward: request %s %s from %s ignored; not logging",
|
|
dns.Type(q.Qtype),
|
|
host,
|
|
ip,
|
|
)
|
|
}
|
|
|
|
if s.stats != nil && s.stats.ShouldCount(host, q.Qtype, q.Qclass) {
|
|
s.updateStats(dctx, elapsed, *dctx.result, ip)
|
|
}
|
|
|
|
return resultCodeSuccess
|
|
}
|
|
|
|
// 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 net.IP,
|
|
) {
|
|
pctx := ctx.proxyCtx
|
|
e := stats.Entry{}
|
|
e.Domain = strings.ToLower(pctx.Req.Question[0].Name)
|
|
e.Domain = e.Domain[:len(e.Domain)-1] // remove last "."
|
|
|
|
if clientID := ctx.clientID; clientID != "" {
|
|
e.Client = clientID
|
|
} else if clientIP != nil {
|
|
e.Client = clientIP.String()
|
|
}
|
|
|
|
e.Time = uint32(elapsed / 1000)
|
|
e.Result = stats.RNotFiltered
|
|
|
|
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)
|
|
}
|