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" "fmt"
"net" "net"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter" "github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
@ -139,48 +140,60 @@ func resultRulesToJSONRules(rules []*dnsfilter.ResultRule) (jsonRules []jobject)
return jsonRules 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 { if a == nil || len(a.Answer) == 0 {
return nil return nil
} }
answers = []jobject{} answers = make([]*dnsAnswer, 0, len(a.Answer))
for _, k := range a.Answer { for _, k := range a.Answer {
header := k.Header() header := k.Header()
answer := jobject{ answer := &dnsAnswer{
"type": dns.TypeToString[header.Rrtype], Type: dns.TypeToString[header.Rrtype],
"ttl": header.Ttl, 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) { switch v := k.(type) {
case nil:
// Probably unlikely, but go on.
case *dns.A: case *dns.A:
answer["value"] = v.A answer.Value = v.A.String()
case *dns.AAAA: case *dns.AAAA:
answer["value"] = v.AAAA answer.Value = v.AAAA.String()
case *dns.MX: 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: case *dns.CNAME:
answer["value"] = v.Target answer.Value = v.Target
case *dns.NS: case *dns.NS:
answer["value"] = v.Ns answer.Value = v.Ns
case *dns.SPF: case *dns.SPF:
answer["value"] = v.Txt answer.Value = strings.Join(v.Txt, "\n")
case *dns.TXT: case *dns.TXT:
answer["value"] = v.Txt answer.Value = strings.Join(v.Txt, "\n")
case *dns.PTR: case *dns.PTR:
answer["value"] = v.Ptr answer.Value = v.Ptr
case *dns.SOA: 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: 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: 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: 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: default:
// type unknown, marshall it as-is answer.Value = v.String()
answer["value"] = v
} }
answers = append(answers, answer) answers = append(answers, answer)
} }

View file

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