chore(Refactor) #110
39 changed files with 162 additions and 161 deletions
|
@ -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"
|
||||
)
|
|
@ -6,7 +6,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"git.froth.zone/sam/awl/cli"
|
||||
cli "git.froth.zone/sam/awl/cmd"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
|
@ -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.
|
|
@ -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"
|
||||
)
|
||||
|
|
@ -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"
|
||||
)
|
|
@ -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"
|
||||
)
|
2
doc/wiki
2
doc/wiki
|
@ -1 +1 @@
|
|||
Subproject commit d41f6197e638d7f0c0b203bd8b7f1fa0fc4b7f97
|
||||
Subproject commit 0fba1fbe4b12e8c88514b3f7d98be3e75a5a034d
|
4
go.mod
4
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
|
||||
|
|
8
go.sum
8
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=
|
||||
|
|
6
main.go
6
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"
|
||||
|
|
|
@ -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.
|
|
@ -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 <LogLevel> YYYY/MM/DD HH:MM:SS (local time) <the message to log>.
|
||||
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...))
|
||||
}
|
|
@ -7,7 +7,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"git.froth.zone/sam/awl/logawl"
|
||||
"git.froth.zone/sam/awl/pkg/logawl"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
|
@ -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"
|
|
@ -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"
|
||||
)
|
|
@ -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")
|
|
@ -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"
|
||||
)
|
|
@ -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,
|
|
@ -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"
|
||||
)
|
|
@ -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{}
|
||||
|
|
@ -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{})
|
|
@ -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 {
|
|
@ -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)
|
|
@ -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 {
|
|
@ -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"
|
||||
)
|
|
@ -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)
|
|
@ -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) {
|
|
@ -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"
|
||||
)
|
||||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
|
||||
"git.froth.zone/sam/awl/logawl"
|
||||
"git.froth.zone/sam/awl/pkg/logawl"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
|
@ -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"
|
||||
)
|
||||
|
|
@ -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.
|
|
@ -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"
|
||||
)
|
12
template.mk
12
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
|
||||
|
|
Loading…
Reference in a new issue