2019-08-26 11:54:38 +03:00
|
|
|
package querylog
|
2018-12-05 14:03:41 +03:00
|
|
|
|
|
|
|
import (
|
2019-08-26 11:54:38 +03:00
|
|
|
"os"
|
2019-02-10 20:47:43 +03:00
|
|
|
"path/filepath"
|
2018-12-05 14:03:41 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
2019-02-25 16:44:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2018-12-05 14:03:41 +03:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-08-26 11:54:38 +03:00
|
|
|
queryLogFileName = "querylog.json" // .gz added during compression
|
2018-12-05 14:03:41 +03:00
|
|
|
)
|
|
|
|
|
2019-02-10 20:47:43 +03:00
|
|
|
// queryLog is a structure that writes and reads the DNS query log
|
|
|
|
type queryLog struct {
|
2019-11-12 15:36:17 +03:00
|
|
|
conf *Config
|
|
|
|
lock sync.Mutex
|
2019-08-08 12:56:02 +03:00
|
|
|
logFile string // path to the log file
|
2019-02-10 20:47:43 +03:00
|
|
|
|
2019-09-27 18:58:57 +03:00
|
|
|
bufferLock sync.RWMutex
|
|
|
|
buffer []*logEntry
|
2019-05-15 13:11:36 +03: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 18:58:57 +03:00
|
|
|
fileWriteLock sync.Mutex
|
2019-02-10 20:47:43 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 15:37:37 +03: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 11:15:22 +03:00
|
|
|
ClientProto string `json:"CP"` // "" or "doh"
|
|
|
|
|
2020-05-26 15:37:37 +03: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 14:12:00 +03:00
|
|
|
// create a new instance of the query log
|
2019-08-26 11:54:38 +03:00
|
|
|
func newQueryLog(conf Config) *queryLog {
|
|
|
|
l := queryLog{}
|
|
|
|
l.logFile = filepath.Join(conf.BaseDir, queryLogFileName)
|
2019-11-12 15:36:17 +03:00
|
|
|
l.conf = &Config{}
|
|
|
|
*l.conf = conf
|
2019-09-27 18:58:57 +03:00
|
|
|
if !checkInterval(l.conf.Interval) {
|
|
|
|
l.conf.Interval = 1
|
|
|
|
}
|
2020-01-16 14:25:40 +03:00
|
|
|
return &l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) Start() {
|
2019-09-27 18:58:57 +03:00
|
|
|
if l.conf.HTTPRegister != nil {
|
|
|
|
l.initWeb()
|
|
|
|
}
|
|
|
|
go l.periodicRotate()
|
2019-08-26 11:54:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) Close() {
|
|
|
|
_ = l.flushLogBuffer(true)
|
|
|
|
}
|
|
|
|
|
2019-09-27 18:58:57 +03:00
|
|
|
func checkInterval(days uint32) bool {
|
|
|
|
return days == 1 || days == 7 || days == 30 || days == 90
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:29:36 +03:00
|
|
|
func (l *queryLog) WriteDiskConfig(c *Config) {
|
|
|
|
*c = *l.conf
|
2019-09-27 18:58:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear memory buffer and remove log files
|
|
|
|
func (l *queryLog) clear() {
|
2019-08-26 11:54:38 +03:00
|
|
|
l.fileFlushLock.Lock()
|
|
|
|
defer l.fileFlushLock.Unlock()
|
|
|
|
|
2019-09-27 18:58:57 +03:00
|
|
|
l.bufferLock.Lock()
|
|
|
|
l.buffer = nil
|
2019-08-26 11:54:38 +03:00
|
|
|
l.flushPending = false
|
2019-09-27 18:58:57 +03:00
|
|
|
l.bufferLock.Unlock()
|
2019-08-26 11:54:38 +03:00
|
|
|
|
|
|
|
err := os.Remove(l.logFile + ".1")
|
2019-10-07 12:03:56 +03:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2019-08-26 11:54:38 +03:00
|
|
|
log.Error("file remove: %s: %s", l.logFile+".1", err)
|
2019-02-10 20:47:43 +03:00
|
|
|
}
|
2019-08-26 11:54:38 +03:00
|
|
|
|
|
|
|
err = os.Remove(l.logFile)
|
2019-10-07 12:03:56 +03:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2019-08-26 11:54:38 +03:00
|
|
|
log.Error("file remove: %s: %s", l.logFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Query log: cleared")
|
2019-02-10 20:47:43 +03:00
|
|
|
}
|
2018-12-05 14:03:41 +03:00
|
|
|
|
2019-11-21 16:13:19 +03:00
|
|
|
func (l *queryLog) Add(params AddParams) {
|
2019-09-27 18:58:57 +03:00
|
|
|
if !l.conf.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:13:19 +03:00
|
|
|
if params.Question == nil || len(params.Question.Question) != 1 || len(params.Question.Question[0].Name) == 0 ||
|
|
|
|
params.ClientIP == nil {
|
2019-11-12 18:14:33 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:13:19 +03:00
|
|
|
if params.Result == nil {
|
|
|
|
params.Result = &dnsfilter.Result{}
|
2018-12-06 17:27:38 +03:00
|
|
|
}
|
|
|
|
|
2018-12-05 14:03:41 +03:00
|
|
|
now := time.Now()
|
|
|
|
entry := logEntry{
|
2020-03-03 20:21:53 +03:00
|
|
|
IP: l.getClientIP(params.ClientIP.String()),
|
2019-10-24 20:00:58 +03:00
|
|
|
Time: now,
|
|
|
|
|
2020-05-29 11:15:22 +03:00
|
|
|
Result: *params.Result,
|
|
|
|
Elapsed: params.Elapsed,
|
|
|
|
Upstream: params.Upstream,
|
|
|
|
ClientProto: params.ClientProto,
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
2019-11-21 16:13:19 +03:00
|
|
|
q := params.Question.Question[0]
|
2019-10-24 20:00:58 +03: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 14:03:41 +03:00
|
|
|
|
2019-11-21 16:13:19 +03: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 18:58:57 +03:00
|
|
|
l.bufferLock.Lock()
|
|
|
|
l.buffer = append(l.buffer, &entry)
|
2019-05-15 13:11:36 +03:00
|
|
|
needFlush := false
|
2020-05-28 15:29:36 +03: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 12:31:50 +03:00
|
|
|
needFlush = len(l.buffer) >= int(l.conf.MemSize)
|
2019-05-15 13:11:36 +03:00
|
|
|
if needFlush {
|
|
|
|
l.flushPending = true
|
|
|
|
}
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
2019-09-27 18:58:57 +03:00
|
|
|
l.bufferLock.Unlock()
|
2018-12-05 14:03:41 +03:00
|
|
|
|
|
|
|
// if buffer needs to be flushed to disk, do it now
|
2019-05-15 13:11:36 +03:00
|
|
|
if needFlush {
|
2020-05-28 15:29:36 +03:00
|
|
|
go func() {
|
|
|
|
_ = l.flushLogBuffer(false)
|
|
|
|
}()
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
|
|
|
}
|