2018-01-16 18:21:17 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-07-08 11:36:58 +02:00
|
|
|
"io"
|
2018-01-16 18:21:17 +01:00
|
|
|
"net"
|
2018-01-19 12:57:47 +01:00
|
|
|
"strings"
|
2018-01-16 18:21:17 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jedisct1/dlog"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PluginQueryLog struct {
|
2020-07-08 11:36:58 +02:00
|
|
|
logger io.Writer
|
2018-01-20 13:27:37 +01:00
|
|
|
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 {
|
2020-07-08 13:45:25 +02:00
|
|
|
plugin.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.queryLogFile)
|
2018-01-16 18:21:17 +01:00
|
|
|
plugin.format = proxy.queryLogFormat
|
2018-01-20 13:27:37 +01:00
|
|
|
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 {
|
2023-05-13 11:22:52 +02:00
|
|
|
var clientIPStr string
|
|
|
|
switch pluginsState.clientProto {
|
|
|
|
case "udp":
|
|
|
|
clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String()
|
|
|
|
case "tcp", "local_doh":
|
|
|
|
clientIPStr = (*pluginsState.clientAddr).(*net.TCPAddr).IP.String()
|
|
|
|
default:
|
|
|
|
// Ignore internal flow.
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-17 09:38:53 +01:00
|
|
|
question := msg.Question[0]
|
2018-01-19 12:57:47 +01:00
|
|
|
qType, ok := dns.TypeToString[question.Qtype]
|
|
|
|
if !ok {
|
|
|
|
qType = string(qType)
|
|
|
|
}
|
2018-01-20 13:27:37 +01:00
|
|
|
if len(plugin.ignoredQtypes) > 0 {
|
|
|
|
for _, ignoredQtype := range plugin.ignoredQtypes {
|
|
|
|
if strings.EqualFold(ignoredQtype, qType) {
|
|
|
|
return nil
|
2018-01-19 12:57:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 09:38:53 +01:00
|
|
|
qName := pluginsState.qName
|
2019-09-11 14:28:33 +02:00
|
|
|
|
2020-03-13 17:50:58 +01:00
|
|
|
if pluginsState.cacheHit {
|
2019-09-11 14:28:33 +02:00
|
|
|
pluginsState.serverName = "-"
|
2019-09-11 14:34:18 +02:00
|
|
|
} else {
|
|
|
|
switch pluginsState.returnCode {
|
|
|
|
case PluginsReturnCodeSynth, PluginsReturnCodeCloak, PluginsReturnCodeParseError:
|
|
|
|
pluginsState.serverName = "-"
|
|
|
|
}
|
2019-09-11 14:28:33 +02:00
|
|
|
}
|
2018-06-04 23:18:28 +02:00
|
|
|
returnCode, ok := PluginsReturnCodeToString[pluginsState.returnCode]
|
|
|
|
if !ok {
|
|
|
|
returnCode = string(returnCode)
|
|
|
|
}
|
2018-01-19 12:57:47 +01:00
|
|
|
|
2019-05-28 22:42:22 +02:00
|
|
|
var requestDuration time.Duration
|
2019-05-26 21:16:47 +02:00
|
|
|
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)
|
2022-03-23 17:48:48 +01:00
|
|
|
line = fmt.Sprintf(
|
|
|
|
"%s\t%s\t%s\t%s\t%s\t%dms\t%s\n",
|
|
|
|
tsStr,
|
|
|
|
clientIPStr,
|
|
|
|
StringQuote(qName),
|
|
|
|
qType,
|
|
|
|
returnCode,
|
|
|
|
requestDuration/time.Millisecond,
|
|
|
|
StringQuote(pluginsState.serverName),
|
|
|
|
)
|
2018-01-16 18:21:17 +01:00
|
|
|
} else if plugin.format == "ltsv" {
|
2019-06-03 13:16:59 +02:00
|
|
|
cached := 0
|
|
|
|
if pluginsState.cacheHit {
|
|
|
|
cached = 1
|
|
|
|
}
|
2019-06-03 16:44:09 +02:00
|
|
|
line = fmt.Sprintf("time:%d\thost:%s\tmessage:%s\ttype:%s\treturn:%s\tcached:%d\tduration:%d\tserver:%s\n",
|
|
|
|
time.Now().Unix(), clientIPStr, StringQuote(qName), qType, returnCode, cached, requestDuration/time.Millisecond, StringQuote(pluginsState.serverName))
|
2018-01-16 18:21:17 +01:00
|
|
|
} else {
|
|
|
|
dlog.Fatalf("Unexpected log format: [%s]", plugin.format)
|
|
|
|
}
|
2018-03-02 10:34:00 +01:00
|
|
|
if plugin.logger == nil {
|
2018-01-16 18:21:17 +01:00
|
|
|
return errors.New("Log file not initialized")
|
|
|
|
}
|
2019-12-09 12:55:26 +01:00
|
|
|
_, _ = plugin.logger.Write([]byte(line))
|
|
|
|
|
2018-01-16 18:21:17 +01:00
|
|
|
return nil
|
|
|
|
}
|