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-08-26 11:54:38 +03:00
|
|
|
logBufferCap = 5000 // maximum capacity of logBuffer before it's flushed to disk
|
|
|
|
queryLogFileName = "querylog.json" // .gz added during compression
|
|
|
|
queryLogSize = 5000 // maximum API response for /querylog
|
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-08-26 11:54:38 +03:00
|
|
|
conf Config
|
2019-08-08 12:56:02 +03:00
|
|
|
logFile string // path to the log file
|
2019-02-10 20:47:43 +03:00
|
|
|
|
2018-12-05 14:03:41 +03:00
|
|
|
logBufferLock sync.RWMutex
|
|
|
|
logBuffer []*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
|
2018-12-05 14:03:41 +03:00
|
|
|
|
2019-08-26 11:54:38 +03:00
|
|
|
cache []*logEntry
|
|
|
|
lock sync.RWMutex
|
2019-02-10 20:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newQueryLog creates 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)
|
|
|
|
l.conf = conf
|
|
|
|
go l.periodicQueryLogRotate()
|
|
|
|
go l.fillFromFile()
|
|
|
|
return &l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) Close() {
|
|
|
|
_ = l.flushLogBuffer(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) Configure(conf Config) {
|
|
|
|
l.conf = conf
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear memory buffer and remove the file
|
|
|
|
func (l *queryLog) Clear() {
|
|
|
|
l.fileFlushLock.Lock()
|
|
|
|
defer l.fileFlushLock.Unlock()
|
|
|
|
|
|
|
|
l.logBufferLock.Lock()
|
|
|
|
l.logBuffer = nil
|
|
|
|
l.flushPending = false
|
|
|
|
l.logBufferLock.Unlock()
|
|
|
|
|
|
|
|
l.lock.Lock()
|
|
|
|
l.cache = nil
|
|
|
|
l.lock.Unlock()
|
|
|
|
|
|
|
|
err := os.Remove(l.logFile + ".1")
|
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
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 {
|
|
|
|
Question []byte
|
|
|
|
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
|
|
|
Time time.Time
|
|
|
|
Elapsed time.Duration
|
|
|
|
IP string
|
|
|
|
Upstream string `json:",omitempty"` // if empty, means it was cached
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:54:38 +03:00
|
|
|
// getIPString is a helper function that extracts IP address from net.Addr
|
|
|
|
func getIPString(addr net.Addr) string {
|
|
|
|
switch addr := addr.(type) {
|
|
|
|
case *net.UDPAddr:
|
|
|
|
return addr.IP.String()
|
|
|
|
case *net.TCPAddr:
|
|
|
|
return addr.IP.String()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) Add(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Result, elapsed time.Duration, addr net.Addr, upstream string) {
|
2018-12-05 14:03:41 +03:00
|
|
|
var q []byte
|
|
|
|
var a []byte
|
|
|
|
var err error
|
2019-08-26 11:54:38 +03:00
|
|
|
ip := getIPString(addr)
|
2018-12-05 14:03:41 +03:00
|
|
|
|
|
|
|
if question != nil {
|
|
|
|
q, err = question.Pack()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to pack question for querylog: %s", err)
|
2019-08-26 11:54:38 +03:00
|
|
|
return
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
|
|
|
}
|
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{
|
|
|
|
Question: q,
|
|
|
|
Answer: a,
|
2018-12-06 17:27:38 +03:00
|
|
|
Result: *result,
|
2018-12-05 14:03:41 +03:00
|
|
|
Time: now,
|
|
|
|
Elapsed: elapsed,
|
|
|
|
IP: ip,
|
|
|
|
Upstream: upstream,
|
|
|
|
}
|
|
|
|
|
2019-02-10 20:47:43 +03:00
|
|
|
l.logBufferLock.Lock()
|
|
|
|
l.logBuffer = append(l.logBuffer, &entry)
|
2019-05-15 13:11:36 +03:00
|
|
|
needFlush := false
|
|
|
|
if !l.flushPending {
|
|
|
|
needFlush = len(l.logBuffer) >= logBufferCap
|
|
|
|
if needFlush {
|
|
|
|
l.flushPending = true
|
|
|
|
}
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
2019-02-10 20:47:43 +03:00
|
|
|
l.logBufferLock.Unlock()
|
2019-08-26 11:54:38 +03:00
|
|
|
l.lock.Lock()
|
|
|
|
l.cache = append(l.cache, &entry)
|
|
|
|
if len(l.cache) > queryLogSize {
|
|
|
|
toremove := len(l.cache) - queryLogSize
|
|
|
|
l.cache = l.cache[toremove:]
|
2018-12-05 14:03:41 +03:00
|
|
|
}
|
2019-08-26 11:54:38 +03:00
|
|
|
l.lock.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-02-10 20:47:43 +03:00
|
|
|
// getQueryLogJson returns a map with the current query log ready to be converted to a JSON
|
2019-08-26 11:54:38 +03:00
|
|
|
func (l *queryLog) GetData() []map[string]interface{} {
|
|
|
|
l.lock.RLock()
|
|
|
|
values := make([]*logEntry, len(l.cache))
|
|
|
|
copy(values, l.cache)
|
|
|
|
l.lock.RUnlock()
|
2018-12-05 14:03:41 +03:00
|
|
|
|
|
|
|
// reverse it so that newest is first
|
|
|
|
for left, right := 0, len(values)-1; left < right; left, right = left+1, right-1 {
|
|
|
|
values[left], values[right] = values[right], values[left]
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:11:01 +03:00
|
|
|
// iterate
|
2018-12-05 14:03:41 +03:00
|
|
|
var data = []map[string]interface{}{}
|
|
|
|
for _, entry := range values {
|
|
|
|
var q *dns.Msg
|
|
|
|
var a *dns.Msg
|
|
|
|
|
|
|
|
if len(entry.Question) > 0 {
|
|
|
|
q = new(dns.Msg)
|
|
|
|
if err := q.Unpack(entry.Question); err != nil {
|
|
|
|
// ignore, log and move on
|
|
|
|
log.Printf("Failed to unpack dns message question: %s", err)
|
|
|
|
q = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(entry.Answer) > 0 {
|
|
|
|
a = new(dns.Msg)
|
|
|
|
if err := a.Unpack(entry.Answer); err != nil {
|
|
|
|
// ignore, log and move on
|
|
|
|
log.Printf("Failed to unpack dns message question: %s", err)
|
|
|
|
a = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonEntry := map[string]interface{}{
|
|
|
|
"reason": entry.Result.Reason.String(),
|
|
|
|
"elapsedMs": strconv.FormatFloat(entry.Elapsed.Seconds()*1000, 'f', -1, 64),
|
|
|
|
"time": entry.Time.Format(time.RFC3339),
|
|
|
|
"client": entry.IP,
|
|
|
|
}
|
|
|
|
if q != nil {
|
|
|
|
jsonEntry["question"] = map[string]interface{}{
|
|
|
|
"host": strings.ToLower(strings.TrimSuffix(q.Question[0].Name, ".")),
|
|
|
|
"type": dns.Type(q.Question[0].Qtype).String(),
|
|
|
|
"class": dns.Class(q.Question[0].Qclass).String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-10 20:47:43 +03:00
|
|
|
return data
|
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:
|
|
|
|
answer["value"] = v.A
|
|
|
|
case *dns.AAAA:
|
|
|
|
answer["value"] = v.AAAA
|
|
|
|
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
|
|
|
|
}
|