2019-08-26 08:54:38 +00:00
|
|
|
package querylog
|
2018-12-05 11:03:41 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-26 08:54:38 +00:00
|
|
|
"os"
|
2019-02-10 17:47:43 +00:00
|
|
|
"path/filepath"
|
2018-12-05 11:03:41 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
2019-02-25 13:44:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2018-12-05 11:03:41 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-08-26 08:54:38 +00:00
|
|
|
queryLogFileName = "querylog.json" // .gz added during compression
|
2018-12-05 11:03:41 +00:00
|
|
|
)
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
// queryLog is a structure that writes and reads the DNS query log
|
|
|
|
type queryLog struct {
|
2019-11-12 12:36:17 +00:00
|
|
|
conf *Config
|
|
|
|
lock sync.Mutex
|
2019-08-08 09:56:02 +00:00
|
|
|
logFile string // path to the log file
|
2019-02-10 17:47:43 +00:00
|
|
|
|
2019-09-27 15:58:57 +00:00
|
|
|
bufferLock sync.RWMutex
|
|
|
|
buffer []*logEntry
|
2019-05-15 10:11:36 +00:00
|
|
|
fileFlushLock sync.Mutex // synchronize a file-flushing goroutine and main thread
|
|
|
|
flushPending bool // don't start another goroutine while the previous one is still running
|
2019-09-27 15:58:57 +00:00
|
|
|
fileWriteLock sync.Mutex
|
2019-02-10 17:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:37:37 +00:00
|
|
|
// logEntry - represents a single log entry
|
|
|
|
type logEntry struct {
|
|
|
|
IP string `json:"IP"` // Client IP
|
|
|
|
Time time.Time `json:"T"`
|
|
|
|
|
|
|
|
QHost string `json:"QH"`
|
|
|
|
QType string `json:"QT"`
|
|
|
|
QClass string `json:"QC"`
|
|
|
|
|
2020-05-29 08:15:22 +00:00
|
|
|
ClientProto string `json:"CP"` // "" or "doh"
|
|
|
|
|
2020-05-26 12:37:37 +00:00
|
|
|
Answer []byte `json:",omitempty"` // sometimes empty answers happen like binerdunt.top or rev2.globalrootservers.net
|
|
|
|
OrigAnswer []byte `json:",omitempty"`
|
|
|
|
|
|
|
|
Result dnsfilter.Result
|
|
|
|
Elapsed time.Duration
|
|
|
|
Upstream string `json:",omitempty"` // if empty, means it was cached
|
|
|
|
}
|
|
|
|
|
2019-09-04 11:12:00 +00:00
|
|
|
// create a new instance of the query log
|
2019-08-26 08:54:38 +00:00
|
|
|
func newQueryLog(conf Config) *queryLog {
|
|
|
|
l := queryLog{}
|
|
|
|
l.logFile = filepath.Join(conf.BaseDir, queryLogFileName)
|
2019-11-12 12:36:17 +00:00
|
|
|
l.conf = &Config{}
|
|
|
|
*l.conf = conf
|
2019-09-27 15:58:57 +00:00
|
|
|
if !checkInterval(l.conf.Interval) {
|
|
|
|
l.conf.Interval = 1
|
|
|
|
}
|
2020-01-16 11:25:40 +00:00
|
|
|
return &l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) Start() {
|
2019-09-27 15:58:57 +00:00
|
|
|
if l.conf.HTTPRegister != nil {
|
|
|
|
l.initWeb()
|
|
|
|
}
|
|
|
|
go l.periodicRotate()
|
2019-08-26 08:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) Close() {
|
|
|
|
_ = l.flushLogBuffer(true)
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:58:57 +00:00
|
|
|
func checkInterval(days uint32) bool {
|
|
|
|
return days == 1 || days == 7 || days == 30 || days == 90
|
|
|
|
}
|
|
|
|
|
2020-05-28 12:29:36 +00:00
|
|
|
func (l *queryLog) WriteDiskConfig(c *Config) {
|
|
|
|
*c = *l.conf
|
2019-09-27 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear memory buffer and remove log files
|
|
|
|
func (l *queryLog) clear() {
|
2019-08-26 08:54:38 +00:00
|
|
|
l.fileFlushLock.Lock()
|
|
|
|
defer l.fileFlushLock.Unlock()
|
|
|
|
|
2019-09-27 15:58:57 +00:00
|
|
|
l.bufferLock.Lock()
|
|
|
|
l.buffer = nil
|
2019-08-26 08:54:38 +00:00
|
|
|
l.flushPending = false
|
2019-09-27 15:58:57 +00:00
|
|
|
l.bufferLock.Unlock()
|
2019-08-26 08:54:38 +00:00
|
|
|
|
|
|
|
err := os.Remove(l.logFile + ".1")
|
2019-10-07 09:03:56 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2019-08-26 08:54:38 +00:00
|
|
|
log.Error("file remove: %s: %s", l.logFile+".1", err)
|
2019-02-10 17:47:43 +00:00
|
|
|
}
|
2019-08-26 08:54:38 +00:00
|
|
|
|
|
|
|
err = os.Remove(l.logFile)
|
2019-10-07 09:03:56 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2019-08-26 08:54:38 +00:00
|
|
|
log.Error("file remove: %s: %s", l.logFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Query log: cleared")
|
2019-02-10 17:47:43 +00:00
|
|
|
}
|
2018-12-05 11:03:41 +00:00
|
|
|
|
2019-11-21 13:13:19 +00:00
|
|
|
func (l *queryLog) Add(params AddParams) {
|
2019-09-27 15:58:57 +00:00
|
|
|
if !l.conf.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:13:19 +00:00
|
|
|
if params.Question == nil || len(params.Question.Question) != 1 || len(params.Question.Question[0].Name) == 0 ||
|
|
|
|
params.ClientIP == nil {
|
2019-11-12 15:14:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:13:19 +00:00
|
|
|
if params.Result == nil {
|
|
|
|
params.Result = &dnsfilter.Result{}
|
2018-12-06 14:27:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 11:03:41 +00:00
|
|
|
now := time.Now()
|
|
|
|
entry := logEntry{
|
2020-03-03 17:21:53 +00:00
|
|
|
IP: l.getClientIP(params.ClientIP.String()),
|
2019-10-24 17:00:58 +00:00
|
|
|
Time: now,
|
|
|
|
|
2020-05-29 08:15:22 +00:00
|
|
|
Result: *params.Result,
|
|
|
|
Elapsed: params.Elapsed,
|
|
|
|
Upstream: params.Upstream,
|
|
|
|
ClientProto: params.ClientProto,
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
2019-11-21 13:13:19 +00:00
|
|
|
q := params.Question.Question[0]
|
2019-10-24 17:00:58 +00:00
|
|
|
entry.QHost = strings.ToLower(q.Name[:len(q.Name)-1]) // remove the last dot
|
|
|
|
entry.QType = dns.Type(q.Qtype).String()
|
|
|
|
entry.QClass = dns.Class(q.Qclass).String()
|
2018-12-05 11:03:41 +00:00
|
|
|
|
2019-11-21 13:13:19 +00:00
|
|
|
if params.Answer != nil {
|
|
|
|
a, err := params.Answer.Pack()
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Querylog: Answer.Pack(): %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
entry.Answer = a
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.OrigAnswer != nil {
|
|
|
|
a, err := params.OrigAnswer.Pack()
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Querylog: OrigAnswer.Pack(): %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
entry.OrigAnswer = a
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:58:57 +00:00
|
|
|
l.bufferLock.Lock()
|
|
|
|
l.buffer = append(l.buffer, &entry)
|
2019-05-15 10:11:36 +00:00
|
|
|
needFlush := false
|
2020-05-28 12:29:36 +00:00
|
|
|
|
|
|
|
if !l.conf.FileEnabled {
|
|
|
|
if len(l.buffer) > int(l.conf.MemSize) {
|
|
|
|
// writing to file is disabled - just remove the oldest entry from array
|
|
|
|
l.buffer = l.buffer[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if !l.flushPending {
|
2019-11-08 09:31:50 +00:00
|
|
|
needFlush = len(l.buffer) >= int(l.conf.MemSize)
|
2019-05-15 10:11:36 +00:00
|
|
|
if needFlush {
|
|
|
|
l.flushPending = true
|
|
|
|
}
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
2019-09-27 15:58:57 +00:00
|
|
|
l.bufferLock.Unlock()
|
2018-12-05 11:03:41 +00:00
|
|
|
|
|
|
|
// if buffer needs to be flushed to disk, do it now
|
2019-05-15 10:11:36 +00:00
|
|
|
if needFlush {
|
2020-05-28 12:29:36 +00:00
|
|
|
go func() {
|
|
|
|
_ = l.flushLogBuffer(false)
|
|
|
|
}()
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
}
|