mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 04:55:33 +03:00
f9fe3172c4
Merge in DNS/adguard-home from 4299-querylog-stats-clients to master Squashed commit of the following: commit 33b80b67224f7c1a15bee8e6a23d9d5bab6ac629 Merge: 61964fdd5d5a7295
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 12:43:22 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit 61964fdd02221abbddedf2d6d02bb0bce6845362 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 12:42:01 2023 +0300 dnsforward: imp code commit 7382168500bab6ca7494d39aabfc2d7bfceb5d24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 11:13:07 2023 +0300 all: imp code, chlog commit c7852902f635af6c296dcb6735f7b0bfb83f4e87 Merge: aa4dc0a5a55cbbe7
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 6 14:34:24 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit aa4dc0a54e95bc5b24718ec158340b631a822801 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 6 12:54:02 2023 +0300 all: imp code commit dd541f0cd7ecbf0afcf10ccbd130fd1d1fa4c1c4 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 13:01:53 2023 +0300 querylog: fix typo commit d2c8fdb35b04d27c8957fa027882fde704cc07be Merge: 83d0baa12eb3bf6e
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 12:36:49 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit 83d0baa1f1202f9c62d4be2041d7aed12ee9ab2c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 12:35:15 2023 +0300 all: add tests commit a459f19f25cf9646d145813fe7834b2d9979c516 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 29 16:51:53 2023 +0300 all: add clients querylog stats ignore
270 lines
5.9 KiB
Go
270 lines
5.9 KiB
Go
// Package querylog provides query log functions and interfaces.
|
|
package querylog
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
const (
|
|
queryLogFileName = "querylog.json" // .gz added during compression
|
|
)
|
|
|
|
// queryLog is a structure that writes and reads the DNS query log
|
|
type queryLog struct {
|
|
findClient func(ids []string) (c *Client, err error)
|
|
|
|
// confMu protects conf.
|
|
confMu *sync.RWMutex
|
|
conf *Config
|
|
|
|
// logFile is the path to the log file.
|
|
logFile string
|
|
|
|
// bufferLock protects buffer.
|
|
bufferLock sync.RWMutex
|
|
// buffer contains recent log entries. The entries in this buffer must not
|
|
// be modified.
|
|
buffer []*logEntry
|
|
|
|
fileFlushLock sync.Mutex // synchronize a file-flushing goroutine and main thread
|
|
flushPending bool // don't start another goroutine while the previous one is still running
|
|
fileWriteLock sync.Mutex
|
|
|
|
anonymizer *aghnet.IPMut
|
|
}
|
|
|
|
// ClientProto values are names of the client protocols.
|
|
type ClientProto string
|
|
|
|
// Client protocol names.
|
|
const (
|
|
ClientProtoDoH ClientProto = "doh"
|
|
ClientProtoDoQ ClientProto = "doq"
|
|
ClientProtoDoT ClientProto = "dot"
|
|
ClientProtoDNSCrypt ClientProto = "dnscrypt"
|
|
ClientProtoPlain ClientProto = ""
|
|
)
|
|
|
|
// NewClientProto validates that the client protocol name is valid and returns
|
|
// the name as a ClientProto.
|
|
func NewClientProto(s string) (cp ClientProto, err error) {
|
|
switch cp = ClientProto(s); cp {
|
|
case
|
|
ClientProtoDoH,
|
|
ClientProtoDoQ,
|
|
ClientProtoDoT,
|
|
ClientProtoDNSCrypt,
|
|
ClientProtoPlain:
|
|
|
|
return cp, nil
|
|
default:
|
|
return "", fmt.Errorf("invalid client proto: %q", s)
|
|
}
|
|
}
|
|
|
|
func (l *queryLog) Start() {
|
|
if l.conf.HTTPRegister != nil {
|
|
l.initWeb()
|
|
}
|
|
|
|
go l.periodicRotate()
|
|
}
|
|
|
|
func (l *queryLog) Close() {
|
|
l.confMu.RLock()
|
|
defer l.confMu.RUnlock()
|
|
|
|
if l.conf.FileEnabled {
|
|
err := l.flushLogBuffer()
|
|
if err != nil {
|
|
log.Error("querylog: closing: %s", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkInterval(ivl time.Duration) (ok bool) {
|
|
// The constants for possible values of query log's rotation interval.
|
|
const (
|
|
quarterDay = timeutil.Day / 4
|
|
day = timeutil.Day
|
|
week = timeutil.Day * 7
|
|
month = timeutil.Day * 30
|
|
threeMonths = timeutil.Day * 90
|
|
)
|
|
|
|
return ivl == quarterDay || ivl == day || ivl == week || ivl == month || ivl == threeMonths
|
|
}
|
|
|
|
// validateIvl returns an error if ivl is less than an hour or more than a
|
|
// year.
|
|
func validateIvl(ivl time.Duration) (err error) {
|
|
if ivl < time.Hour {
|
|
return errors.Error("less than an hour")
|
|
}
|
|
|
|
if ivl > timeutil.Day*365 {
|
|
return errors.Error("more than a year")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *queryLog) WriteDiskConfig(c *Config) {
|
|
l.confMu.RLock()
|
|
defer l.confMu.RUnlock()
|
|
|
|
*c = *l.conf
|
|
c.Ignored = l.conf.Ignored.Clone()
|
|
}
|
|
|
|
// Clear memory buffer and remove log files
|
|
func (l *queryLog) clear() {
|
|
l.fileFlushLock.Lock()
|
|
defer l.fileFlushLock.Unlock()
|
|
|
|
func() {
|
|
l.bufferLock.Lock()
|
|
defer l.bufferLock.Unlock()
|
|
|
|
l.buffer = nil
|
|
l.flushPending = false
|
|
}()
|
|
|
|
oldLogFile := l.logFile + ".1"
|
|
err := os.Remove(oldLogFile)
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
log.Error("removing old log file %q: %s", oldLogFile, err)
|
|
}
|
|
|
|
err = os.Remove(l.logFile)
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
log.Error("removing log file %q: %s", l.logFile, err)
|
|
}
|
|
|
|
log.Debug("querylog: cleared")
|
|
}
|
|
|
|
func (l *queryLog) Add(params *AddParams) {
|
|
var isEnabled, fileIsEnabled bool
|
|
var memSize uint32
|
|
func() {
|
|
l.confMu.RLock()
|
|
defer l.confMu.RUnlock()
|
|
|
|
isEnabled, fileIsEnabled = l.conf.Enabled, l.conf.FileEnabled
|
|
memSize = l.conf.MemSize
|
|
}()
|
|
|
|
if !isEnabled {
|
|
return
|
|
}
|
|
|
|
err := params.validate()
|
|
if err != nil {
|
|
log.Error("querylog: adding record: %s, skipping", err)
|
|
|
|
return
|
|
}
|
|
|
|
if params.Result == nil {
|
|
params.Result = &filtering.Result{}
|
|
}
|
|
|
|
now := time.Now()
|
|
q := params.Question.Question[0]
|
|
entry := &logEntry{
|
|
Time: now,
|
|
|
|
QHost: strings.ToLower(q.Name[:len(q.Name)-1]),
|
|
QType: dns.Type(q.Qtype).String(),
|
|
QClass: dns.Class(q.Qclass).String(),
|
|
|
|
ClientID: params.ClientID,
|
|
ClientProto: params.ClientProto,
|
|
|
|
Result: *params.Result,
|
|
Upstream: params.Upstream,
|
|
|
|
IP: params.ClientIP,
|
|
|
|
Elapsed: params.Elapsed,
|
|
|
|
Cached: params.Cached,
|
|
AuthenticatedData: params.AuthenticatedData,
|
|
}
|
|
|
|
if params.ReqECS != nil {
|
|
entry.ReqECS = params.ReqECS.String()
|
|
}
|
|
|
|
entry.addResponse(params.Answer, false)
|
|
entry.addResponse(params.OrigAnswer, true)
|
|
|
|
needFlush := false
|
|
func() {
|
|
l.bufferLock.Lock()
|
|
defer l.bufferLock.Unlock()
|
|
|
|
l.buffer = append(l.buffer, entry)
|
|
|
|
if !fileIsEnabled {
|
|
if len(l.buffer) > int(memSize) {
|
|
// Writing to file is disabled, so just remove the oldest entry
|
|
// from the slices.
|
|
//
|
|
// TODO(a.garipov): This should be replaced by a proper ring
|
|
// buffer, but it's currently difficult to do that.
|
|
l.buffer[0] = nil
|
|
l.buffer = l.buffer[1:]
|
|
}
|
|
} else if !l.flushPending {
|
|
needFlush = len(l.buffer) >= int(memSize)
|
|
if needFlush {
|
|
l.flushPending = true
|
|
}
|
|
}
|
|
}()
|
|
|
|
if needFlush {
|
|
go func() {
|
|
flushErr := l.flushLogBuffer()
|
|
if flushErr != nil {
|
|
log.Error("querylog: flushing after adding: %s", err)
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
|
|
// ShouldLog returns true if request for the host should be logged.
|
|
func (l *queryLog) ShouldLog(host string, _, _ uint16, ids []string) bool {
|
|
l.confMu.RLock()
|
|
defer l.confMu.RUnlock()
|
|
|
|
c, err := l.findClient(ids)
|
|
if err != nil {
|
|
log.Error("querylog: finding client: %s", err)
|
|
}
|
|
|
|
if c != nil && c.IgnoreQueryLog {
|
|
return false
|
|
}
|
|
|
|
return !l.isIgnored(host)
|
|
}
|
|
|
|
// isIgnored returns true if the host is in the ignored domains list. It
|
|
// assumes that l.confMu is locked for reading.
|
|
func (l *queryLog) isIgnored(host string) bool {
|
|
return l.conf.Ignored.Has(host)
|
|
}
|