From 85a00bffbff14d11f369435693f4d791d6bcc03e Mon Sep 17 00:00:00 2001 From: grumbulon Date: Sun, 3 Jul 2022 17:58:29 +0000 Subject: [PATCH] Add debug logging (#23) Close #12 Reviewed-on: https://git.froth.zone/sam/awl/pulls/23 Co-authored-by: grumbulon Co-committed-by: grumbulon --- cli.go | 4 ++++ query.go | 26 ++++++++++++++------------ util/Logger.go | 13 +++++++++++++ util/helpers.go | 2 ++ 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 util/Logger.go diff --git a/cli.go b/cli.go index 948991f..02fd58f 100644 --- a/cli.go +++ b/cli.go @@ -137,6 +137,10 @@ func prepareCLI() *cli.App { Aliases: []string{"x"}, Usage: "do a reverse lookup", }, + &cli.BoolFlag{ + Name: "debug", + Usage: "enable verbose logging", + }, }, Action: doQuery, } diff --git a/query.go b/query.go index 678c55d..f2ec51f 100644 --- a/query.go +++ b/query.go @@ -10,7 +10,6 @@ import ( "strings" "time" - "git.froth.zone/sam/awl/logawl" "git.froth.zone/sam/awl/query" "git.froth.zone/sam/awl/util" "github.com/miekg/dns" @@ -22,25 +21,23 @@ func doQuery(c *cli.Context) error { err error resp util.Response isHTTPS bool - Logger = logawl.New() //init logger ) - + resp.Logger = util.InitLogger(c.Bool("debug")) //init logger resp.Answers, err = parseArgs(c.Args().Slice()) if err != nil { - Logger.Error("Unable to parse args") + resp.Logger.Error("unable to parse args") return err } port := c.Int("port") - if c.Bool("debug") { - Logger.SetLevel(3) - } - Logger.Debug("Starting awl") + resp.Logger.Debug("starting awl") // If port is not set, set it if port == 0 { if c.Bool("tls") || c.Bool("quic") { + resp.Logger.Debug("setting port to 853") port = 853 } else { + resp.Logger.Debug("setting port to 53") port = 53 } } @@ -70,7 +67,7 @@ func doQuery(c *cli.Context) error { if !strings.HasSuffix(resp.Answers.Name, ".") { resp.Answers.Name = fmt.Sprintf("%s.", resp.Answers.Name) } - + resp.Logger.Debug("packing DNS message") msg := new(dns.Msg) msg.SetQuestion(resp.Answers.Name, resp.Answers.Request) @@ -85,7 +82,7 @@ func doQuery(c *cli.Context) error { } // Set the zero flag if requested (does nothing) if c.Bool("z") { - Logger.Debug("Setting message to zero") + resp.Logger.Debug("setting message to zero") msg.Zero = true } // Disable DNSSEC validation @@ -102,7 +99,7 @@ func doQuery(c *cli.Context) error { } // Set DNSSEC if requested if c.Bool("dnssec") { - Logger.Debug("Using DNSSEC") + resp.Logger.Debug("using DNSSEC") msg.SetEdns0(1232, true) } @@ -110,8 +107,10 @@ func doQuery(c *cli.Context) error { // Make the DNS request if isHTTPS { + resp.Logger.Debug("resolving DoH query") in, resp.Answers.RTT, err = query.ResolveHTTPS(msg, resp.Answers.Server) } else if c.Bool("quic") { + resp.Logger.Debug("resolving DoQ query") in, resp.Answers.RTT, err = query.ResolveQUIC(msg, resp.Answers.Server) } else { @@ -119,8 +118,10 @@ func doQuery(c *cli.Context) error { // Set TCP/UDP, depending on flags if c.Bool("tcp") || c.Bool("tls") { + resp.Logger.Debug("using tcp") d.Net = "tcp" } else { + resp.Logger.Debug("using udp") d.Net = "udp" } @@ -136,7 +137,7 @@ func doQuery(c *cli.Context) error { if c.Bool("tls") { d.Net += "-tls" } - + resp.Logger.Debug("exchanging DNS message") in, resp.Answers.RTT, err = d.Exchange(msg, resp.Answers.Server) if err != nil { return err @@ -151,6 +152,7 @@ func doQuery(c *cli.Context) error { case c.Bool("6"): d.Net += "6" } + resp.Logger.Debug("exchanging DNS message") in, resp.Answers.RTT, err = d.Exchange(msg, resp.Answers.Server) } } diff --git a/util/Logger.go b/util/Logger.go new file mode 100644 index 0000000..3d39476 --- /dev/null +++ b/util/Logger.go @@ -0,0 +1,13 @@ +package util + +import "git.froth.zone/sam/awl/logawl" + +func InitLogger(debug bool) (Logger *logawl.Logger) { + Logger = logawl.New() + + if debug { + Logger.SetLevel(3) + } + + return +} diff --git a/util/helpers.go b/util/helpers.go index 814bcb6..e37e70c 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -8,11 +8,13 @@ import ( "strings" "time" + "git.froth.zone/sam/awl/logawl" "github.com/miekg/dns" ) type Response struct { Answers Answers `json:"Response"` // These be DNS query answers + Logger *logawl.Logger } // The Answers struct is the basic structure of a DNS request