From 395869d04f03fdb4452f303fe9d61284b7283264 Mon Sep 17 00:00:00 2001 From: Mathijs van Mourick <14082160+mmourick@users.noreply.github.com> Date: Tue, 12 Dec 2023 14:17:37 +0100 Subject: [PATCH 1/5] fix: use bracket notation around ipv6 addresses to remove ambiguity with port --- pkg/core/core.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/core/core.go b/pkg/core/core.go index dabd91c..d884368 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -2,6 +2,7 @@ package core import ( "fmt" + "net" "runtime" "time" @@ -60,6 +61,11 @@ func SendDNSQuery(client *dns.Client, msg dns.Msg, dnsServerIP string) (*dns.Msg logrus.Fatal("Please specify a DNS server IP explicitly with the `--dns-server-ip` flag.") } dnsServerIP = conf.Servers[0] + + } + // If the server IP is IPv6, wrap it in square brackets to remove ambiguity from port number and address. + if net.ParseIP(dnsServerIP).To4() == nil { + dnsServerIP = "[" + dnsServerIP + "]" } logrus.Debugf("Sending DNS query to %s", dnsServerIP) From b96372ac0e2c8c0972256ed1f388e3398873fe7c Mon Sep 17 00:00:00 2001 From: Mathijs van Mourick <14082160+mmourick@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:42:20 +0100 Subject: [PATCH 2/5] feat: add dns server port flag --- cmd/root.go | 2 ++ pkg/core/core.go | 1 + pkg/model/flagstore.go | 1 + 3 files changed, 4 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index d70a336..0103993 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -52,6 +52,7 @@ var ( msg := core.PrepareDNSQuery(domainName, queryType.Type) response, _, err := core.SendDNSQuery(&client, msg, flagStore.DNSServerIP) + response, _, err := core.SendDNSQuery(&client, msg, flagStore.DNSServerIP, flagStore.DNSServerPort) if err != nil { log.Fatal(err) } @@ -66,6 +67,7 @@ func init() { setupCobraUsageTemplate() rootCmd.CompletionOptions.DisableDefaultCmd = true rootCmd.Flags().StringVar(&flagStore.DNSServerIP, "dns-server-ip", "", "IP address of the DNS server") + rootCmd.Flags().StringVar(&flagStore.DNSServerPort, "dns-server-port", "53", "port of the DNS server") rootCmd.Flags().StringVarP(&flagStore.UserSpecifiedQueryType, "query-type", "q", "", "specific query type to filter on") rootCmd.Flags().BoolVarP(&flagStore.Debug, "debug", "d", false, "verbose logging") } diff --git a/pkg/core/core.go b/pkg/core/core.go index d884368..24ef887 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -50,6 +50,7 @@ func PrepareDNSQuery(domainName string, queryType uint16) dns.Msg { // SendDNSQuery sends a DNS query to a given DNS server. func SendDNSQuery(client *dns.Client, msg dns.Msg, dnsServerIP string) (*dns.Msg, time.Duration, error) { +func SendDNSQuery(client *dns.Client, msg dns.Msg, dnsServerIP, dnsServerPort string) (*dns.Msg, time.Duration, error) { if dnsServerIP == "" { goOS := runtime.GOOS if goOS == windows { diff --git a/pkg/model/flagstore.go b/pkg/model/flagstore.go index 6ab6796..4af3dd0 100644 --- a/pkg/model/flagstore.go +++ b/pkg/model/flagstore.go @@ -2,6 +2,7 @@ package model type Flagstore struct { DNSServerIP string + DNSServerPort string UserSpecifiedQueryType string Debug bool } From 461962e062a188115fe221518352766f1e1ee3f0 Mon Sep 17 00:00:00 2001 From: Mathijs van Mourick <14082160+mmourick@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:47:36 +0100 Subject: [PATCH 3/5] refactor: more clean handling of dns server ip --- cmd/root.go | 1 - pkg/core/core.go | 10 +++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 0103993..e8a651b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -51,7 +51,6 @@ var ( for _, queryType := range queryTypes { msg := core.PrepareDNSQuery(domainName, queryType.Type) - response, _, err := core.SendDNSQuery(&client, msg, flagStore.DNSServerIP) response, _, err := core.SendDNSQuery(&client, msg, flagStore.DNSServerIP, flagStore.DNSServerPort) if err != nil { log.Fatal(err) diff --git a/pkg/core/core.go b/pkg/core/core.go index 24ef887..9757c18 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -49,7 +49,6 @@ func PrepareDNSQuery(domainName string, queryType uint16) dns.Msg { } // SendDNSQuery sends a DNS query to a given DNS server. -func SendDNSQuery(client *dns.Client, msg dns.Msg, dnsServerIP string) (*dns.Msg, time.Duration, error) { func SendDNSQuery(client *dns.Client, msg dns.Msg, dnsServerIP, dnsServerPort string) (*dns.Msg, time.Duration, error) { if dnsServerIP == "" { goOS := runtime.GOOS @@ -64,13 +63,10 @@ func SendDNSQuery(client *dns.Client, msg dns.Msg, dnsServerIP, dnsServerPort st dnsServerIP = conf.Servers[0] } - // If the server IP is IPv6, wrap it in square brackets to remove ambiguity from port number and address. - if net.ParseIP(dnsServerIP).To4() == nil { - dnsServerIP = "[" + dnsServerIP + "]" - } - logrus.Debugf("Sending DNS query to %s", dnsServerIP) - response, timeDuration, err := client.Exchange(&msg, dnsServerIP+":53") + addr := net.JoinHostPort(dnsServerIP, dnsServerPort) + + response, timeDuration, err := client.Exchange(&msg, addr) if err != nil { logrus.Debug("Failed to receive DNS response.") From 740789da0a13ad331cb5fceceb290e8a628403ae Mon Sep 17 00:00:00 2001 From: Mathijs van Mourick <14082160+mmourick@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:59:41 +0100 Subject: [PATCH 4/5] chore: add back debug logging --- pkg/core/core.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/core/core.go b/pkg/core/core.go index 9757c18..8158578 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -66,6 +66,7 @@ func SendDNSQuery(client *dns.Client, msg dns.Msg, dnsServerIP, dnsServerPort st addr := net.JoinHostPort(dnsServerIP, dnsServerPort) + logrus.Debugf("Sending DNS query to %s", addr) response, timeDuration, err := client.Exchange(&msg, addr) if err != nil { From 9f2d041aea69716df9cddacbd791b8280e78a28f Mon Sep 17 00:00:00 2001 From: Mathijs van Mourick <14082160+mmourick@users.noreply.github.com> Date: Wed, 10 Jan 2024 00:00:52 +0100 Subject: [PATCH 5/5] chore: formatting --- pkg/core/core.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/core/core.go b/pkg/core/core.go index 8158578..5444fe7 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -61,7 +61,6 @@ func SendDNSQuery(client *dns.Client, msg dns.Msg, dnsServerIP, dnsServerPort st logrus.Fatal("Please specify a DNS server IP explicitly with the `--dns-server-ip` flag.") } dnsServerIP = conf.Servers[0] - } addr := net.JoinHostPort(dnsServerIP, dnsServerPort)