From b9abedfca2e81b599884c2a97e642186bd9fec91 Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Tue, 13 Sep 2022 03:04:45 +0200 Subject: [PATCH 01/11] API work Signed-off-by: Sam Therapy --- .editorconfig | 12 ++ cli/cli.go | 39 ++++--- cli/cli_test.go | 4 +- cli/dig.go | 8 +- go.mod | 17 ++- go.sum | 28 ++--- logawl/logger.go | 20 ++++ logawl/logging_test.go | 5 + query/QUIC_test.go | 8 +- query/general_test.go | 11 +- query/print.go | 29 +++-- query/print_test.go | 107 +++++++++-------- query/query.go | 203 +++++++++++++++++++++++++++++++- query/query_test.go | 40 ++++--- query/resolver.go | 4 +- query/struct.go | 254 ++++++----------------------------------- util/options.go | 174 +++++++++++++++++++--------- util/query.go | 26 +++-- 18 files changed, 572 insertions(+), 417 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ebe51d3 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = false \ No newline at end of file diff --git a/cli/cli.go b/cli/cli.go index 8a593cb..159b6d3 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -116,7 +116,6 @@ func ParseCLI(version string) (util.Options, error) { opts := util.Options{ Logger: util.InitLogger(*verbosity), - Port: *port, IPv4: *ipv4, IPv6: *ipv6, Short: *short, @@ -128,29 +127,29 @@ func ParseCLI(version string) (util.Options, error) { HTTPS: *https, QUIC: *quic, Truncate: *truncate, - ShowQuery: false, - AA: *aaflag, - AD: *adflag, - TC: *tcflag, - Z: *zflag, - CD: *cdflag, - QR: *qrflag, - RD: *rdflag, - RA: *raflag, Reverse: *reverse, - HumanTTL: false, - ShowTTL: true, JSON: *json, XML: *xml, YAML: *yaml, + HeaderFlags: util.HeaderFlags{ + AA: *aaflag, + AD: *adflag, + TC: *tcflag, + Z: *zflag, + CD: *cdflag, + QR: *qrflag, + RD: *rdflag, + RA: *raflag, + }, Request: util.Request{ Type: dns.StringToType[strings.ToUpper(*qType)], Class: dns.StringToClass[strings.ToUpper(*class)], Name: *query, Timeout: time.Duration(*timeout * float32(time.Second)), Retries: *retry, + Port: *port, }, - Display: util.Displays{ + Display: util.Display{ Comments: !*noC, Question: !*noQ, Opt: !*noOpt, @@ -158,6 +157,9 @@ func ParseCLI(version string) (util.Options, error) { Authority: !*noAuth, Additional: !*noAdd, Statistics: !*noStats, + HumanTTL: false, + ShowQuery: false, + TTL: true, }, EDNS: util.EDNS{ EnableEDNS: !*edns, @@ -199,15 +201,15 @@ func ParseCLI(version string) (util.Options, error) { opts.Logger.Info("Dig/Drill flags parsed") opts.Logger.Debug(fmt.Sprintf("%+v", opts)) - if opts.Port == 0 { + if opts.Request.Port == 0 { if opts.TLS || opts.QUIC { - opts.Port = 853 + opts.Request.Port = 853 } else { - opts.Port = 53 + opts.Request.Port = 53 } } - opts.Logger.Info("Port set to", opts.Port) + opts.Logger.Info("Port set to", opts.Request.Port) // Set timeout to 0.5 seconds if set below 0.5 if opts.Request.Timeout < (time.Second / 2) { @@ -218,6 +220,9 @@ func ParseCLI(version string) (util.Options, error) { opts.Request.Retries = 0 } + opts.Logger.Info("Options fully populated") + opts.Logger.Debug(fmt.Sprintf("%+v", opts)) + return opts, nil } diff --git a/cli/cli_test.go b/cli/cli_test.go index 0930fbe..59353c9 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -18,7 +18,7 @@ func TestEmpty(t *testing.T) { opts, err := cli.ParseCLI("TEST") assert.NilError(t, err) - assert.Equal(t, opts.Port, 53) + assert.Equal(t, opts.Request.Port, 53) assert.Assert(t, opts.IPv4) os.Args = args @@ -31,7 +31,7 @@ func TestTLSPort(t *testing.T) { opts, err := cli.ParseCLI("TEST") assert.NilError(t, err) - assert.Equal(t, opts.Port, 853) + assert.Equal(t, opts.Request.Port, 853) os.Args = args } diff --git a/cli/dig.go b/cli/dig.go index 6d75b6c..00ee8a6 100644 --- a/cli/dig.go +++ b/cli/dig.go @@ -45,13 +45,13 @@ func ParseDig(arg string, opts *util.Options) error { // End DNS query flags case "qr": - opts.ShowQuery = isNo + opts.Display.ShowQuery = isNo case "ttlunits": - opts.HumanTTL = isNo + opts.Display.HumanTTL = isNo case "ttl", "ttlid": - opts.ShowTTL = isNo + opts.Display.TTL = isNo case "class": - opts.ShowClass = isNo + opts.Display.ShowClass = isNo // EDNS queries case "dnssec": diff --git a/go.mod b/go.mod index 9f7fcb1..1fa792b 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/miekg/dns v1.1.50 github.com/stefansundin/go-zflag v1.1.1 golang.org/x/net v0.0.0-20220909164309-bea034e7d591 + golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 gopkg.in/yaml.v3 v3.0.1 gotest.tools/v3 v3.3.0 ) @@ -18,22 +19,18 @@ require ( github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 // indirect github.com/ameshkov/dnsstamps v1.0.3 // indirect + github.com/fsnotify/fsnotify v1.4.9 // indirect + github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/golang/mock v1.6.0 // indirect github.com/google/go-cmp v0.5.8 // indirect github.com/kr/pretty v0.3.0 // indirect - github.com/marten-seemann/qtls-go1-19 v0.1.0 // indirect - golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 // indirect -) - -require ( - github.com/fsnotify/fsnotify v1.5.4 // indirect - github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/marten-seemann/qtls-go1-18 v0.1.2 // indirect + 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 + github.com/onsi/ginkgo v1.16.4 // indirect + golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d // indirect + golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 golang.org/x/text v0.3.7 // indirect golang.org/x/tools v0.1.12 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect diff --git a/go.sum b/go.sum index f7afb59..4f95d5f 100644 --- a/go.sum +++ b/go.sum @@ -15,9 +15,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5 h1:RAV05c0xOkJ3dZGS0JFybxFKZ2WMLabgx3uXnd7rpGs= github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= -github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= -github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= @@ -57,8 +56,8 @@ github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= -github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= -github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= +github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc= +github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.13.0 h1:7lLHu94wT9Ij0o6EWWclhu0aOh32VxhkwEJvzuWPeak= @@ -77,12 +76,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-20220827204233-334a2380cb91 h1:tnebWN09GYg9OLPss1KXj8txwZc6X6uMr6VFdcGNbHw= -golang.org/x/exp v0.0.0-20220827204233-334a2380cb91/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= -golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= -golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= +golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d h1:3qF+Z8Hkrw9sOhrFHti9TlB1Hkac1x+DNRkv0XQiFjo= +golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA= +golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= 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= @@ -94,8 +91,6 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220907135653-1e95f45603a7 h1:1WGATo9HAhkWMbfyuVU0tEFP88OIkUvwaHFveQPvzCQ= -golang.org/x/net v0.0.0-20220907135653-1e95f45603a7/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.0.0-20220909164309-bea034e7d591 h1:D0B/7al0LLrVC8aWF4+oxpv/m8bc7ViFfVS8/gXGdqI= golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -118,13 +113,8 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220907062415-87db552b00fd h1:AZeIEzg+8RCELJYq8w+ODLVxFgLMMigSwO/ffKPEd9U= -golang.org/x/sys v0.0.0-20220907062415-87db552b00fd/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY= -golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 h1:wM1k/lXfpc5HdkJJyW9GELpd8ERGdnh8sMGL6Gzq3Ho= -golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM= +golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/logawl/logger.go b/logawl/logger.go index e6aebfd..b0993ea 100644 --- a/logawl/logger.go +++ b/logawl/logger.go @@ -139,17 +139,37 @@ func (l *Logger) Debug(v ...any) { l.Println(DebugLevel, v...) } +// 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...)) +} + // Info calls print directly with Info level. func (l *Logger) Info(v ...any) { l.Println(InfoLevel, v...) } +// 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...)) +} + // Warn calls print directly with Warn level. func (l *Logger) Warn(v ...any) { l.Println(WarnLevel, v...) } +// 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...)) +} + // Error calls print directly with Error level. func (l *Logger) Error(v ...any) { l.Println(ErrLevel, v...) } + +// 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...)) +} diff --git a/logawl/logging_test.go b/logawl/logging_test.go index 1b63b01..24f001c 100644 --- a/logawl/logging_test.go +++ b/logawl/logging_test.go @@ -52,6 +52,7 @@ func TestLogger(t *testing.T) { case 0: fn := func() { logger.Error("Test", "E") + logger.Errorf("%s", "Test") } var buffer bytes.Buffer @@ -62,6 +63,7 @@ func TestLogger(t *testing.T) { case 1: fn := func() { logger.Warn("Test") + logger.Warnf("%s", "Test") } var buffer bytes.Buffer @@ -72,6 +74,7 @@ func TestLogger(t *testing.T) { case 2: fn := func() { logger.Info("Test") + logger.Infof("%s", "Test") } var buffer bytes.Buffer @@ -83,6 +86,8 @@ func TestLogger(t *testing.T) { fn := func() { logger.Debug("Test") logger.Debug("Test 2") + logger.Debugf("%s", "Test") + logger.Debugf("%s %d", "Test", 2) } var buffer bytes.Buffer diff --git a/query/QUIC_test.go b/query/QUIC_test.go index 6158254..6d83199 100644 --- a/query/QUIC_test.go +++ b/query/QUIC_test.go @@ -22,8 +22,7 @@ func TestQuic(t *testing.T) { opts := util.Options{ QUIC: true, Logger: util.InitLogger(0), - Port: 853, - Request: util.Request{Server: "dns.adguard.com"}, + Request: util.Request{Server: "dns.adguard.com", Port: 853}, } testCase := util.Request{Server: "dns.//./,,adguard.com", Type: dns.TypeA, Name: "git.froth.zone"} testCase2 := util.Request{Server: "dns.adguard.com", Type: dns.TypeA, Name: "git.froth.zone"} @@ -54,7 +53,7 @@ func TestQuic(t *testing.T) { resolver, err := query.LoadResolver(opts) assert.NilError(t, err) - testCase2.Server = net.JoinHostPort(testCase2.Server, strconv.Itoa(opts.Port)) + testCase2.Server = net.JoinHostPort(testCase2.Server, strconv.Itoa(opts.Request.Port)) // if the domain is not canonical, make it canonical if !strings.HasSuffix(testCase2.Name, ".") { @@ -78,8 +77,7 @@ func TestInvalidQuic(t *testing.T) { opts := util.Options{ QUIC: true, Logger: util.InitLogger(0), - Port: 853, - Request: util.Request{Server: "example.com", Type: dns.TypeA, Name: "git.froth.zone", Timeout: 10 * time.Millisecond}, + Request: util.Request{Server: "example.com", Port: 853, Type: dns.TypeA, Name: "git.froth.zone", Timeout: 10 * time.Millisecond}, } resolver, err := query.LoadResolver(opts) assert.NilError(t, err) diff --git a/query/general_test.go b/query/general_test.go index 312cf60..fe06c12 100644 --- a/query/general_test.go +++ b/query/general_test.go @@ -17,9 +17,9 @@ func TestResolve(t *testing.T) { opts := util.Options{ Logger: util.InitLogger(0), - Port: 53, Request: util.Request{ Server: "8.8.4.1", + Port: 53, Type: dns.TypeA, Name: "example.com.", Timeout: time.Second / 2, @@ -42,9 +42,9 @@ func TestTruncate(t *testing.T) { opts := util.Options{ Logger: util.InitLogger(0), IPv4: true, - Port: 5301, Request: util.Request{ Server: "madns.binarystar.systems", + Port: 5301, Type: dns.TypeTXT, Name: "limit.txt.example.", }, @@ -70,9 +70,10 @@ func TestResolveAgain(t *testing.T) { util.Options{ Logger: util.InitLogger(0), TCP: true, - Port: 53, + Request: util.Request{ Server: "8.8.4.4", + Port: 53, Type: dns.TypeA, Name: "example.com.", }, @@ -81,9 +82,9 @@ func TestResolveAgain(t *testing.T) { { util.Options{ Logger: util.InitLogger(0), - Port: 53, Request: util.Request{ Server: "8.8.4.4", + Port: 53, Type: dns.TypeAAAA, Name: "example.com.", }, @@ -93,9 +94,9 @@ func TestResolveAgain(t *testing.T) { util.Options{ Logger: util.InitLogger(0), TLS: true, - Port: 853, Request: util.Request{ Server: "dns.google", + Port: 853, Type: dns.TypeAAAA, Name: "example.com.", }, diff --git a/query/print.go b/query/print.go index 5eaa6bf..c026343 100644 --- a/query/print.go +++ b/query/print.go @@ -55,8 +55,21 @@ func PrintSpecial(msg *dns.Msg, opts util.Options) (string, error) { func MakePrintable(msg *dns.Msg, opts util.Options) (*Message, error) { var err error + // The things I do for compatibility ret := Message{ - Header: msg.MsgHdr, + Header: Header{ + ID: msg.Id, + Response: msg.Response, + Opcode: dns.OpcodeToString[msg.Opcode], + Authoritative: msg.Authoritative, + Truncated: msg.Truncated, + RecursionDesired: msg.RecursionDesired, + RecursionAvailable: msg.RecursionAvailable, + Zero: msg.Zero, + AuthenticatedData: msg.AuthenticatedData, + CheckingDisabled: msg.CheckingDisabled, + Status: dns.RcodeToString[msg.Rcode], + }, } for _, question := range msg.Question { @@ -85,8 +98,8 @@ func MakePrintable(msg *dns.Msg, opts util.Options) (*Message, error) { name string ) - if opts.ShowTTL { - if opts.HumanTTL { + if opts.Display.TTL { + if opts.Display.HumanTTL { ttl = (time.Duration(answer.Header().Ttl) * time.Second).String() } else { ttl = strconv.Itoa(int(answer.Header().Ttl)) @@ -122,8 +135,8 @@ func MakePrintable(msg *dns.Msg, opts util.Options) (*Message, error) { name string ) - if opts.ShowTTL { - if opts.HumanTTL { + if opts.Display.TTL { + if opts.Display.HumanTTL { ttl = (time.Duration(ns.Header().Ttl) * time.Second).String() } else { ttl = strconv.Itoa(int(ns.Header().Ttl)) @@ -139,7 +152,7 @@ func MakePrintable(msg *dns.Msg, opts util.Options) (*Message, error) { name = ns.Header().Name } - ret.Ns = append(ret.Ns, Answer{ + ret.Authority = append(ret.Authority, Answer{ RRHeader: RRHeader{ Name: name, Type: dns.TypeToString[ns.Header().Rrtype], @@ -162,8 +175,8 @@ func MakePrintable(msg *dns.Msg, opts util.Options) (*Message, error) { name string ) - if opts.ShowTTL { - if opts.HumanTTL { + if opts.Display.TTL { + if opts.Display.HumanTTL { ttl = (time.Duration(additional.Header().Ttl) * time.Second).String() } else { ttl = strconv.Itoa(int(additional.Header().Ttl)) diff --git a/query/print_test.go b/query/print_test.go index 8dce5d1..4eade9b 100644 --- a/query/print_test.go +++ b/query/print_test.go @@ -16,15 +16,16 @@ func TestRealPrint(t *testing.T) { opts := []util.Options{ { - Logger: util.InitLogger(0), - Port: 53, - TCP: true, - ShowQuery: true, - RD: true, - ShowTTL: true, - HumanTTL: true, - JSON: true, - Display: util.Displays{ + Logger: util.InitLogger(0), + + TCP: true, + + HeaderFlags: util.HeaderFlags{ + RD: true, + }, + + JSON: true, + Display: util.Display{ Comments: true, Question: true, Answer: true, @@ -32,9 +33,13 @@ func TestRealPrint(t *testing.T) { Additional: true, Statistics: true, UcodeTranslate: true, + TTL: true, + HumanTTL: true, + ShowQuery: true, }, Request: util.Request{ Server: "a.gtld-servers.net", + Port: 53, Type: dns.StringToType["NS"], Class: 1, Name: "google.com.", @@ -44,17 +49,18 @@ func TestRealPrint(t *testing.T) { }, }, { - Logger: util.InitLogger(0), - Port: 53, - TCP: true, - ShowQuery: true, - RD: true, + Logger: util.InitLogger(0), + + TCP: true, + HeaderFlags: util.HeaderFlags{ + RD: true, + }, Verbosity: 0, - ShowTTL: true, - Short: true, - Identify: true, - YAML: false, - Display: util.Displays{ + + Short: true, + Identify: true, + YAML: false, + Display: util.Display{ Comments: true, Question: true, Answer: true, @@ -62,9 +68,12 @@ func TestRealPrint(t *testing.T) { Additional: true, Statistics: true, UcodeTranslate: true, + TTL: true, + ShowQuery: true, }, Request: util.Request{ Server: "ns1.google.com", + Port: 53, Type: dns.StringToType["NS"], Class: 1, Name: "google.com.", @@ -76,16 +85,14 @@ func TestRealPrint(t *testing.T) { }, }, { - Logger: util.InitLogger(0), - Port: 53, - HTTPS: true, - ShowQuery: true, - RD: true, - ShowTTL: true, - HumanTTL: true, - Identify: true, - XML: true, - Display: util.Displays{ + Logger: util.InitLogger(0), + HTTPS: true, + HeaderFlags: util.HeaderFlags{ + RD: true, + }, + Identify: true, + XML: true, + Display: util.Display{ Comments: true, Question: true, Answer: true, @@ -93,9 +100,13 @@ func TestRealPrint(t *testing.T) { Additional: true, Statistics: true, UcodeTranslate: false, + TTL: true, + HumanTTL: true, + ShowQuery: true, }, Request: util.Request{ Server: "https://dns.froth.zone/dns-query", + Port: 443, Type: dns.StringToType["NS"], Class: 1, Name: "freecumextremist.com.", @@ -108,14 +119,13 @@ func TestRealPrint(t *testing.T) { }, }, { - Logger: util.InitLogger(0), - Port: 853, - TLS: true, - ShowQuery: true, - RD: true, + Logger: util.InitLogger(0), + TLS: true, + HeaderFlags: util.HeaderFlags{ + RD: true, + }, Verbosity: 0, - ShowTTL: false, - Display: util.Displays{ + Display: util.Display{ Comments: true, Question: true, Answer: true, @@ -123,25 +133,29 @@ func TestRealPrint(t *testing.T) { Additional: true, Statistics: true, UcodeTranslate: true, + TTL: false, + ShowQuery: true, }, Request: util.Request{ Server: "dns.google", + Port: 853, Type: dns.StringToType["NS"], Class: 1, Name: "freecumextremist.com.", }, }, { - Logger: util.InitLogger(0), - Port: 53, - TCP: true, - ShowQuery: true, - AA: true, - RD: true, + Logger: util.InitLogger(0), + TCP: true, + + HeaderFlags: util.HeaderFlags{ + AA: true, + RD: true, + }, Verbosity: 0, - ShowTTL: true, - YAML: true, - Display: util.Displays{ + + YAML: true, + Display: util.Display{ Comments: true, Question: true, Answer: true, @@ -149,9 +163,12 @@ func TestRealPrint(t *testing.T) { Additional: true, Statistics: true, UcodeTranslate: false, + TTL: true, + ShowQuery: true, }, Request: util.Request{ Server: "rin.froth.zone", + Port: 53, Type: dns.StringToType["A"], Class: 1, Name: "froth.zone.", diff --git a/query/query.go b/query/query.go index 9b1cda9..1b8dcaf 100644 --- a/query/query.go +++ b/query/query.go @@ -5,10 +5,13 @@ package query import ( "fmt" "strconv" + "strings" + "time" "git.froth.zone/sam/awl/util" "github.com/dchest/uniuri" "github.com/miekg/dns" + "golang.org/x/net/idna" ) const ( @@ -16,6 +19,202 @@ const ( udp = "udp" ) +// ToString turns the response into something that looks a lot like dig +// +// Much of this is taken from https://github.com/miekg/dns/blob/master/msg.go#L900 +func ToString(res util.Response, opts util.Options) (string, error) { + if res.DNS == nil { + return " MsgHdr", errNoMessage + } + + var ( + s string + opt *dns.OPT + ) + + if !opts.Short { + if opts.Display.Comments { + s += res.DNS.MsgHdr.String() + " " + s += "QUERY: " + strconv.Itoa(len(res.DNS.Question)) + ", " + s += "ANSWER: " + strconv.Itoa(len(res.DNS.Answer)) + ", " + s += "AUTHORITY: " + strconv.Itoa(len(res.DNS.Ns)) + ", " + s += "ADDITIONAL: " + strconv.Itoa(len(res.DNS.Extra)) + "\n" + opt = res.DNS.IsEdns0() + + if opt != nil && opts.Display.Opt { + // OPT PSEUDOSECTION + s += opt.String() + "\n" + } + } + + if opts.Display.Question { + if len(res.DNS.Question) > 0 { + if opts.Display.Comments { + s += "\n;; QUESTION SECTION:\n" + } + + for _, r := range res.DNS.Question { + str, err := stringParse(r.String(), false, opts) + if err != nil { + return "", fmt.Errorf("%w", err) + } + + s += str + "\n" + } + } + } + + if opts.Display.Answer { + if len(res.DNS.Answer) > 0 { + if opts.Display.Comments { + s += "\n;; ANSWER SECTION:\n" + } + + for _, r := range res.DNS.Answer { + if r != nil { + str, err := stringParse(r.String(), true, opts) + if err != nil { + return "", fmt.Errorf("%w", err) + } + + s += str + "\n" + } + } + } + } + + if opts.Display.Authority { + if len(res.DNS.Ns) > 0 { + if opts.Display.Comments { + s += "\n;; AUTHORITY SECTION:\n" + } + + for _, r := range res.DNS.Ns { + if r != nil { + str, err := stringParse(r.String(), true, opts) + if err != nil { + return "", fmt.Errorf("%w", err) + } + + s += str + "\n" + } + } + } + } + + if opts.Display.Additional { + if len(res.DNS.Extra) > 0 && (opt == nil || len(res.DNS.Extra) > 1) { + if opts.Display.Comments { + s += "\n;; ADDITIONAL SECTION:\n" + } + + for _, r := range res.DNS.Extra { + if r != nil && r.Header().Rrtype != dns.TypeOPT { + str, err := stringParse(r.String(), true, opts) + if err != nil { + return "", fmt.Errorf("%w", err) + } + + s += str + "\n" + } + } + } + } + + if opts.Display.Statistics { + s += "\n;; Query time: " + res.RTT.String() + // Add extra information to server string + var extra string + + switch { + case opts.TCP: + extra = ":" + strconv.Itoa(opts.Request.Port) + " (TCP)" + case opts.TLS: + extra = ":" + strconv.Itoa(opts.Request.Port) + " (TLS)" + case opts.HTTPS, opts.DNSCrypt: + extra = "" + case opts.QUIC: + extra = ":" + strconv.Itoa(opts.Request.Port) + " (QUIC)" + default: + extra = ":" + strconv.Itoa(opts.Request.Port) + " (UDP)" + } + + s += "\n;; SERVER: " + opts.Request.Server + extra + s += "\n;; WHEN: " + time.Now().Format(time.RFC1123Z) + s += "\n;; MSG SIZE rcvd: " + strconv.Itoa(res.DNS.Len()) + "\n" + } + } else { + // Print just the responses, nothing else + for i, resp := range res.DNS.Answer { + temp := strings.Split(resp.String(), "\t") + s += temp[len(temp)-1] + + if opts.Identify { + s += " from server " + opts.Request.Server + " in " + res.RTT.String() + } + + // Don't print newline on last line + if i != len(res.DNS.Answer)-1 { + s += "\n" + } + } + } + + return s, nil +} + +// stringParse edits the raw responses to user requests. +func stringParse(str string, isAns bool, opts util.Options) (string, error) { + split := strings.Split(str, "\t") + + // Make edits if so requested + + // TODO: make less ew? + // This exists because the question section should be left alone EXCEPT for punycode. + + if isAns { + if !opts.Display.TTL { + // Remove from existence + split = append(split[:1], split[2:]...) + } + + if !opts.Display.ShowClass { + // Position depends on if the TTL is there or not. + if opts.Display.TTL { + split = append(split[:2], split[3:]...) + } else { + split = append(split[:1], split[2:]...) + } + } + + if opts.Display.TTL && opts.Display.HumanTTL { + ttl, _ := strconv.Atoi(split[1]) + split[1] = (time.Duration(ttl) * time.Second).String() + } + } + + if opts.Display.UcodeTranslate { + var ( + err error + semi string + ) + + if strings.HasPrefix(split[0], ";") { + split[0] = strings.TrimPrefix(split[0], ";") + semi = ";" + } + + split[0], err = idna.ToUnicode(split[0]) + if err != nil { + return "", fmt.Errorf("punycode: %w", err) + } + + split[0] = semi + split[0] + } + + return strings.Join(split, "\t"), nil +} + // 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) { @@ -102,7 +301,7 @@ func CreateQuery(opts util.Options) (util.Response, error) { opts.Logger.Debug(req) if !opts.Short { - if opts.ShowQuery { + if opts.Display.ShowQuery { opts.Logger.Info("Printing constructed query") var ( @@ -133,7 +332,7 @@ func CreateQuery(opts util.Options) (util.Response, error) { fmt.Println(str) - opts.ShowQuery = false + opts.Display.ShowQuery = false } } diff --git a/query/query_test.go b/query/query_test.go index 6b4d504..7c76d34 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -16,18 +16,20 @@ func TestCreateQ(t *testing.T) { in := []util.Options{ { - Logger: util.InitLogger(0), - Port: 53, - Z: true, - ShowQuery: true, - YAML: true, + Logger: util.InitLogger(0), + HeaderFlags: util.HeaderFlags{ + Z: true, + }, + + YAML: true, Request: util.Request{ Server: "8.8.4.4", + Port: 53, Type: dns.TypeA, Name: "example.com.", }, - Display: util.Displays{ + Display: util.Display{ Comments: true, Question: true, Opt: true, @@ -35,6 +37,7 @@ func TestCreateQ(t *testing.T) { Authority: true, Additional: true, Statistics: true, + ShowQuery: true, }, EDNS: util.EDNS{ ZFlag: 1, @@ -50,18 +53,19 @@ func TestCreateQ(t *testing.T) { }, }, { - Logger: util.InitLogger(0), - Port: 53, - Z: true, - ShowQuery: true, - XML: true, + Logger: util.InitLogger(0), + HeaderFlags: util.HeaderFlags{ + Z: true, + }, + XML: true, Request: util.Request{ Server: "8.8.4.4", + Port: 53, Type: dns.TypeA, Name: "example.com.", }, - Display: util.Displays{ + Display: util.Display{ Comments: true, Question: true, Opt: true, @@ -70,22 +74,21 @@ func TestCreateQ(t *testing.T) { Additional: true, Statistics: true, UcodeTranslate: true, + ShowQuery: true, }, }, { Logger: util.InitLogger(0), - Port: 853, - // Z: true, - ShowQuery: true, - JSON: true, - QUIC: true, + JSON: true, + QUIC: true, Request: util.Request{ Server: "dns.adguard.com", + Port: 853, Type: dns.TypeA, Name: "example.com.", }, - Display: util.Displays{ + Display: util.Display{ Comments: true, Question: true, Opt: true, @@ -93,6 +96,7 @@ func TestCreateQ(t *testing.T) { Authority: true, Additional: true, Statistics: true, + ShowQuery: true, }, EDNS: util.EDNS{ EnableEDNS: true, diff --git a/query/resolver.go b/query/resolver.go index 2ede1db..049522e 100644 --- a/query/resolver.go +++ b/query/resolver.go @@ -31,7 +31,7 @@ func LoadResolver(opts util.Options) (Resolver, error) { }, nil case opts.QUIC: opts.Logger.Info("loading DNS-over-QUIC resolver") - opts.Request.Server = net.JoinHostPort(opts.Request.Server, strconv.Itoa(opts.Port)) + opts.Request.Server = net.JoinHostPort(opts.Request.Server, strconv.Itoa(opts.Request.Port)) return &QUICResolver{ opts: opts, @@ -48,7 +48,7 @@ func LoadResolver(opts util.Options) (Resolver, error) { }, nil default: opts.Logger.Info("loading standard/DNS-over-TLS resolver") - opts.Request.Server = net.JoinHostPort(opts.Request.Server, strconv.Itoa(opts.Port)) + opts.Request.Server = net.JoinHostPort(opts.Request.Server, strconv.Itoa(opts.Request.Port)) return &StandardResolver{ opts: opts, diff --git a/query/struct.go b/query/struct.go index 2617b4f..a4199e3 100644 --- a/query/struct.go +++ b/query/struct.go @@ -4,48 +4,61 @@ package query import ( "errors" - "fmt" - "strconv" - "strings" - "time" - - "git.froth.zone/sam/awl/util" - "github.com/miekg/dns" - "golang.org/x/net/idna" ) // Message is for overall DNS responses. // -//nolint:govet // Better output is worth 32 bytes. +//nolint:govet // Better looking output is worth a few bytes. type Message struct { - Header dns.MsgHdr `json:"header,omitempty" xml:"header,omitempty" yaml:",omitempty"` - Opt []Opts `json:"opt,omitempty" xml:"opt,omitempty" yaml:"opt,omitempty"` - Question []Question `json:"question,omitempty" xml:"question,omitempty" yaml:",omitempty"` - Answer []Answer `json:"answer,omitempty" xml:"answer,omitempty" yaml:",omitempty"` - Ns []Answer `json:"ns,omitempty" xml:"ns,omitempty" yaml:",omitempty"` - Additional []Answer `json:"additional,omitempty" xml:"additional,omitempty" yaml:",omitempty"` + // Header section + Header Header `json:"header,omitempty" xml:"header,omitempty" yaml:"header,omitempty"` + // Opt Pseudosection + Opt []Opts `json:"opt,omitempty" xml:"opt,omitempty" yaml:"opt,omitempty"` + // Question Section + Question []Question `json:"question,omitempty" xml:"question,omitempty" yaml:"question,omitempty"` + // Answer Section + Answer []Answer `json:"answer,omitempty" xml:"answer,omitempty" yaml:"answer,omitempty"` + // Authority Section + Authority []Answer `json:"authority,omitempty" xml:"authority,omitempty" yaml:"authority,omitempty"` + // Additional Section + Additional []Answer `json:"additional,omitempty" xml:"additional,omitempty" yaml:"additional,omitempty"` +} + +// Header is the header. +type Header struct { + Opcode string `json:"opcode," xml:"opcode," yaml:"opcode"` + Status string `json:"status," xml:"status," yaml:"status"` + ID uint16 `json:"id," xml:"id," yaml:"id"` + Response bool `json:"response," xml:"response," yaml:"response"` + Authoritative bool `json:"authoritative," xml:"authoritative," yaml:"authoritative"` + Truncated bool `json:"truncated," xml:"truncated," yaml:"truncated"` + RecursionDesired bool `json:"recursionDesired," xml:"recursionDesired," yaml:"recursionDesired"` + RecursionAvailable bool `json:"recursionAvailable," xml:"recursionAvailable," yaml:"recursionAvailable"` + Zero bool `json:"zero," xml:"zero," yaml:"zero"` + AuthenticatedData bool `json:"authenticatedData," xml:"authenticatedData," yaml:"authenticatedData"` + CheckingDisabled bool `json:"checkingDisabled," xml:"checkingDisabled," yaml:"checkingDisabled"` } // Question is a DNS Query. type Question struct { - Name string `json:"name,omitempty" xml:"name,omitempty" yaml:",omitempty"` - Class string `json:"class,omitempty" xml:"class,omitempty" yaml:",omitempty"` - Type string `json:"type,omitempty" xml:"type,omitempty" yaml:",omitempty"` + Name string `json:"name,omitempty" xml:"name,omitempty" yaml:"name,omitempty"` + Class string `json:"class,omitempty" xml:"class,omitempty" yaml:"class,omitempty"` + Type string `json:"type,omitempty" xml:"type,omitempty" yaml:"type,omitempty"` } // RRHeader is for DNS Resource Headers. type RRHeader struct { - Name string `json:"name,omitempty" xml:"name,omitempty" yaml:",omitempty"` - TTL string `json:"ttl,omitempty" xml:"ttl,omitempty" yaml:",omitempty"` - Class string `json:"class,omitempty" xml:"class,omitempty" yaml:",omitempty"` - Type string `json:"type,omitempty" xml:"type,omitempty" yaml:",omitempty"` + Name string `json:"name,omitempty" xml:"name,omitempty" yaml:"name,omitempty"` + TTL string `json:"ttl,omitempty" xml:"ttl,omitempty" yaml:"ttl,omitempty"` + Class string `json:"class,omitempty" xml:"class,omitempty" yaml:"class,omitempty"` + Type string `json:"type,omitempty" xml:"type,omitempty" yaml:"type,omitempty"` Rdlength uint16 `json:"-" xml:"-" yaml:"-"` } // Opts is for the OPT pseudosection, nearly exclusively for EDNS. type Opts struct { Name string `json:"name,omitempty" xml:"name,omitempty" yaml:",omitempty"` - Value string `json:"value" xml:"value" yaml:""` + Value string `json:"value" xml:"value" yaml:"value"` } // Answer is for a DNS Response. @@ -54,199 +67,4 @@ type Answer struct { RRHeader `json:"header,omitempty" xml:"header,omitempty" yaml:"header,omitempty"` } -// ToString turns the response into something that looks a lot like dig -// -// Much of this is taken from https://github.com/miekg/dns/blob/master/msg.go#L900 -func ToString(res util.Response, opts util.Options) (string, error) { - if res.DNS == nil { - return " MsgHdr", errNoMessage - } - - var ( - s string - opt *dns.OPT - ) - - if !opts.Short { - if opts.Display.Comments { - s += res.DNS.MsgHdr.String() + " " - s += "QUERY: " + strconv.Itoa(len(res.DNS.Question)) + ", " - s += "ANSWER: " + strconv.Itoa(len(res.DNS.Answer)) + ", " - s += "AUTHORITY: " + strconv.Itoa(len(res.DNS.Ns)) + ", " - s += "ADDITIONAL: " + strconv.Itoa(len(res.DNS.Extra)) + "\n" - opt = res.DNS.IsEdns0() - - if opt != nil && opts.Display.Opt { - // OPT PSEUDOSECTION - s += opt.String() + "\n" - } - } - - if opts.Display.Question { - if len(res.DNS.Question) > 0 { - if opts.Display.Comments { - s += "\n;; QUESTION SECTION:\n" - } - - for _, r := range res.DNS.Question { - str, err := stringParse(r.String(), false, opts) - if err != nil { - return "", fmt.Errorf("%w", err) - } - - s += str + "\n" - } - } - } - - if opts.Display.Answer { - if len(res.DNS.Answer) > 0 { - if opts.Display.Comments { - s += "\n;; ANSWER SECTION:\n" - } - - for _, r := range res.DNS.Answer { - if r != nil { - str, err := stringParse(r.String(), true, opts) - if err != nil { - return "", fmt.Errorf("%w", err) - } - - s += str + "\n" - } - } - } - } - - if opts.Display.Authority { - if len(res.DNS.Ns) > 0 { - if opts.Display.Comments { - s += "\n;; AUTHORITY SECTION:\n" - } - - for _, r := range res.DNS.Ns { - if r != nil { - str, err := stringParse(r.String(), true, opts) - if err != nil { - return "", fmt.Errorf("%w", err) - } - - s += str + "\n" - } - } - } - } - - if opts.Display.Additional { - if len(res.DNS.Extra) > 0 && (opt == nil || len(res.DNS.Extra) > 1) { - if opts.Display.Comments { - s += "\n;; ADDITIONAL SECTION:\n" - } - - for _, r := range res.DNS.Extra { - if r != nil && r.Header().Rrtype != dns.TypeOPT { - str, err := stringParse(r.String(), true, opts) - if err != nil { - return "", fmt.Errorf("%w", err) - } - - s += str + "\n" - } - } - } - } - - if opts.Display.Statistics { - s += "\n;; Query time: " + res.RTT.String() - // Add extra information to server string - var extra string - - switch { - case opts.TCP: - extra = ":" + strconv.Itoa(opts.Port) + " (TCP)" - case opts.TLS: - extra = ":" + strconv.Itoa(opts.Port) + " (TLS)" - case opts.HTTPS, opts.DNSCrypt: - extra = "" - case opts.QUIC: - extra = ":" + strconv.Itoa(opts.Port) + " (QUIC)" - default: - extra = ":" + strconv.Itoa(opts.Port) + " (UDP)" - } - - s += "\n;; SERVER: " + opts.Request.Server + extra - s += "\n;; WHEN: " + time.Now().Format(time.RFC1123Z) - s += "\n;; MSG SIZE rcvd: " + strconv.Itoa(res.DNS.Len()) + "\n" - } - } else { - // Print just the responses, nothing else - for i, resp := range res.DNS.Answer { - temp := strings.Split(resp.String(), "\t") - s += temp[len(temp)-1] - - if opts.Identify { - s += " from server " + opts.Request.Server + " in " + res.RTT.String() - } - - // Don't print newline on last line - if i != len(res.DNS.Answer)-1 { - s += "\n" - } - } - } - - return s, nil -} - -func stringParse(str string, isAns bool, opts util.Options) (string, error) { - split := strings.Split(str, "\t") - - // Make edits if so requested - - // TODO: make less ew? - // This exists because the question section should be left alone EXCEPT for punycode. - - if isAns { - if !opts.ShowTTL { - // Remove from existence - split = append(split[:1], split[2:]...) - } - - if !opts.ShowClass { - // Position depends on if the TTL is there or not. - if opts.ShowTTL { - split = append(split[:2], split[3:]...) - } else { - split = append(split[:1], split[2:]...) - } - } - - if opts.ShowTTL && opts.HumanTTL { - ttl, _ := strconv.Atoi(split[1]) - split[1] = (time.Duration(ttl) * time.Second).String() - } - } - - if opts.Display.UcodeTranslate { - var ( - err error - semi string - ) - - if strings.HasPrefix(split[0], ";") { - split[0] = strings.TrimPrefix(split[0], ";") - semi = ";" - } - - split[0], err = idna.ToUnicode(split[0]) - if err != nil { - return "", fmt.Errorf("punycode: %w", err) - } - - split[0] = semi + split[0] - } - - return strings.Join(split, "\t"), nil -} - var errNoMessage = errors.New("no message") diff --git a/util/options.go b/util/options.go index 91e5f14..139a558 100644 --- a/util/options.go +++ b/util/options.go @@ -12,67 +12,133 @@ import ( // Options is the grand structure for all query options. type Options struct { - Logger *logawl.Logger - TLSHost string + // The logger + Logger *logawl.Logger + // Host to verify TLS cert with + TLSHost string `json:"tlsHost"` + // EDNS Options EDNS - Request Request - Port int - Verbosity int - Display Displays - TC bool - ShowTTL bool - ShowClass bool - ShowQuery bool - AA bool - AD bool - CD bool - QR bool - RD bool - RA bool - IPv4 bool - Z bool - Reverse bool - HumanTTL bool - Truncate bool - Short bool - Identify bool - JSON bool - XML bool - YAML bool - QUIC bool - HTTPS bool - TLSNoVerify bool - TLS bool - DNSCrypt bool - TCP bool - IPv6 bool + // DNS request :) + Request Request + + // Verbosity levels, see [logawl.AllLevels] + Verbosity int `json:"-"` + // Display options + Display Display + // Ignore Truncation + Truncate bool `json:"ignoreTruncate"` + // Print only the answer + Short bool `json:"short"` + // When Short is true, display where the query came from + Identify bool `json:"identify"` + // Perform a reverse DNS query when true + Reverse bool `json:"reverse"` + + HeaderFlags + + // Display resposne as JSON + JSON bool `json:"-" xml:"-" yaml:"-"` + // Display response as XML + XML bool `json:"-" xml:"-" yaml:"-"` + // Display response as YAML + YAML bool `json:"-" xml:"-" yaml:"-"` + + // Use TCP instead of UDP to make the query + TCP bool `json:"tcp"` + // Use DNS-over-TLS to make the query + TLS bool `json:"dns_over_tls"` + // When using TLS, ignore certificates + TLSNoVerify bool `json:"tlsNoVerify"` + // Use DNS-over-HTTPS to make the query + HTTPS bool `json:"dns_over_https"` + // Use DNS-over-QUIC to make the query + QUIC bool `json:"dns_over_quic"` + // Use DNSCrypt to make the query + DNSCrypt bool `json:"dnscrpyt"` + + // Force IPv4 only + IPv4 bool `json:"force_IPv4"` + // Force IPv6 only + IPv6 bool `json:"force_IPv6"` } -// Displays contains toggles for what to (and not to) display. -type Displays struct { - Comments bool - Question bool // QUESTION SECTION - Opt bool // OPT PSEUDOSECTION - Answer bool // ANSWER SECTION - Authority bool // AUTHORITY SECTION - Additional bool // ADDITIONAL SECTION - Statistics bool // Query time, message size, etc. - UcodeTranslate bool // Translate Punycode back to Unicode +// HeaderFlags are the flags that are in DNS headers. +type HeaderFlags struct { + // Authoritative Answer DNS query flag + AA bool `json:"authoritative,"` + // Authenticated Data DNS query flag + AD bool `json:"authenticatedData,"` + // Checking Disabled DNS query flag + CD bool `json:"checkingDisabled,"` + // QueRy DNS query flag + QR bool `json:"query"` + // Recursion Desired DNS query flag + RD bool `json:"recursionDesired"` + // Recursion Available DNS query flag + RA bool `json:"recursionAvailable"` + // TrunCated DNS query flag + TC bool `json:"truncated"` + // Zero DNS query flag + Z bool `json:"zero"` +} + +// Display contains toggles for what to (and not to) display. +type Display struct { + /* Section displaying */ + + // Comments? + Comments bool `json:"comments"` + // QUESTION SECTION + Question bool `json:"question"` + // OPT PSEUDOSECTION + Opt bool `json:"opt"` + // ANSWER SECTION + Answer bool `json:"answer"` + // AUTHORITY SECTION + Authority bool `json:"authority"` + // ADDITIONAL SECTION + Additional bool `json:"additional"` + // Query time, message size, etc. + Statistics bool `json:"statistics"` + // Display TTL in response + TTL bool `json:"ttl"` + + /* Answer formatting */ + + // Display Class in response + ShowClass bool `json:"showClass"` + // Display query before it is sent + ShowQuery bool `json:"showQuery"` + // Display TTL as human-readable + HumanTTL bool `json:"HumanTTL"` + // Translate Punycode back to Unicode + UcodeTranslate bool `json:"unicode"` } // EDNS contains toggles for various EDNS options. type EDNS struct { - Subnet dns.EDNS0_SUBNET - ZFlag uint16 - BufSize uint16 - EnableEDNS bool - Cookie bool - DNSSEC bool - Expire bool - KeepOpen bool - Nsid bool - Padding bool - Version uint8 + // Subnet to originate query from. + Subnet dns.EDNS0_SUBNET `json:"subnet"` + // Must Be Zero flag + ZFlag uint16 `json:"zflag"` + // UDP buffer size + BufSize uint16 `json:"bufSize"` + // Enable/Disable EDNS entirely + EnableEDNS bool `json:"edns"` + // Sending EDNS cookie + Cookie bool `json:"cookie"` + // Enabling DNSSEC + DNSSEC bool `json:"dnssec"` + // Sending EDNS Expire + Expire bool `json:"expire"` + // Sending EDNS TCP keepopen + KeepOpen bool `json:"keepOpen"` + // Sending EDNS NSID + Nsid bool `json:"nsid"` + // Send EDNS Padding + Padding bool `json:"padding"` + // Set EDNS version (default: 0) + Version uint8 `json:"version"` } // ParseSubnet takes a subnet argument and makes it into one that the DNS library diff --git a/util/query.go b/util/query.go index 20851b6..79f2dbf 100644 --- a/util/query.go +++ b/util/query.go @@ -10,16 +10,26 @@ import ( // Response is the DNS response. type Response struct { - DNS *dns.Msg // The full DNS response - RTT time.Duration `json:"rtt"` // The time it took to make the DNS query + // The full DNS response + DNS *dns.Msg `json:"response"` + // The time it took to make the DNS query + RTT time.Duration `json:"rtt"` } // Request is a structure for a DNS query. type Request struct { - Server string `json:"server"` - Name string `json:"name"` - Timeout time.Duration - Retries int - Type uint16 `json:"request"` - Class uint16 `json:"class"` + // Server to query, eg. 8.8.8.8 + Server string `json:"server"` + // Domain to query, eg. example.com + Name string `json:"name"` + // Duration to wait until marking request as failed + Timeout time.Duration `json:"timeout"` + // Port to make DNS request on + Port int `json:"port"` + // Number of failures to make before giving up + Retries int `json:"retries"` + // Request type, eg. A, AAAA, NAPTR + Type uint16 `json:"type"` + // Request class, eg. IN + Class uint16 `json:"class"` } -- 2.45.2 From 288a409fc1d9ecdc34ddb4116c618ed81e4e1c17 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Fri, 16 Sep 2022 18:44:20 -0400 Subject: [PATCH 02/11] move files into more appropriate packages and into pkg directory --- cli/cli.go | 2 +- cli/dig.go | 2 +- cli/dig_test.go | 2 +- cli/misc.go | 2 +- cli/misc_test.go | 2 +- doc/wiki | 2 +- main.go | 4 ++-- {logawl => pkg/logawl}/docs.go | 0 {logawl => pkg/logawl}/logawl.go | 0 {logawl => pkg/logawl}/logger.go | 0 {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 | 10 +++------- {query => pkg/query}/query_test.go | 4 ++-- {query => pkg/query}/struct.go | 0 {query => pkg/resolvers}/DNSCrypt.go | 4 ++-- {query => pkg/resolvers}/DNSCrypt_test.go | 6 +++--- {query => pkg/resolvers}/HTTPS.go | 4 ++-- {query => pkg/resolvers}/HTTPS_test.go | 14 +++++++------- {query => pkg/resolvers}/QUIC.go | 4 ++-- {query => pkg/resolvers}/QUIC_test.go | 12 ++++++------ {query => pkg/resolvers}/general.go | 4 ++-- {query => pkg/resolvers}/general_test.go | 11 ++++++----- {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 +- 34 files changed, 62 insertions(+), 60 deletions(-) rename {logawl => pkg/logawl}/docs.go (100%) rename {logawl => pkg/logawl}/logawl.go (100%) rename {logawl => pkg/logawl}/logger.go (100%) 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 (98%) rename {query => pkg/query}/query_test.go (97%) rename {query => pkg/query}/struct.go (100%) rename {query => pkg/resolvers}/DNSCrypt.go (95%) rename {query => pkg/resolvers}/DNSCrypt_test.go (94%) rename {query => pkg/resolvers}/HTTPS.go (97%) rename {query => pkg/resolvers}/HTTPS_test.go (90%) rename {query => pkg/resolvers}/QUIC.go (97%) rename {query => pkg/resolvers}/QUIC_test.go (89%) rename {query => pkg/resolvers}/general.go (96%) rename {query => pkg/resolvers}/general_test.go (89%) 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 (94%) 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/cli/cli.go index 159b6d3..4a61b7d 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -11,7 +11,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/dig.go b/cli/dig.go index 00ee8a6..35ba5fe 100644 --- a/cli/dig.go +++ b/cli/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/cli/dig_test.go index 5293bb2..7073dbf 100644 --- a/cli/dig_test.go +++ b/cli/dig_test.go @@ -6,7 +6,7 @@ import ( "testing" "git.froth.zone/sam/awl/cli" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "gotest.tools/v3/assert" ) diff --git a/cli/misc.go b/cli/misc.go index 85228fd..3011cd6 100644 --- a/cli/misc.go +++ b/cli/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/cli/misc_test.go index 286e0e3..3176cd3 100644 --- a/cli/misc_test.go +++ b/cli/misc_test.go @@ -7,7 +7,7 @@ import ( "testing" "git.froth.zone/sam/awl/cli" - "git.froth.zone/sam/awl/util" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) diff --git a/doc/wiki b/doc/wiki index 6f3070f..0fba1fb 160000 --- a/doc/wiki +++ b/doc/wiki @@ -1 +1 @@ -Subproject commit 6f3070f5933d0cc48bc8bb5290a1d0cc1825cd75 +Subproject commit 0fba1fbe4b12e8c88514b3f7d98be3e75a5a034d diff --git a/main.go b/main.go index 641deac..494e786 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,8 @@ import ( "strings" "git.froth.zone/sam/awl/cli" - "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" ) 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 100% rename from logawl/logawl.go rename to pkg/logawl/logawl.go diff --git a/logawl/logger.go b/pkg/logawl/logger.go similarity index 100% rename from logawl/logger.go rename to pkg/logawl/logger.go 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 c026343..800fe56 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 4eade9b..d80a2f2 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 98% rename from query/query.go rename to pkg/query/query.go index 1b8dcaf..729db25 100644 --- a/query/query.go +++ b/pkg/query/query.go @@ -8,17 +8,13 @@ import ( "strings" "time" - "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" "golang.org/x/net/idna" ) -const ( - tcp = "tcp" - udp = "udp" -) - // ToString turns the response into something that looks a lot like dig // // Much of this is taken from https://github.com/miekg/dns/blob/master/msg.go#L900 @@ -336,7 +332,7 @@ func CreateQuery(opts util.Options) (util.Response, error) { } } - resolver, err := LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) if err != nil { return util.Response{}, err } 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 7c76d34..fce2cf3 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 95% rename from query/DNSCrypt.go rename to pkg/resolvers/DNSCrypt.go index 53afc3d..18927ef 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" ) 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 37df392..763dde0 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 97% rename from query/HTTPS.go rename to pkg/resolvers/HTTPS.go index 0c56002..d090a18 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" ) diff --git a/query/HTTPS_test.go b/pkg/resolvers/HTTPS_test.go similarity index 90% rename from query/HTTPS_test.go rename to pkg/resolvers/HTTPS_test.go index b844847..533c12d 100644 --- a/query/HTTPS_test.go +++ b/pkg/resolvers/HTTPS_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/resolvers" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) @@ -26,7 +26,7 @@ func TestResolveHTTPS(t *testing.T) { }, } // testCase := util.Request{Server: "https://dns9.quad9.net/dns-query", Type: dns.TypeA, Name: "git.froth.zone."} - resolver, err := query.LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) assert.NilError(t, err) msg := new(dns.Msg) @@ -49,7 +49,7 @@ func Test2ResolveHTTPS(t *testing.T) { var err error testCase := util.Request{Type: dns.TypeA, Name: "git.froth.zone"} - resolver, err := query.LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) assert.NilError(t, err) msg := new(dns.Msg) @@ -77,7 +77,7 @@ func Test3ResolveHTTPS(t *testing.T) { // testCase.Name = fmt.Sprintf("%s.", testCase.Name) // } - resolver, err := query.LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) assert.NilError(t, err) msg := new(dns.Msg) @@ -103,7 +103,7 @@ func Test404ResolveHTTPS(t *testing.T) { }, } // testCase := util.Request{Server: "https://dns9.quad9.net/dns-query", Type: dns.TypeA, Name: "git.froth.zone."} - resolver, err := query.LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) assert.NilError(t, err) msg := new(dns.Msg) diff --git a/query/QUIC.go b/pkg/resolvers/QUIC.go similarity index 97% rename from query/QUIC.go rename to pkg/resolvers/QUIC.go index 9191fdd..94df961 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" ) diff --git a/query/QUIC_test.go b/pkg/resolvers/QUIC_test.go similarity index 89% rename from query/QUIC_test.go rename to pkg/resolvers/QUIC_test.go index 6d83199..0bfe025 100644 --- a/query/QUIC_test.go +++ b/pkg/resolvers/QUIC_test.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: BSD-3-Clause -package query_test +package resolvers_test import ( "fmt" @@ -10,8 +10,8 @@ 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" ) @@ -35,7 +35,7 @@ func TestQuic(t *testing.T) { for i := range testCases { switch i { case 0: - resolver, err := query.LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) assert.NilError(t, err) // if the domain is not canonical, make it canonical if !strings.HasSuffix(testCase.Name, ".") { @@ -50,7 +50,7 @@ func TestQuic(t *testing.T) { assert.ErrorContains(t, err, "fully qualified") assert.Equal(t, res, util.Response{}) case 1: - resolver, err := query.LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) assert.NilError(t, err) testCase2.Server = net.JoinHostPort(testCase2.Server, strconv.Itoa(opts.Request.Port)) @@ -79,7 +79,7 @@ func TestInvalidQuic(t *testing.T) { Logger: util.InitLogger(0), Request: util.Request{Server: "example.com", Port: 853, Type: dns.TypeA, Name: "git.froth.zone", Timeout: 10 * time.Millisecond}, } - resolver, err := query.LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) assert.NilError(t, err) msg := new(dns.Msg) diff --git a/query/general.go b/pkg/resolvers/general.go similarity index 96% rename from query/general.go rename to pkg/resolvers/general.go index 8e0973b..907e6d3 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" ) diff --git a/query/general_test.go b/pkg/resolvers/general_test.go similarity index 89% rename from query/general_test.go rename to pkg/resolvers/general_test.go index fe06c12..c4379bb 100644 --- a/query/general_test.go +++ b/pkg/resolvers/general_test.go @@ -1,13 +1,14 @@ // 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/query" + "git.froth.zone/sam/awl/pkg/resolvers" + "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" ) @@ -26,7 +27,7 @@ func TestResolve(t *testing.T) { Retries: 0, }, } - resolver, err := query.LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) assert.NilError(t, err) msg := new(dns.Msg) @@ -49,7 +50,7 @@ func TestTruncate(t *testing.T) { Name: "limit.txt.example.", }, } - resolver, err := query.LoadResolver(opts) + resolver, err := resolvers.LoadResolver(opts) assert.NilError(t, err) msg := new(dns.Msg) 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 139a558..9ae3a1f 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 94% rename from util/options_test.go rename to pkg/util/options_test.go index 741e7c2..1e28e95 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 5a77748..4725b4d 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" ) -- 2.45.2 From 790529267a8c0bc367fc7970fdd3145809820e8e Mon Sep 17 00:00:00 2001 From: grumbulon Date: Fri, 16 Sep 2022 18:48:34 -0400 Subject: [PATCH 03/11] logawl cleanup --- pkg/logawl/logawl.go | 18 +++++------ pkg/logawl/logger.go | 74 ++++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/pkg/logawl/logawl.go b/pkg/logawl/logawl.go index ee5032b..b14a0ad 100644 --- a/pkg/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/pkg/logawl/logger.go b/pkg/logawl/logger.go index b0993ea..5aaad39 100644 --- a/pkg/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...)) } -- 2.45.2 From 80e7ded4fb01bf2da27e9d443527cfc2d2365a05 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Fri, 16 Sep 2022 19:20:00 -0400 Subject: [PATCH 04/11] resolver receiever consistency --- pkg/resolvers/DNSCrypt.go | 16 ++++++++-------- pkg/resolvers/HTTPS.go | 12 ++++++------ pkg/resolvers/QUIC.go | 26 +++++++++++++------------- pkg/resolvers/general.go | 30 +++++++++++++++--------------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/pkg/resolvers/DNSCrypt.go b/pkg/resolvers/DNSCrypt.go index 18927ef..dca3531 100644 --- a/pkg/resolvers/DNSCrypt.go +++ b/pkg/resolvers/DNSCrypt.go @@ -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/pkg/resolvers/HTTPS.go b/pkg/resolvers/HTTPS.go index d090a18..e4c4388 100644 --- a/pkg/resolvers/HTTPS.go +++ b/pkg/resolvers/HTTPS.go @@ -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/pkg/resolvers/QUIC.go b/pkg/resolvers/QUIC.go index 94df961..60999be 100644 --- a/pkg/resolvers/QUIC.go +++ b/pkg/resolvers/QUIC.go @@ -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/pkg/resolvers/general.go b/pkg/resolvers/general.go index 907e6d3..88353af 100644 --- a/pkg/resolvers/general.go +++ b/pkg/resolvers/general.go @@ -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,53 +27,53 @@ 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.Debug("Using", dnsClient.Net, "for making the request") + resolver.opts.Logger.Debug("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) } - 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 { -- 2.45.2 From 323a99b6523daf0c27675137b38a9092050624d1 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Fri, 16 Sep 2022 19:47:03 -0400 Subject: [PATCH 05/11] wrapcheck fix --- pkg/query/query.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pkg/query/query.go b/pkg/query/query.go index c048130..e1fc132 100644 --- a/pkg/query/query.go +++ b/pkg/query/query.go @@ -12,11 +12,6 @@ import ( "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) { @@ -140,7 +135,7 @@ func CreateQuery(opts util.Options) (util.Response, error) { resolver, err := resolvers.LoadResolver(opts) if err != nil { - return util.Response{}, err + return util.Response{}, fmt.Errorf("unable to load resolvers: %v", err) } opts.Logger.Info("Query successfully loaded") -- 2.45.2 From 7b3b9eeb5318ef6cd65b7747731a28df465cf103 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Fri, 16 Sep 2022 19:50:29 -0400 Subject: [PATCH 06/11] WARN [linter] revive: the following rules (context-keys-type,errorf,modifies-value-receiver,time-naming,unexported-return,var-declaration) --- pkg/query/query.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/query/query.go b/pkg/query/query.go index e1fc132..9ab22a7 100644 --- a/pkg/query/query.go +++ b/pkg/query/query.go @@ -135,7 +135,7 @@ func CreateQuery(opts util.Options) (util.Response, error) { resolver, err := resolvers.LoadResolver(opts) if err != nil { - return util.Response{}, fmt.Errorf("unable to load resolvers: %v", err) + return util.Response{}, fmt.Errorf("unable to load resolvers: %w", err) } opts.Logger.Info("Query successfully loaded") -- 2.45.2 From 20f9cd0c4fe766f71fee0b9101e6abbb392b610e Mon Sep 17 00:00:00 2001 From: grumbulon Date: Fri, 16 Sep 2022 20:03:47 -0400 Subject: [PATCH 07/11] test --- cli/cli_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cli/cli_test.go b/cli/cli_test.go index bc6802b..e830c2f 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -3,7 +3,6 @@ package cli_test import ( - "os" "testing" "time" @@ -28,8 +27,6 @@ func TestTLSPort(t *testing.T) { assert.NilError(t, err) assert.Equal(t, opts.Request.Port, 853) - - os.Args = args } func TestSubnet(t *testing.T) { -- 2.45.2 From fa05b3c699644e244b00aae08360817adeefed39 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Thu, 22 Sep 2022 19:41:52 -0400 Subject: [PATCH 08/11] change cli dir to cmd, update path in main.go --- {cli => cmd}/cli.go | 0 {cli => cmd}/cli_test.go | 0 {cli => cmd}/dig.go | 0 {cli => cmd}/dig_test.go | 0 {cli => cmd}/docs.go | 0 {cli => cmd}/misc.go | 0 {cli => cmd}/misc_test.go | 0 main.go | 2 +- 8 files changed, 1 insertion(+), 1 deletion(-) rename {cli => cmd}/cli.go (100%) rename {cli => cmd}/cli_test.go (100%) rename {cli => cmd}/dig.go (100%) rename {cli => cmd}/dig_test.go (100%) rename {cli => cmd}/docs.go (100%) rename {cli => cmd}/misc.go (100%) rename {cli => cmd}/misc_test.go (100%) diff --git a/cli/cli.go b/cmd/cli.go similarity index 100% rename from cli/cli.go rename to cmd/cli.go diff --git a/cli/cli_test.go b/cmd/cli_test.go similarity index 100% rename from cli/cli_test.go rename to cmd/cli_test.go diff --git a/cli/dig.go b/cmd/dig.go similarity index 100% rename from cli/dig.go rename to cmd/dig.go diff --git a/cli/dig_test.go b/cmd/dig_test.go similarity index 100% rename from cli/dig_test.go rename to cmd/dig_test.go 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 100% rename from cli/misc.go rename to cmd/misc.go diff --git a/cli/misc_test.go b/cmd/misc_test.go similarity index 100% rename from cli/misc_test.go rename to cmd/misc_test.go diff --git a/main.go b/main.go index b164e61..448b8a1 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( "os" "strings" - "git.froth.zone/sam/awl/cli" + cli "git.froth.zone/sam/awl/cmd" "git.froth.zone/sam/awl/pkg/query" "git.froth.zone/sam/awl/pkg/util" ) -- 2.45.2 From 862b91012311b87d2be8b3d0111b33bea9da5cf3 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Thu, 22 Sep 2022 19:43:09 -0400 Subject: [PATCH 09/11] oops --- cmd/cli_test.go | 2 +- cmd/dig_test.go | 2 +- cmd/misc_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/cli_test.go b/cmd/cli_test.go index 4730aa0..7e25649 100644 --- a/cmd/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/cmd/dig_test.go b/cmd/dig_test.go index 2b5b94f..6ba9cf5 100644 --- a/cmd/dig_test.go +++ b/cmd/dig_test.go @@ -5,7 +5,7 @@ package cli_test import ( "testing" - "git.froth.zone/sam/awl/cli" + cli "git.froth.zone/sam/awl/cmd" "git.froth.zone/sam/awl/pkg/util" "gotest.tools/v3/assert" ) diff --git a/cmd/misc_test.go b/cmd/misc_test.go index d2f9a09..fa20dbe 100644 --- a/cmd/misc_test.go +++ b/cmd/misc_test.go @@ -5,7 +5,7 @@ package cli_test import ( "testing" - "git.froth.zone/sam/awl/cli" + cli "git.froth.zone/sam/awl/cmd" "git.froth.zone/sam/awl/pkg/util" "github.com/miekg/dns" "gotest.tools/v3/assert" -- 2.45.2 From 085324758eba8dc43eccc75a0b1a1aadf4de9d38 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Thu, 22 Sep 2022 19:45:30 -0400 Subject: [PATCH 10/11] hardcoding stuff is good actually --- template.mk | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 -- 2.45.2 From fb483b70bd6b02eb3bc673305556fafa20500400 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Sat, 24 Sep 2022 19:02:08 -0400 Subject: [PATCH 11/11] re-add lost stuff in previous rebase --- go.sum | 4 ---- pkg/util/query.go | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/go.sum b/go.sum index 6ac42ef..a53af50 100644 --- a/go.sum +++ b/go.sum @@ -77,12 +77,8 @@ 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/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-20220914170420-dc92f8653013 h1:ZjglnWxEUdPyXl4o/j4T89SRCI+4X6NW6185PNLEOF4= -golang.org/x/exp v0.0.0-20220914170420-dc92f8653013/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= 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= diff --git a/pkg/util/query.go b/pkg/util/query.go index 79f2dbf..8e1dd72 100644 --- a/pkg/util/query.go +++ b/pkg/util/query.go @@ -13,23 +13,23 @@ type Response struct { // The full DNS response DNS *dns.Msg `json:"response"` // The time it took to make the DNS query - RTT time.Duration `json:"rtt"` + RTT time.Duration `json:"rtt" example:"2000000000"` } // Request is a structure for a DNS query. type Request struct { - // Server to query, eg. 8.8.8.8 - Server string `json:"server"` - // Domain to query, eg. example.com - Name string `json:"name"` + // Server to query + Server string `json:"server" example:"1.0.0.1"` + // Domain to query + Name string `json:"name" example:"example.com"` // Duration to wait until marking request as failed - Timeout time.Duration `json:"timeout"` + Timeout time.Duration `json:"timeout" example:"2000000000"` // Port to make DNS request on - Port int `json:"port"` + Port int `json:"port" example:"53"` // Number of failures to make before giving up - Retries int `json:"retries"` + Retries int `json:"retries" example:"2"` // Request type, eg. A, AAAA, NAPTR - Type uint16 `json:"type"` + Type uint16 `json:"type" example:"1"` // Request class, eg. IN - Class uint16 `json:"class"` + Class uint16 `json:"class" example:"1"` } -- 2.45.2