dns-over-https/json-dns/response.go

93 lines
3.0 KiB
Go
Raw Permalink Normal View History

2017-09-17 16:55:31 +00:00
/*
2018-03-20 16:14:59 +00:00
DNS-over-HTTPS
Copyright (C) 2017-2018 Star Brilliant <m13253@hotmail.com>
2017-09-17 16:55:31 +00:00
2018-03-20 16:14:59 +00:00
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
2017-09-17 16:55:31 +00:00
2018-03-20 16:14:59 +00:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2017-09-17 16:55:31 +00:00
2018-03-20 16:14:59 +00:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
2017-09-17 16:55:31 +00:00
*/
2020-08-01 21:58:24 +00:00
package jsondns
2017-09-17 16:55:31 +00:00
import (
"encoding/json"
2017-09-17 16:55:31 +00:00
"time"
)
type QuestionList []Question
func (ql *QuestionList) UnmarshalJSON(b []byte) error {
2020-08-01 21:58:24 +00:00
// 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
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
}
2017-09-17 16:55:31 +00:00
type Response struct {
// Standard DNS response code (32 bit integer)
2018-03-20 16:14:59 +00:00
Status uint32 `json:"Status"`
2017-09-17 16:55:31 +00:00
// Whether the response is truncated
2018-03-20 16:14:59 +00:00
TC bool `json:"TC"`
2017-09-17 16:55:31 +00:00
// Recursion desired
2018-03-20 16:14:59 +00:00
RD bool `json:"RD"`
2017-09-17 16:55:31 +00:00
// Recursion available
2018-03-20 16:14:59 +00:00
RA bool `json:"RA"`
2017-09-17 16:55:31 +00:00
// Whether all response data was validated with DNSSEC
// FIXME: We don't have DNSSEC yet! This bit is not reliable!
2018-03-20 16:14:59 +00:00
AD bool `json:"AD"`
2017-09-17 16:55:31 +00:00
// Whether the client asked to disable DNSSEC
2020-08-01 21:58:24 +00:00
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"`
2017-09-17 16:55:31 +00:00
// Least time-to-live
2018-03-20 16:14:59 +00:00
HaveTTL bool `json:"-"`
LeastTTL uint32 `json:"-"`
EarliestExpires time.Time `json:"-"`
2017-09-17 16:55:31 +00:00
}
type Question struct {
// FQDN with trailing dot
2018-03-20 16:14:59 +00:00
Name string `json:"name"`
2017-09-17 16:55:31 +00:00
// Standard DNS RR type
2018-03-20 16:14:59 +00:00
Type uint16 `json:"type"`
2017-09-17 16:55:31 +00:00
}
type RR struct {
Question
// Record's time-to-live in seconds
2018-03-20 16:14:59 +00:00
TTL uint32 `json:"TTL"`
2017-09-17 16:55:31 +00:00
// TTL in absolute time
2018-03-20 16:14:59 +00:00
Expires time.Time `json:"-"`
ExpiresStr string `json:"Expires"`
2017-09-17 16:55:31 +00:00
// Data
2018-03-20 16:14:59 +00:00
Data string `json:"data"`
2017-09-17 16:55:31 +00:00
}