From 34feec9f5d267247b7a62cadb379e10712beadab Mon Sep 17 00:00:00 2001 From: leixiang Date: Sat, 1 Aug 2020 13:26:35 +0800 Subject: [PATCH] json-dns/response.go: Fix variant question response in Response.Question Known affected DoH server: https://www.alidns.com/faqs/#dns-safe --- json-dns/response.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/json-dns/response.go b/json-dns/response.go index c22c73f..1d71ad5 100644 --- a/json-dns/response.go +++ b/json-dns/response.go @@ -24,9 +24,29 @@ package jsonDNS import ( + "encoding/json" "time" ) +type QuestionList []Question + +// Fix variant question response in Response.Question +// +// Solution taken from: +// https://engineering.bitnami.com/articles/dealing-with-json-with-non-homogeneous-types-in-go.html +// https://archive.is/NU4zR +func (ql *QuestionList) UnmarshalJSON(b []byte) error { + if len(b) > 0 && b[0] == '[' { + return json.Unmarshal(b, (*[]Question)(ql)) + } + var q Question + if err := json.Unmarshal(b, &q); err != nil { + return err + } + *ql = []Question{q} + return nil +} + type Response struct { // Standard DNS response code (32 bit integer) Status uint32 `json:"Status"` @@ -40,13 +60,13 @@ type Response struct { // FIXME: We don't have DNSSEC yet! This bit is not reliable! AD bool `json:"AD"` // Whether the client asked to disable DNSSEC - CD bool `json:"CD"` - Question []Question `json:"Question"` - Answer []RR `json:"Answer,omitempty"` - Authority []RR `json:"Authority,omitempty"` - Additional []RR `json:"Additional,omitempty"` - Comment string `json:"Comment,omitempty"` - EdnsClientSubnet string `json:"edns_client_subnet,omitempty"` + CD bool `json:"CD"` + Question QuestionList `json:"Question"` + Answer []RR `json:"Answer,omitempty"` + Authority []RR `json:"Authority,omitempty"` + Additional []RR `json:"Additional,omitempty"` + Comment string `json:"Comment,omitempty"` + EdnsClientSubnet string `json:"edns_client_subnet,omitempty"` // Least time-to-live HaveTTL bool `json:"-"` LeastTTL uint32 `json:"-"`