feat: Add status for non NOERROR responses

Closes https://github.com/mr-karan/doggo/issues/5
pull/7/head
Karan Sharma 2020-12-19 10:56:33 +05:30
parent 90fee8799e
commit 8d1b6ad9fa
6 changed files with 23 additions and 9 deletions

View File

@ -67,3 +67,4 @@
- [ ] Mkdocs init project
- [ ] Custom Index (Landing Page)
- [ ] Homebrew - Goreleaser
- [ ] Separate Authority/Answer in JSON output.

View File

@ -26,7 +26,6 @@ func main() {
// Configure Flags.
f := flag.NewFlagSet("config", flag.ContinueOnError)
hub.flag = f
// Custom Help Text.
f.Usage = renderCustomHelp

View File

@ -6,7 +6,6 @@ import (
"github.com/miekg/dns"
"github.com/mr-karan/doggo/pkg/resolvers"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)
// Hub represents the structure for all app wide configuration.
@ -18,7 +17,6 @@ type Hub struct {
Questions []dns.Question
Resolver []resolvers.Resolver
Nameservers []Nameserver
flag *pflag.FlagSet
}
// QueryFlags is used store the query params
@ -36,7 +34,6 @@ type QueryFlags struct {
Ndots int `koanf:"ndots"`
Color bool `koanf:"color"`
Timeout time.Duration `koanf:"timeout"`
isNdotsSet bool
}
// Nameserver represents the type of Nameserver

View File

@ -14,6 +14,11 @@ import (
// to all resolvers. It returns a list of []resolver.Response from
// each resolver
func (hub *Hub) Lookup() ([][]resolvers.Response, error) {
// check if ndots is 0 (that means it's not supplied by user)
if hub.QueryFlags.Ndots == 0 {
// set the default as 1
hub.QueryFlags.Ndots = 1
}
questions, err := hub.prepareQuestions()
if err != nil {
return nil, err
@ -79,7 +84,7 @@ func (hub *Hub) prepareQuestions() ([]dns.Question, error) {
func fetchDomainList(d string, ndots int) ([]string, error) {
if runtime.GOOS == "windows" {
// TODO: Add a method for reading system default nameserver in windows.
return []string{d}, nil
return []string{dns.Fqdn(d)}, nil
}
cfg, err := dns.ClientConfigFromFile(DefaultResolvConfPath)
if err != nil {

View File

@ -20,6 +20,7 @@ type Output struct {
Address string `json:"address"`
TimeTaken string `json:"rtt"`
Nameserver string `json:"nameserver"`
Status string `json:"status"`
}
type Query struct {
@ -68,6 +69,7 @@ func (hub *Hub) outputTerminal(out []Output) {
yellow := color.New(color.FgYellow, color.Bold).SprintFunc()
cyan := color.New(color.FgCyan, color.Bold).SprintFunc()
red := color.New(color.FgRed, color.Bold).SprintFunc()
magenta := color.New(color.FgMagenta, color.Bold).SprintFunc()
if !hub.QueryFlags.Color {
color.NoColor = true // disables colorized output
@ -78,8 +80,16 @@ func (hub *Hub) outputTerminal(out []Output) {
if hub.QueryFlags.DisplayTimeTaken {
header = append(header, "Time Taken")
}
outputStatus := false
for _, o := range out {
if dns.StringToRcode[o.Status] != dns.RcodeSuccess {
header = append(header, "Status")
outputStatus = true
}
}
table.SetHeader(header)
table.SetAutoWrapText(false)
table.SetAutoWrapText(true)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
@ -99,7 +109,7 @@ func (hub *Hub) outputTerminal(out []Output) {
case "AAAA":
typOut = blue(o.Type)
case "MX":
typOut = red(o.Type)
typOut = magenta(o.Type)
case "NS":
typOut = cyan(o.Type)
case "CNAME":
@ -116,6 +126,9 @@ func (hub *Hub) outputTerminal(out []Output) {
if hub.QueryFlags.DisplayTimeTaken {
output = append(output, o.TimeTaken)
}
if outputStatus {
output = append(output, red(o.Status))
}
table.Append(output)
}
table.Render()
@ -166,6 +179,7 @@ func collectOutput(responses [][]resolvers.Response) []Output {
Address: addr,
TimeTaken: rtt,
Nameserver: r.Nameserver,
Status: dns.RcodeToString[r.Message.Rcode],
}
out = append(out, o)
}

View File

@ -14,8 +14,6 @@ func (hub *Hub) loadQueryArgs() error {
if err != nil {
return err
}
// check if ndots is set
hub.QueryFlags.isNdotsSet = isFlagPassed("ndots", hub.flag)
// Load all fallbacks in internal query flags.
hub.loadFallbacks()