Pull request: querylog: make answer actually a string

Merge in DNS/adguard-home from 2609-dns-answer-string to master

Updates #2609.

Squashed commit of the following:

commit 1922f90c50f47735d8025d862fbf60bcf707140f
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Jan 28 15:19:11 2021 +0300

    querylog: stricter typing

commit 6ef73a45d822f77cfda44d8c093f272704d5afb6
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Jan 28 15:04:06 2021 +0300

    querylog: make answer actually a string
This commit is contained in:
Ainar Garipov 2021-01-28 16:00:29 +03:00
parent 154c9c1c26
commit 3af079a81b
2 changed files with 35 additions and 21 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
@ -139,48 +140,60 @@ func resultRulesToJSONRules(rules []*dnsfilter.ResultRule) (jsonRules []jobject)
return jsonRules
}
func answerToMap(a *dns.Msg) (answers []jobject) {
type dnsAnswer struct {
Type string `json:"type"`
Value string `json:"value"`
TTL uint32 `json:"ttl"`
}
func answerToMap(a *dns.Msg) (answers []*dnsAnswer) {
if a == nil || len(a.Answer) == 0 {
return nil
}
answers = []jobject{}
answers = make([]*dnsAnswer, 0, len(a.Answer))
for _, k := range a.Answer {
header := k.Header()
answer := jobject{
"type": dns.TypeToString[header.Rrtype],
"ttl": header.Ttl,
answer := &dnsAnswer{
Type: dns.TypeToString[header.Rrtype],
TTL: header.Ttl,
}
// try most common record types
// Some special treatment for some well-known types.
//
// TODO(a.garipov): Consider just calling String() for everyone
// instead.
switch v := k.(type) {
case nil:
// Probably unlikely, but go on.
case *dns.A:
answer["value"] = v.A
answer.Value = v.A.String()
case *dns.AAAA:
answer["value"] = v.AAAA
answer.Value = v.AAAA.String()
case *dns.MX:
answer["value"] = fmt.Sprintf("%v %v", v.Preference, v.Mx)
answer.Value = fmt.Sprintf("%v %v", v.Preference, v.Mx)
case *dns.CNAME:
answer["value"] = v.Target
answer.Value = v.Target
case *dns.NS:
answer["value"] = v.Ns
answer.Value = v.Ns
case *dns.SPF:
answer["value"] = v.Txt
answer.Value = strings.Join(v.Txt, "\n")
case *dns.TXT:
answer["value"] = v.Txt
answer.Value = strings.Join(v.Txt, "\n")
case *dns.PTR:
answer["value"] = v.Ptr
answer.Value = v.Ptr
case *dns.SOA:
answer["value"] = fmt.Sprintf("%v %v %v %v %v %v %v", v.Ns, v.Mbox, v.Serial, v.Refresh, v.Retry, v.Expire, v.Minttl)
answer.Value = fmt.Sprintf("%v %v %v %v %v %v %v", v.Ns, v.Mbox, v.Serial, v.Refresh, v.Retry, v.Expire, v.Minttl)
case *dns.CAA:
answer["value"] = fmt.Sprintf("%v %v \"%v\"", v.Flag, v.Tag, v.Value)
answer.Value = fmt.Sprintf("%v %v \"%v\"", v.Flag, v.Tag, v.Value)
case *dns.HINFO:
answer["value"] = fmt.Sprintf("\"%v\" \"%v\"", v.Cpu, v.Os)
answer.Value = fmt.Sprintf("\"%v\" \"%v\"", v.Cpu, v.Os)
case *dns.RRSIG:
answer["value"] = fmt.Sprintf("%v %v %v %v %v %v %v %v %v", dns.TypeToString[v.TypeCovered], v.Algorithm, v.Labels, v.OrigTtl, v.Expiration, v.Inception, v.KeyTag, v.SignerName, v.Signature)
answer.Value = fmt.Sprintf("%v %v %v %v %v %v %v %v %v", dns.TypeToString[v.TypeCovered], v.Algorithm, v.Labels, v.OrigTtl, v.Expiration, v.Inception, v.KeyTag, v.SignerName, v.Signature)
default:
// type unknown, marshall it as-is
answer["value"] = v
answer.Value = v.String()
}
answers = append(answers, answer)
}

View File

@ -1738,8 +1738,9 @@
'description': 'DNS answer section'
'properties':
'ttl':
'type': 'integer'
'example': 55
'format': 'uint32'
'type': 'integer'
'type':
'type': 'string'
'example': 'A'