2019-08-26 11:54:38 +03:00
|
|
|
package querylog
|
2018-12-05 14:03:41 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-12-24 23:06:36 +03:00
|
|
|
"net"
|
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
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/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-09-27 18:58:57 +03:00
|
|
|
logBufferCap = 5000 // maximum capacity of buffer before it's flushed to disk
|
2019-08-26 11:54:38 +03:00
|
|
|
queryLogFileName = "querylog.json" // .gz added during compression
|
2019-09-16 17:07:18 +03:00
|
|
|
getDataLimit = 500 // GetData(): maximum log entries to return
|
|
|
|
|
2019-10-24 20:00:58 +03:00
|
|
|
// maximum entries to parse when searching
|
|
|
|
maxSearchEntries = 50000
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if l.conf.HTTPRegister != nil {
|
|
|
|
l.initWeb()
|
|
|
|
}
|
|
|
|
go l.periodicRotate()
|
2019-08-26 11:54:38 +03:00
|
|
|
return &l
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) WriteDiskConfig(dc *DiskConfig) {
|
|
|
|
dc.Enabled = l.conf.Enabled
|
|
|
|
dc.Interval = l.conf.Interval
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
type logEntry struct {
|
2019-10-24 20:00:58 +03:00
|
|
|
IP string `json:"IP"`
|
|
|
|
Time time.Time `json:"T"`
|
|
|
|
|
|
|
|
QHost string `json:"QH"`
|
|
|
|
QType string `json:"QT"`
|
|
|
|
QClass string `json:"QC"`
|
|
|
|
|
2018-12-05 14:03:41 +03:00
|
|
|
Answer []byte `json:",omitempty"` // sometimes empty answers happen like binerdunt.top or rev2.globalrootservers.net
|
2018-12-06 17:27:38 +03:00
|
|
|
Result dnsfilter.Result
|
2018-12-05 14:03:41 +03:00
|
|
|
Elapsed time.Duration
|
|
|
|
Upstream string `json:",omitempty"` // if empty, means it was cached
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:09:10 +03:00
|
|
|
func (l *queryLog) Add(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Result, elapsed time.Duration, ip net.IP, upstream string) {
|
2019-09-27 18:58:57 +03:00
|
|
|
if !l.conf.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-24 20:00:58 +03:00
|
|
|
if question == nil || len(question.Question) != 1 || len(question.Question[0].Name) == 0 ||
|
|
|
|
ip == nil {
|
2019-11-12 18:14:33 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-24 20:00:58 +03:00
|
|
|
var a []byte
|
|
|
|
var err error
|
2018-12-24 15:19:52 +03:00
|
|
|
|
2018-12-05 14:03:41 +03:00
|
|
|
if answer != nil {
|
|
|
|
a, err = answer.Pack()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to pack answer for querylog: %s", err)
|
2019-08-26 11:54:38 +03:00
|
|
|
return
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 17:27:38 +03:00
|
|
|
if result == nil {
|
|
|
|
result = &dnsfilter.Result{}
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:03:41 +03:00
|
|
|
now := time.Now()
|
|
|
|
entry := logEntry{
|
2019-11-19 14:09:10 +03:00
|
|
|
IP: ip.String(),
|
2019-10-24 20:00:58 +03:00
|
|
|
Time: now,
|
|
|
|
|
2018-12-05 14:03:41 +03:00
|
|
|
Answer: a,
|
2018-12-06 17:27:38 +03:00
|
|
|
Result: *result,
|
2018-12-05 14:03:41 +03:00
|
|
|
Elapsed: elapsed,
|
|
|
|
Upstream: upstream,
|
|
|
|
}
|
2019-10-24 20:00:58 +03:00
|
|
|
q := question.Question[0]
|
|
|
|
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-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
|
|
|
|
if !l.flushPending {
|
2019-09-27 18:58:57 +03:00
|
|
|
needFlush = len(l.buffer) >= logBufferCap
|
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 {
|
2018-12-05 14:03:41 +03:00
|
|
|
// write to file
|
|
|
|
// do it in separate goroutine -- we are stalling DNS response this whole time
|
2019-06-06 22:42:17 +03:00
|
|
|
go l.flushLogBuffer(false) // nolint
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 17:07:18 +03:00
|
|
|
// Return TRUE if this entry is needed
|
2019-09-27 18:58:57 +03:00
|
|
|
func isNeeded(entry *logEntry, params getDataParams) bool {
|
|
|
|
if params.ResponseStatus == responseStatusFiltered && !entry.Result.IsFiltered {
|
|
|
|
return false
|
2019-09-16 17:07:18 +03:00
|
|
|
}
|
|
|
|
|
2019-10-24 20:00:58 +03:00
|
|
|
if len(params.QuestionType) != 0 {
|
|
|
|
if entry.QType != params.QuestionType {
|
|
|
|
return false
|
2019-09-16 17:07:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 20:00:58 +03:00
|
|
|
if len(params.Domain) != 0 {
|
|
|
|
if (params.StrictMatchDomain && entry.QHost != params.Domain) ||
|
|
|
|
(!params.StrictMatchDomain && strings.Index(entry.QHost, params.Domain) == -1) {
|
2019-09-16 17:07:18 +03:00
|
|
|
return false
|
|
|
|
}
|
2019-10-24 20:00:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(params.Client) != 0 {
|
|
|
|
if (params.StrictMatchClient && entry.IP != params.Client) ||
|
|
|
|
(!params.StrictMatchClient && strings.Index(entry.IP, params.Client) == -1) {
|
2019-09-16 17:07:18 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-24 20:00:58 +03:00
|
|
|
func (l *queryLog) readFromFile(params getDataParams) ([]*logEntry, time.Time, int) {
|
2019-09-16 17:07:18 +03:00
|
|
|
entries := []*logEntry{}
|
2019-10-24 20:00:58 +03:00
|
|
|
oldest := time.Time{}
|
2019-09-16 17:07:18 +03:00
|
|
|
|
|
|
|
r := l.OpenReader()
|
|
|
|
if r == nil {
|
2019-10-24 20:00:58 +03:00
|
|
|
return entries, time.Time{}, 0
|
2019-09-16 17:07:18 +03:00
|
|
|
}
|
2019-10-24 20:00:58 +03:00
|
|
|
r.BeginRead(params.OlderThan, getDataLimit, ¶ms)
|
|
|
|
total := uint64(0)
|
|
|
|
for total <= maxSearchEntries {
|
2019-09-16 17:07:18 +03:00
|
|
|
newEntries := []*logEntry{}
|
|
|
|
for {
|
|
|
|
entry := r.Next()
|
|
|
|
if entry == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isNeeded(entry, params) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(newEntries) == getDataLimit {
|
|
|
|
newEntries = newEntries[1:]
|
|
|
|
}
|
|
|
|
newEntries = append(newEntries, entry)
|
|
|
|
}
|
2018-12-05 14:03:41 +03:00
|
|
|
|
2019-10-24 20:00:58 +03:00
|
|
|
log.Debug("entries: +%d (%d) [%d]", len(newEntries), len(entries), r.Total())
|
2019-09-16 17:07:18 +03:00
|
|
|
|
|
|
|
entries = append(newEntries, entries...)
|
|
|
|
if len(entries) > getDataLimit {
|
|
|
|
toremove := len(entries) - getDataLimit
|
|
|
|
entries = entries[toremove:]
|
|
|
|
break
|
|
|
|
}
|
2019-10-24 20:00:58 +03:00
|
|
|
if r.Total() == 0 || len(entries) == getDataLimit {
|
2019-09-16 17:07:18 +03:00
|
|
|
break
|
|
|
|
}
|
2019-10-24 20:00:58 +03:00
|
|
|
total += r.Total()
|
|
|
|
oldest = r.Oldest()
|
|
|
|
r.BeginReadPrev(getDataLimit)
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-16 17:07:18 +03:00
|
|
|
r.Close()
|
2019-10-24 20:00:58 +03:00
|
|
|
return entries, oldest, int(total)
|
2019-09-16 17:07:18 +03:00
|
|
|
}
|
|
|
|
|
2019-09-27 18:58:57 +03:00
|
|
|
// Parameters for getData()
|
|
|
|
type getDataParams struct {
|
|
|
|
OlderThan time.Time // return entries that are older than this value
|
|
|
|
Domain string // filter by domain name in question
|
|
|
|
Client string // filter by client IP
|
2019-10-24 20:00:58 +03:00
|
|
|
QuestionType string // filter by question type
|
2019-09-27 18:58:57 +03:00
|
|
|
ResponseStatus responseStatusType // filter by response status
|
|
|
|
StrictMatchDomain bool // if Domain value must be matched strictly
|
|
|
|
StrictMatchClient bool // if Client value must be matched strictly
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response status
|
|
|
|
type responseStatusType int32
|
|
|
|
|
|
|
|
// Response status constants
|
|
|
|
const (
|
|
|
|
responseStatusAll responseStatusType = iota + 1
|
|
|
|
responseStatusFiltered
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get log entries
|
2019-10-24 20:00:58 +03:00
|
|
|
func (l *queryLog) getData(params getDataParams) map[string]interface{} {
|
2018-12-05 14:03:41 +03:00
|
|
|
var data = []map[string]interface{}{}
|
2019-09-16 17:07:18 +03:00
|
|
|
|
2019-10-24 20:00:58 +03:00
|
|
|
var oldest time.Time
|
2019-09-16 17:07:18 +03:00
|
|
|
now := time.Now()
|
|
|
|
entries := []*logEntry{}
|
|
|
|
total := 0
|
|
|
|
|
|
|
|
// add from file
|
2019-10-24 20:00:58 +03:00
|
|
|
entries, oldest, total = l.readFromFile(params)
|
2019-09-16 17:07:18 +03:00
|
|
|
|
|
|
|
if params.OlderThan.IsZero() {
|
|
|
|
params.OlderThan = now
|
|
|
|
}
|
|
|
|
|
|
|
|
// add from memory buffer
|
2019-09-27 18:58:57 +03:00
|
|
|
l.bufferLock.Lock()
|
|
|
|
total += len(l.buffer)
|
|
|
|
for _, entry := range l.buffer {
|
2019-09-16 17:07:18 +03:00
|
|
|
|
|
|
|
if !isNeeded(entry, params) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.Time.UnixNano() >= params.OlderThan.UnixNano() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) == getDataLimit {
|
|
|
|
entries = entries[1:]
|
|
|
|
}
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
2019-09-27 18:58:57 +03:00
|
|
|
l.bufferLock.Unlock()
|
2019-09-16 17:07:18 +03:00
|
|
|
|
|
|
|
// process the elements from latest to oldest
|
|
|
|
for i := len(entries) - 1; i >= 0; i-- {
|
|
|
|
entry := entries[i]
|
2018-12-05 14:03:41 +03:00
|
|
|
var a *dns.Msg
|
|
|
|
|
|
|
|
if len(entry.Answer) > 0 {
|
|
|
|
a = new(dns.Msg)
|
|
|
|
if err := a.Unpack(entry.Answer); err != nil {
|
2019-10-24 20:00:58 +03:00
|
|
|
log.Debug("Failed to unpack dns message answer: %s: %s", err, string(entry.Answer))
|
2018-12-05 14:03:41 +03:00
|
|
|
a = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonEntry := map[string]interface{}{
|
|
|
|
"reason": entry.Result.Reason.String(),
|
|
|
|
"elapsedMs": strconv.FormatFloat(entry.Elapsed.Seconds()*1000, 'f', -1, 64),
|
2019-09-16 17:07:18 +03:00
|
|
|
"time": entry.Time.Format(time.RFC3339Nano),
|
2018-12-05 14:03:41 +03:00
|
|
|
"client": entry.IP,
|
|
|
|
}
|
2019-11-12 18:14:33 +03:00
|
|
|
jsonEntry["question"] = map[string]interface{}{
|
2019-10-24 20:00:58 +03:00
|
|
|
"host": entry.QHost,
|
|
|
|
"type": entry.QType,
|
|
|
|
"class": entry.QClass,
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if a != nil {
|
|
|
|
jsonEntry["status"] = dns.RcodeToString[a.Rcode]
|
|
|
|
}
|
|
|
|
if len(entry.Result.Rule) > 0 {
|
|
|
|
jsonEntry["rule"] = entry.Result.Rule
|
|
|
|
jsonEntry["filterId"] = entry.Result.FilterID
|
|
|
|
}
|
|
|
|
|
2019-07-23 12:21:37 +03:00
|
|
|
if len(entry.Result.ServiceName) != 0 {
|
|
|
|
jsonEntry["service_name"] = entry.Result.ServiceName
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:11:01 +03:00
|
|
|
answers := answerToMap(a)
|
|
|
|
if answers != nil {
|
2018-12-05 14:03:41 +03:00
|
|
|
jsonEntry["answer"] = answers
|
|
|
|
}
|
|
|
|
|
|
|
|
data = append(data, jsonEntry)
|
|
|
|
}
|
|
|
|
|
2019-09-16 17:07:18 +03:00
|
|
|
log.Debug("QueryLog: prepared data (%d/%d) older than %s in %s",
|
|
|
|
len(entries), total, params.OlderThan, time.Since(now))
|
2019-10-24 20:00:58 +03:00
|
|
|
|
|
|
|
var result = map[string]interface{}{}
|
|
|
|
if len(entries) == getDataLimit {
|
|
|
|
oldest = entries[0].Time
|
|
|
|
}
|
|
|
|
result["oldest"] = ""
|
|
|
|
if !oldest.IsZero() {
|
|
|
|
result["oldest"] = oldest.Format(time.RFC3339Nano)
|
|
|
|
}
|
|
|
|
result["data"] = data
|
|
|
|
return result
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
2018-12-24 23:06:36 +03:00
|
|
|
|
2019-01-24 20:11:01 +03:00
|
|
|
func answerToMap(a *dns.Msg) []map[string]interface{} {
|
|
|
|
if a == nil || len(a.Answer) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var answers = []map[string]interface{}{}
|
|
|
|
for _, k := range a.Answer {
|
|
|
|
header := k.Header()
|
|
|
|
answer := map[string]interface{}{
|
|
|
|
"type": dns.TypeToString[header.Rrtype],
|
|
|
|
"ttl": header.Ttl,
|
|
|
|
}
|
|
|
|
// try most common record types
|
|
|
|
switch v := k.(type) {
|
|
|
|
case *dns.A:
|
2019-11-19 14:09:40 +03:00
|
|
|
answer["value"] = v.A.String()
|
2019-01-24 20:11:01 +03:00
|
|
|
case *dns.AAAA:
|
2019-11-19 14:09:40 +03:00
|
|
|
answer["value"] = v.AAAA.String()
|
2019-01-24 20:11:01 +03:00
|
|
|
case *dns.MX:
|
|
|
|
answer["value"] = fmt.Sprintf("%v %v", v.Preference, v.Mx)
|
|
|
|
case *dns.CNAME:
|
|
|
|
answer["value"] = v.Target
|
|
|
|
case *dns.NS:
|
|
|
|
answer["value"] = v.Ns
|
|
|
|
case *dns.SPF:
|
|
|
|
answer["value"] = v.Txt
|
|
|
|
case *dns.TXT:
|
|
|
|
answer["value"] = v.Txt
|
|
|
|
case *dns.PTR:
|
|
|
|
answer["value"] = v.Ptr
|
|
|
|
case *dns.SOA:
|
|
|
|
answer["value"] = fmt.Sprintf("%v %v %v %v %v %v %v", v.Ns, v.Mbox, v.Serial, v.Refresh, v.Retry, v.Expire, v.Minttl)
|
|
|
|
case *dns.CAA:
|
|
|
|
answer["value"] = fmt.Sprintf("%v %v \"%v\"", v.Flag, v.Tag, v.Value)
|
|
|
|
case *dns.HINFO:
|
|
|
|
answer["value"] = fmt.Sprintf("\"%v\" \"%v\"", v.Cpu, v.Os)
|
|
|
|
case *dns.RRSIG:
|
|
|
|
answer["value"] = fmt.Sprintf("%v %v %v %v %v %v %v %v %v", dns.TypeToString[v.TypeCovered], v.Algorithm, v.Labels, v.OrigTtl, v.Expiration, v.Inception, v.KeyTag, v.SignerName, v.Signature)
|
|
|
|
default:
|
|
|
|
// type unknown, marshall it as-is
|
|
|
|
answer["value"] = v
|
|
|
|
}
|
|
|
|
answers = append(answers, answer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return answers
|
|
|
|
}
|