* querylog: don't return entries without Question data

This commit is contained in:
Simon Zolin 2019-11-12 18:14:33 +03:00
parent ec5c5e8109
commit 0d4e95b36d
1 changed files with 25 additions and 21 deletions

View File

@ -124,13 +124,15 @@ func (l *queryLog) Add(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Res
var err error var err error
ip := getIPString(addr) ip := getIPString(addr)
if question != nil { if question == nil {
return
}
q, err = question.Pack() q, err = question.Pack()
if err != nil { if err != nil {
log.Printf("failed to pack question for querylog: %s", err) log.Printf("failed to pack question for querylog: %s", err)
return return
} }
}
if answer != nil { if answer != nil {
a, err = answer.Pack() a, err = answer.Pack()
@ -333,19 +335,23 @@ func (l *queryLog) getData(params getDataParams) []map[string]interface{} {
var q *dns.Msg var q *dns.Msg
var a *dns.Msg var a *dns.Msg
if len(entry.Question) > 0 { if len(entry.Question) == 0 {
continue
}
q = new(dns.Msg) q = new(dns.Msg)
if err := q.Unpack(entry.Question); err != nil { if err := q.Unpack(entry.Question); err != nil {
// ignore, log and move on log.Tracef("q.Unpack(): %s", err)
log.Printf("Failed to unpack dns message question: %s", err) continue
q = nil
} }
if len(q.Question) != 1 {
log.Tracef("len(q.Question) != 1")
continue
} }
if len(entry.Answer) > 0 { if len(entry.Answer) > 0 {
a = new(dns.Msg) a = new(dns.Msg)
if err := a.Unpack(entry.Answer); err != nil { if err := a.Unpack(entry.Answer); err != nil {
// ignore, log and move on log.Debug("Failed to unpack dns message answer: %s", err)
log.Printf("Failed to unpack dns message question: %s", err)
a = nil a = nil
} }
} }
@ -356,13 +362,11 @@ func (l *queryLog) getData(params getDataParams) []map[string]interface{} {
"time": entry.Time.Format(time.RFC3339Nano), "time": entry.Time.Format(time.RFC3339Nano),
"client": entry.IP, "client": entry.IP,
} }
if q != nil {
jsonEntry["question"] = map[string]interface{}{ jsonEntry["question"] = map[string]interface{}{
"host": strings.ToLower(strings.TrimSuffix(q.Question[0].Name, ".")), "host": strings.ToLower(strings.TrimSuffix(q.Question[0].Name, ".")),
"type": dns.Type(q.Question[0].Qtype).String(), "type": dns.Type(q.Question[0].Qtype).String(),
"class": dns.Class(q.Question[0].Qclass).String(), "class": dns.Class(q.Question[0].Qclass).String(),
} }
}
if a != nil { if a != nil {
jsonEntry["status"] = dns.RcodeToString[a.Rcode] jsonEntry["status"] = dns.RcodeToString[a.Rcode]