feat: refactor app in a separate package

This commit is contained in:
Karan Sharma 2021-02-27 10:56:33 +05:30
parent 4eca206b66
commit 3b049a88ca
8 changed files with 151 additions and 151 deletions

35
internal/app/app.go Normal file
View file

@ -0,0 +1,35 @@
package app
import (
"github.com/miekg/dns"
"github.com/mr-karan/doggo/pkg/models"
"github.com/mr-karan/doggo/pkg/resolvers"
"github.com/sirupsen/logrus"
)
// App represents the structure for all app wide configuration.
type App struct {
Logger *logrus.Logger
Version string
QueryFlags models.QueryFlags
Questions []dns.Question
Resolvers []resolvers.Resolver
ResolverOpts resolvers.Options
Nameservers []models.Nameserver
}
// NewApp initializes an instance of App which holds app wide configuration.
func New(logger *logrus.Logger, buildVersion string) App {
app := App{
Logger: logger,
Version: buildVersion,
QueryFlags: models.QueryFlags{
QNames: []string{},
QTypes: []string{},
QClasses: []string{},
Nameservers: []string{},
},
Nameservers: []models.Nameserver{},
}
return app
}

108
internal/app/nameservers.go Normal file
View file

@ -0,0 +1,108 @@
package app
import (
"fmt"
"net"
"net/url"
"github.com/mr-karan/doggo/pkg/config"
"github.com/mr-karan/doggo/pkg/models"
)
// LoadNameservers reads all the user given
// nameservers and loads to App.
func (app *App) LoadNameservers() error {
for _, srv := range app.QueryFlags.Nameservers {
ns, err := initNameserver(srv)
if err != nil {
return fmt.Errorf("error parsing nameserver: %s", srv)
}
// check if properly initialised.
if ns.Address != "" && ns.Type != "" {
app.Nameservers = append(app.Nameservers, ns)
}
}
// Set `ndots` to the user specified value.
app.ResolverOpts.Ndots = app.QueryFlags.Ndots
// fallback to system nameserver
// in case no nameserver is specified by user.
if len(app.Nameservers) == 0 {
ns, ndots, search, err := getDefaultServers()
if err != nil {
return fmt.Errorf("error fetching system default nameserver")
}
// `-1` indicates the flag is not set.
// use from config if user hasn't specified any value.
if app.ResolverOpts.Ndots == -1 {
app.ResolverOpts.Ndots = ndots
}
if len(search) > 0 && app.QueryFlags.UseSearchList {
app.ResolverOpts.SearchList = search
}
app.Nameservers = append(app.Nameservers, ns...)
}
// if the user hasn't given any override of `ndots` AND has
// given a custom nameserver. Set `ndots` to 1 as the fallback value
if app.ResolverOpts.Ndots == -1 {
app.ResolverOpts.Ndots = 0
}
return nil
}
func initNameserver(n string) (models.Nameserver, error) {
// Instantiate a UDP resolver with default port as a fallback.
ns := models.Nameserver{
Type: models.UDPResolver,
Address: net.JoinHostPort(n, models.DefaultUDPPort),
}
u, err := url.Parse(n)
if err != nil {
return ns, err
}
if u.Scheme == "https" {
ns.Type = models.DOHResolver
ns.Address = u.String()
}
if u.Scheme == "tls" {
ns.Type = models.DOTResolver
if u.Port() == "" {
ns.Address = net.JoinHostPort(u.Hostname(), models.DefaultTLSPort)
} else {
ns.Address = net.JoinHostPort(u.Hostname(), u.Port())
}
}
if u.Scheme == "tcp" {
ns.Type = models.TCPResolver
if u.Port() == "" {
ns.Address = net.JoinHostPort(u.Hostname(), models.DefaultTCPPort)
} else {
ns.Address = net.JoinHostPort(u.Hostname(), u.Port())
}
}
if u.Scheme == "udp" {
ns.Type = models.UDPResolver
if u.Port() == "" {
ns.Address = net.JoinHostPort(u.Hostname(), models.DefaultUDPPort)
} else {
ns.Address = net.JoinHostPort(u.Hostname(), u.Port())
}
}
return ns, nil
}
func getDefaultServers() ([]models.Nameserver, int, []string, error) {
dnsServers, ndots, search, err := config.GetDefaultServers()
if err != nil {
return nil, 0, nil, err
}
servers := make([]models.Nameserver, 0, len(dnsServers))
for _, s := range dnsServers {
ns := models.Nameserver{
Type: models.UDPResolver,
Address: net.JoinHostPort(s, models.DefaultUDPPort),
}
servers = append(servers, ns)
}
return servers, ndots, search, nil
}

140
internal/app/output.go Normal file
View file

@ -0,0 +1,140 @@
package app
import (
"encoding/json"
"fmt"
"os"
"github.com/fatih/color"
"github.com/miekg/dns"
"github.com/mr-karan/doggo/pkg/resolvers"
"github.com/olekukonko/tablewriter"
)
func (app *App) outputJSON(rsp []resolvers.Response) {
// Pretty print with 4 spaces.
res, err := json.MarshalIndent(rsp, "", " ")
if err != nil {
app.Logger.WithError(err).Error("unable to output data in JSON")
app.Logger.Exit(-1)
}
fmt.Printf("%s", res)
}
func (app *App) outputTerminal(rsp []resolvers.Response) {
var (
green = color.New(color.FgGreen, color.Bold).SprintFunc()
blue = color.New(color.FgBlue, color.Bold).SprintFunc()
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()
)
// Disables colorized output if user specified.
if !app.QueryFlags.Color {
color.NoColor = true
}
// Conditional Time column.
table := tablewriter.NewWriter(os.Stdout)
header := []string{"Name", "Type", "Class", "TTL", "Address", "Nameserver"}
if app.QueryFlags.DisplayTimeTaken {
header = append(header, "Time Taken")
}
// Show output in case if it's not
// a NOERROR.
outputStatus := false
for _, r := range rsp {
for _, a := range r.Authorities {
if dns.StringToRcode[a.Status] != dns.RcodeSuccess {
outputStatus = true
}
}
for _, a := range r.Answers {
if dns.StringToRcode[a.Status] != dns.RcodeSuccess {
outputStatus = true
}
}
}
if outputStatus {
header = append(header, "Status")
}
// Formatting options for the table.
table.SetHeader(header)
table.SetAutoWrapText(true)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
for _, r := range rsp {
for _, ans := range r.Answers {
var typOut string
switch typ := ans.Type; typ {
case "A":
typOut = blue(ans.Type)
case "AAAA":
typOut = blue(ans.Type)
case "MX":
typOut = magenta(ans.Type)
case "NS":
typOut = cyan(ans.Type)
case "CNAME":
typOut = yellow(ans.Type)
case "TXT":
typOut = yellow(ans.Type)
case "SOA":
typOut = red(ans.Type)
default:
typOut = blue(ans.Type)
}
output := []string{green(ans.Name), typOut, ans.Class, ans.TTL, ans.Address, ans.Nameserver}
// Print how long it took
if app.QueryFlags.DisplayTimeTaken {
output = append(output, ans.RTT)
}
if outputStatus {
output = append(output, red(ans.Status))
}
table.Append(output)
}
for _, auth := range r.Authorities {
var typOut string
switch typ := auth.Type; typ {
case "SOA":
typOut = red(auth.Type)
default:
typOut = blue(auth.Type)
}
output := []string{green(auth.Name), typOut, auth.Class, auth.TTL, auth.MName, auth.Nameserver}
// Print how long it took
if app.QueryFlags.DisplayTimeTaken {
output = append(output, auth.RTT)
}
if outputStatus {
output = append(output, red(auth.Status))
}
table.Append(output)
}
}
table.Render()
}
// Output takes a list of `dns.Answers` and based
// on the output format specified displays the information.
func (app *App) Output(responses []resolvers.Response) {
if app.QueryFlags.ShowJSON {
app.outputJSON(responses)
} else {
app.outputTerminal(responses)
}
}

35
internal/app/questions.go Normal file
View file

@ -0,0 +1,35 @@
package app
import (
"strings"
"github.com/miekg/dns"
)
// LoadFallbacks sets fallbacks for options
// that are not specified by the user but necessary
// for the resolver.
func (app *App) LoadFallbacks() {
if len(app.QueryFlags.QTypes) == 0 {
app.QueryFlags.QTypes = append(app.QueryFlags.QTypes, "A")
}
if len(app.QueryFlags.QClasses) == 0 {
app.QueryFlags.QClasses = append(app.QueryFlags.QClasses, "IN")
}
}
// PrepareQuestions takes a list of query names, query types and query classes
// and prepare a question for each combination of the above.
func (app *App) PrepareQuestions() {
for _, n := range app.QueryFlags.QNames {
for _, t := range app.QueryFlags.QTypes {
for _, c := range app.QueryFlags.QClasses {
app.Questions = append(app.Questions, dns.Question{
Name: n,
Qtype: dns.StringToType[strings.ToUpper(t)],
Qclass: dns.StringToClass[strings.ToUpper(c)],
})
}
}
}
}