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