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