dnscrypt-proxy/dnscrypt-proxy/plugin_query_log.go

102 lines
2.7 KiB
Go
Raw Normal View History

2018-01-16 18:21:17 +01:00
package main
import (
"errors"
"fmt"
"net"
"strings"
2018-01-16 18:21:17 +01:00
"time"
"github.com/jedisct1/dlog"
"github.com/miekg/dns"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
2018-01-16 18:21:17 +01:00
)
type PluginQueryLog struct {
logger *lumberjack.Logger
format string
ignoredQtypes []string
2018-01-16 18:21:17 +01:00
}
func (plugin *PluginQueryLog) Name() string {
return "query_log"
}
func (plugin *PluginQueryLog) Description() string {
return "Log DNS queries."
}
func (plugin *PluginQueryLog) Init(proxy *Proxy) error {
plugin.logger = &lumberjack.Logger{LocalTime: true, MaxSize: proxy.logMaxSize, MaxAge: proxy.logMaxAge, MaxBackups: proxy.logMaxBackups, Filename: proxy.queryLogFile, Compress: true}
2018-01-16 18:21:17 +01:00
plugin.format = proxy.queryLogFormat
plugin.ignoredQtypes = proxy.queryLogIgnoredQtypes
2018-01-16 18:21:17 +01:00
return nil
}
func (plugin *PluginQueryLog) Drop() error {
return nil
}
func (plugin *PluginQueryLog) Reload() error {
return nil
}
func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) error {
questions := msg.Question
if len(questions) == 0 {
return nil
}
question := questions[0]
qType, ok := dns.TypeToString[question.Qtype]
if !ok {
qType = string(qType)
}
if len(plugin.ignoredQtypes) > 0 {
for _, ignoredQtype := range plugin.ignoredQtypes {
if strings.EqualFold(ignoredQtype, qType) {
return nil
}
}
}
2018-01-16 18:21:17 +01:00
var clientIPStr string
if pluginsState.clientProto == "udp" {
clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String()
} else {
clientIPStr = (*pluginsState.clientAddr).(*net.TCPAddr).IP.String()
}
2018-01-17 17:03:42 +01:00
qName := StripTrailingDot(question.Name)
2018-06-04 23:18:28 +02:00
returnCode, ok := PluginsReturnCodeToString[pluginsState.returnCode]
if !ok {
returnCode = string(returnCode)
}
var requestDuration time.Duration
if !pluginsState.requestStart.IsZero() && !pluginsState.requestEnd.IsZero() {
requestDuration = pluginsState.requestEnd.Sub(pluginsState.requestStart)
}
2018-01-16 18:21:17 +01:00
var line string
if plugin.format == "tsv" {
now := time.Now()
year, month, day := now.Date()
hour, minute, second := now.Clock()
tsStr := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d]", year, int(month), day, hour, minute, second)
line = fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%dms\n", tsStr, clientIPStr, StringQuote(qName), qType, returnCode, requestDuration/time.Millisecond)
2018-01-16 18:21:17 +01:00
} else if plugin.format == "ltsv" {
cached := 0
if pluginsState.cacheHit {
cached = 1
}
line = fmt.Sprintf("time:%d\thost:%s\tmessage:%s\ttype:%s\treturn:%s\tcached:%d\tduration:%d\n",
time.Now().Unix(), clientIPStr, StringQuote(qName), qType, returnCode, cached, requestDuration/time.Millisecond)
2018-01-16 18:21:17 +01:00
} else {
dlog.Fatalf("Unexpected log format: [%s]", plugin.format)
}
if plugin.logger == nil {
2018-01-16 18:21:17 +01:00
return errors.New("Log file not initialized")
}
plugin.logger.Write([]byte(line))
2018-01-16 18:21:17 +01:00
return nil
}