mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-17 13:11:42 +03:00
434032f8e3
Merge in DNS/adguard-home from 3772-cache-upstreams to master
Updates #3772.
Squashed commit of the following:
commit 809e874fe8847fdbd7f8a7221f18393509f41339
Merge: 5774798c 2d328ea8
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Dec 7 17:15:59 2021 +0300
Merge branch 'master' into 3772-cache-upstreams
commit 5774798ce1897bf292be49410794ce06cd084f93
Author: Ildar Kamalov <ik@adguard.com>
Date: Tue Dec 7 16:48:38 2021 +0300
client: fix formatting
commit 4b4bb668c762c9b4d47cf40a007f34fe47a6e260
Author: Ildar Kamalov <ik@adguard.com>
Date: Tue Dec 7 16:47:18 2021 +0300
client: remove comment
commit a2a9f904a6ab52194bb811a2a83967660a25f2cc
Author: Ildar Kamalov <ik@adguard.com>
Date: Tue Dec 7 16:45:31 2021 +0300
client: handle cached upstream
commit a56804c4910da838cb10c8ef0af2a525e9722b1c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Fri Nov 26 15:33:43 2021 +0300
querylog: rm todo
commit 16c0a62ad2dfc7fcb5a6adbc868b296b7840fd27
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Fri Nov 26 15:31:53 2021 +0300
all: revise log of changes
commit 9f4b793205f17b6e85d74e83126093ed09eb24e3
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Nov 25 17:23:42 2021 +0300
WIP
commit 7fbc6d596295570d2c61c3e507160f65c9adccb8
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Nov 25 17:03:42 2021 +0300
all: imp upstream info in querylog
121 lines
2.9 KiB
Go
121 lines
2.9 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"
|
|
)
|
|
|
|
// Write Stats data and logs
|
|
func (s *Server) processQueryLogsAndStats(ctx *dnsContext) (rc resultCode) {
|
|
elapsed := time.Since(ctx.startTime)
|
|
pctx := ctx.proxyCtx
|
|
|
|
shouldLog := true
|
|
msg := pctx.Req
|
|
|
|
// don't log ANY request if refuseAny is enabled
|
|
if len(msg.Question) >= 1 && msg.Question[0].Qtype == dns.TypeANY && s.conf.RefuseAny {
|
|
shouldLog = false
|
|
}
|
|
|
|
ip, _ := netutil.IPAndPortFromAddr(pctx.Addr)
|
|
ip = netutil.CloneIP(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 {
|
|
p := querylog.AddParams{
|
|
Question: msg,
|
|
Answer: pctx.Res,
|
|
OrigAnswer: ctx.origResp,
|
|
Result: ctx.result,
|
|
Elapsed: elapsed,
|
|
ClientIP: ip,
|
|
ClientID: ctx.clientID,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
s.updateStats(ctx, elapsed, *ctx.result, ip)
|
|
|
|
return resultCodeSuccess
|
|
}
|
|
|
|
func (s *Server) updateStats(
|
|
ctx *dnsContext,
|
|
elapsed time.Duration,
|
|
res filtering.Result,
|
|
clientIP net.IP,
|
|
) {
|
|
if s.stats == nil {
|
|
return
|
|
}
|
|
|
|
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)
|
|
}
|