2020-05-08 15:39:37 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2021-12-06 14:26:43 +00:00
|
|
|
"net"
|
2020-05-08 15:39:37 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-05-21 13:15:47 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-11-03 12:39:55 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
2020-05-08 15:39:37 +00:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-12-06 14:26:43 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 13:03:37 +00:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2020-11-03 12:39:55 +00:00
|
|
|
"github.com/miekg/dns"
|
2020-05-08 15:39:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Write Stats data and logs
|
2021-11-15 14:42:10 +00:00
|
|
|
func (s *Server) processQueryLogsAndStats(ctx *dnsContext) (rc resultCode) {
|
2020-05-08 15:39:37 +00:00
|
|
|
elapsed := time.Since(ctx.startTime)
|
2021-01-27 15:32:13 +00:00
|
|
|
pctx := ctx.proxyCtx
|
2020-05-08 15:39:37 +00:00
|
|
|
|
|
|
|
shouldLog := true
|
2021-01-27 15:32:13 +00:00
|
|
|
msg := pctx.Req
|
2020-05-08 15:39:37 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-12-07 14:43:51 +00:00
|
|
|
ip, _ := netutil.IPAndPortFromAddr(pctx.Addr)
|
|
|
|
ip = netutil.CloneIP(ip)
|
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
s.serverLock.RLock()
|
|
|
|
defer s.serverLock.RUnlock()
|
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
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.
|
2020-05-08 15:39:37 +00:00
|
|
|
if shouldLog && s.queryLog != nil {
|
|
|
|
p := querylog.AddParams{
|
|
|
|
Question: msg,
|
2021-01-27 15:32:13 +00:00
|
|
|
Answer: pctx.Res,
|
2020-05-08 15:39:37 +00:00
|
|
|
OrigAnswer: ctx.origResp,
|
|
|
|
Result: ctx.result,
|
|
|
|
Elapsed: elapsed,
|
2021-08-09 13:03:37 +00:00
|
|
|
ClientIP: ip,
|
2021-01-27 15:32:13 +00:00
|
|
|
ClientID: ctx.clientID,
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
2020-05-29 08:15:22 +00:00
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
switch pctx.Proto {
|
2020-11-03 12:39:55 +00:00
|
|
|
case proxy.ProtoHTTPS:
|
2021-06-18 15:13:36 +00:00
|
|
|
p.ClientProto = querylog.ClientProtoDoH
|
2020-11-03 12:39:55 +00:00
|
|
|
case proxy.ProtoQUIC:
|
2021-06-18 15:13:36 +00:00
|
|
|
p.ClientProto = querylog.ClientProtoDoQ
|
2020-11-03 12:39:55 +00:00
|
|
|
case proxy.ProtoTLS:
|
2021-06-18 15:13:36 +00:00
|
|
|
p.ClientProto = querylog.ClientProtoDoT
|
2021-01-27 15:32:13 +00:00
|
|
|
case proxy.ProtoDNSCrypt:
|
|
|
|
p.ClientProto = querylog.ClientProtoDNSCrypt
|
2020-11-03 12:39:55 +00:00
|
|
|
default:
|
2021-12-07 14:43:51 +00:00
|
|
|
// Consider this a plain DNS-over-UDP or DNS-over-TCP request.
|
2020-05-29 08:15:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
if pctx.Upstream != nil {
|
|
|
|
p.Upstream = pctx.Upstream.Address()
|
2021-12-07 14:43:51 +00:00
|
|
|
} else if cachedUps := pctx.CachedUpstreamAddr; cachedUps != "" {
|
|
|
|
p.Upstream = pctx.CachedUpstreamAddr
|
|
|
|
p.Cached = true
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
s.queryLog.Add(p)
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
s.updateStats(ctx, elapsed, *ctx.result, ip)
|
2020-05-08 15:39:37 +00:00
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
return resultCodeSuccess
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
func (s *Server) updateStats(
|
|
|
|
ctx *dnsContext,
|
|
|
|
elapsed time.Duration,
|
|
|
|
res filtering.Result,
|
|
|
|
clientIP net.IP,
|
|
|
|
) {
|
2020-05-08 15:39:37 +00:00
|
|
|
if s.stats == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
pctx := ctx.proxyCtx
|
2020-05-08 15:39:37 +00:00
|
|
|
e := stats.Entry{}
|
2021-01-27 15:32:13 +00:00
|
|
|
e.Domain = strings.ToLower(pctx.Req.Question[0].Name)
|
2020-05-08 15:39:37 +00:00
|
|
|
e.Domain = e.Domain[:len(e.Domain)-1] // remove last "."
|
2021-01-27 15:32:13 +00:00
|
|
|
|
|
|
|
if clientID := ctx.clientID; clientID != "" {
|
|
|
|
e.Client = clientID
|
2021-12-06 14:26:43 +00:00
|
|
|
} else if clientIP != nil {
|
|
|
|
e.Client = clientIP.String()
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
e.Time = uint32(elapsed / 1000)
|
|
|
|
e.Result = stats.RNotFiltered
|
|
|
|
|
|
|
|
switch res.Reason {
|
2021-05-21 13:15:47 +00:00
|
|
|
case filtering.FilteredSafeBrowsing:
|
2020-05-08 15:39:37 +00:00
|
|
|
e.Result = stats.RSafeBrowsing
|
2021-05-21 13:15:47 +00:00
|
|
|
case filtering.FilteredParental:
|
2020-05-08 15:39:37 +00:00
|
|
|
e.Result = stats.RParental
|
2021-05-21 13:15:47 +00:00
|
|
|
case filtering.FilteredSafeSearch:
|
2020-05-08 15:39:37 +00:00
|
|
|
e.Result = stats.RSafeSearch
|
2021-05-21 15:30:57 +00:00
|
|
|
case filtering.FilteredBlockList,
|
|
|
|
filtering.FilteredInvalid,
|
|
|
|
filtering.FilteredBlockedService:
|
2020-05-08 15:39:37 +00:00
|
|
|
e.Result = stats.RFiltered
|
|
|
|
}
|
|
|
|
|
|
|
|
s.stats.Update(e)
|
|
|
|
}
|