2021-09-28 12:42:01 +02:00
|
|
|
package netxlite
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import "github.com/miekg/dns"
|
|
|
|
|
2021-09-28 10:47:59 +02:00
|
|
|
// The DNSEncoder encodes DNS queries to bytes
|
|
|
|
type DNSEncoder interface {
|
2021-09-29 20:21:25 +02:00
|
|
|
// Encode transforms its arguments into a serialized DNS query.
|
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
//
|
|
|
|
// - domain is the domain for the query (e.g., x.org);
|
|
|
|
//
|
|
|
|
// - qtype is the query type (e.g., dns.TypeA);
|
|
|
|
//
|
|
|
|
// - padding is whether to add padding to the query.
|
|
|
|
//
|
|
|
|
// On success, this function returns a valid byte array and
|
|
|
|
// a nil error. On failure, we have an error and the byte array is nil.
|
2021-02-02 12:05:47 +01:00
|
|
|
Encode(domain string, qtype uint16, padding bool) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:47:59 +02:00
|
|
|
// DNSEncoderMiekg uses github.com/miekg/dns to implement the Encoder.
|
|
|
|
type DNSEncoderMiekg struct{}
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
const (
|
2021-09-28 10:47:59 +02:00
|
|
|
// dnsPaddingDesiredBlockSize is the size that the padded query should be multiple of
|
|
|
|
dnsPaddingDesiredBlockSize = 128
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2021-09-28 10:47:59 +02:00
|
|
|
// dnsEDNS0MaxResponseSize is the maximum response size for EDNS0
|
|
|
|
dnsEDNS0MaxResponseSize = 4096
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2021-09-28 10:47:59 +02:00
|
|
|
// dnsDNSSECEnabled turns on support for DNSSEC when using EDNS0
|
|
|
|
dnsDNSSECEnabled = true
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-09-28 10:47:59 +02:00
|
|
|
func (e *DNSEncoderMiekg) Encode(domain string, qtype uint16, padding bool) ([]byte, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
question := dns.Question{
|
|
|
|
Name: dns.Fqdn(domain),
|
|
|
|
Qtype: qtype,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}
|
|
|
|
query := new(dns.Msg)
|
|
|
|
query.Id = dns.Id()
|
|
|
|
query.RecursionDesired = true
|
|
|
|
query.Question = make([]dns.Question, 1)
|
|
|
|
query.Question[0] = question
|
|
|
|
if padding {
|
2021-09-28 10:47:59 +02:00
|
|
|
query.SetEdns0(dnsEDNS0MaxResponseSize, dnsDNSSECEnabled)
|
2021-02-02 12:05:47 +01:00
|
|
|
// Clients SHOULD pad queries to the closest multiple of
|
|
|
|
// 128 octets RFC8467#section-4.1. We inflate the query
|
|
|
|
// length by the size of the option (i.e. 4 octets). The
|
|
|
|
// cast to uint is necessary to make the modulus operation
|
|
|
|
// work as intended when the desiredBlockSize is smaller
|
|
|
|
// than (query.Len()+4) ¯\_(ツ)_/¯.
|
2021-09-28 10:47:59 +02:00
|
|
|
remainder := (dnsPaddingDesiredBlockSize - uint(query.Len()+4)) % dnsPaddingDesiredBlockSize
|
2021-02-02 12:05:47 +01:00
|
|
|
opt := new(dns.EDNS0_PADDING)
|
|
|
|
opt.Padding = make([]byte, remainder)
|
|
|
|
query.IsEdns0().Option = append(query.IsEdns0().Option, opt)
|
|
|
|
}
|
|
|
|
return query.Pack()
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:47:59 +02:00
|
|
|
var _ DNSEncoder = &DNSEncoderMiekg{}
|