From 2f3e744c8a405404ed81748d0013e8424b96fd62 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Wed, 17 Jun 2026 13:14:06 +0200 Subject: [PATCH] split dnsmap --- maps/dnsmap/dnsmap.go | 53 ++++++++++++++++++++++++++++++++++++++ maps/dnsmap/dnsmap_test.go | 21 +++++++++++++++ maps/mapsutil.go | 44 ------------------------------- maps/mapsutil_test.go | 14 ---------- 4 files changed, 74 insertions(+), 58 deletions(-) create mode 100644 maps/dnsmap/dnsmap.go create mode 100644 maps/dnsmap/dnsmap_test.go diff --git a/maps/dnsmap/dnsmap.go b/maps/dnsmap/dnsmap.go new file mode 100644 index 00000000..8c281412 --- /dev/null +++ b/maps/dnsmap/dnsmap.go @@ -0,0 +1,53 @@ +// Package dnsmap converts DNS messages into matcher maps. +// +// It lives in its own package (rather than in mapsutil) so that the heavy +// github.com/miekg/dns dependency is only linked into binaries that actually +// convert DNS messages, instead of every consumer of the generic map helpers. +package dnsmap + +import ( + "fmt" + + "github.com/miekg/dns" +) + +const defaultFormat = "%s" + +// DNSToMap converts a DNS message to a matcher map. +func DNSToMap(msg *dns.Msg, format string) (m map[string]interface{}) { + m = make(map[string]interface{}) + + if format == "" { + format = defaultFormat + } + + m[fmt.Sprintf(format, "rcode")] = msg.Rcode + + var qs string + for _, question := range msg.Question { + qs += fmt.Sprintln(question.String()) + } + m[fmt.Sprintf(format, "question")] = qs + + var exs string + for _, extra := range msg.Extra { + exs += fmt.Sprintln(extra.String()) + } + m[fmt.Sprintf(format, "extra")] = exs + + var ans string + for _, answer := range msg.Answer { + ans += fmt.Sprintln(answer.String()) + } + m[fmt.Sprintf(format, "answer")] = ans + + var nss string + for _, ns := range msg.Ns { + nss += fmt.Sprintln(ns.String()) + } + m[fmt.Sprintf(format, "ns")] = nss + + m[fmt.Sprintf(format, "raw")] = msg.String() + + return m +} diff --git a/maps/dnsmap/dnsmap_test.go b/maps/dnsmap/dnsmap_test.go new file mode 100644 index 00000000..c9f2f115 --- /dev/null +++ b/maps/dnsmap/dnsmap_test.go @@ -0,0 +1,21 @@ +package dnsmap + +import ( + "net" + "testing" + + "github.com/miekg/dns" + "github.com/stretchr/testify/require" +) + +func TestDNSToMap(t *testing.T) { + msg := dns.Msg{} + msg.Rcode = 1 + msg.Question = []dns.Question{{Name: "test", Qtype: 1, Qclass: 1}} + msg.Extra = []dns.RR{&dns.A{Hdr: dns.RR_Header{Name: "test", Rrtype: 1, Class: 1, Ttl: 1}, A: net.ParseIP("0.0.0.0")}} + msg.Answer = []dns.RR{&dns.A{Hdr: dns.RR_Header{Name: "test", Rrtype: 1, Class: 1, Ttl: 1}, A: net.ParseIP("0.0.0.0")}} + msg.Ns = []dns.RR{&dns.A{Hdr: dns.RR_Header{Name: "test", Rrtype: 1, Class: 1, Ttl: 1}, A: net.ParseIP("0.0.0.0")}} + m := DNSToMap(&msg, "") + require.NotNil(t, m) + require.NotEmpty(t, m) +} diff --git a/maps/mapsutil.go b/maps/mapsutil.go index 1c77a8ae..6c3cc8f6 100644 --- a/maps/mapsutil.go +++ b/maps/mapsutil.go @@ -10,7 +10,6 @@ import ( "strings" "time" - "github.com/miekg/dns" "golang.org/x/exp/constraints" extmaps "golang.org/x/exp/maps" ) @@ -61,49 +60,6 @@ func HTTPToMap(resp *http.Response, body, headers string, duration time.Duration return m } -// DNSToMap Converts DNS to Matcher Map -func DNSToMap(msg *dns.Msg, format string) (m map[string]interface{}) { - m = make(map[string]interface{}) - - if format == "" { - format = defaultFormat - } - - m[fmt.Sprintf(format, "rcode")] = msg.Rcode - - var qs string - - for _, question := range msg.Question { - qs += fmt.Sprintln(question.String()) - } - - m[fmt.Sprintf(format, "question")] = qs - - var exs string - for _, extra := range msg.Extra { - exs += fmt.Sprintln(extra.String()) - } - - m[fmt.Sprintf(format, "extra")] = exs - - var ans string - for _, answer := range msg.Answer { - ans += fmt.Sprintln(answer.String()) - } - - m[fmt.Sprintf(format, "answer")] = ans - - var nss string - for _, ns := range msg.Ns { - nss += fmt.Sprintln(ns.String()) - } - - m[fmt.Sprintf(format, "ns")] = nss - m[fmt.Sprintf(format, "raw")] = msg.String() - - return m -} - // HTTPRequestToMap Converts HTTP Request to Matcher Map func HTTPRequestToMap(req *http.Request) (map[string]interface{}, error) { m := make(map[string]interface{}) diff --git a/maps/mapsutil_test.go b/maps/mapsutil_test.go index 6f7f48b0..29b55f69 100644 --- a/maps/mapsutil_test.go +++ b/maps/mapsutil_test.go @@ -3,14 +3,12 @@ package mapsutil import ( "crypto/tls" "io" - "net" "net/http" "net/url" "strings" "testing" "time" - "github.com/miekg/dns" "github.com/stretchr/testify/require" "golang.org/x/exp/maps" ) @@ -84,18 +82,6 @@ func TestHTTPToMap(t *testing.T) { require.NotEmpty(t, m) } -func TestDNSToMap(t *testing.T) { - msg := dns.Msg{} - msg.Rcode = 1 - msg.Question = []dns.Question{{Name: "test", Qtype: 1, Qclass: 1}} - msg.Extra = []dns.RR{&dns.A{Hdr: dns.RR_Header{Name: "test", Rrtype: 1, Class: 1, Ttl: 1}, A: net.ParseIP("0.0.0.0")}} - msg.Answer = []dns.RR{&dns.A{Hdr: dns.RR_Header{Name: "test", Rrtype: 1, Class: 1, Ttl: 1}, A: net.ParseIP("0.0.0.0")}} - msg.Ns = []dns.RR{&dns.A{Hdr: dns.RR_Header{Name: "test", Rrtype: 1, Class: 1, Ttl: 1}, A: net.ParseIP("0.0.0.0")}} - m := DNSToMap(&msg, "") - require.NotNil(t, m) - require.NotEmpty(t, m) -} - func TestHTTPRequestToMap(t *testing.T) { m, err := HTTPRequestToMap(req)