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-16 19:53:18 +00:00
|
|
|
func CheckDomain(domain, tld string) Domain {
|
|
|
|
nameservers := []string{}
|
|
|
|
// Create a new DNS message
|
|
|
|
m := new(dns.Msg)
|
|
|
|
// Set the question section of the message with the domain and query type
|
|
|
|
m.SetQuestion(dns.Fqdn(fmt.Sprintf("%v.%v", domain, tld)), dns.TypeNS)
|
2023-03-09 23:26:08 +00:00
|
|
|
|
2024-07-16 19:53:18 +00:00
|
|
|
// Create a DNS client
|
|
|
|
c := new(dns.Client)
|
|
|
|
// Define the .com TLD server
|
|
|
|
ns, err := net.LookupNS(tld)
|
|
|
|
if err != nil {
|
2024-07-12 13:51:36 +00:00
|
|
|
return Domain{
|
2024-07-16 19:53:18 +00:00
|
|
|
Name: domain + "." + tld,
|
|
|
|
NS: nameservers,
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-16 19:53:18 +00:00
|
|
|
var tldServer string
|
|
|
|
if len(ns) > 0 {
|
|
|
|
tldServer = strings.Trim(ns[0].Host, ".") + ":53"
|
|
|
|
} else {
|
|
|
|
//log.Fatalf("Could not resolve %v", tld)
|
|
|
|
return Domain{
|
|
|
|
Name: domain + "." + tld,
|
|
|
|
NS: nameservers,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the DNS query to the TLD server
|
|
|
|
r, _, err := c.Exchange(m, tldServer)
|
|
|
|
if err != nil {
|
|
|
|
//log.Fatalf("Failed to query the TLD server: %v", err)
|
|
|
|
return Domain{
|
|
|
|
Name: domain + "." + tld,
|
|
|
|
NS: nameservers,
|
|
|
|
}
|
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 {
|
|
|
|
//log.Fatalf("Failed to get a valid response: %v", r.Rcode)
|
|
|
|
return Domain{
|
|
|
|
Name: domain + "." + tld,
|
|
|
|
NS: nameservers,
|
|
|
|
}
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 19:53:18 +00:00
|
|
|
// Print the nameservers from the response
|
|
|
|
for _, ans := range r.Ns {
|
|
|
|
if ns, ok := ans.(*dns.NS); ok {
|
|
|
|
nameservers = append(nameservers, strings.ToLower(ns.Ns))
|
|
|
|
}
|
|
|
|
}
|
2023-03-09 23:26:08 +00:00
|
|
|
|
2024-07-16 19:53:18 +00:00
|
|
|
slices.Sort(nameservers)
|
|
|
|
return Domain{
|
|
|
|
Name: domain + "." + tld,
|
|
|
|
NS: nameservers,
|
|
|
|
}
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|