diff --git a/go.mod b/go.mod index 186cffc..05cd367 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/ameshkov/dnscrypt/v2 v2.2.7 github.com/jedisct1/go-dnsstamps v0.0.0-20230211133001-124a632de565 github.com/jessevdk/go-flags v1.5.0 + github.com/json-iterator/go v1.1.12 github.com/miekg/dns v1.1.57 github.com/natesales/bgptools-go v0.0.0-20230212051756-2b519d61269c github.com/quic-go/quic-go v0.40.0 @@ -27,6 +28,8 @@ require ( github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect github.com/kr/pretty v0.3.1 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/onsi/ginkgo/v2 v2.13.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/quic-go/qpack v0.4.0 // indirect diff --git a/go.sum b/go.sum index 6907199..621bb6b 100644 --- a/go.sum +++ b/go.sum @@ -29,18 +29,25 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/pprof v0.0.0-20231212022811-ec68065c825e h1:bwOy7hAFd0C91URzMIEBfr6BAz29yk7Qj0cy6S7DJlU= github.com/google/pprof v0.0.0-20231212022811-ec68065c825e/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik= github.com/jedisct1/go-dnsstamps v0.0.0-20230211133001-124a632de565 h1:BPBMaUCgtmiHvqgugbSuegXjADJfERsPbmRqgdq8Pjo= github.com/jedisct1/go-dnsstamps v0.0.0-20230211133001-124a632de565/go.mod h1:mEGEFZsGe4sG5Mb3Xi89pmsy+TZ0946ArbYMGKAM5uA= github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM= github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/natesales/bgptools-go v0.0.0-20230212051756-2b519d61269c h1:bblm7D7Ld1/zkWvMU1j60lMk5h/F4AiKW23j1yffM5Y= github.com/natesales/bgptools-go v0.0.0-20230212051756-2b519d61269c/go.mod h1:jl8YnQACciyOXRgNIRhURrCF9FmRHjnfT8UCj3LBkyY= github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs= @@ -63,6 +70,7 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs github.com/sthorne/odoh-go v1.0.4 h1:RPJceVs/dIpNE3gyzk6wdSay+nR+tI1YXBUwykvCEXI= github.com/sthorne/odoh-go v1.0.4/go.mod h1:KdB/NGiepr9bLVs3k26uWl4HHPHqa2DaoPUgUfKNmJU= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= diff --git a/main.go b/main.go index c72766e..df6bb83 100644 --- a/main.go +++ b/main.go @@ -450,6 +450,7 @@ All long form (--) flags can be toggled with the dig-standard +[no]flag notation } entries = append(entries, &output.Entry{ + Queries: msgs, Replies: replies, Server: server, Txp: txp, diff --git a/main_test.go b/main_test.go index d1adad7..829f123 100644 --- a/main_test.go +++ b/main_test.go @@ -66,9 +66,10 @@ func TestMainJSONFormat(t *testing.T) { "--format=json", ) assert.Nil(t, err) - assert.Contains(t, out.String(), `"Preference":0,"Mx":"."`) - assert.Contains(t, out.String(), `"Ns":"a.iana-servers.net."`) - assert.Contains(t, out.String(), `"Txt":["v=spf1 -all"]`) + o := strings.ReplaceAll(out.String(), `\\"`, `"`) + assert.Contains(t, o, `"preference":0,"mx":"."`) + assert.Contains(t, o, `"ns":"a.iana-servers.net."`) + assert.Contains(t, o, `"txt":["v=spf1 -all"`) } func TestMainInvalidOutputFormat(t *testing.T) { @@ -200,7 +201,8 @@ func TestMainPad(t *testing.T) { "--format=json", ) assert.Nil(t, err) - assert.Contains(t, out.String(), `"Truncated":false`) + o := strings.ReplaceAll(out.String(), `\\"`, `"`) + assert.Contains(t, o, `"truncated":false`) } func TestMainChaosClass(t *testing.T) { diff --git a/output/output.go b/output/output.go index a7e92be..ae6c3ab 100644 --- a/output/output.go +++ b/output/output.go @@ -23,6 +23,7 @@ type Printer struct { // Entry stores the replies from a server type Entry struct { + Queries []dns.Msg Replies []*dns.Msg Server string diff --git a/output/structured.go b/output/structured.go index 6d000d4..c18e466 100644 --- a/output/structured.go +++ b/output/structured.go @@ -1,45 +1,27 @@ package output import ( - "encoding/json" - "time" + "strings" - "github.com/miekg/dns" + jsoniter "github.com/json-iterator/go" + "github.com/json-iterator/go/extra" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" "github.com/natesales/q/util" ) -type reply struct { - Server string - QueryTime int64 - Answers []dns.RR - ID uint16 - Truncated bool -} - func (p Printer) PrintStructured(entries []*Entry) { - out := make([]reply, 0) - for _, entry := range entries { - for _, r := range entry.Replies { - out = append(out, reply{ - Server: entry.Server, - QueryTime: int64(entry.Time.Round(time.Millisecond)), - Answers: r.Answer, - ID: r.Id, - Truncated: r.Truncated, - }) - } - } - - var b []byte - var err error + var marshaler func(any) ([]byte, error) if p.Opts.Format == "json" { - b, err = json.Marshal(out) + extra.SetNamingStrategy(strings.ToLower) + json := jsoniter.ConfigCompatibleWithStandardLibrary + marshaler = json.Marshal } else { // yaml - b, err = yaml.Marshal(out) + marshaler = yaml.Marshal } + + b, err := marshaler(entries) if err != nil { log.Fatalf("error marshaling output: %s", err) }