2019-08-26 09:54:38 +01:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
2023-03-23 10:46:57 +00:00
|
|
|
"fmt"
|
2019-08-26 09:54:38 +01:00
|
|
|
"net"
|
2021-04-02 15:30:39 +01:00
|
|
|
"path/filepath"
|
2023-03-31 16:44:51 +01:00
|
|
|
"sync"
|
2019-08-26 09:54:38 +01:00
|
|
|
"time"
|
|
|
|
|
2023-10-03 18:50:43 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2022-08-04 17:05:28 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
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-08-26 09:54:38 +01:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// QueryLog - main interface
|
|
|
|
type QueryLog interface {
|
2020-01-16 11:25:40 +00:00
|
|
|
Start()
|
|
|
|
|
2019-09-04 12:12:00 +01:00
|
|
|
// Close query log object
|
2019-08-26 09:54:38 +01:00
|
|
|
Close()
|
|
|
|
|
2019-09-04 12:12:00 +01:00
|
|
|
// Add a log entry
|
2021-12-13 15:06:01 +00:00
|
|
|
Add(params *AddParams)
|
2019-09-04 12:12:00 +01:00
|
|
|
|
2019-09-27 16:58:57 +01:00
|
|
|
// WriteDiskConfig - write configuration
|
2020-05-28 13:29:36 +01:00
|
|
|
WriteDiskConfig(c *Config)
|
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
|
|
|
ShouldLog(host string, qType, qClass uint16, ids []string) bool
|
2019-08-26 09:54:38 +01:00
|
|
|
}
|
|
|
|
|
2022-05-25 16:00:50 +01:00
|
|
|
// Config is the query log configuration structure.
|
2023-04-05 13:50:14 +01:00
|
|
|
//
|
|
|
|
// Do not alter any fields of this structure after using it.
|
2019-08-26 09:54:38 +01:00
|
|
|
type Config struct {
|
2023-09-05 13:13:35 +01:00
|
|
|
// Ignored contains the list of host names, which should not be written to
|
|
|
|
// log, and matches them.
|
|
|
|
Ignored *aghnet.IgnoreEngine
|
2023-03-31 16:44:51 +01:00
|
|
|
|
2022-05-25 16:00:50 +01:00
|
|
|
// Anonymizer processes the IP addresses to anonymize those if needed.
|
|
|
|
Anonymizer *aghnet.IPMut
|
|
|
|
|
2023-03-31 16:44:51 +01:00
|
|
|
// ConfigModified is called when the configuration is changed, for example
|
|
|
|
// by HTTP requests.
|
2019-09-27 16:58:57 +01:00
|
|
|
ConfigModified func()
|
|
|
|
|
2021-04-02 15:30:39 +01:00
|
|
|
// HTTPRegister registers an HTTP handler.
|
2022-08-04 17:05:28 +01:00
|
|
|
HTTPRegister aghhttp.RegisterFunc
|
2021-04-02 15:30:39 +01:00
|
|
|
|
|
|
|
// FindClient returns client information by their IDs.
|
|
|
|
FindClient func(ids []string) (c *Client, err error)
|
|
|
|
|
|
|
|
// BaseDir is the base directory for log files.
|
|
|
|
BaseDir string
|
|
|
|
|
2023-03-31 16:44:51 +01:00
|
|
|
// RotationIvl is the interval for log rotation. After that period, the old
|
|
|
|
// log file will be renamed, NOT deleted, so the actual log retention time
|
|
|
|
// is twice the interval.
|
2021-07-01 16:50:28 +01:00
|
|
|
RotationIvl time.Duration
|
2021-04-02 15:30:39 +01:00
|
|
|
|
2023-03-31 16:44:51 +01:00
|
|
|
// MemSize is the number of entries kept in a memory buffer before they are
|
|
|
|
// flushed to disk.
|
2024-01-11 15:38:30 +00:00
|
|
|
MemSize uint
|
2021-04-02 15:30:39 +01:00
|
|
|
|
|
|
|
// Enabled tells if the query log is enabled.
|
|
|
|
Enabled bool
|
|
|
|
|
|
|
|
// FileEnabled tells if the query log writes logs to files.
|
|
|
|
FileEnabled bool
|
|
|
|
|
|
|
|
// AnonymizeClientIP tells if the query log should anonymize clients' IP
|
|
|
|
// addresses.
|
|
|
|
AnonymizeClientIP bool
|
2019-08-26 09:54:38 +01:00
|
|
|
}
|
|
|
|
|
2021-12-07 14:43:51 +00:00
|
|
|
// AddParams is the parameters for adding an entry.
|
2019-11-21 13:13:19 +00:00
|
|
|
type AddParams struct {
|
2021-12-07 14:43:51 +00:00
|
|
|
Question *dns.Msg
|
2021-12-13 15:06:01 +00:00
|
|
|
|
2022-03-03 14:52:11 +00:00
|
|
|
// ReqECS is the IP network extracted from EDNS Client-Subnet option of a
|
|
|
|
// request.
|
|
|
|
ReqECS *net.IPNet
|
|
|
|
|
2021-12-07 14:43:51 +00:00
|
|
|
// Answer is the response which is sent to the client, if any.
|
|
|
|
Answer *dns.Msg
|
2021-12-13 15:06:01 +00:00
|
|
|
|
2021-12-07 14:43:51 +00:00
|
|
|
// OrigAnswer is the response from an upstream server. It's only set if the
|
|
|
|
// answer has been modified by filtering.
|
|
|
|
OrigAnswer *dns.Msg
|
2021-12-13 15:06:01 +00:00
|
|
|
|
2021-12-07 14:43:51 +00:00
|
|
|
// Result is the filtering result (optional).
|
|
|
|
Result *filtering.Result
|
2021-12-13 15:06:01 +00:00
|
|
|
|
2021-12-07 14:43:51 +00:00
|
|
|
ClientID string
|
2021-12-13 15:06:01 +00:00
|
|
|
|
2021-12-07 14:43:51 +00:00
|
|
|
// Upstream is the URL of the upstream DNS server.
|
2021-12-13 15:06:01 +00:00
|
|
|
Upstream string
|
|
|
|
|
2020-11-03 12:39:55 +00:00
|
|
|
ClientProto ClientProto
|
2021-12-13 15:06:01 +00:00
|
|
|
|
2022-05-25 16:00:50 +01:00
|
|
|
ClientIP net.IP
|
|
|
|
|
|
|
|
// Elapsed is the time spent for processing the request.
|
|
|
|
Elapsed time.Duration
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
// Cached indicates if the response is served from cache.
|
|
|
|
Cached bool
|
|
|
|
|
|
|
|
// AuthenticatedData shows if the response had the AD bit set.
|
|
|
|
AuthenticatedData bool
|
2019-11-21 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 15:30:39 +01:00
|
|
|
// validate returns an error if the parameters aren't valid.
|
|
|
|
func (p *AddParams) validate() (err error) {
|
|
|
|
switch {
|
|
|
|
case p.Question == nil:
|
2021-05-24 15:28:11 +01:00
|
|
|
return errors.Error("question is nil")
|
2021-04-02 15:30:39 +01:00
|
|
|
case len(p.Question.Question) != 1:
|
2021-05-24 15:28:11 +01:00
|
|
|
return errors.Error("more than one question")
|
2021-04-02 15:30:39 +01:00
|
|
|
case len(p.Question.Question[0].Name) == 0:
|
2021-05-24 15:28:11 +01:00
|
|
|
return errors.Error("no host in question")
|
2021-04-02 15:30:39 +01:00
|
|
|
case p.ClientIP == nil:
|
2021-05-24 15:28:11 +01:00
|
|
|
return errors.Error("no client ip")
|
2021-04-02 15:30:39 +01:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of the query log.
|
2023-03-23 10:46:57 +00:00
|
|
|
func New(conf Config) (ql QueryLog, err error) {
|
2019-08-26 09:54:38 +01:00
|
|
|
return newQueryLog(conf)
|
|
|
|
}
|
2021-04-02 15:30:39 +01:00
|
|
|
|
|
|
|
// newQueryLog crates a new queryLog.
|
2023-03-23 10:46:57 +00:00
|
|
|
func newQueryLog(conf Config) (l *queryLog, err error) {
|
2021-04-02 15:30:39 +01:00
|
|
|
findClient := conf.FindClient
|
|
|
|
if findClient == nil {
|
|
|
|
findClient = func(_ []string) (_ *Client, _ error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 15:38:30 +00:00
|
|
|
memSize := conf.MemSize
|
|
|
|
if memSize == 0 {
|
|
|
|
// If query log is enabled, we still need to write entries to a file.
|
|
|
|
// And all writing goes through a buffer.
|
|
|
|
memSize = 1
|
2023-10-03 18:50:43 +01:00
|
|
|
}
|
|
|
|
|
2021-04-02 15:30:39 +01:00
|
|
|
l = &queryLog{
|
|
|
|
findClient: findClient,
|
|
|
|
|
2024-01-11 15:38:30 +00:00
|
|
|
buffer: aghalg.NewRingBuffer[*logEntry](memSize),
|
2023-10-03 18:50:43 +01:00
|
|
|
|
2023-03-31 16:44:51 +01:00
|
|
|
conf: &Config{},
|
|
|
|
confMu: &sync.RWMutex{},
|
|
|
|
logFile: filepath.Join(conf.BaseDir, queryLogFileName),
|
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
anonymizer: conf.Anonymizer,
|
2021-04-02 15:30:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*l.conf = conf
|
|
|
|
|
2023-03-23 10:46:57 +00:00
|
|
|
err = validateIvl(conf.RotationIvl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unsupported interval: %w", err)
|
2021-04-02 15:30:39 +01:00
|
|
|
}
|
|
|
|
|
2023-03-23 10:46:57 +00:00
|
|
|
return l, nil
|
2021-04-02 15:30:39 +01:00
|
|
|
}
|