switch to miekg/dns for better results

This commit is contained in:
Seraphim Strub 2024-07-16 19:53:18 +00:00
parent a76b11b2b2
commit 1130cfaff0
5 changed files with 79 additions and 35 deletions

View file

@ -1,8 +1,11 @@
package dns package dns
import ( import (
"fmt"
"github.com/miekg/dns"
"net" "net"
"sort" "slices"
"strings"
) )
type Domain struct { type Domain struct {
@ -10,30 +13,63 @@ type Domain struct {
NS []string NS []string
} }
func CheckDomain(domain string) Domain { func CheckDomain(domain, tld string) Domain {
nameservers, err := net.LookupNS(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)
if len(nameservers) > 0 && err == nil { // Create a DNS client
c := new(dns.Client)
// Define the .com TLD server
ns, err := net.LookupNS(tld)
if err != nil {
return Domain{ return Domain{
Name: domain, Name: domain + "." + tld,
NS: nsToArray(nameservers), NS: nameservers,
} }
} }
var tldServer string
if len(ns) > 0 {
tldServer = strings.Trim(ns[0].Host, ".") + ":53"
} else {
//log.Fatalf("Could not resolve %v", tld)
return Domain{ return Domain{
Name: domain, Name: domain + "." + tld,
NS: []string{}, NS: nameservers,
} }
} }
func nsToArray(nameservers []*net.NS) []string { // Send the DNS query to the TLD server
var nsArray []string r, _, err := c.Exchange(m, tldServer)
for _, nameserver := range nameservers { if err != nil {
nsArray = append(nsArray, nameserver.Host) //log.Fatalf("Failed to query the TLD server: %v", err)
return Domain{
Name: domain + "." + tld,
NS: nameservers,
}
} }
sort.SliceStable(nsArray, func(i, j int) bool { // Check for response errors
return nsArray[i] < nsArray[j] if r.Rcode != dns.RcodeSuccess {
}) //log.Fatalf("Failed to get a valid response: %v", r.Rcode)
return Domain{
return nsArray Name: domain + "." + tld,
NS: nameservers,
}
}
// 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))
}
}
slices.Sort(nameservers)
return Domain{
Name: domain + "." + tld,
NS: nameservers,
}
} }

View file

@ -67,7 +67,7 @@ func main() {
case <-ticker.C: case <-ticker.C:
for d, tlds := range config.Domains { for d, tlds := range config.Domains {
for _, tld := range tlds { for _, tld := range tlds {
go checkDomain(0, fmt.Sprintf("%v.%v", d, tld), query, client) go checkDomain(0, d, tld, query, client)
} }
} }
@ -85,18 +85,19 @@ func main() {
<-s <-s
} }
func checkDomain(counter int, d string, query *db.Queries, client webhook.Client) { func checkDomain(counter int, d, tld string, query *db.Queries, client webhook.Client) {
domain := dns.CheckDomain(d) domain := dns.CheckDomain(d, tld)
retrievedDomainJson, dbErr := query.GetItem(context.TODO(), d) fqdn := fmt.Sprintf("%s.%s", d, tld)
retrievedDomainJson, dbErr := query.GetItem(context.TODO(), fqdn)
retrievedDomain := dns.Domain{} retrievedDomain := dns.Domain{}
if err := json.Unmarshal(retrievedDomainJson.Data, &retrievedDomain); !errors.Is(dbErr, sql.ErrNoRows) && err != nil { if err := json.Unmarshal(retrievedDomainJson.Data, &retrievedDomain); !errors.Is(dbErr, sql.ErrNoRows) && err != nil {
logger.Error("failed unmarshalling deal", slog.Any("error", err)) logger.Error("failed unmarshalling deal", slog.Any("error", err))
} }
if reflect.DeepEqual(domain, retrievedDomain) { if reflect.DeepEqual(domain, retrievedDomain) {
logger.Debug("domain did not change", slog.String("domain", d)) logger.Debug("domain did not change", slog.String("domain", fqdn))
return return
} }
logger.Info("domain changed", slog.String("domain", d)) logger.Info("domain changed", slog.String("domain", fqdn))
counter += 1 counter += 1
if counter >= 2 { if counter >= 2 {
go sendWebhook(client, domain, retrievedDomain) go sendWebhook(client, domain, retrievedDomain)
@ -113,7 +114,7 @@ func checkDomain(counter int, d string, query *db.Queries, client webhook.Client
return return
} }
time.Sleep(1 * time.Minute) time.Sleep(1 * time.Minute)
checkDomain(counter, d, query, client) checkDomain(counter, d, tld, query, client)
} }
func sendWebhook(client webhook.Client, domain dns.Domain, oldDomain dns.Domain) { func sendWebhook(client webhook.Client, domain dns.Domain, oldDomain dns.Domain) {

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"grow.rievo.dev/discordBots/cmd/domaincheckbot/config" "grow.rievo.dev/discordBots/cmd/domaincheckbot/config"
"grow.rievo.dev/discordBots/cmd/domaincheckbot/dns" "grow.rievo.dev/discordBots/cmd/domaincheckbot/dns"
"log" "log"
@ -12,11 +11,11 @@ func main() {
for domain, tlds := range config.Domains { for domain, tlds := range config.Domains {
for _, tld := range tlds { for _, tld := range tlds {
d := dns.CheckDomain(fmt.Sprintf("%v.%v", domain, tld)) d := dns.CheckDomain(domain, tld)
if len(d.NS) > 0 && d.NS[0] == "ns.rievo.ch." { if len(d.NS) > 0 && d.NS[0] == "ns.rievo.ch." {
continue continue
} }
log.Println(dns.CheckDomain(fmt.Sprintf("%v.%v", domain, tld))) log.Println(d)
} }
} }

4
go.mod
View file

@ -5,6 +5,7 @@ go 1.22
require ( require (
github.com/disgoorg/disgo v0.18.8 github.com/disgoorg/disgo v0.18.8
github.com/disgoorg/snowflake/v2 v2.0.1 github.com/disgoorg/snowflake/v2 v2.0.1
github.com/miekg/dns v1.1.61
golang.org/x/net v0.27.0 golang.org/x/net v0.27.0
modernc.org/sqlite v1.30.2 modernc.org/sqlite v1.30.2
) )
@ -20,7 +21,10 @@ require (
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad // indirect github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad // indirect
golang.org/x/crypto v0.25.0 // indirect golang.org/x/crypto v0.25.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect golang.org/x/sys v0.22.0 // indirect
golang.org/x/tools v0.22.0 // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/libc v1.52.1 // indirect modernc.org/libc v1.52.1 // indirect
modernc.org/mathutil v1.6.0 // indirect modernc.org/mathutil v1.6.0 // indirect

12
go.sum
View file

@ -18,6 +18,8 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/miekg/dns v1.1.61 h1:nLxbwF3XxhwVSm8g9Dghm9MHPaUZuqhPiGL+675ZmEs=
github.com/miekg/dns v1.1.61/go.mod h1:mnAarhS3nWaW+NVP2wTkYVIZyHNJ098SJZUki3eykwQ=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -30,15 +32,17 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/cc/v4 v4.21.2 h1:dycHFB/jDc3IyacKipCNSDrjIC0Lm1hyoWOZTRR20Lk= modernc.org/cc/v4 v4.21.2 h1:dycHFB/jDc3IyacKipCNSDrjIC0Lm1hyoWOZTRR20Lk=