feat: Refactor output format
This commit is contained in:
parent
539e89e1fe
commit
4e5b074987
11 changed files with 447 additions and 384 deletions
|
@ -1,77 +1,92 @@
|
|||
package resolvers
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ClassicResolver represents the config options for setting up a Resolver.
|
||||
type ClassicResolver struct {
|
||||
client *dns.Client
|
||||
server string
|
||||
client *dns.Client
|
||||
server string
|
||||
resolverOptions Options
|
||||
}
|
||||
|
||||
// ClassicResolverOpts holds options for setting up a Classic resolver.
|
||||
type ClassicResolverOpts struct {
|
||||
IPv4Only bool
|
||||
IPv6Only bool
|
||||
Timeout time.Duration
|
||||
UseTLS bool
|
||||
UseTCP bool
|
||||
}
|
||||
|
||||
// NewClassicResolver accepts a list of nameservers and configures a DNS resolver.
|
||||
func NewClassicResolver(server string, opts ClassicResolverOpts) (Resolver, error) {
|
||||
func NewClassicResolver(server string, classicOpts ClassicResolverOpts, resolverOpts Options) (Resolver, error) {
|
||||
net := "udp"
|
||||
client := &dns.Client{
|
||||
Timeout: opts.Timeout,
|
||||
Timeout: resolverOpts.Timeout,
|
||||
Net: "udp",
|
||||
}
|
||||
|
||||
if opts.UseTCP {
|
||||
if classicOpts.UseTCP {
|
||||
net = "tcp"
|
||||
}
|
||||
|
||||
if opts.IPv4Only {
|
||||
if classicOpts.IPv4Only {
|
||||
net = net + "4"
|
||||
}
|
||||
if opts.IPv6Only {
|
||||
if classicOpts.IPv6Only {
|
||||
net = net + "6"
|
||||
}
|
||||
|
||||
if opts.UseTLS {
|
||||
if classicOpts.UseTLS {
|
||||
net = net + "-tls"
|
||||
}
|
||||
|
||||
client.Net = net
|
||||
|
||||
return &ClassicResolver{
|
||||
client: client,
|
||||
server: server,
|
||||
client: client,
|
||||
server: server,
|
||||
resolverOptions: resolverOpts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Lookup prepare a list of DNS messages to be sent to the server.
|
||||
// It's possible to send multiple question in one message
|
||||
// but some nameservers are not able to
|
||||
func (r *ClassicResolver) Lookup(questions []dns.Question) ([]Response, error) {
|
||||
// Lookup takes a dns.Question and sends them to DNS Server.
|
||||
// It parses the Response from the server in a custom output format.
|
||||
func (r *ClassicResolver) Lookup(question dns.Question) (Response, error) {
|
||||
var (
|
||||
messages = prepareMessages(questions)
|
||||
responses []Response
|
||||
rsp Response
|
||||
messages = prepareMessages(question, r.resolverOptions.Ndots, r.resolverOptions.SearchList)
|
||||
)
|
||||
|
||||
for _, msg := range messages {
|
||||
r.resolverOptions.Logger.WithFields(logrus.Fields{
|
||||
"domain": msg.Question[0].Name,
|
||||
"ndots": r.resolverOptions.Ndots,
|
||||
"nameserver": r.server,
|
||||
}).Debug("Attempting to resolve")
|
||||
in, rtt, err := r.client.Exchange(&msg, r.server)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return rsp, err
|
||||
}
|
||||
rsp := Response{
|
||||
Message: *in,
|
||||
RTT: rtt,
|
||||
Nameserver: r.server,
|
||||
// pack questions in output.
|
||||
for _, q := range msg.Question {
|
||||
ques := Question{
|
||||
Name: q.Name,
|
||||
Class: dns.ClassToString[q.Qclass],
|
||||
Type: dns.TypeToString[q.Qtype],
|
||||
}
|
||||
rsp.Questions = append(rsp.Questions, ques)
|
||||
}
|
||||
// get the authorities and answers.
|
||||
output := parseMessage(in, rtt, r.server)
|
||||
rsp.Authorities = output.Authorities
|
||||
rsp.Answers = output.Answers
|
||||
|
||||
if len(output.Answers) > 0 {
|
||||
// stop iterating the searchlist.
|
||||
break
|
||||
}
|
||||
responses = append(responses, rsp)
|
||||
}
|
||||
return responses, nil
|
||||
return rsp, nil
|
||||
}
|
||||
|
|
|
@ -9,20 +9,18 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// DOHResolver represents the config options for setting up a DOH based resolver.
|
||||
type DOHResolver struct {
|
||||
client *http.Client
|
||||
server string
|
||||
}
|
||||
|
||||
type DOHResolverOpts struct {
|
||||
Timeout time.Duration
|
||||
client *http.Client
|
||||
server string
|
||||
resolverOptions Options
|
||||
}
|
||||
|
||||
// NewDOHResolver accepts a nameserver address and configures a DOH based resolver.
|
||||
func NewDOHResolver(server string, opts DOHResolverOpts) (Resolver, error) {
|
||||
func NewDOHResolver(server string, resolverOpts Options) (Resolver, error) {
|
||||
// do basic validation
|
||||
u, err := url.ParseRequestURI(server)
|
||||
if err != nil {
|
||||
|
@ -32,52 +30,72 @@ func NewDOHResolver(server string, opts DOHResolverOpts) (Resolver, error) {
|
|||
return nil, fmt.Errorf("missing https in %s", server)
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Timeout: opts.Timeout,
|
||||
Timeout: resolverOpts.Timeout,
|
||||
}
|
||||
return &DOHResolver{
|
||||
client: httpClient,
|
||||
server: server,
|
||||
client: httpClient,
|
||||
server: server,
|
||||
resolverOptions: resolverOpts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *DOHResolver) Lookup(questions []dns.Question) ([]Response, error) {
|
||||
// Lookup takes a dns.Question and sends them to DNS Server.
|
||||
// It parses the Response from the server in a custom output format.
|
||||
func (r *DOHResolver) Lookup(question dns.Question) (Response, error) {
|
||||
var (
|
||||
messages = prepareMessages(questions)
|
||||
responses []Response
|
||||
rsp Response
|
||||
messages = prepareMessages(question, r.resolverOptions.Ndots, r.resolverOptions.SearchList)
|
||||
)
|
||||
|
||||
for _, msg := range messages {
|
||||
r.resolverOptions.Logger.WithFields(logrus.Fields{
|
||||
"domain": msg.Question[0].Name,
|
||||
"ndots": r.resolverOptions.Ndots,
|
||||
"nameserver": r.server,
|
||||
}).Debug("Attempting to resolve")
|
||||
// get the DNS Message in wire format.
|
||||
b, err := msg.Pack()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return rsp, err
|
||||
}
|
||||
now := time.Now()
|
||||
// Make an HTTP POST request to the DNS server with the DNS message as wire format bytes in the body.
|
||||
resp, err := d.client.Post(d.server, "application/dns-message", bytes.NewBuffer(b))
|
||||
resp, err := r.client.Post(r.server, "application/dns-message", bytes.NewBuffer(b))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return rsp, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("error from nameserver %s", resp.Status)
|
||||
return rsp, fmt.Errorf("error from nameserver %s", resp.Status)
|
||||
}
|
||||
rtt := time.Since(now)
|
||||
// extract the binary response in DNS Message.
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return rsp, err
|
||||
}
|
||||
|
||||
err = msg.Unpack(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return rsp, err
|
||||
}
|
||||
rsp := Response{
|
||||
Message: msg,
|
||||
RTT: rtt,
|
||||
Nameserver: d.server,
|
||||
// pack questions in output.
|
||||
for _, q := range msg.Question {
|
||||
ques := Question{
|
||||
Name: q.Name,
|
||||
Class: dns.ClassToString[q.Qclass],
|
||||
Type: dns.TypeToString[q.Qtype],
|
||||
}
|
||||
rsp.Questions = append(rsp.Questions, ques)
|
||||
}
|
||||
// get the authorities and answers.
|
||||
output := parseMessage(&msg, rtt, r.server)
|
||||
rsp.Authorities = output.Authorities
|
||||
rsp.Answers = output.Answers
|
||||
|
||||
if len(output.Answers) > 0 {
|
||||
// stop iterating the searchlist.
|
||||
break
|
||||
}
|
||||
responses = append(responses, rsp)
|
||||
}
|
||||
return responses, nil
|
||||
return rsp, nil
|
||||
}
|
||||
|
|
|
@ -4,20 +4,58 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Options represent a set of common options
|
||||
// to configure a Resolver.
|
||||
type Options struct {
|
||||
SearchList []string
|
||||
Ndots int
|
||||
Timeout time.Duration
|
||||
Logger *logrus.Logger
|
||||
}
|
||||
|
||||
// Resolver implements the configuration for a DNS
|
||||
// Client. Different types of providers can load
|
||||
// a DNS Resolver satisfying this interface.
|
||||
type Resolver interface {
|
||||
Lookup([]dns.Question) ([]Response, error)
|
||||
Lookup(dns.Question) (Response, error)
|
||||
}
|
||||
|
||||
// Response represents a custom output format
|
||||
// for DNS queries. It wraps metadata about the DNS query
|
||||
// and the DNS Answer as well.
|
||||
type Response struct {
|
||||
Message dns.Msg
|
||||
RTT time.Duration
|
||||
Nameserver string
|
||||
Answers []Answer `json:"answers"`
|
||||
Authorities []Authority `json:"authorities"`
|
||||
Questions []Question `json:"questions"`
|
||||
}
|
||||
|
||||
type Question struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Class string `json:"class"`
|
||||
}
|
||||
|
||||
type Answer struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Class string `json:"class"`
|
||||
TTL string `json:"ttl"`
|
||||
Address string `json:"address"`
|
||||
Status string `json:"status"`
|
||||
RTT string `json:"rtt"`
|
||||
Nameserver string `json:"nameserver"`
|
||||
}
|
||||
|
||||
type Authority struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Class string `json:"class"`
|
||||
TTL string `json:"ttl"`
|
||||
MName string `json:"mname"`
|
||||
Status string `json:"status"`
|
||||
RTT string `json:"rtt"`
|
||||
Nameserver string `json:"nameserver"`
|
||||
}
|
||||
|
|
|
@ -1,20 +1,156 @@
|
|||
package resolvers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// prepareMessages takes a slice fo `dns.Question`
|
||||
// and initialises `dns.Messages` for each question
|
||||
func prepareMessages(questions []dns.Question) []dns.Msg {
|
||||
var messages = make([]dns.Msg, 0, len(questions))
|
||||
for _, q := range questions {
|
||||
// prepareMessages takes a DNS Question and returns the
|
||||
// corresponding DNS messages for the same.
|
||||
func prepareMessages(q dns.Question, ndots int, searchList []string) []dns.Msg {
|
||||
var (
|
||||
possibleQNames = constructPossibleQuestions(q.Name, ndots, searchList)
|
||||
messages = make([]dns.Msg, 0, len(possibleQNames))
|
||||
)
|
||||
|
||||
for _, qName := range possibleQNames {
|
||||
msg := dns.Msg{}
|
||||
// generate a random id for the transaction.
|
||||
msg.Id = dns.Id()
|
||||
msg.RecursionDesired = true
|
||||
// It's recommended to only send 1 question for 1 DNS message.
|
||||
msg.Question = []dns.Question{q}
|
||||
msg.Question = []dns.Question{{
|
||||
Name: qName,
|
||||
Qtype: q.Qtype,
|
||||
Qclass: q.Qclass,
|
||||
}}
|
||||
messages = append(messages, msg)
|
||||
}
|
||||
|
||||
return messages
|
||||
}
|
||||
|
||||
// NameList returns all of the names that should be queried based on the
|
||||
// config. It is based off of go's net/dns name building, but it does not
|
||||
// check the length of the resulting names.
|
||||
// NOTE: It is taken from `miekg/dns/clientconfig.go: func (c *ClientConfig) NameList`
|
||||
// and slightly modified.
|
||||
func constructPossibleQuestions(name string, ndots int, searchList []string) []string {
|
||||
// if this domain is already fully qualified, no append needed.
|
||||
if dns.IsFqdn(name) {
|
||||
return []string{name}
|
||||
}
|
||||
|
||||
// Check to see if the name has more labels than Ndots. Do this before making
|
||||
// the domain fully qualified.
|
||||
hasNdots := dns.CountLabel(name) > ndots
|
||||
// Make the domain fully qualified.
|
||||
name = dns.Fqdn(name)
|
||||
|
||||
// Make a list of names based off search.
|
||||
names := []string{}
|
||||
|
||||
// If name has enough dots, try that first.
|
||||
if hasNdots {
|
||||
names = append(names, name)
|
||||
}
|
||||
for _, s := range searchList {
|
||||
names = append(names, dns.Fqdn(name+s))
|
||||
}
|
||||
// If we didn't have enough dots, try after suffixes.
|
||||
if !hasNdots {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// parseMessage takes a `dns.Message` and returns a custom
|
||||
// Response data struct.
|
||||
func parseMessage(msg *dns.Msg, rtt time.Duration, server string) Response {
|
||||
var resp Response
|
||||
timeTaken := fmt.Sprintf("%dms", rtt.Milliseconds())
|
||||
|
||||
// Parse Authorities section.
|
||||
for _, ns := range msg.Ns {
|
||||
// check for SOA record
|
||||
soa, ok := ns.(*dns.SOA)
|
||||
if !ok {
|
||||
// Currently we only check for SOA in Authority.
|
||||
// If it's not SOA, skip this message.
|
||||
continue
|
||||
}
|
||||
mname := soa.Ns + " " + soa.Mbox +
|
||||
" " + strconv.FormatInt(int64(soa.Serial), 10) +
|
||||
" " + strconv.FormatInt(int64(soa.Refresh), 10) +
|
||||
" " + strconv.FormatInt(int64(soa.Retry), 10) +
|
||||
" " + strconv.FormatInt(int64(soa.Expire), 10) +
|
||||
" " + strconv.FormatInt(int64(soa.Minttl), 10)
|
||||
h := ns.Header()
|
||||
name := h.Name
|
||||
qclass := dns.Class(h.Class).String()
|
||||
ttl := strconv.FormatInt(int64(h.Ttl), 10) + "s"
|
||||
qtype := dns.Type(h.Rrtype).String()
|
||||
auth := Authority{
|
||||
Name: name,
|
||||
Type: qtype,
|
||||
TTL: ttl,
|
||||
Class: qclass,
|
||||
MName: mname,
|
||||
Nameserver: server,
|
||||
RTT: timeTaken,
|
||||
Status: dns.RcodeToString[msg.Rcode],
|
||||
}
|
||||
resp.Authorities = append(resp.Authorities, auth)
|
||||
}
|
||||
// Parse Answers section.
|
||||
for _, a := range msg.Answer {
|
||||
addr := ""
|
||||
switch t := a.(type) {
|
||||
case *dns.A:
|
||||
addr = t.A.String()
|
||||
case *dns.AAAA:
|
||||
addr = t.AAAA.String()
|
||||
case *dns.CNAME:
|
||||
addr = t.Target
|
||||
case *dns.CAA:
|
||||
addr = t.Tag + " " + t.Value
|
||||
case *dns.HINFO:
|
||||
addr = t.Cpu + " " + t.Os
|
||||
case *dns.PTR:
|
||||
addr = t.Ptr
|
||||
case *dns.SRV:
|
||||
addr = strconv.Itoa(int(t.Priority)) + " " +
|
||||
strconv.Itoa(int(t.Weight)) + " " +
|
||||
t.Target + ":" + strconv.Itoa(int(t.Port))
|
||||
case *dns.TXT:
|
||||
addr = t.String()
|
||||
case *dns.NS:
|
||||
addr = t.Ns
|
||||
case *dns.MX:
|
||||
addr = strconv.Itoa(int(t.Preference)) + " " + t.Mx
|
||||
case *dns.SOA:
|
||||
addr = t.String()
|
||||
case *dns.NAPTR:
|
||||
addr = t.String()
|
||||
}
|
||||
h := a.Header()
|
||||
name := h.Name
|
||||
qclass := dns.Class(h.Class).String()
|
||||
ttl := strconv.FormatInt(int64(h.Ttl), 10) + "s"
|
||||
qtype := dns.Type(h.Rrtype).String()
|
||||
ans := Answer{
|
||||
Name: name,
|
||||
Type: qtype,
|
||||
TTL: ttl,
|
||||
Class: qclass,
|
||||
Address: addr,
|
||||
RTT: timeTaken,
|
||||
Nameserver: server,
|
||||
}
|
||||
resp.Answers = append(resp.Answers, ans)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue