sometimes answer can be empty, therefore question could be lost -- pass both to querylog

This commit is contained in:
Eugene Bujak 2018-09-05 21:25:11 +03:00
parent 606bed9d20
commit e122d9138b

View File

@ -19,24 +19,26 @@ import (
var logBuffer = ring.Ring{} var logBuffer = ring.Ring{}
type logEntry struct { type logEntry struct {
R *dns.Msg Question *dns.Msg
Result dnsfilter.Result Answer *dns.Msg
Time time.Time Result dnsfilter.Result
Elapsed time.Duration Time time.Time
IP string Elapsed time.Duration
IP string
} }
func init() { func init() {
logBuffer.SetCapacity(1000) logBuffer.SetCapacity(1000)
} }
func logRequest(r *dns.Msg, result dnsfilter.Result, elapsed time.Duration, ip string) { func logRequest(question *dns.Msg, answer *dns.Msg, result dnsfilter.Result, elapsed time.Duration, ip string) {
entry := logEntry{ entry := logEntry{
R: r, Question: question,
Result: result, Answer: answer,
Time: time.Now(), Result: result,
Elapsed: elapsed, Time: time.Now(),
IP: ip, Elapsed: elapsed,
IP: ip,
} }
logBuffer.Enqueue(entry) logBuffer.Enqueue(entry)
} }
@ -57,21 +59,21 @@ func handler(w http.ResponseWriter, r *http.Request) {
"client": entry.IP, "client": entry.IP,
} }
question := map[string]interface{}{ question := map[string]interface{}{
"host": strings.ToLower(strings.TrimSuffix(entry.R.Question[0].Name, ".")), "host": strings.ToLower(strings.TrimSuffix(entry.Question.Question[0].Name, ".")),
"type": dns.Type(entry.R.Question[0].Qtype).String(), "type": dns.Type(entry.Question.Question[0].Qtype).String(),
"class": dns.Class(entry.R.Question[0].Qclass).String(), "class": dns.Class(entry.Question.Question[0].Qclass).String(),
} }
jsonentry["question"] = question jsonentry["question"] = question
status, _ := response.Typify(entry.R, time.Now().UTC()) status, _ := response.Typify(entry.Answer, time.Now().UTC())
jsonentry["status"] = status.String() jsonentry["status"] = status.String()
if len(entry.Result.Rule) > 0 { if len(entry.Result.Rule) > 0 {
jsonentry["rule"] = entry.Result.Rule jsonentry["rule"] = entry.Result.Rule
} }
if len(entry.R.Answer) > 0 { if entry.Answer != nil && len(entry.Answer.Answer) > 0 {
var answers = []map[string]interface{}{} var answers = []map[string]interface{}{}
for _, k := range entry.R.Answer { for _, k := range entry.Answer.Answer {
header := k.Header() header := k.Header()
answer := map[string]interface{}{ answer := map[string]interface{}{
"type": dns.TypeToString[header.Rrtype], "type": dns.TypeToString[header.Rrtype],