2023-03-09 23:26:08 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2024-07-16 19:53:18 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/miekg/dns"
|
2023-03-09 23:26:08 +00:00
|
|
|
"net"
|
2024-07-16 19:53:18 +00:00
|
|
|
"slices"
|
|
|
|
"strings"
|
2023-03-09 23:26:08 +00:00
|
|
|
)
|
|
|
|
|
2024-07-12 13:51:36 +00:00
|
|
|
type Domain struct {
|
|
|
|
Name string
|
|
|
|
NS []string
|
|
|
|
}
|
|
|
|
|
2024-07-17 20:30:46 +00:00
|
|
|
func CheckDomain(d, tld string) Domain {
|
2024-07-16 19:53:18 +00:00
|
|
|
nameservers := []string{}
|
2024-07-17 20:30:46 +00:00
|
|
|
fqdn := fmt.Sprintf("%v.%v", d, tld)
|
|
|
|
|
|
|
|
tldServers, err := getNameserverForTLD(tld)
|
|
|
|
if err != nil {
|
|
|
|
return Domain{fqdn, nameservers}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tldServer := range tldServers {
|
|
|
|
nameservers, err = queryTLDNamserver(fqdn, tldServer)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-03-09 23:26:08 +00:00
|
|
|
|
2024-07-16 19:53:18 +00:00
|
|
|
if err != nil {
|
2024-07-12 13:51:36 +00:00
|
|
|
return Domain{
|
2024-07-17 20:30:46 +00:00
|
|
|
Name: fqdn,
|
2024-07-16 19:53:18 +00:00
|
|
|
NS: nameservers,
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-17 20:30:46 +00:00
|
|
|
|
|
|
|
slices.Sort(nameservers)
|
|
|
|
return Domain{
|
|
|
|
Name: fqdn,
|
|
|
|
NS: nameservers,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNameserverForTLD(tld string) ([]string, error) {
|
|
|
|
ns, err := net.LookupNS(tld)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tldServer []string
|
2024-07-16 19:53:18 +00:00
|
|
|
if len(ns) > 0 {
|
2024-07-17 20:30:46 +00:00
|
|
|
for _, n := range ns {
|
|
|
|
tldServer = append(tldServer, strings.Trim(n.Host, ".")+":53")
|
2024-07-16 19:53:18 +00:00
|
|
|
}
|
2024-07-17 20:30:46 +00:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("no nameserver found for TLD %v", tld)
|
2024-07-16 19:53:18 +00:00
|
|
|
}
|
2024-07-17 20:30:46 +00:00
|
|
|
return tldServer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryTLDNamserver(fqdn, tldServer string) ([]string, error) {
|
|
|
|
// Create a new DNS message
|
|
|
|
m := new(dns.Msg)
|
|
|
|
// Set the question section of the message with the fqdn and query type
|
|
|
|
m.SetQuestion(dns.Fqdn(fqdn), dns.TypeNS)
|
|
|
|
|
|
|
|
// Create a DNS client
|
|
|
|
c := new(dns.Client)
|
2024-07-16 19:53:18 +00:00
|
|
|
|
|
|
|
// Send the DNS query to the TLD server
|
|
|
|
r, _, err := c.Exchange(m, tldServer)
|
|
|
|
if err != nil {
|
2024-07-17 20:30:46 +00:00
|
|
|
return nil, fmt.Errorf("dns query error: %v", err)
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 19:53:18 +00:00
|
|
|
// Check for response errors
|
|
|
|
if r.Rcode != dns.RcodeSuccess {
|
2024-07-17 20:30:46 +00:00
|
|
|
return nil, fmt.Errorf("failed to get a valid response for %v: %v", fqdn, r.Rcode)
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 19:53:18 +00:00
|
|
|
// Print the nameservers from the response
|
2024-07-17 20:30:46 +00:00
|
|
|
nameservers := []string{}
|
2024-07-16 19:53:18 +00:00
|
|
|
for _, ans := range r.Ns {
|
|
|
|
if ns, ok := ans.(*dns.NS); ok {
|
|
|
|
nameservers = append(nameservers, strings.ToLower(ns.Ns))
|
|
|
|
}
|
|
|
|
}
|
2024-07-17 20:30:46 +00:00
|
|
|
return nameservers, nil
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|