2019-08-26 11:54:38 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2021-04-02 17:30:39 +03:00
|
|
|
"path/filepath"
|
2019-08-26 11:54:38 +03:00
|
|
|
"time"
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
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"
|
2021-04-02 17:30:39 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-09-30 21:17:54 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2019-08-26 11:54:38 +03:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// QueryLog - main interface
|
|
|
|
type QueryLog interface {
|
2020-01-16 14:25:40 +03:00
|
|
|
Start()
|
|
|
|
|
2019-09-04 14:12:00 +03:00
|
|
|
// Close query log object
|
2019-08-26 11:54:38 +03:00
|
|
|
Close()
|
|
|
|
|
2019-09-04 14:12:00 +03:00
|
|
|
// Add a log entry
|
2021-12-13 18:06:01 +03:00
|
|
|
Add(params *AddParams)
|
2019-09-04 14:12:00 +03:00
|
|
|
|
2019-09-27 18:58:57 +03:00
|
|
|
// WriteDiskConfig - write configuration
|
2020-05-28 15:29:36 +03:00
|
|
|
WriteDiskConfig(c *Config)
|
2019-08-26 11:54:38 +03:00
|
|
|
}
|
|
|
|
|
2022-05-25 18:00:50 +03:00
|
|
|
// Config is the query log configuration structure.
|
2019-08-26 11:54:38 +03:00
|
|
|
type Config struct {
|
2022-05-25 18:00:50 +03:00
|
|
|
// Anonymizer processes the IP addresses to anonymize those if needed.
|
|
|
|
Anonymizer *aghnet.IPMut
|
|
|
|
|
2021-04-02 17:30:39 +03:00
|
|
|
// ConfigModified is called when the configuration is changed, for
|
|
|
|
// example by HTTP requests.
|
2019-09-27 18:58:57 +03:00
|
|
|
ConfigModified func()
|
|
|
|
|
2021-04-02 17:30:39 +03:00
|
|
|
// HTTPRegister registers an HTTP handler.
|
2022-08-04 19:05:28 +03:00
|
|
|
HTTPRegister aghhttp.RegisterFunc
|
2021-04-02 17:30:39 +03: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
|
|
|
|
|
2021-07-01 18:50:28 +03: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. The value must be one of:
|
|
|
|
//
|
2021-10-29 13:43:08 +03:00
|
|
|
// 6 * time.Hour
|
|
|
|
// 1 * timeutil.Day
|
|
|
|
// 7 * timeutil.Day
|
|
|
|
// 30 * timeutil.Day
|
|
|
|
// 90 * timeutil.Day
|
2021-07-01 18:50:28 +03:00
|
|
|
//
|
|
|
|
RotationIvl time.Duration
|
2021-04-02 17:30:39 +03:00
|
|
|
|
|
|
|
// MemSize is the number of entries kept in a memory buffer before they
|
|
|
|
// are flushed to disk.
|
|
|
|
MemSize uint32
|
|
|
|
|
|
|
|
// 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 11:54:38 +03:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:43:51 +03:00
|
|
|
// AddParams is the parameters for adding an entry.
|
2019-11-21 16:13:19 +03:00
|
|
|
type AddParams struct {
|
2021-12-07 17:43:51 +03:00
|
|
|
Question *dns.Msg
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2022-03-03 17:52:11 +03:00
|
|
|
// ReqECS is the IP network extracted from EDNS Client-Subnet option of a
|
|
|
|
// request.
|
|
|
|
ReqECS *net.IPNet
|
|
|
|
|
2021-12-07 17:43:51 +03:00
|
|
|
// Answer is the response which is sent to the client, if any.
|
|
|
|
Answer *dns.Msg
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2021-12-07 17:43:51 +03: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 18:06:01 +03:00
|
|
|
|
2021-12-07 17:43:51 +03:00
|
|
|
// Result is the filtering result (optional).
|
|
|
|
Result *filtering.Result
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2021-12-07 17:43:51 +03:00
|
|
|
ClientID string
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2021-12-07 17:43:51 +03:00
|
|
|
// Upstream is the URL of the upstream DNS server.
|
2021-12-13 18:06:01 +03:00
|
|
|
Upstream string
|
|
|
|
|
2020-11-03 15:39:55 +03:00
|
|
|
ClientProto ClientProto
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2022-05-25 18:00:50 +03:00
|
|
|
ClientIP net.IP
|
|
|
|
|
|
|
|
// Elapsed is the time spent for processing the request.
|
|
|
|
Elapsed time.Duration
|
|
|
|
|
2021-12-13 18:06:01 +03: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 16:13:19 +03:00
|
|
|
}
|
|
|
|
|
2021-04-02 17:30:39 +03: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 17:28:11 +03:00
|
|
|
return errors.Error("question is nil")
|
2021-04-02 17:30:39 +03:00
|
|
|
case len(p.Question.Question) != 1:
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("more than one question")
|
2021-04-02 17:30:39 +03:00
|
|
|
case len(p.Question.Question[0].Name) == 0:
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("no host in question")
|
2021-04-02 17:30:39 +03:00
|
|
|
case p.ClientIP == nil:
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("no client ip")
|
2021-04-02 17:30:39 +03:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of the query log.
|
|
|
|
func New(conf Config) (ql QueryLog) {
|
2019-08-26 11:54:38 +03:00
|
|
|
return newQueryLog(conf)
|
|
|
|
}
|
2021-04-02 17:30:39 +03:00
|
|
|
|
|
|
|
// newQueryLog crates a new queryLog.
|
|
|
|
func newQueryLog(conf Config) (l *queryLog) {
|
|
|
|
findClient := conf.FindClient
|
|
|
|
if findClient == nil {
|
|
|
|
findClient = func(_ []string) (_ *Client, _ error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l = &queryLog{
|
|
|
|
findClient: findClient,
|
|
|
|
|
2021-12-06 17:26:43 +03:00
|
|
|
logFile: filepath.Join(conf.BaseDir, queryLogFileName),
|
|
|
|
anonymizer: conf.Anonymizer,
|
2021-04-02 17:30:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
l.conf = &Config{}
|
|
|
|
*l.conf = conf
|
|
|
|
|
|
|
|
if !checkInterval(conf.RotationIvl) {
|
|
|
|
log.Info(
|
2021-10-29 13:43:08 +03:00
|
|
|
"querylog: warning: unsupported rotation interval %s, setting to 1 day",
|
2021-04-02 17:30:39 +03:00
|
|
|
conf.RotationIvl,
|
|
|
|
)
|
2021-09-30 21:17:54 +03:00
|
|
|
l.conf.RotationIvl = timeutil.Day
|
2021-04-02 17:30:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|