2020-05-08 18:39:37 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2021-12-06 17:26:43 +03:00
|
|
|
"net"
|
2020-05-08 18:39:37 +03:00
|
|
|
"time"
|
|
|
|
|
2023-07-11 20:42:40 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-11-03 15:39:55 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
2020-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
2020-05-08 18:39:37 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-12-06 17:26:43 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 16:03:37 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2020-11-03 15:39:55 +03:00
|
|
|
"github.com/miekg/dns"
|
2022-10-21 20:42:00 +03:00
|
|
|
"golang.org/x/exp/slices"
|
2020-05-08 18:39:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Write Stats data and logs
|
2021-12-13 18:06:01 +03:00
|
|
|
func (s *Server) processQueryLogsAndStats(dctx *dnsContext) (rc resultCode) {
|
2023-06-28 13:46:04 +03:00
|
|
|
log.Debug("dnsforward: started processing querylog and stats")
|
|
|
|
defer log.Debug("dnsforward: finished processing querylog and stats")
|
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
elapsed := time.Since(dctx.startTime)
|
|
|
|
pctx := dctx.proxyCtx
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2023-06-28 13:46:04 +03:00
|
|
|
q := pctx.Req.Question[0]
|
2023-07-11 20:42:40 +03:00
|
|
|
host := aghnet.NormalizeDomain(q.Name)
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2021-12-07 17:43:51 +03:00
|
|
|
ip, _ := netutil.IPAndPortFromAddr(pctx.Addr)
|
2022-10-21 20:42:00 +03:00
|
|
|
ip = slices.Clone(ip)
|
2021-12-06 17:26:43 +03:00
|
|
|
s.anonymizer.Load()(ip)
|
|
|
|
|
2023-06-28 13:46:04 +03:00
|
|
|
log.Debug("dnsforward: client ip for stats and querylog: %s", ip)
|
2021-12-06 17:26:43 +03:00
|
|
|
|
2023-04-07 13:17:40 +03:00
|
|
|
ipStr := ip.String()
|
|
|
|
ids := []string{ipStr, dctx.clientID}
|
2023-06-28 13:46:04 +03:00
|
|
|
qt, cl := q.Qtype, q.Qclass
|
2023-04-07 13:17:40 +03:00
|
|
|
|
2021-12-06 17:26:43 +03:00
|
|
|
// 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.
|
2023-06-28 13:46:04 +03:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
|
|
|
if s.shouldLog(host, qt, cl, ids) {
|
2022-03-03 17:52:11 +03:00
|
|
|
s.logQuery(dctx, pctx, elapsed, ip)
|
2023-02-07 17:50:39 +03:00
|
|
|
} else {
|
2023-02-13 18:15:33 +03:00
|
|
|
log.Debug(
|
2023-06-28 13:46:04 +03:00
|
|
|
"dnsforward: request %s %s %q from %s ignored; not adding to querylog",
|
|
|
|
dns.Class(cl),
|
|
|
|
dns.Type(qt),
|
2023-02-13 18:15:33 +03:00
|
|
|
host,
|
|
|
|
ip,
|
|
|
|
)
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2023-06-28 13:46:04 +03:00
|
|
|
if s.shouldCountStat(host, qt, cl, ids) {
|
2023-04-07 13:17:40 +03:00
|
|
|
s.updateStats(dctx, elapsed, *dctx.result, ipStr)
|
2023-06-28 13:46:04 +03:00
|
|
|
} else {
|
|
|
|
log.Debug(
|
|
|
|
"dnsforward: request %s %s %q from %s ignored; not counting in stats",
|
|
|
|
dns.Class(cl),
|
|
|
|
dns.Type(qt),
|
|
|
|
host,
|
|
|
|
ip,
|
|
|
|
)
|
2022-03-03 17:52:11 +03:00
|
|
|
}
|
2020-05-08 18:39:37 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return resultCodeSuccess
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
|
|
|
|
2023-06-28 13:46:04 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-03-03 17:52:11 +03:00
|
|
|
// 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,
|
2022-05-25 18:00:50 +03:00
|
|
|
Elapsed: elapsed,
|
2022-03-03 17:52:11 +03:00
|
|
|
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.
|
2021-12-06 17:26:43 +03:00
|
|
|
func (s *Server) updateStats(
|
|
|
|
ctx *dnsContext,
|
|
|
|
elapsed time.Duration,
|
|
|
|
res filtering.Result,
|
2023-04-07 13:17:40 +03:00
|
|
|
clientIP string,
|
2021-12-06 17:26:43 +03:00
|
|
|
) {
|
2021-01-27 18:32:13 +03:00
|
|
|
pctx := ctx.proxyCtx
|
2023-08-09 14:33:52 +03:00
|
|
|
e := &stats.Entry{
|
2023-07-11 20:42:40 +03:00
|
|
|
Domain: aghnet.NormalizeDomain(pctx.Req.Question[0].Name),
|
|
|
|
Result: stats.RNotFiltered,
|
2023-08-09 14:33:52 +03:00
|
|
|
Time: elapsed,
|
|
|
|
}
|
|
|
|
|
|
|
|
if pctx.Upstream != nil {
|
|
|
|
e.Upstream = pctx.Upstream.Address()
|
2023-06-20 10:36:20 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
if clientID := ctx.clientID; clientID != "" {
|
|
|
|
e.Client = clientID
|
2023-04-07 13:17:40 +03:00
|
|
|
} else {
|
|
|
|
e.Client = clientIP
|
2020-05-08 18:39:37 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2020-05-08 18:39:37 +03:00
|
|
|
switch res.Reason {
|
2021-05-21 16:15:47 +03:00
|
|
|
case filtering.FilteredSafeBrowsing:
|
2020-05-08 18:39:37 +03:00
|
|
|
e.Result = stats.RSafeBrowsing
|
2021-05-21 16:15:47 +03:00
|
|
|
case filtering.FilteredParental:
|
2020-05-08 18:39:37 +03:00
|
|
|
e.Result = stats.RParental
|
2021-05-21 16:15:47 +03:00
|
|
|
case filtering.FilteredSafeSearch:
|
2020-05-08 18:39:37 +03:00
|
|
|
e.Result = stats.RSafeSearch
|
2023-07-11 20:42:40 +03:00
|
|
|
case
|
|
|
|
filtering.FilteredBlockList,
|
2021-05-21 18:30:57 +03:00
|
|
|
filtering.FilteredInvalid,
|
|
|
|
filtering.FilteredBlockedService:
|
2020-05-08 18:39:37 +03:00
|
|
|
e.Result = stats.RFiltered
|
|
|
|
}
|
|
|
|
|
|
|
|
s.stats.Update(e)
|
|
|
|
}
|