Add debug logging (#23)
continuous-integration/drone/push Build is passing Details

Close #12

Reviewed-on: #23
Co-authored-by: grumbulon <grumbulon@grumbulon.xyz>
Co-committed-by: grumbulon <grumbulon@grumbulon.xyz>
This commit is contained in:
grumbulon 2022-07-03 17:58:29 +00:00 committed by Sam
parent 1ec4cb4d05
commit 85a00bffbf
4 changed files with 33 additions and 12 deletions

4
cli.go
View File

@ -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,
}

View File

@ -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)
}
}

13
util/Logger.go Normal file
View File

@ -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
}

View File

@ -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