2019-08-26 11:54:38 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2019-09-27 18:58:57 +03:00
|
|
|
"net/http"
|
2021-04-02 17:30:39 +03:00
|
|
|
"path/filepath"
|
2019-08-26 11:54:38 +03:00
|
|
|
"time"
|
|
|
|
|
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
|
2019-11-21 16:13:19 +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
|
|
|
}
|
|
|
|
|
|
|
|
// Config - configuration object
|
|
|
|
type Config struct {
|
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.
|
2019-09-27 18:58:57 +03:00
|
|
|
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request))
|
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
|
2021-12-06 17:26:43 +03:00
|
|
|
|
|
|
|
// Anonymizer proccesses the IP addresses to anonymize those if needed.
|
|
|
|
Anonymizer *aghnet.IPMut
|
2019-08-26 11:54:38 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 16:13:19 +03:00
|
|
|
// AddParams - parameters for Add()
|
|
|
|
type AddParams struct {
|
2020-05-29 11:15:22 +03:00
|
|
|
Question *dns.Msg
|
|
|
|
Answer *dns.Msg // The response we sent to the client (optional)
|
|
|
|
OrigAnswer *dns.Msg // The response from an upstream server (optional)
|
2021-05-21 16:15:47 +03:00
|
|
|
Result *filtering.Result // Filtering result (optional)
|
2020-05-29 11:15:22 +03:00
|
|
|
Elapsed time.Duration // Time spent for processing the request
|
2021-01-27 18:32:13 +03:00
|
|
|
ClientID string
|
2020-05-29 11:15:22 +03:00
|
|
|
ClientIP net.IP
|
|
|
|
Upstream string // Upstream server URL
|
2020-11-03 15:39:55 +03:00
|
|
|
ClientProto ClientProto
|
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
|
|
|
|
}
|