Skip to content

Commit

Permalink
Retrieve DNS ip address from local configuration
Browse files Browse the repository at this point in the history
If a explicit DNS server ip address is not provided then use the OS default settings. Only supports UNIX-like systems.
  • Loading branch information
mmourick committed Jul 11, 2023
1 parent 06872d9 commit 17a019e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Expand Up @@ -54,7 +54,7 @@ var (

func init() {
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.Flags().StringVar(&dnsServerIP, "dns-server-ip", "8.8.8.8", "IP address of the DNS server")
rootCmd.Flags().StringVar(&dnsServerIP, "dns-server-ip", "", "IP address of the DNS server")
rootCmd.Flags().StringVarP(&userSpecifiedQueryType, "query-type", "q", "", "Specific query type to filter on")
}

Expand Down
30 changes: 18 additions & 12 deletions pkg/core/core.go
Expand Up @@ -2,25 +2,17 @@ package core

import (
"fmt"
"os"
"runtime"
"time"

model "github.com/bschaatsbergen/dnsee/pkg/model"
"github.com/fatih/color"
"github.com/miekg/dns"
)

func GetDNSRecords(domainName string, recordType uint16, dnsServerIP string) ([]dns.RR, error) {
msg := dns.Msg{}
msg.SetQuestion(dns.Fqdn(domainName), recordType)

client := dns.Client{}
response, _, err := client.Exchange(&msg, dnsServerIP)
if err != nil {
return nil, err
}

return response.Answer, nil
}
const Windows = "windows"
const ResolverPath = "/etc/resolv.conf"

func GetQueryTypes() []model.QueryType {
return []model.QueryType{
Expand All @@ -42,6 +34,20 @@ func PrepareDNSQuery(domainName string, queryType uint16) dns.Msg {
}

func SendDNSQuery(client *dns.Client, msg dns.Msg, dnsServerIP string) (*dns.Msg, time.Duration, error) {
if dnsServerIP == "" {
goOS := runtime.GOOS
if goOS == Windows {
fmt.Println("Unable to retrieve DNS configuration on Windows systems. \nPlease specify ip explicitely with the --dns-server-ip flag.")
os.Exit(2)
}
conf, err := dns.ClientConfigFromFile(ResolverPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Println("Could not retrieve DNS server ip from system configuration. \nPlease specify ip explicitely with the --dns-server-ip flag.")
os.Exit(2)
}
dnsServerIP = conf.Servers[0]
}
return client.Exchange(&msg, dnsServerIP+":53")
}

Expand Down

0 comments on commit 17a019e

Please sign in to comment.