From 3a0a8f015ae3c71af1d06ed57c24ae411fba534e Mon Sep 17 00:00:00 2001 From: grumbulon Date: Sat, 24 Sep 2022 23:11:09 +0000 Subject: [PATCH] chore(Refactor) (#110) refactor Co-authored-by: Sam Therapy Reviewed-on: https://git.froth.zone/sam/awl/pulls/110 Reviewed-by: Sam --- {cli => cmd}/cli.go | 2 +- {cli => cmd}/cli_test.go | 2 +- {cli => cmd}/dig.go | 2 +- {cli => cmd}/dig_test.go | 4 +- {cli => cmd}/docs.go | 0 {cli => cmd}/misc.go | 2 +- {cli => cmd}/misc_test.go | 4 +- doc/wiki | 2 +- go.mod | 4 +- go.sum | 8 +-- main.go | 6 +- {logawl => pkg/logawl}/docs.go | 0 {logawl => pkg/logawl}/logawl.go | 18 +++--- {logawl => pkg/logawl}/logger.go | 74 +++++++++++------------ {logawl => pkg/logawl}/logging_test.go | 2 +- {query => pkg/query}/docs.go | 0 {query => pkg/query}/print.go | 2 +- {query => pkg/query}/print_test.go | 4 +- {query => pkg/query}/query.go | 12 ++-- {query => pkg/query}/query_test.go | 4 +- {query => pkg/query}/struct.go | 0 {query => pkg/resolvers}/DNSCrypt.go | 20 +++--- {query => pkg/resolvers}/DNSCrypt_test.go | 6 +- {query => pkg/resolvers}/HTTPS.go | 16 ++--- {query => pkg/resolvers}/HTTPS_test.go | 10 +-- {query => pkg/resolvers}/QUIC.go | 30 ++++----- {query => pkg/resolvers}/QUIC_test.go | 8 +-- {query => pkg/resolvers}/general.go | 38 ++++++------ {query => pkg/resolvers}/general_test.go | 6 +- {query => pkg/resolvers}/resolver.go | 9 ++- {util => pkg/util}/docs.go | 0 {util => pkg/util}/logger.go | 2 +- {util => pkg/util}/logger_test.go | 4 +- {util => pkg/util}/options.go | 2 +- {util => pkg/util}/options_test.go | 2 +- {util => pkg/util}/query.go | 0 {util => pkg/util}/reverseDNS.go | 4 +- {util => pkg/util}/reverseDNS_test.go | 2 +- template.mk | 12 ++-- 39 files changed, 162 insertions(+), 161 deletions(-) rename {cli => cmd}/cli.go (99%) rename {cli => cmd}/cli_test.go (98%) rename {cli => cmd}/dig.go (99%) rename {cli => cmd}/dig_test.go (96%) rename {cli => cmd}/docs.go (100%) rename {cli => cmd}/misc.go (99%) rename {cli => cmd}/misc_test.go (98%) rename {logawl => pkg/logawl}/docs.go (100%) rename {logawl => pkg/logawl}/logawl.go (78%) rename {logawl => pkg/logawl}/logger.go (59%) rename {logawl => pkg/logawl}/logging_test.go (98%) rename {query => pkg/query}/docs.go (100%) rename {query => pkg/query}/print.go (99%) rename {query => pkg/query}/print_test.go (98%) rename {query => pkg/query}/query.go (94%) rename {query => pkg/query}/query_test.go (97%) rename {query => pkg/query}/struct.go (100%) rename {query => pkg/resolvers}/DNSCrypt.go (63%) rename {query => pkg/resolvers}/DNSCrypt_test.go (94%) rename {query => pkg/resolvers}/HTTPS.go (78%) rename {query => pkg/resolvers}/HTTPS_test.go (89%) rename {query => pkg/resolvers}/QUIC.go (67%) rename {query => pkg/resolvers}/QUIC_test.go (92%) rename {query => pkg/resolvers}/general.go (58%) rename {query => pkg/resolvers}/general_test.go (95%) rename {query => pkg/resolvers}/resolver.go (93%) rename {util => pkg/util}/docs.go (100%) rename {util => pkg/util}/logger.go (84%) rename {util => pkg/util}/logger_test.go (77%) rename {util => pkg/util}/options.go (99%) rename {util => pkg/util}/options_test.go (93%) rename {util => pkg/util}/query.go (100%) rename {util => pkg/util}/reverseDNS.go (90%) rename {util => pkg/util}/reverseDNS_test.go (97%) diff --git a/cli/cli.go b/cmd/cli.go similarity index 99% rename from cli/cli.go rename to cmd/cli.go index 1cf35e3..14a1161 100644 --- a/cli/cli.go +++ b/cmd/cli.go @@ -10,7 +10,7 @@ import ( "strings" "time" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" flag "github.com/stefansundin/go-zflag" ) diff --git a/cli/cli_test.go b/cmd/cli_test.go similarity index 98% rename from cli/cli_test.go rename to cmd/cli_test.go index 4730aa0..7e25649 100644 --- a/cli/cli_test.go +++ b/cmd/cli_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - "git.froth.zone/sam/awl/cli" + cli "git.froth.zone/sam/awl/cmd" "gotest.tools/v3/assert" ) diff --git a/cli/dig.go b/cmd/dig.go similarity index 99% rename from cli/dig.go rename to cmd/dig.go index 5ac8477..5a610c9 100644 --- a/cli/dig.go +++ b/cmd/dig.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" ) // ParseDig parses commands from the popular DNS tool dig. diff --git a/cli/dig_test.go b/cmd/dig_test.go similarity index 96% rename from cli/dig_test.go rename to cmd/dig_test.go index 572872e..6ba9cf5 100644 --- a/cli/dig_test.go +++ b/cmd/dig_test.go @@ -5,8 +5,8 @@ package cli_test import ( "testing" - "git.froth.zone/sam/awl/cli" - "git.froth.zone/sam/awl/util" + cli "git.froth.zone/sam/awl/cmd" + "git.froth.zone/sam/awl/pkg/util" "gotest.tools/v3/assert" ) diff --git a/cli/docs.go b/cmd/docs.go similarity index 100% rename from cli/docs.go rename to cmd/docs.go diff --git a/cli/misc.go b/cmd/misc.go similarity index 99% rename from cli/misc.go rename to cmd/misc.go index b181226..799a377 100644 --- a/cli/misc.go +++ b/cmd/misc.go @@ -8,7 +8,7 @@ import ( "strings" "git.froth.zone/sam/awl/conf" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "golang.org/x/net/idna" ) diff --git a/cli/misc_test.go b/cmd/misc_test.go similarity index 98% rename from cli/misc_test.go rename to cmd/misc_test.go index e6ac236..fa20dbe 100644 --- a/cli/misc_test.go +++ b/cmd/misc_test.go @@ -5,8 +5,8 @@ package cli_test import ( "testing" - "git.froth.zone/sam/awl/cli" - "git.froth.zone/sam/awl/util" + cli "git.froth.zone/sam/awl/cmd" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) diff --git a/doc/wiki b/doc/wiki index d41f619..0fba1fb 160000 --- a/doc/wiki +++ b/doc/wiki @@ -1 +1 @@ -Subproject commit d41f6197e638d7f0c0b203bd8b7f1fa0fc4b7f97 +Subproject commit 0fba1fbe4b12e8c88514b3f7d98be3e75a5a034d diff --git a/go.mod b/go.mod index 57a2419..4d4f49e 100644 --- a/go.mod +++ b/go.mod @@ -28,8 +28,8 @@ require ( github.com/marten-seemann/qtls-go1-19 v0.1.0 // indirect github.com/nxadm/tail v1.4.8 // indirect github.com/onsi/ginkgo v1.16.5 // indirect - golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect - golang.org/x/exp v0.0.0-20220914170420-dc92f8653013 // indirect + golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 // indirect + golang.org/x/exp v0.0.0-20220921164117-439092de6870 // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/tools v0.1.12 // indirect diff --git a/go.sum b/go.sum index 2d70a93..a53af50 100644 --- a/go.sum +++ b/go.sum @@ -77,10 +77,10 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM= -golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/exp v0.0.0-20220914170420-dc92f8653013 h1:ZjglnWxEUdPyXl4o/j4T89SRCI+4X6NW6185PNLEOF4= -golang.org/x/exp v0.0.0-20220914170420-dc92f8653013/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= +golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 h1:WJywXQVIb56P2kAvXeMGTIgQ1ZHQxR60+F9dLsodECc= +golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/exp v0.0.0-20220921164117-439092de6870 h1:j8b6j9gzSigH28O5SjSpQSSh9lFd6f5D/q0aHjNTulc= +golang.org/x/exp v0.0.0-20220921164117-439092de6870/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= diff --git a/main.go b/main.go index 7771998..448b8a1 100644 --- a/main.go +++ b/main.go @@ -9,9 +9,9 @@ import ( "os" "strings" - "git.froth.zone/sam/awl/cli" - "git.froth.zone/sam/awl/query" - "git.froth.zone/sam/awl/util" + cli "git.froth.zone/sam/awl/cmd" + "git.froth.zone/sam/awl/pkg/query" + "git.froth.zone/sam/awl/pkg/util" ) var version = "DEV" diff --git a/logawl/docs.go b/pkg/logawl/docs.go similarity index 100% rename from logawl/docs.go rename to pkg/logawl/docs.go diff --git a/logawl/logawl.go b/pkg/logawl/logawl.go similarity index 78% rename from logawl/logawl.go rename to pkg/logawl/logawl.go index ee5032b..b14a0ad 100644 --- a/logawl/logawl.go +++ b/pkg/logawl/logawl.go @@ -25,22 +25,22 @@ type ( ) // SetLevel stores whatever input value is in mem address of l.level. -func (l *Logger) SetLevel(level Level) { - atomic.StoreInt32((*int32)(&l.Level), int32(level)) +func (logger *Logger) SetLevel(level Level) { + atomic.StoreInt32((*int32)(&logger.Level), int32(level)) } // GetLevel gets the logger level. -func (l *Logger) GetLevel() Level { - return l.level() +func (logger *Logger) GetLevel() Level { + return logger.level() } // Retrieves whatever was stored in mem address of l.level. -func (l *Logger) level() Level { - return Level(atomic.LoadInt32((*int32)(&l.Level))) +func (logger *Logger) level() Level { + return Level(atomic.LoadInt32((*int32)(&logger.Level))) } // UnMarshalLevel unmarshalls the int value of level for writing the header. -func (l *Logger) UnMarshalLevel(lv Level) (string, error) { +func (logger *Logger) UnMarshalLevel(lv Level) (string, error) { switch lv { case ErrLevel: return "ERROR ", nil @@ -56,8 +56,8 @@ func (l *Logger) UnMarshalLevel(lv Level) (string, error) { } // IsLevel returns true if the logger level is above the level given. -func (l *Logger) IsLevel(level Level) bool { - return l.level() >= level +func (logger *Logger) IsLevel(level Level) bool { + return logger.level() >= level } // AllLevels is an array of all valid log levels. diff --git a/logawl/logger.go b/pkg/logawl/logger.go similarity index 59% rename from logawl/logger.go rename to pkg/logawl/logger.go index b0993ea..5aaad39 100644 --- a/logawl/logger.go +++ b/pkg/logawl/logger.go @@ -20,28 +20,28 @@ func New() *Logger { } // Println takes any and prints it out to Logger -> Out (io.Writer (default is std.Err)). -func (l *Logger) Println(level Level, v ...any) { - if atomic.LoadInt32(&l.isDiscard) != 0 { +func (logger *Logger) Println(level Level, in ...any) { + if atomic.LoadInt32(&logger.isDiscard) != 0 { return } // If verbose is not set --debug etc print _nothing_ - if l.IsLevel(level) { + if logger.IsLevel(level) { switch level { // Goes through log levels and does stuff based on them (currently nothing) case ErrLevel: - if err := l.Printer(ErrLevel, fmt.Sprintln(v...)); err != nil { - fmt.Fprintln(os.Stderr, "Logger failed: ", err) + if err := logger.Printer(ErrLevel, fmt.Sprintln(in...)); err != nil { + fmt.Fprintln(logger.Out, "Logger failed: ", err) } case WarnLevel: - if err := l.Printer(WarnLevel, fmt.Sprintln(v...)); err != nil { - fmt.Fprintln(os.Stderr, "Logger failed: ", err) + if err := logger.Printer(WarnLevel, fmt.Sprintln(in...)); err != nil { + fmt.Fprintln(logger.Out, "Logger failed: ", err) } case InfoLevel: - if err := l.Printer(InfoLevel, fmt.Sprintln(v...)); err != nil { - fmt.Fprintln(os.Stderr, "Logger failed: ", err) + if err := logger.Printer(InfoLevel, fmt.Sprintln(in...)); err != nil { + fmt.Fprintln(logger.Out, "Logger failed: ", err) } case DebugLevel: - if err := l.Printer(DebugLevel, fmt.Sprintln(v...)); err != nil { - fmt.Fprintln(os.Stderr, "Logger failed: ", err) + if err := logger.Printer(DebugLevel, fmt.Sprintln(in...)); err != nil { + fmt.Fprintln(logger.Out, "Logger failed: ", err) } default: break @@ -50,8 +50,8 @@ func (l *Logger) Println(level Level, v ...any) { } // FormatHeader formats the log header as such YYYY/MM/DD HH:MM:SS (local time) . -func (l *Logger) FormatHeader(buf *[]byte, t time.Time, line int, level Level) error { - if lvl, err := l.UnMarshalLevel(level); err == nil { +func (logger *Logger) FormatHeader(buf *[]byte, t time.Time, line int, level Level) error { + if lvl, err := logger.UnMarshalLevel(level); err == nil { // This is ugly but functional // maybe there can be an append func or something in the future *buf = append(*buf, lvl...) @@ -81,27 +81,27 @@ func (l *Logger) FormatHeader(buf *[]byte, t time.Time, line int, level Level) e } // Printer prints the formatted message directly to stdErr. -func (l *Logger) Printer(level Level, s string) error { +func (logger *Logger) Printer(level Level, s string) error { now := time.Now() var line int - l.Mu.Lock() - defer l.Mu.Unlock() + logger.Mu.Lock() + defer logger.Mu.Unlock() - l.buf = l.buf[:0] + logger.buf = logger.buf[:0] - if err := l.FormatHeader(&l.buf, now, line, level); err != nil { + if err := logger.FormatHeader(&logger.buf, now, line, level); err != nil { return err } - l.buf = append(l.buf, s...) + logger.buf = append(logger.buf, s...) if len(s) == 0 || s[len(s)-1] != '\n' { - l.buf = append(l.buf, '\n') + logger.buf = append(logger.buf, '\n') } - _, err := l.Out.Write(l.buf) + _, err := logger.Out.Write(logger.buf) if err != nil { return fmt.Errorf("logger printing: %w", err) } @@ -135,41 +135,41 @@ func formatter(buf *[]byte, i int, wid int) { } // Debug calls print directly with Debug level. -func (l *Logger) Debug(v ...any) { - l.Println(DebugLevel, v...) +func (logger *Logger) Debug(in ...any) { + logger.Println(DebugLevel, in...) } // Debugf calls print after formatting the string with Debug level. -func (l *Logger) Debugf(format string, v ...any) { - l.Println(ErrLevel, fmt.Sprintf(format, v...)) +func (logger *Logger) Debugf(format string, in ...any) { + logger.Println(ErrLevel, fmt.Sprintf(format, in...)) } // Info calls print directly with Info level. -func (l *Logger) Info(v ...any) { - l.Println(InfoLevel, v...) +func (logger *Logger) Info(in ...any) { + logger.Println(InfoLevel, in...) } // Infof calls print after formatting the string with Info level. -func (l *Logger) Infof(format string, v ...any) { - l.Println(ErrLevel, fmt.Sprintf(format, v...)) +func (logger *Logger) Infof(format string, in ...any) { + logger.Println(ErrLevel, fmt.Sprintf(format, in...)) } // Warn calls print directly with Warn level. -func (l *Logger) Warn(v ...any) { - l.Println(WarnLevel, v...) +func (logger *Logger) Warn(in ...any) { + logger.Println(WarnLevel, in...) } // Warnf calls print after formatting the string with Warn level. -func (l *Logger) Warnf(format string, v ...any) { - l.Println(WarnLevel, fmt.Sprintf(format, v...)) +func (logger *Logger) Warnf(format string, in ...any) { + logger.Println(WarnLevel, fmt.Sprintf(format, in...)) } // Error calls print directly with Error level. -func (l *Logger) Error(v ...any) { - l.Println(ErrLevel, v...) +func (logger *Logger) Error(in ...any) { + logger.Println(ErrLevel, in...) } // Errorf calls print after formatting the string with Error level. -func (l *Logger) Errorf(format string, v ...any) { - l.Println(ErrLevel, fmt.Sprintf(format, v...)) +func (logger *Logger) Errorf(format string, in ...any) { + logger.Println(ErrLevel, fmt.Sprintf(format, in...)) } diff --git a/logawl/logging_test.go b/pkg/logawl/logging_test.go similarity index 98% rename from logawl/logging_test.go rename to pkg/logawl/logging_test.go index 24f001c..6d0bdd3 100644 --- a/logawl/logging_test.go +++ b/pkg/logawl/logging_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - "git.froth.zone/sam/awl/logawl" + "git.froth.zone/sam/awl/pkg/logawl" "gotest.tools/v3/assert" ) diff --git a/query/docs.go b/pkg/query/docs.go similarity index 100% rename from query/docs.go rename to pkg/query/docs.go diff --git a/query/print.go b/pkg/query/print.go similarity index 99% rename from query/print.go rename to pkg/query/print.go index d3a79c3..d46d40d 100644 --- a/query/print.go +++ b/pkg/query/print.go @@ -12,7 +12,7 @@ import ( "strings" "time" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "golang.org/x/net/idna" "gopkg.in/yaml.v3" diff --git a/query/print_test.go b/pkg/query/print_test.go similarity index 98% rename from query/print_test.go rename to pkg/query/print_test.go index 1dd0226..3173d7f 100644 --- a/query/print_test.go +++ b/pkg/query/print_test.go @@ -5,8 +5,8 @@ package query_test import ( "testing" - "git.froth.zone/sam/awl/query" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/query" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) diff --git a/query/query.go b/pkg/query/query.go similarity index 94% rename from query/query.go rename to pkg/query/query.go index 1bca238..9ab22a7 100644 --- a/query/query.go +++ b/pkg/query/query.go @@ -6,16 +6,12 @@ import ( "fmt" "strconv" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/resolvers" + "git.froth.zone/sam/awl/pkg/util" "github.com/dchest/uniuri" "github.com/miekg/dns" ) -const ( - tcp = "tcp" - udp = "udp" -) - // CreateQuery creates a DNS query from the options given. // It sets query flags and EDNS flags from the respective options. func CreateQuery(opts util.Options) (util.Response, error) { @@ -137,9 +133,9 @@ func CreateQuery(opts util.Options) (util.Response, error) { } } - resolver, err := LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) if err != nil { - return util.Response{}, err + return util.Response{}, fmt.Errorf("unable to load resolvers: %w", err) } opts.Logger.Info("Query successfully loaded") diff --git a/query/query_test.go b/pkg/query/query_test.go similarity index 97% rename from query/query_test.go rename to pkg/query/query_test.go index 51cf527..4ec40cc 100644 --- a/query/query_test.go +++ b/pkg/query/query_test.go @@ -5,8 +5,8 @@ package query_test import ( "testing" - "git.froth.zone/sam/awl/query" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/query" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) diff --git a/query/struct.go b/pkg/query/struct.go similarity index 100% rename from query/struct.go rename to pkg/query/struct.go diff --git a/query/DNSCrypt.go b/pkg/resolvers/DNSCrypt.go similarity index 63% rename from query/DNSCrypt.go rename to pkg/resolvers/DNSCrypt.go index 53afc3d..dca3531 100644 --- a/query/DNSCrypt.go +++ b/pkg/resolvers/DNSCrypt.go @@ -1,12 +1,12 @@ // SPDX-License-Identifier: BSD-3-Clause -package query +package resolvers import ( "fmt" "time" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/ameshkov/dnscrypt/v2" "github.com/miekg/dns" ) @@ -19,28 +19,28 @@ type DNSCryptResolver struct { var _ Resolver = (*DNSCryptResolver)(nil) // LookUp performs a DNS query. -func (r *DNSCryptResolver) LookUp(msg *dns.Msg) (util.Response, error) { +func (resolver *DNSCryptResolver) LookUp(msg *dns.Msg) (util.Response, error) { client := dnscrypt.Client{ - Timeout: r.opts.Request.Timeout, + Timeout: resolver.opts.Request.Timeout, UDPSize: 1232, } - if r.opts.TCP || r.opts.TLS { + if resolver.opts.TCP || resolver.opts.TLS { client.Net = tcp } else { client.Net = udp } switch { - case r.opts.IPv4: + case resolver.opts.IPv4: client.Net += "4" - case r.opts.IPv6: + case resolver.opts.IPv6: client.Net += "6" } - r.opts.Logger.Debug("Using", client.Net, "for making the request") + resolver.opts.Logger.Debug("Using", client.Net, "for making the request") - resolverInf, err := client.Dial(r.opts.Request.Server) + resolverInf, err := client.Dial(resolver.opts.Request.Server) if err != nil { return util.Response{}, fmt.Errorf("dnscrypt: dial: %w", err) } @@ -53,7 +53,7 @@ func (r *DNSCryptResolver) LookUp(msg *dns.Msg) (util.Response, error) { return util.Response{}, fmt.Errorf("dnscrypt: exchange: %w", err) } - r.opts.Logger.Info("Request successful") + resolver.opts.Logger.Info("Request successful") return util.Response{ DNS: res, diff --git a/query/DNSCrypt_test.go b/pkg/resolvers/DNSCrypt_test.go similarity index 94% rename from query/DNSCrypt_test.go rename to pkg/resolvers/DNSCrypt_test.go index a1e7294..98f5e2f 100644 --- a/query/DNSCrypt_test.go +++ b/pkg/resolvers/DNSCrypt_test.go @@ -1,12 +1,12 @@ // SPDX-License-Identifier: BSD-3-Clause -package query_test +package resolvers_test import ( "testing" - "git.froth.zone/sam/awl/query" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/query" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) diff --git a/query/HTTPS.go b/pkg/resolvers/HTTPS.go similarity index 78% rename from query/HTTPS.go rename to pkg/resolvers/HTTPS.go index 4b78bee..79cc2ea 100644 --- a/query/HTTPS.go +++ b/pkg/resolvers/HTTPS.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: BSD-3-Clause -package query +package resolvers import ( "bytes" @@ -9,7 +9,7 @@ import ( "net/http" "time" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" ) @@ -21,11 +21,11 @@ type HTTPSResolver struct { var _ Resolver = (*HTTPSResolver)(nil) // LookUp performs a DNS query. -func (r *HTTPSResolver) LookUp(msg *dns.Msg) (util.Response, error) { +func (resolver *HTTPSResolver) LookUp(msg *dns.Msg) (util.Response, error) { var resp util.Response httpR := &http.Client{ - Timeout: r.opts.Request.Timeout, + Timeout: resolver.opts.Request.Timeout, } buf, err := msg.Pack() @@ -33,9 +33,9 @@ func (r *HTTPSResolver) LookUp(msg *dns.Msg) (util.Response, error) { return util.Response{}, fmt.Errorf("doh: packing: %w", err) } - r.opts.Logger.Debug("https: sending HTTPS request") + resolver.opts.Logger.Debug("https: sending HTTPS request") - req, err := http.NewRequest("POST", r.opts.Request.Server, bytes.NewBuffer(buf)) + req, err := http.NewRequest("POST", resolver.opts.Request.Server, bytes.NewBuffer(buf)) if err != nil { return util.Response{}, fmt.Errorf("doh: request creation: %w", err) } @@ -55,7 +55,7 @@ func (r *HTTPSResolver) LookUp(msg *dns.Msg) (util.Response, error) { return util.Response{}, &ErrHTTPStatus{res.StatusCode} } - r.opts.Logger.Debug("https: reading response") + resolver.opts.Logger.Debug("https: reading response") fullRes, err := io.ReadAll(res.Body) if err != nil { @@ -67,7 +67,7 @@ func (r *HTTPSResolver) LookUp(msg *dns.Msg) (util.Response, error) { return util.Response{}, fmt.Errorf("doh: body close: %w", err) } - r.opts.Logger.Debug("https: unpacking response") + resolver.opts.Logger.Debug("https: unpacking response") resp.DNS = &dns.Msg{} diff --git a/query/HTTPS_test.go b/pkg/resolvers/HTTPS_test.go similarity index 89% rename from query/HTTPS_test.go rename to pkg/resolvers/HTTPS_test.go index ce9473a..2c3742a 100644 --- a/query/HTTPS_test.go +++ b/pkg/resolvers/HTTPS_test.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: BSD-3-Clause -package query_test +package resolvers_test import ( "errors" "testing" - "git.froth.zone/sam/awl/query" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/resolvers" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) @@ -76,7 +76,7 @@ func TestHTTPS(t *testing.T) { t.Run(test.name, func(t *testing.T) { t.Parallel() - resolver, err := query.LoadResolver(test.opts) + resolver, err := resolvers.LoadResolver(test.opts) assert.NilError(t, err) msg := new(dns.Msg) @@ -88,7 +88,7 @@ func TestHTTPS(t *testing.T) { assert.NilError(t, err) assert.Assert(t, res != util.Response{}) } else { - if errors.Is(err, &query.ErrHTTPStatus{}) { + if errors.Is(err, &resolvers.ErrHTTPStatus{}) { assert.ErrorContains(t, err, "404") } assert.Equal(t, res, util.Response{}) diff --git a/query/QUIC.go b/pkg/resolvers/QUIC.go similarity index 67% rename from query/QUIC.go rename to pkg/resolvers/QUIC.go index 9191fdd..60999be 100644 --- a/query/QUIC.go +++ b/pkg/resolvers/QUIC.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: BSD-3-Clause -package query +package resolvers import ( "crypto/tls" @@ -8,7 +8,7 @@ import ( "io" "time" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/lucas-clemente/quic-go" "github.com/miekg/dns" ) @@ -21,28 +21,28 @@ type QUICResolver struct { var _ Resolver = (*QUICResolver)(nil) // LookUp performs a DNS query. -func (r *QUICResolver) LookUp(msg *dns.Msg) (util.Response, error) { +func (resolver *QUICResolver) LookUp(msg *dns.Msg) (util.Response, error) { var resp util.Response tls := &tls.Config{ //nolint:gosec // This is intentional if the user requests it - InsecureSkipVerify: r.opts.TLSNoVerify, - ServerName: r.opts.TLSHost, + InsecureSkipVerify: resolver.opts.TLSNoVerify, + ServerName: resolver.opts.TLSHost, MinVersion: tls.VersionTLS12, NextProtos: []string{"doq"}, } conf := new(quic.Config) - conf.HandshakeIdleTimeout = r.opts.Request.Timeout + conf.HandshakeIdleTimeout = resolver.opts.Request.Timeout - r.opts.Logger.Debug("quic: making query") + resolver.opts.Logger.Debug("quic: making query") - connection, err := quic.DialAddr(r.opts.Request.Server, tls, conf) + connection, err := quic.DialAddr(resolver.opts.Request.Server, tls, conf) if err != nil { return util.Response{}, fmt.Errorf("doq: dial: %w", err) } - r.opts.Logger.Debug("quic: packing query") + resolver.opts.Logger.Debug("quic: packing query") // Compress request to over-the-wire buf, err := msg.Pack() @@ -52,21 +52,21 @@ func (r *QUICResolver) LookUp(msg *dns.Msg) (util.Response, error) { t := time.Now() - r.opts.Logger.Debug("quic: creating stream") + resolver.opts.Logger.Debug("quic: creating stream") stream, err := connection.OpenStream() if err != nil { return util.Response{}, fmt.Errorf("doq: quic stream creation: %w", err) } - r.opts.Logger.Debug("quic: writing to stream") + resolver.opts.Logger.Debug("quic: writing to stream") _, err = stream.Write(buf) if err != nil { return util.Response{}, fmt.Errorf("doq: quic stream write: %w", err) } - r.opts.Logger.Debug("quic: reading stream") + resolver.opts.Logger.Debug("quic: reading stream") fullRes, err := io.ReadAll(stream) if err != nil { @@ -75,14 +75,14 @@ func (r *QUICResolver) LookUp(msg *dns.Msg) (util.Response, error) { resp.RTT = time.Since(t) - r.opts.Logger.Debug("quic: closing connection") + resolver.opts.Logger.Debug("quic: closing connection") // Close with error: no error err = connection.CloseWithError(0, "") if err != nil { return util.Response{}, fmt.Errorf("doq: quic connection close: %w", err) } - r.opts.Logger.Debug("quic: closing stream") + resolver.opts.Logger.Debug("quic: closing stream") err = stream.Close() if err != nil { @@ -91,7 +91,7 @@ func (r *QUICResolver) LookUp(msg *dns.Msg) (util.Response, error) { resp.DNS = &dns.Msg{} - r.opts.Logger.Debug("quic: unpacking response") + resolver.opts.Logger.Debug("quic: unpacking response") err = resp.DNS.Unpack(fullRes) if err != nil { diff --git a/query/QUIC_test.go b/pkg/resolvers/QUIC_test.go similarity index 92% rename from query/QUIC_test.go rename to pkg/resolvers/QUIC_test.go index c30ead3..0691558 100644 --- a/query/QUIC_test.go +++ b/pkg/resolvers/QUIC_test.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: BSD-3-Clause -package query_test +package resolvers_test import ( "testing" "time" - "git.froth.zone/sam/awl/query" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/resolvers" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) @@ -85,7 +85,7 @@ func TestQuic(t *testing.T) { t.Run(test.name, func(t *testing.T) { t.Parallel() - resolver, err := query.LoadResolver(test.opts) + resolver, err := resolvers.LoadResolver(test.opts) assert.NilError(t, err) msg := new(dns.Msg) diff --git a/query/general.go b/pkg/resolvers/general.go similarity index 58% rename from query/general.go rename to pkg/resolvers/general.go index 6f4c6fa..1f94c82 100644 --- a/query/general.go +++ b/pkg/resolvers/general.go @@ -1,13 +1,13 @@ // SPDX-License-Identifier: BSD-3-Clause -package query +package resolvers import ( "crypto/tls" "fmt" "net" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" ) @@ -19,7 +19,7 @@ type StandardResolver struct { var _ Resolver = (*StandardResolver)(nil) // LookUp performs a DNS query. -func (r *StandardResolver) LookUp(msg *dns.Msg) (util.Response, error) { +func (resolver *StandardResolver) LookUp(msg *dns.Msg) (util.Response, error) { var ( resp util.Response err error @@ -27,46 +27,46 @@ func (r *StandardResolver) LookUp(msg *dns.Msg) (util.Response, error) { dnsClient := new(dns.Client) dnsClient.Dialer = &net.Dialer{ - Timeout: r.opts.Request.Timeout, + Timeout: resolver.opts.Request.Timeout, } - if r.opts.TCP || r.opts.TLS { + if resolver.opts.TCP || resolver.opts.TLS { dnsClient.Net = tcp } else { dnsClient.Net = udp } switch { - case r.opts.IPv4: + case resolver.opts.IPv4: dnsClient.Net += "4" - case r.opts.IPv6: + case resolver.opts.IPv6: dnsClient.Net += "6" } - if r.opts.TLS { + if resolver.opts.TLS { dnsClient.Net += "-tls" dnsClient.TLSConfig = &tls.Config{ //nolint:gosec // This is intentional if the user requests it - InsecureSkipVerify: r.opts.TLSNoVerify, - ServerName: r.opts.TLSHost, + InsecureSkipVerify: resolver.opts.TLSNoVerify, + ServerName: resolver.opts.TLSHost, } } - r.opts.Logger.Info("Using", dnsClient.Net, "for making the request") + resolver.opts.Logger.Info("Using", dnsClient.Net, "for making the request") - resp.DNS, resp.RTT, err = dnsClient.Exchange(msg, r.opts.Request.Server) + resp.DNS, resp.RTT, err = dnsClient.Exchange(msg, resolver.opts.Request.Server) if err != nil { return util.Response{}, fmt.Errorf("standard: DNS exchange: %w", err) } switch dns.RcodeToString[resp.DNS.MsgHdr.Rcode] { case "BADCOOKIE": - if !r.opts.BadCookie { + if !resolver.opts.BadCookie { fmt.Printf(";; BADCOOKIE, retrying.\n\n") msg.Extra = resp.DNS.Extra - resp.DNS, resp.RTT, err = dnsClient.Exchange(msg, r.opts.Request.Server) + resp.DNS, resp.RTT, err = dnsClient.Exchange(msg, resolver.opts.Request.Server) if err != nil { return util.Response{}, fmt.Errorf("badcookie: DNS exchange: %w", err) @@ -77,21 +77,21 @@ func (r *StandardResolver) LookUp(msg *dns.Msg) (util.Response, error) { break } - r.opts.Logger.Info("Request successful") + resolver.opts.Logger.Info("Request successful") - if resp.DNS.MsgHdr.Truncated && !r.opts.Truncate { + if resp.DNS.MsgHdr.Truncated && !resolver.opts.Truncate { fmt.Printf(";; Truncated, retrying with TCP\n\n") dnsClient.Net = tcp switch { - case r.opts.IPv4: + case resolver.opts.IPv4: dnsClient.Net += "4" - case r.opts.IPv6: + case resolver.opts.IPv6: dnsClient.Net += "6" } - resp.DNS, resp.RTT, err = dnsClient.Exchange(msg, r.opts.Request.Server) + resp.DNS, resp.RTT, err = dnsClient.Exchange(msg, resolver.opts.Request.Server) } if err != nil { diff --git a/query/general_test.go b/pkg/resolvers/general_test.go similarity index 95% rename from query/general_test.go rename to pkg/resolvers/general_test.go index 5068be1..6e7774d 100644 --- a/query/general_test.go +++ b/pkg/resolvers/general_test.go @@ -1,14 +1,14 @@ // SPDX-License-Identifier: BSD-3-Clause -package query_test +package resolvers_test import ( "os" "testing" "time" - "git.froth.zone/sam/awl/query" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/query" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) diff --git a/query/resolver.go b/pkg/resolvers/resolver.go similarity index 93% rename from query/resolver.go rename to pkg/resolvers/resolver.go index 049522e..a600f20 100644 --- a/query/resolver.go +++ b/pkg/resolvers/resolver.go @@ -1,16 +1,21 @@ // SPDX-License-Identifier: BSD-3-Clause -package query +package resolvers import ( "net" "strconv" "strings" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" ) +const ( + tcp = "tcp" + udp = "udp" +) + // Resolver is the main resolver interface. type Resolver interface { LookUp(*dns.Msg) (util.Response, error) diff --git a/util/docs.go b/pkg/util/docs.go similarity index 100% rename from util/docs.go rename to pkg/util/docs.go diff --git a/util/logger.go b/pkg/util/logger.go similarity index 84% rename from util/logger.go rename to pkg/util/logger.go index 8e5a32c..a09ae11 100644 --- a/util/logger.go +++ b/pkg/util/logger.go @@ -2,7 +2,7 @@ package util -import "git.froth.zone/sam/awl/logawl" +import "git.froth.zone/sam/awl/pkg/logawl" // InitLogger initializes the logawl instance. func InitLogger(verbosity int) (log *logawl.Logger) { diff --git a/util/logger_test.go b/pkg/util/logger_test.go similarity index 77% rename from util/logger_test.go rename to pkg/util/logger_test.go index 938dc05..2e07862 100644 --- a/util/logger_test.go +++ b/pkg/util/logger_test.go @@ -5,8 +5,8 @@ package util_test import ( "testing" - "git.froth.zone/sam/awl/logawl" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/logawl" + "git.froth.zone/sam/awl/pkg/util" "gotest.tools/v3/assert" ) diff --git a/util/options.go b/pkg/util/options.go similarity index 99% rename from util/options.go rename to pkg/util/options.go index 1abd2f4..0dbc1b0 100644 --- a/util/options.go +++ b/pkg/util/options.go @@ -6,7 +6,7 @@ import ( "fmt" "net" - "git.froth.zone/sam/awl/logawl" + "git.froth.zone/sam/awl/pkg/logawl" "github.com/miekg/dns" ) diff --git a/util/options_test.go b/pkg/util/options_test.go similarity index 93% rename from util/options_test.go rename to pkg/util/options_test.go index 6ec6a0c..c04060e 100644 --- a/util/options_test.go +++ b/pkg/util/options_test.go @@ -5,7 +5,7 @@ package util_test import ( "testing" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "gotest.tools/v3/assert" ) diff --git a/util/query.go b/pkg/util/query.go similarity index 100% rename from util/query.go rename to pkg/util/query.go diff --git a/util/reverseDNS.go b/pkg/util/reverseDNS.go similarity index 90% rename from util/reverseDNS.go rename to pkg/util/reverseDNS.go index 1d6c7fc..391bd7e 100644 --- a/util/reverseDNS.go +++ b/pkg/util/reverseDNS.go @@ -13,8 +13,8 @@ type errReverseDNS struct { addr string } -func (e *errReverseDNS) Error() string { - return fmt.Sprintf("reverseDNS: invalid value %s given", e.addr) +func (errDNS *errReverseDNS) Error() string { + return fmt.Sprintf("reverseDNS: invalid value %s given", errDNS.addr) } // ReverseDNS is given an IP or phone number and returns a canonical string to be queried. diff --git a/util/reverseDNS_test.go b/pkg/util/reverseDNS_test.go similarity index 97% rename from util/reverseDNS_test.go rename to pkg/util/reverseDNS_test.go index e4c1a33..42099e6 100644 --- a/util/reverseDNS_test.go +++ b/pkg/util/reverseDNS_test.go @@ -5,7 +5,7 @@ package util_test import ( "testing" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) diff --git a/template.mk b/template.mk index 742a3f5..f45bc86 100644 --- a/template.mk +++ b/template.mk @@ -63,14 +63,14 @@ test-ci: ## fuzz: runs fuzz tests fuzz: $(TEST_SOURCES) - $(TEST) -fuzz=FuzzFlags -fuzztime 10000x ./cli - $(TEST) -fuzz=FuzzDig -fuzztime 10000x ./cli - $(TEST) -fuzz=FuzzParseArgs -fuzztime 10000x ./cli + $(TEST) -fuzz=FuzzFlags -fuzztime 10000x ./cmd + $(TEST) -fuzz=FuzzDig -fuzztime 10000x ./cmd + $(TEST) -fuzz=FuzzParseArgs -fuzztime 10000x ./cmd fuzz-ci: $(TEST_SOURCES) - $(TEST) -fuzz=FuzzFlags -fuzztime 1000x ./cli - $(TEST) -fuzz=FuzzDig -fuzztime 1000x ./cli - $(TEST) -fuzz=FuzzParseArgs -fuzztime 1000x ./cli + $(TEST) -fuzz=FuzzFlags -fuzztime 1000x ./cmd + $(TEST) -fuzz=FuzzDig -fuzztime 1000x ./cmd + $(TEST) -fuzz=FuzzParseArgs -fuzztime 1000x ./cmd .PHONY: full_test full_test: test fuzz