diff --git a/dnscrypt-proxy/config.go b/dnscrypt-proxy/config.go index 479e5ffc..b78058a0 100644 --- a/dnscrypt-proxy/config.go +++ b/dnscrypt-proxy/config.go @@ -93,7 +93,7 @@ func ConfigLoad(proxy *Proxy, config_file string) error { } else { config.QueryLog.Format = strings.ToLower(config.QueryLog.Format) } - if config.QueryLog.Format != "tsv" { + if config.QueryLog.Format != "tsv" && config.QueryLog.Format != "ltsv" { return errors.New("Unsupported query log format") } proxy.queryLogFile = config.QueryLog.File diff --git a/dnscrypt-proxy/dnscrypt-proxy.toml b/dnscrypt-proxy/dnscrypt-proxy.toml index 88e1322f..c161cb41 100644 --- a/dnscrypt-proxy/dnscrypt-proxy.toml +++ b/dnscrypt-proxy/dnscrypt-proxy.toml @@ -56,7 +56,7 @@ block_ipv6 = false ### Full path to the query log file file = "/tmp/query.log" -### Query log format (currently supported: tsv) +### Query log format (currently supported: tsv and ltsv) format = "tsv" diff --git a/dnscrypt-proxy/plugins.go b/dnscrypt-proxy/plugins.go index 00630cf2..7868d14f 100644 --- a/dnscrypt-proxy/plugins.go +++ b/dnscrypt-proxy/plugins.go @@ -12,6 +12,7 @@ import ( "time" lru "github.com/hashicorp/golang-lru" + "github.com/jedisct1/dlog" "github.com/miekg/dns" ) @@ -225,7 +226,8 @@ func (plugin *PluginBlockIPv6) Eval(pluginsState *PluginsState, msg *dns.Msg) er type PluginQueryLog struct { sync.Mutex - outFd *os.File + outFd *os.File + format string } func (plugin *PluginQueryLog) Name() string { @@ -244,6 +246,7 @@ func (plugin *PluginQueryLog) Init(proxy *Proxy) error { return err } plugin.outFd = outFd + plugin.format = proxy.queryLogFormat return nil } @@ -262,10 +265,6 @@ func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) err return nil } question := questions[0] - 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) var clientIPStr string if pluginsState.clientProto == "udp" { clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String() @@ -280,7 +279,19 @@ func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) err if !ok { qType = string(qType) } - line := fmt.Sprintf("%s\t%s\t%s\t%s\n", tsStr, clientIPStr, qName, qType) + 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\n", tsStr, clientIPStr, qName, qType) + } else if plugin.format == "ltsv" { + line = fmt.Sprintf("time:%d\thost:%s\tmessage:%s\ttype:%s\n", + time.Now().Unix(), clientIPStr, qName, qType) + } else { + dlog.Fatalf("Unexpected log format: [%s]", plugin.format) + } plugin.Lock() if plugin.outFd == nil { return errors.New("Log file not initialized")