Skip to content

Commit

Permalink
nameservers configuration handled
Browse files Browse the repository at this point in the history
  • Loading branch information
5amu committed Jul 5, 2022
1 parent b8f75a5 commit b8cd3c7
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions cmd/dnshunter/runner.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,81 @@
package main

import (
"encoding/json"
"fmt"
"net"
"os"
"strings"

"github.com/5amu/dnshunter/internal"
"github.com/5amu/dnshunter/internal/common"
"github.com/5amu/dnshunter/internal/output"
"github.com/miekg/dns"
)

func run(outfile string, nsfile string, domain string) error {

c := new(dns.Client)

nameservers, err := getNameservers(domain, nsfile)
if err != nil {
return err
}
fmt.Println(nameservers)

var results []*output.CheckOutput
for _, check := range internal.CheckList {
check.Init(c)
check.Start("google.com", []string{})
fmt.Println(check.Results())
check.Start(domain, nameservers)

r := check.Results()
results = append(results, r)
fmt.Println(r)
}

if outfile != "" {
if data, err := json.Marshal(results); err != nil {
return err
} else {
if err := os.WriteFile(outfile, data, os.ModeAppend); err != nil {
return err
}
}
}

return nil
}

func getNameservers(domain string, nsfile string) ([]string, error) {
if data, err := os.ReadFile(nsfile); err != nil || nsfile != "" {
return nameserversFromDNS(domain)
} else {
return strings.Split(string(data), "\n"), nil
}
}

func nameserversFromDNS(domain string) (result []string, err error) {
c := new(dns.Client)

m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(domain), dns.TypeNS)
m.RecursionDesired = true

r, _, err := c.Exchange(m, net.JoinHostPort(common.DefaultNameserver, "53"))
if err != nil {
return nil, err
}

if r.Rcode != dns.RcodeSuccess {
return nil, fmt.Errorf("invalid answer from %v after NS query for %v", common.DefaultNameserver, domain)
}

for _, r := range r.Answer {
// google.com. 14332 IN NS ns3.google.com.
splitted := strings.Split(r.String(), "\t")
last := splitted[len(splitted)-1]

result = append(result, last)
}

return result, nil
}

0 comments on commit b8cd3c7

Please sign in to comment.