chore: minor cosmetic changes
parent
7ef1a4465c
commit
81a04b6d93
10
TODO.md
10
TODO.md
|
@ -38,14 +38,10 @@
|
||||||
- [x] Fix an issue while loading free form args, where the same records are being added twice
|
- [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] Remove urfave/cli in favour of `pflag + koanf`
|
||||||
- [x] Flags - Remove uneeded ones
|
- [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
|
## Tests
|
||||||
- [ ] Add tests for Command Line Usage.
|
- [ ] Add tests for Resolvers.
|
||||||
|
- [ ] Add tests for CLI Output.
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
- [ ] README
|
- [ ] README
|
||||||
|
@ -73,3 +69,5 @@
|
||||||
- [ ] bash
|
- [ ] bash
|
||||||
- [ ] zsh
|
- [ ] zsh
|
||||||
- [ ] fish
|
- [ ] fish
|
||||||
|
- [ ] Support non RFC Compliant DOH Google response (_ugh_)
|
||||||
|
|
||||||
|
|
17
cmd/cli.go
17
cmd/cli.go
|
@ -50,7 +50,7 @@ func main() {
|
||||||
f.Bool("color", true, "Show colored output")
|
f.Bool("color", true, "Show colored output")
|
||||||
f.Bool("debug", false, "Enable debug mode")
|
f.Bool("debug", false, "Enable debug mode")
|
||||||
|
|
||||||
// Parse and Load Flags
|
// Parse and Load Flags.
|
||||||
err := f.Parse(os.Args[1:])
|
err := f.Parse(os.Args[1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hub.Logger.WithError(err).Error("error parsing flags")
|
hub.Logger.WithError(err).Error("error parsing flags")
|
||||||
|
@ -81,40 +81,43 @@ func main() {
|
||||||
// which will be parsed separately.
|
// which will be parsed separately.
|
||||||
hub.UnparsedArgs = f.Args()
|
hub.UnparsedArgs = f.Args()
|
||||||
|
|
||||||
// Parse Query Args
|
// Parse Query Args.
|
||||||
err = hub.loadQueryArgs()
|
err = hub.loadQueryArgs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hub.Logger.WithError(err).Error("error parsing flags/arguments")
|
hub.Logger.WithError(err).Error("error parsing flags/arguments")
|
||||||
hub.Logger.Exit(2)
|
hub.Logger.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load Nameservers
|
// Load Nameservers.
|
||||||
err = hub.loadNameservers()
|
err = hub.loadNameservers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hub.Logger.WithError(err).Error("error loading nameservers")
|
hub.Logger.WithError(err).Error("error loading nameservers")
|
||||||
hub.Logger.Exit(2)
|
hub.Logger.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load Resolvers
|
// Load Resolvers.
|
||||||
err = hub.loadResolvers()
|
err = hub.loadResolvers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hub.Logger.WithError(err).Error("error loading resolver")
|
hub.Logger.WithError(err).Error("error loading resolver")
|
||||||
hub.Logger.Exit(2)
|
hub.Logger.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start App
|
|
||||||
// Run the app.
|
// Run the app.
|
||||||
hub.Logger.Debug("Starting doggo 🐶")
|
hub.Logger.Debug("Starting doggo 🐶")
|
||||||
|
|
||||||
if len(hub.QueryFlags.QNames) == 0 {
|
if len(hub.QueryFlags.QNames) == 0 {
|
||||||
f.Usage()
|
f.Usage()
|
||||||
hub.Logger.Exit(0)
|
hub.Logger.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve Queries.
|
// Resolve Queries.
|
||||||
err = hub.Lookup()
|
responses, err := hub.Lookup()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hub.Logger.WithError(err).Error("error looking up DNS records")
|
hub.Logger.WithError(err).Error("error looking up DNS records")
|
||||||
hub.Logger.Exit(2)
|
hub.Logger.Exit(2)
|
||||||
}
|
}
|
||||||
|
//Send the output.
|
||||||
|
hub.Output(responses)
|
||||||
|
|
||||||
|
// Quitting.
|
||||||
|
hub.Logger.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/spf13/pflag"
|
"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 {
|
type Hub struct {
|
||||||
Logger *logrus.Logger
|
Logger *logrus.Logger
|
||||||
Version string
|
Version string
|
||||||
|
@ -21,7 +21,8 @@ type Hub struct {
|
||||||
flag *pflag.FlagSet
|
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 {
|
type QueryFlags struct {
|
||||||
QNames []string `koanf:"query"`
|
QNames []string `koanf:"query"`
|
||||||
QTypes []string `koanf:"type"`
|
QTypes []string `koanf:"type"`
|
||||||
|
@ -39,7 +40,7 @@ type QueryFlags struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nameserver represents the type of Nameserver
|
// Nameserver represents the type of Nameserver
|
||||||
// along with it's address.
|
// along with the server address.
|
||||||
type Nameserver struct {
|
type Nameserver struct {
|
||||||
Address string
|
Address string
|
||||||
Type string
|
Type string
|
||||||
|
@ -47,7 +48,6 @@ type Nameserver struct {
|
||||||
|
|
||||||
// NewHub initializes an instance of Hub which holds app wide configuration.
|
// NewHub initializes an instance of Hub which holds app wide configuration.
|
||||||
func NewHub(logger *logrus.Logger, buildVersion string) *Hub {
|
func NewHub(logger *logrus.Logger, buildVersion string) *Hub {
|
||||||
// Initialise Resolver
|
|
||||||
hub := &Hub{
|
hub := &Hub{
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
Version: buildVersion,
|
Version: buildVersion,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -11,10 +10,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Lookup sends the DNS queries to the server.
|
// 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()
|
questions, err := hub.prepareQuestions()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
hub.Questions = questions
|
hub.Questions = questions
|
||||||
// for each type of resolver do a DNS lookup
|
// for each type of resolver do a DNS lookup
|
||||||
|
@ -22,15 +24,11 @@ func (hub *Hub) Lookup() error {
|
||||||
for _, r := range hub.Resolver {
|
for _, r := range hub.Resolver {
|
||||||
resp, err := r.Lookup(hub.Questions)
|
resp, err := r.Lookup(hub.Questions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
responses = append(responses, resp)
|
responses = append(responses, resp)
|
||||||
}
|
}
|
||||||
if len(responses) == 0 {
|
return responses, nil
|
||||||
return errors.New(`no DNS records found`)
|
|
||||||
}
|
|
||||||
hub.Output(responses)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepareQuestions takes a list of hostnames and some
|
// prepareQuestions takes a list of hostnames and some
|
||||||
|
@ -60,8 +58,9 @@ func (hub *Hub) prepareQuestions() ([]dns.Question, error) {
|
||||||
"domain": d,
|
"domain": d,
|
||||||
"ndots": hub.QueryFlags.Ndots,
|
"ndots": hub.QueryFlags.Ndots,
|
||||||
}).Debug("Attmepting to resolve")
|
}).Debug("Attmepting to resolve")
|
||||||
question := dns.Question{}
|
question := dns.Question{
|
||||||
question.Name = d
|
Name: d,
|
||||||
|
}
|
||||||
// iterate on a list of query types.
|
// iterate on a list of query types.
|
||||||
for _, q := range hub.QueryFlags.QTypes {
|
for _, q := range hub.QueryFlags.QTypes {
|
||||||
question.Qtype = dns.StringToType[strings.ToUpper(q)]
|
question.Qtype = dns.StringToType[strings.ToUpper(q)]
|
||||||
|
@ -69,7 +68,6 @@ func (hub *Hub) prepareQuestions() ([]dns.Question, error) {
|
||||||
for _, c := range hub.QueryFlags.QClasses {
|
for _, c := range hub.QueryFlags.QClasses {
|
||||||
question.Qclass = dns.StringToClass[strings.ToUpper(c)]
|
question.Qclass = dns.StringToClass[strings.ToUpper(c)]
|
||||||
// append a new question for each possible pair.
|
// append a new question for each possible pair.
|
||||||
|
|
||||||
questions = append(questions, question)
|
questions = append(questions, question)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,21 @@ import (
|
||||||
"github.com/miekg/dns"
|
"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
|
// loadNameservers reads all the user given
|
||||||
// nameservers and loads to Hub.
|
// nameservers and loads to Hub.
|
||||||
func (hub *Hub) loadNameservers() error {
|
func (hub *Hub) loadNameservers() error {
|
||||||
|
@ -34,7 +49,6 @@ func (hub *Hub) loadNameservers() error {
|
||||||
if !hub.QueryFlags.isNdotsSet {
|
if !hub.QueryFlags.isNdotsSet {
|
||||||
hub.QueryFlags.Ndots = ndots
|
hub.QueryFlags.Ndots = ndots
|
||||||
}
|
}
|
||||||
// hub.QueryFlags.Ndots = ndots
|
|
||||||
hub.Nameservers = append(hub.Nameservers, ns...)
|
hub.Nameservers = append(hub.Nameservers, ns...)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -74,7 +88,7 @@ func getDefaultServers() ([]Nameserver, int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func initNameserver(n string) (Nameserver, 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{
|
ns := Nameserver{
|
||||||
Type: UDPResolver,
|
Type: UDPResolver,
|
||||||
Address: net.JoinHostPort(n, DefaultUDPPort),
|
Address: net.JoinHostPort(n, DefaultUDPPort),
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Output has a list of fields which are produced for the output
|
|
||||||
type Output struct {
|
type Output struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
|
@ -182,8 +181,6 @@ func collectOutput(responses [][]resolvers.Response) []Output {
|
||||||
addr = t.Tag + " " + t.Value
|
addr = t.Tag + " " + t.Value
|
||||||
case *dns.HINFO:
|
case *dns.HINFO:
|
||||||
addr = t.Cpu + " " + t.Os
|
addr = t.Cpu + " " + t.Os
|
||||||
// case *dns.LOC:
|
|
||||||
// addr = t.String()
|
|
||||||
case *dns.PTR:
|
case *dns.PTR:
|
||||||
addr = t.Ptr
|
addr = t.Ptr
|
||||||
case *dns.SRV:
|
case *dns.SRV:
|
||||||
|
|
|
@ -6,22 +6,6 @@ import (
|
||||||
"github.com/mr-karan/doggo/pkg/resolvers"
|
"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
|
// loadResolvers loads differently configured
|
||||||
// resolvers based on a list of nameserver.
|
// resolvers based on a list of nameserver.
|
||||||
func (hub *Hub) loadResolvers() error {
|
func (hub *Hub) loadResolvers() error {
|
||||||
|
@ -65,7 +49,7 @@ func (hub *Hub) loadResolvers() error {
|
||||||
}
|
}
|
||||||
hub.Resolver = append(hub.Resolver, rslvr)
|
hub.Resolver = append(hub.Resolver, rslvr)
|
||||||
}
|
}
|
||||||
if ns.Type == UDPResolver || ns.Type == SystemResolver {
|
if ns.Type == UDPResolver {
|
||||||
hub.Logger.Debug("initiating UDP resolver")
|
hub.Logger.Debug("initiating UDP resolver")
|
||||||
rslvr, err := resolvers.NewClassicResolver(ns.Address, resolvers.ClassicResolverOpts{
|
rslvr, err := resolvers.NewClassicResolver(ns.Address, resolvers.ClassicResolverOpts{
|
||||||
IPv4Only: hub.QueryFlags.UseIPv4,
|
IPv4Only: hub.QueryFlags.UseIPv4,
|
||||||
|
|
Loading…
Reference in New Issue