2020-11-03 15:39:55 +03:00
|
|
|
// Package querylog provides query log functions and interfaces.
|
2019-08-26 11:54:38 +03:00
|
|
|
package querylog
|
2018-12-05 14:03:41 +03:00
|
|
|
|
|
|
|
import (
|
2020-11-03 15:39:55 +03:00
|
|
|
"fmt"
|
2019-08-26 11:54:38 +03:00
|
|
|
"os"
|
2018-12-05 14:03:41 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-10-11 17:31:41 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2021-12-06 17:26:43 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-25 16:44:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-09-30 21:17:54 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2018-12-05 14:03:41 +03:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
// queryLogFileName is a name of the log file. ".gz" extension is added later
|
|
|
|
// during compression.
|
|
|
|
const queryLogFileName = "querylog.json"
|
2018-12-05 14:03:41 +03:00
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
// queryLog is a structure that writes and reads the DNS query log.
|
2019-02-10 20:47:43 +03:00
|
|
|
type queryLog struct {
|
2023-04-12 14:48:42 +03:00
|
|
|
// confMu protects conf.
|
|
|
|
confMu *sync.RWMutex
|
2023-06-07 20:04:01 +03:00
|
|
|
|
|
|
|
conf *Config
|
|
|
|
anonymizer *aghnet.IPMut
|
|
|
|
|
|
|
|
findClient func(ids []string) (c *Client, err error)
|
2023-04-12 14:48:42 +03:00
|
|
|
|
2023-03-09 15:39:35 +03:00
|
|
|
// buffer contains recent log entries. The entries in this buffer must not
|
|
|
|
// be modified.
|
2023-10-11 17:31:41 +03:00
|
|
|
buffer *aghalg.RingBuffer[*logEntry]
|
|
|
|
|
|
|
|
// logFile is the path to the log file.
|
|
|
|
logFile string
|
2021-04-02 17:30:39 +03:00
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
// bufferLock protects buffer.
|
|
|
|
bufferLock sync.RWMutex
|
|
|
|
|
|
|
|
// fileFlushLock synchronizes a file-flushing goroutine and main thread.
|
|
|
|
fileFlushLock sync.Mutex
|
2019-09-27 18:58:57 +03:00
|
|
|
fileWriteLock sync.Mutex
|
2021-12-06 17:26:43 +03:00
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
flushPending bool
|
2019-02-10 20:47:43 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 15:39:55 +03:00
|
|
|
// ClientProto values are names of the client protocols.
|
|
|
|
type ClientProto string
|
|
|
|
|
|
|
|
// Client protocol names.
|
|
|
|
const (
|
2021-06-18 18:13:36 +03:00
|
|
|
ClientProtoDoH ClientProto = "doh"
|
|
|
|
ClientProtoDoQ ClientProto = "doq"
|
|
|
|
ClientProtoDoT ClientProto = "dot"
|
2021-01-27 18:32:13 +03:00
|
|
|
ClientProtoDNSCrypt ClientProto = "dnscrypt"
|
|
|
|
ClientProtoPlain ClientProto = ""
|
2020-11-03 15:39:55 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2021-06-18 18:13:36 +03:00
|
|
|
ClientProtoDoH,
|
|
|
|
ClientProtoDoQ,
|
|
|
|
ClientProtoDoT,
|
2021-02-11 13:46:59 +03:00
|
|
|
ClientProtoDNSCrypt,
|
2020-11-03 15:39:55 +03:00
|
|
|
ClientProtoPlain:
|
|
|
|
|
|
|
|
return cp, nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("invalid client proto: %q", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 14:25:40 +03:00
|
|
|
func (l *queryLog) Start() {
|
2019-09-27 18:58:57 +03:00
|
|
|
if l.conf.HTTPRegister != nil {
|
|
|
|
l.initWeb()
|
|
|
|
}
|
2023-04-12 14:48:42 +03:00
|
|
|
|
2019-09-27 18:58:57 +03:00
|
|
|
go l.periodicRotate()
|
2019-08-26 11:54:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) Close() {
|
2023-04-12 14:48:42 +03:00
|
|
|
l.confMu.RLock()
|
|
|
|
defer l.confMu.RUnlock()
|
|
|
|
|
|
|
|
if l.conf.FileEnabled {
|
|
|
|
err := l.flushLogBuffer()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("querylog: closing: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 11:54:38 +03:00
|
|
|
}
|
|
|
|
|
2021-07-01 18:50:28 +03:00
|
|
|
func checkInterval(ivl time.Duration) (ok bool) {
|
|
|
|
// The constants for possible values of query log's rotation interval.
|
|
|
|
const (
|
2021-09-30 21:17:54 +03:00
|
|
|
quarterDay = timeutil.Day / 4
|
|
|
|
day = timeutil.Day
|
|
|
|
week = timeutil.Day * 7
|
|
|
|
month = timeutil.Day * 30
|
|
|
|
threeMonths = timeutil.Day * 90
|
2021-07-01 18:50:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return ivl == quarterDay || ivl == day || ivl == week || ivl == month || ivl == threeMonths
|
2019-09-27 18:58:57 +03:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:48:42 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:29:36 +03:00
|
|
|
func (l *queryLog) WriteDiskConfig(c *Config) {
|
2023-04-12 14:48:42 +03:00
|
|
|
l.confMu.RLock()
|
|
|
|
defer l.confMu.RUnlock()
|
|
|
|
|
2020-05-28 15:29:36 +03:00
|
|
|
*c = *l.conf
|
2019-09-27 18:58:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear memory buffer and remove log files
|
|
|
|
func (l *queryLog) clear() {
|
2019-08-26 11:54:38 +03:00
|
|
|
l.fileFlushLock.Lock()
|
|
|
|
defer l.fileFlushLock.Unlock()
|
|
|
|
|
2023-04-12 14:48:42 +03:00
|
|
|
func() {
|
|
|
|
l.bufferLock.Lock()
|
|
|
|
defer l.bufferLock.Unlock()
|
|
|
|
|
2023-10-18 17:04:44 +03:00
|
|
|
l.buffer.Clear()
|
2023-04-12 14:48:42 +03:00
|
|
|
l.flushPending = false
|
|
|
|
}()
|
2019-08-26 11:54:38 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
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)
|
2019-02-10 20:47:43 +03:00
|
|
|
}
|
2019-08-26 11:54:38 +03:00
|
|
|
|
|
|
|
err = os.Remove(l.logFile)
|
2021-01-27 18:32:13 +03:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
log.Error("removing log file %q: %s", l.logFile, err)
|
2019-08-26 11:54:38 +03:00
|
|
|
}
|
|
|
|
|
2022-05-25 18:00:50 +03:00
|
|
|
log.Debug("querylog: cleared")
|
2019-02-10 20:47:43 +03:00
|
|
|
}
|
2018-12-05 14:03:41 +03:00
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
// newLogEntry creates an instance of logEntry from parameters.
|
|
|
|
func newLogEntry(params *AddParams) (entry *logEntry) {
|
2021-12-13 18:06:01 +03:00
|
|
|
q := params.Question.Question[0]
|
2023-07-12 15:13:31 +03:00
|
|
|
qHost := aghnet.NormalizeDomain(q.Name)
|
2023-06-07 20:04:01 +03:00
|
|
|
|
|
|
|
entry = &logEntry{
|
|
|
|
// TODO(d.kolyshev): Export this timestamp to func params.
|
2023-07-03 14:10:40 +03:00
|
|
|
Time: time.Now(),
|
|
|
|
QHost: qHost,
|
2021-12-13 18:06:01 +03:00
|
|
|
QType: dns.Type(q.Qtype).String(),
|
|
|
|
QClass: dns.Class(q.Qclass).String(),
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
ClientID: params.ClientID,
|
2020-05-29 11:15:22 +03:00
|
|
|
ClientProto: params.ClientProto,
|
2021-12-13 18:06:01 +03:00
|
|
|
|
|
|
|
Result: *params.Result,
|
|
|
|
Upstream: params.Upstream,
|
|
|
|
|
|
|
|
IP: params.ClientIP,
|
|
|
|
|
|
|
|
Elapsed: params.Elapsed,
|
|
|
|
|
|
|
|
Cached: params.Cached,
|
|
|
|
AuthenticatedData: params.AuthenticatedData,
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
|
|
|
|
2022-03-03 17:52:11 +03:00
|
|
|
if params.ReqECS != nil {
|
|
|
|
entry.ReqECS = params.ReqECS.String()
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:48:42 +03:00
|
|
|
entry.addResponse(params.Answer, false)
|
|
|
|
entry.addResponse(params.OrigAnswer, true)
|
2021-04-02 17:30:39 +03:00
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add implements the [QueryLog] interface for *queryLog.
|
|
|
|
func (l *queryLog) Add(params *AddParams) {
|
|
|
|
var isEnabled, fileIsEnabled bool
|
2024-01-30 18:43:51 +03:00
|
|
|
var memSize uint
|
2023-06-07 20:04:01 +03:00
|
|
|
func() {
|
|
|
|
l.confMu.RLock()
|
|
|
|
defer l.confMu.RUnlock()
|
|
|
|
|
|
|
|
isEnabled, fileIsEnabled = l.conf.Enabled, l.conf.FileEnabled
|
|
|
|
memSize = l.conf.MemSize
|
|
|
|
}()
|
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
if !isEnabled {
|
2023-06-07 20:04:01 +03:00
|
|
|
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)
|
|
|
|
|
2023-10-11 17:31:41 +03:00
|
|
|
l.bufferLock.Lock()
|
|
|
|
defer l.bufferLock.Unlock()
|
2023-04-12 14:48:42 +03:00
|
|
|
|
2023-10-11 17:31:41 +03:00
|
|
|
l.buffer.Append(entry)
|
|
|
|
|
|
|
|
if !l.flushPending && fileIsEnabled && l.buffer.Len() >= memSize {
|
|
|
|
l.flushPending = true
|
2018-12-05 14:03:41 +03:00
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
// TODO(s.chzhen): Fix occasional rewrite of entires.
|
2020-05-28 15:29:36 +03:00
|
|
|
go func() {
|
2023-04-12 14:48:42 +03:00
|
|
|
flushErr := l.flushLogBuffer()
|
|
|
|
if flushErr != nil {
|
2023-10-11 17:31:41 +03:00
|
|
|
log.Error("querylog: flushing after adding: %s", flushErr)
|
2023-04-12 14:48:42 +03:00
|
|
|
}
|
2020-05-28 15:29:36 +03:00
|
|
|
}()
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-15 16:53:29 +03:00
|
|
|
|
|
|
|
// ShouldLog returns true if request for the host should be logged.
|
2023-04-12 14:48:42 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
return !l.isIgnored(host)
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:48:42 +03:00
|
|
|
// isIgnored returns true if the host is in the ignored domains list. It
|
|
|
|
// assumes that l.confMu is locked for reading.
|
2023-02-15 16:53:29 +03:00
|
|
|
func (l *queryLog) isIgnored(host string) bool {
|
|
|
|
return l.conf.Ignored.Has(host)
|
|
|
|
}
|