2019-08-26 08:54:38 +00:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2019-09-27 15:58:57 +00:00
|
|
|
"net/http"
|
2021-04-02 14:30:39 +00:00
|
|
|
"path/filepath"
|
2019-08-26 08:54:38 +00:00
|
|
|
"time"
|
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-21 13:15:47 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2021-05-24 14:28:11 +00:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-04-02 14:30:39 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-09-30 18:17:54 +00:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2019-08-26 08:54:38 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// QueryLog - main interface
|
|
|
|
type QueryLog interface {
|
2020-01-16 11:25:40 +00:00
|
|
|
Start()
|
|
|
|
|
2019-09-04 11:12:00 +00:00
|
|
|
// Close query log object
|
2019-08-26 08:54:38 +00:00
|
|
|
Close()
|
|
|
|
|
2019-09-04 11:12:00 +00:00
|
|
|
// Add a log entry
|
2021-12-13 15:06:01 +00:00
|
|
|
Add(params *AddParams)
|
2019-09-04 11:12:00 +00:00
|
|
|
|
2019-09-27 15:58:57 +00:00
|
|
|
// WriteDiskConfig - write configuration
|
2020-05-28 12:29:36 +00:00
|
|
|
WriteDiskConfig(c *Config)
|
2019-08-26 08:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config - configuration object
|
|
|
|
type Config struct {
|
2021-04-02 14:30:39 +00:00
|
|
|
// ConfigModified is called when the configuration is changed, for
|
|
|
|
// example by HTTP requests.
|
2019-09-27 15:58:57 +00:00
|
|
|
ConfigModified func()
|
|
|
|
|
2021-04-02 14:30:39 +00:00
|
|
|
// HTTPRegister registers an HTTP handler.
|
2019-09-27 15:58:57 +00:00
|
|
|
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request))
|
2021-04-02 14:30:39 +00: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 15:50:28 +00: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 10:43:08 +00:00
|
|
|
// 6 * time.Hour
|
|
|
|
// 1 * timeutil.Day
|
|
|
|
// 7 * timeutil.Day
|
|
|
|
// 30 * timeutil.Day
|
|
|
|
// 90 * timeutil.Day
|
2021-07-01 15:50:28 +00:00
|
|
|
//
|
|
|
|
RotationIvl time.Duration
|
2021-04-02 14:30:39 +00: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 14:26:43 +00:00
|
|
|
|
2021-12-27 16:40:39 +00:00
|
|
|
// Anonymizer processes the IP addresses to anonymize those if needed.
|
2021-12-06 14:26:43 +00:00
|
|
|
Anonymizer *aghnet.IPMut
|
2019-08-26 08:54:38 +00: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
|
|
|
|
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
|
|
|
// Elapsed is the time spent for processing the request.
|
2021-12-13 15:06:01 +00:00
|
|
|
Elapsed time.Duration
|
|
|
|
|
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
|
|
|
ClientIP net.IP
|
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
|
|
|
|
|
|
|
// 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 14:30:39 +00: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 14:28:11 +00:00
|
|
|
return errors.Error("question is nil")
|
2021-04-02 14:30:39 +00:00
|
|
|
case len(p.Question.Question) != 1:
|
2021-05-24 14:28:11 +00:00
|
|
|
return errors.Error("more than one question")
|
2021-04-02 14:30:39 +00:00
|
|
|
case len(p.Question.Question[0].Name) == 0:
|
2021-05-24 14:28:11 +00:00
|
|
|
return errors.Error("no host in question")
|
2021-04-02 14:30:39 +00:00
|
|
|
case p.ClientIP == nil:
|
2021-05-24 14:28:11 +00:00
|
|
|
return errors.Error("no client ip")
|
2021-04-02 14:30:39 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of the query log.
|
|
|
|
func New(conf Config) (ql QueryLog) {
|
2019-08-26 08:54:38 +00:00
|
|
|
return newQueryLog(conf)
|
|
|
|
}
|
2021-04-02 14:30:39 +00: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 14:26:43 +00:00
|
|
|
logFile: filepath.Join(conf.BaseDir, queryLogFileName),
|
|
|
|
anonymizer: conf.Anonymizer,
|
2021-04-02 14:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
l.conf = &Config{}
|
|
|
|
*l.conf = conf
|
|
|
|
|
|
|
|
if !checkInterval(conf.RotationIvl) {
|
|
|
|
log.Info(
|
2021-10-29 10:43:08 +00:00
|
|
|
"querylog: warning: unsupported rotation interval %s, setting to 1 day",
|
2021-04-02 14:30:39 +00:00
|
|
|
conf.RotationIvl,
|
|
|
|
)
|
2021-09-30 18:17:54 +00:00
|
|
|
l.conf.RotationIvl = timeutil.Day
|
2021-04-02 14:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|