chore: minor cosmetic changes

pull/2/head
Karan Sharma 2020-12-17 22:08:23 +05:30
parent 7ef1a4465c
commit 81a04b6d93
7 changed files with 45 additions and 51 deletions

10
TODO.md
View File

@ -38,14 +38,10 @@
- [x] Fix an issue while loading free form args, where the same records are being added twice
- [x] Remove urfave/cli in favour of `pflag + koanf`
- [x] Flags - Remove uneeded ones
## Refactors
- [ ] Don't abuse Hub as global. Refactor methods to be independent of hub.
- [ ] Add meaningful comments where required.
- [ ] Meaningful error messages
- [ ] Better debug logs
## Tests
- [ ] Add tests for Command Line Usage.
- [ ] Add tests for Resolvers.
- [ ] Add tests for CLI Output.
## Documentation
- [ ] README
@ -73,3 +69,5 @@
- [ ] bash
- [ ] zsh
- [ ] fish
- [ ] Support non RFC Compliant DOH Google response (_ugh_)

View File

@ -50,7 +50,7 @@ func main() {
f.Bool("color", true, "Show colored output")
f.Bool("debug", false, "Enable debug mode")
// Parse and Load Flags
// Parse and Load Flags.
err := f.Parse(os.Args[1:])
if err != nil {
hub.Logger.WithError(err).Error("error parsing flags")
@ -81,40 +81,43 @@ func main() {
// which will be parsed separately.
hub.UnparsedArgs = f.Args()
// Parse Query Args
// Parse Query Args.
err = hub.loadQueryArgs()
if err != nil {
hub.Logger.WithError(err).Error("error parsing flags/arguments")
hub.Logger.Exit(2)
}
// Load Nameservers
// Load Nameservers.
err = hub.loadNameservers()
if err != nil {
hub.Logger.WithError(err).Error("error loading nameservers")
hub.Logger.Exit(2)
}
// Load Resolvers
// Load Resolvers.
err = hub.loadResolvers()
if err != nil {
hub.Logger.WithError(err).Error("error loading resolver")
hub.Logger.Exit(2)
}
// Start App
// Run the app.
hub.Logger.Debug("Starting doggo 🐶")
if len(hub.QueryFlags.QNames) == 0 {
f.Usage()
hub.Logger.Exit(0)
}
// Resolve Queries.
err = hub.Lookup()
responses, err := hub.Lookup()
if err != nil {
hub.Logger.WithError(err).Error("error looking up DNS records")
hub.Logger.Exit(2)
}
//Send the output.
hub.Output(responses)
// Quitting.
hub.Logger.Exit(0)
}

View File

@ -9,7 +9,7 @@ import (
"github.com/spf13/pflag"
)
// Hub represents the structure for all app wide functions and structs.
// Hub represents the structure for all app wide configuration.
type Hub struct {
Logger *logrus.Logger
Version string
@ -21,7 +21,8 @@ type Hub struct {
flag *pflag.FlagSet
}
// QueryFlags is used store the value of CLI flags.
// QueryFlags is used store the query params
// supplied by the user.
type QueryFlags struct {
QNames []string `koanf:"query"`
QTypes []string `koanf:"type"`
@ -39,7 +40,7 @@ type QueryFlags struct {
}
// Nameserver represents the type of Nameserver
// along with it's address.
// along with the server address.
type Nameserver struct {
Address string
Type string
@ -47,7 +48,6 @@ type Nameserver struct {
// NewHub initializes an instance of Hub which holds app wide configuration.
func NewHub(logger *logrus.Logger, buildVersion string) *Hub {
// Initialise Resolver
hub := &Hub{
Logger: logger,
Version: buildVersion,

View File

@ -1,7 +1,6 @@
package main
import (
"errors"
"runtime"
"strings"
@ -11,10 +10,13 @@ import (
)
// Lookup sends the DNS queries to the server.
func (hub *Hub) Lookup() error {
// It prepares a list of `dns.Questions` and sends
// to all resolvers. It returns a list of []resolver.Response from
// each resolver
func (hub *Hub) Lookup() ([][]resolvers.Response, error) {
questions, err := hub.prepareQuestions()
if err != nil {
return err
return nil, err
}
hub.Questions = questions
// for each type of resolver do a DNS lookup
@ -22,15 +24,11 @@ func (hub *Hub) Lookup() error {
for _, r := range hub.Resolver {
resp, err := r.Lookup(hub.Questions)
if err != nil {
return err
return nil, err
}
responses = append(responses, resp)
}
if len(responses) == 0 {
return errors.New(`no DNS records found`)
}
hub.Output(responses)
return nil
return responses, nil
}
// prepareQuestions takes a list of hostnames and some
@ -60,8 +58,9 @@ func (hub *Hub) prepareQuestions() ([]dns.Question, error) {
"domain": d,
"ndots": hub.QueryFlags.Ndots,
}).Debug("Attmepting to resolve")
question := dns.Question{}
question.Name = d
question := dns.Question{
Name: d,
}
// iterate on a list of query types.
for _, q := range hub.QueryFlags.QTypes {
question.Qtype = dns.StringToType[strings.ToUpper(q)]
@ -69,7 +68,6 @@ func (hub *Hub) prepareQuestions() ([]dns.Question, error) {
for _, c := range hub.QueryFlags.QClasses {
question.Qclass = dns.StringToClass[strings.ToUpper(c)]
// append a new question for each possible pair.
questions = append(questions, question)
}
}

View File

@ -10,6 +10,21 @@ import (
"github.com/miekg/dns"
)
const (
//DefaultResolvConfPath specifies path to default resolv config file on UNIX.
DefaultResolvConfPath = "/etc/resolv.conf"
// DefaultTLSPort specifies the default port for a DNS server connecting over TCP over TLS
DefaultTLSPort = "853"
// DefaultUDPPort specifies the default port for a DNS server connecting over UDP
DefaultUDPPort = "53"
// DefaultTCPPort specifies the default port for a DNS server connecting over TCP
DefaultTCPPort = "53"
UDPResolver = "udp"
DOHResolver = "doh"
TCPResolver = "tcp"
DOTResolver = "dot"
)
// loadNameservers reads all the user given
// nameservers and loads to Hub.
func (hub *Hub) loadNameservers() error {
@ -34,7 +49,6 @@ func (hub *Hub) loadNameservers() error {
if !hub.QueryFlags.isNdotsSet {
hub.QueryFlags.Ndots = ndots
}
// hub.QueryFlags.Ndots = ndots
hub.Nameservers = append(hub.Nameservers, ns...)
}
return nil
@ -74,7 +88,7 @@ func getDefaultServers() ([]Nameserver, int, error) {
}
func initNameserver(n string) (Nameserver, error) {
// Instantiate a dumb UDP resolver as a fallback.
// Instantiate a UDP resolver with default port as a fallback.
ns := Nameserver{
Type: UDPResolver,
Address: net.JoinHostPort(n, DefaultUDPPort),

View File

@ -12,7 +12,6 @@ import (
"github.com/olekukonko/tablewriter"
)
// Output has a list of fields which are produced for the output
type Output struct {
Name string `json:"name"`
Type string `json:"type"`
@ -182,8 +181,6 @@ func collectOutput(responses [][]resolvers.Response) []Output {
addr = t.Tag + " " + t.Value
case *dns.HINFO:
addr = t.Cpu + " " + t.Os
// case *dns.LOC:
// addr = t.String()
case *dns.PTR:
addr = t.Ptr
case *dns.SRV:

View File

@ -6,22 +6,6 @@ import (
"github.com/mr-karan/doggo/pkg/resolvers"
)
const (
//DefaultResolvConfPath specifies path to default resolv config file on UNIX.
DefaultResolvConfPath = "/etc/resolv.conf"
// DefaultTLSPort specifies the default port for a DNS server connecting over TCP over TLS
DefaultTLSPort = "853"
// DefaultUDPPort specifies the default port for a DNS server connecting over UDP
DefaultUDPPort = "53"
// DefaultTCPPort specifies the default port for a DNS server connecting over TCP
DefaultTCPPort = "53"
UDPResolver = "udp"
DOHResolver = "doh"
TCPResolver = "tcp"
DOTResolver = "dot"
SystemResolver = "system"
)
// loadResolvers loads differently configured
// resolvers based on a list of nameserver.
func (hub *Hub) loadResolvers() error {
@ -65,7 +49,7 @@ func (hub *Hub) loadResolvers() error {
}
hub.Resolver = append(hub.Resolver, rslvr)
}
if ns.Type == UDPResolver || ns.Type == SystemResolver {
if ns.Type == UDPResolver {
hub.Logger.Debug("initiating UDP resolver")
rslvr, err := resolvers.NewClassicResolver(ns.Address, resolvers.ClassicResolverOpts{
IPv4Only: hub.QueryFlags.UseIPv4,