Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions maps/dnsmap/dnsmap.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions maps/dnsmap/dnsmap_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
44 changes: 0 additions & 44 deletions maps/mapsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

"github.com/miekg/dns"
"golang.org/x/exp/constraints"
extmaps "golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -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{})
Expand Down
14 changes: 0 additions & 14 deletions maps/mapsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand Down
Loading