mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-01-10 16:07:25 +03:00
cbc7985e75
Merge in DNS/adguard-home from querylog-imp-code to master Squashed commit of the following: commit a58ad36508a2355b686d314dec51ac0b5e357281 Merge: df5494f2c941eb1dd7
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 15:26:55 2023 +0300 Merge remote-tracking branch 'origin/master' into querylog-imp-code commit df5494f2c337736690a3c2a547c2d71858d0378f Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 15:24:43 2023 +0300 querylog: imp code commit 8c3c2b76dd5858e7b107f222c112e9cde2477fb3 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 12:14:15 2023 +0300 all: lint script commit be04a4decfaf20a1649d32ecaab3c1c6bb205ffd Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 12:03:12 2023 +0300 querylog: imp code commit fe7beacff3a5cfcf2332c4998b9c65820284eaf7 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 11:57:33 2023 +0300 querylog: imp docs commit 2ae239c57d12524fbc092f582842af2ad726c1d0 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 11:46:54 2023 +0300 querylog: imp code commit 417216cefbf154fa870f8f43468f35e0e345971f Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 11:25:44 2023 +0300 querylog: imp code commit 514b6ee99113844a4e0dad30dc53703e3220c289 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed May 24 11:14:13 2023 +0300 querylog: imp docs commit 321351a3abb524208daacd5a3a7fbf5f07ab259d Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 16:38:31 2023 +0300 querylog: imp code commit ee91de5c43210b5bc213f933d411adb894d2e586 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 16:01:32 2023 +0300 querylog: imp code commit 862ff12177fb769d5cb2ec250eaee538dc91d70a Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 15:07:24 2023 +0300 querylog: imp code commit cc62c1c4ae8b813d03ccf51b596ba1ebf44d9a1f Merge: 37ace34e924b41100c
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 13:09:10 2023 +0300 Merge remote-tracking branch 'origin/master' into querylog-imp-code commit 37ace34e91e5189bef6e774db960f40cdaa18270 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 11:23:08 2023 +0300 querylog: imp code commit 8417815a6349f10b5dbad410ce28aab98bc479fa Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon May 22 11:08:29 2023 +0300 querylog: imp docs commit 4e5cde74d25713f78675aa3e18083b4fb5e619f3 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri May 19 16:41:34 2023 +0300 querylog: imp code commit 3494eab7006240f652a0217d305ac916bd6c3c83 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri May 19 16:13:08 2023 +0300 all: lint script commit 704534ce6278e7d9b1bef30a3acc4e59f25693bc Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri May 19 16:12:04 2023 +0300 querylog: imp code commit 48510102a2fa5187f78067d2b9157dac62f8bb56 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri May 19 15:52:57 2023 +0300 querylog: imp code commit 89c273aea0e6758eb749a2d3bbaf1bc385a57797 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri May 19 15:40:50 2023 +0300 querylog: imp code commit 0057fe64553ad38de0fda10efb9d3512c9a00e45 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri May 19 13:54:46 2023 +0300 querylog: imp code ... and 1 more commit
282 lines
6.2 KiB
Go
282 lines
6.2 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"
|
|
)
|
|
|
|
// queryLogFileName is a name of the log file. ".gz" extension is added later
|
|
// during compression.
|
|
const queryLogFileName = "querylog.json"
|
|
|
|
// queryLog is a structure that writes and reads the DNS query log.
|
|
type queryLog struct {
|
|
// confMu protects conf.
|
|
confMu *sync.RWMutex
|
|
|
|
conf *Config
|
|
anonymizer *aghnet.IPMut
|
|
|
|
findClient func(ids []string) (c *Client, err error)
|
|
|
|
// logFile is the path to the log file.
|
|
logFile string
|
|
|
|
// buffer contains recent log entries. The entries in this buffer must not
|
|
// be modified.
|
|
buffer []*logEntry
|
|
|
|
// bufferLock protects buffer.
|
|
bufferLock sync.RWMutex
|
|
|
|
// fileFlushLock synchronizes a file-flushing goroutine and main thread.
|
|
fileFlushLock sync.Mutex
|
|
fileWriteLock sync.Mutex
|
|
|
|
flushPending bool
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
|
|
// newLogEntry creates an instance of logEntry from parameters.
|
|
func newLogEntry(params *AddParams) (entry *logEntry) {
|
|
q := params.Question.Question[0]
|
|
|
|
entry = &logEntry{
|
|
// TODO(d.kolyshev): Export this timestamp to func params.
|
|
Time: 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)
|
|
|
|
return entry
|
|
}
|
|
|
|
// Add implements the [QueryLog] interface for *queryLog.
|
|
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{}
|
|
}
|
|
|
|
entry := newLogEntry(params)
|
|
|
|
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)
|
|
}
|