feat: custom response format

pull/2/head
Karan Sharma 2020-12-12 16:27:13 +05:30
parent 0aa3b36b35
commit a7268f578f
7 changed files with 78 additions and 33 deletions

View File

@ -1,18 +1,28 @@
package main package main
import ( import (
"fmt"
"strings" "strings"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
// Lookup sends the DNS queries to the server.
func (hub *Hub) Lookup(c *cli.Context) error { func (hub *Hub) Lookup(c *cli.Context) error {
hub.prepareQuestions() hub.prepareQuestions()
err := hub.Resolver.Lookup(hub.Questions) responses, err := hub.Resolver.Lookup(hub.Questions)
if err != nil { if err != nil {
hub.Logger.Error(err) hub.Logger.Error(err)
} }
for _, r := range responses {
for _, a := range r.Message.Answer {
if t, ok := a.(*dns.A); ok {
fmt.Println(t.String())
}
}
}
return nil return nil
} }

12
cmd/output.go 100644
View File

@ -0,0 +1,12 @@
package main
// Output takes a list of `dns.Answers` and based
// on the output format specified displays the information.
// func (hub *Hub) Output(c *cli.Context) error {
// hub.prepareQuestions()
// answers, err := hub.Resolver.Lookup(hub.Questions)
// if err != nil {
// hub.Logger.Error(err)
// }
// return nil
// }

View File

@ -12,7 +12,7 @@ func (hub *Hub) loadQueryArgs(c *cli.Context) error {
if err != nil { if err != nil {
cli.Exit("Error parsing arguments", -1) cli.Exit("Error parsing arguments", -1)
} }
err = hub.loadResolver(c) err = hub.initResolver(c)
if err != nil { if err != nil {
cli.Exit("Error parsing nameservers", -1) cli.Exit("Error parsing nameservers", -1)
} }

View File

@ -7,9 +7,9 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
// loadResolver checks for various flags and initialises // initResolver checks for various flags and initialises
// the correct resolver based on the config. // the correct resolver based on the config.
func (hub *Hub) loadResolver(c *cli.Context) error { func (hub *Hub) initResolver(c *cli.Context) error {
// check if DOH flag is set. // check if DOH flag is set.
if hub.QueryFlags.IsDOH { if hub.QueryFlags.IsDOH {
rslvr, err := resolvers.NewDOHResolver(hub.QueryFlags.Nameservers.Value()) rslvr, err := resolvers.NewDOHResolver(hub.QueryFlags.Nameservers.Value())

View File

@ -98,22 +98,26 @@ func NewResolverFromResolvFile(resolvFilePath string) (Resolver, error) {
// Lookup prepare a list of DNS messages to be sent to the server. // Lookup prepare a list of DNS messages to be sent to the server.
// It's possible to send multiple question in one message // It's possible to send multiple question in one message
// but some nameservers are not able to // but some nameservers are not able to
func (c *ClassicResolver) Lookup(questions []dns.Question) error { func (c *ClassicResolver) Lookup(questions []dns.Question) ([]Response, error) {
messages := prepareMessages(questions) var (
messages = prepareMessages(questions)
responses []Response
)
for _, msg := range messages { for _, msg := range messages {
for _, srv := range c.servers { for _, srv := range c.servers {
in, rtt, err := c.client.Exchange(&msg, srv) in, rtt, err := c.client.Exchange(&msg, srv)
if err != nil { if err != nil {
return err return nil, err
} }
for _, ans := range in.Answer { msg.Answer = in.Answer
if t, ok := ans.(*dns.A); ok { rsp := Response{
fmt.Println(t.String()) Message: msg,
RTT: rtt,
Nameserver: srv,
}
responses = append(responses, rsp)
} }
} }
fmt.Println("rtt is", rtt) return responses, nil
}
}
return nil
} }

View File

@ -2,7 +2,6 @@ package resolvers
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"time" "time"
@ -27,39 +26,46 @@ func NewDOHResolver(servers []string) (Resolver, error) {
}, nil }, nil
} }
func (r *DOHResolver) Lookup(questions []dns.Question) error { func (r *DOHResolver) Lookup(questions []dns.Question) ([]Response, error) {
messages := prepareMessages(questions) var (
messages = prepareMessages(questions)
responses []Response
)
for _, m := range messages { for _, msg := range messages {
b, err := m.Pack() // get the DNS Message in wire format.
b, err := msg.Pack()
if err != nil { if err != nil {
return err return nil, err
} }
for _, srv := range r.servers { for _, srv := range r.servers {
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 := r.client.Post(srv, "application/dns-message", bytes.NewBuffer(b)) resp, err := r.client.Post(srv, "application/dns-message", bytes.NewBuffer(b))
if err != nil { if err != nil {
return err return nil, err
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return err return nil, err
} }
rtt := time.Since(now)
// extract the binary response in DNS Message.
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return nil, err
} }
r := &dns.Msg{} err = msg.Unpack(body)
err = r.Unpack(body)
if err != nil { if err != nil {
return err return nil, err
} }
for _, ans := range r.Answer { rsp := Response{
if t, ok := ans.(*dns.A); ok { Message: msg,
fmt.Println(t.String()) RTT: rtt,
Nameserver: srv,
}
responses = append(responses, rsp)
} }
} }
} return responses, nil
}
return nil
} }

View File

@ -1,10 +1,23 @@
package resolvers package resolvers
import "github.com/miekg/dns" import (
"time"
"github.com/miekg/dns"
)
// Resolver implements the configuration for a DNS // Resolver implements the configuration for a DNS
// Client. Different types of client like (UDP/TCP/DOH/DOT) // Client. Different types of client like (UDP/TCP/DOH/DOT)
// can be initialised. // can be initialised.
type Resolver interface { type Resolver interface {
Lookup([]dns.Question) error Lookup([]dns.Question) ([]Response, error)
}
// Response represents a custom output format
// which wraps certain metadata about the DNS query
// and the DNS Answer as well.
type Response struct {
Message dns.Msg
RTT time.Duration
Nameserver string
} }