From 1130cfaff00fab66f948ffbb60adff4650628e31 Mon Sep 17 00:00:00 2001 From: Seraphim Strub Date: Tue, 16 Jul 2024 19:53:18 +0000 Subject: [PATCH] switch to miekg/dns for better results --- cmd/domaincheckbot/dns/domain.go | 78 +++++++++++++++++++++++--------- cmd/domaincheckbot/main.go | 15 +++--- cmd/domainchecklist/main.go | 5 +- go.mod | 4 ++ go.sum | 12 +++-- 5 files changed, 79 insertions(+), 35 deletions(-) diff --git a/cmd/domaincheckbot/dns/domain.go b/cmd/domaincheckbot/dns/domain.go index dbd73a7..3126793 100644 --- a/cmd/domaincheckbot/dns/domain.go +++ b/cmd/domaincheckbot/dns/domain.go @@ -1,8 +1,11 @@ package dns import ( + "fmt" + "github.com/miekg/dns" "net" - "sort" + "slices" + "strings" ) type Domain struct { @@ -10,30 +13,63 @@ type Domain struct { NS []string } -func CheckDomain(domain string) Domain { - nameservers, err := net.LookupNS(domain) +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) - 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{ - Name: domain, - NS: nsToArray(nameservers), + Name: domain + "." + tld, + 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{ + 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, + } + } + + // 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, + } + } + + // 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, - NS: []string{}, + Name: domain + "." + tld, + NS: nameservers, } } - -func nsToArray(nameservers []*net.NS) []string { - var nsArray []string - for _, nameserver := range nameservers { - nsArray = append(nsArray, nameserver.Host) - } - - sort.SliceStable(nsArray, func(i, j int) bool { - return nsArray[i] < nsArray[j] - }) - - return nsArray -} diff --git a/cmd/domaincheckbot/main.go b/cmd/domaincheckbot/main.go index fa0cbbd..a1e8f8c 100644 --- a/cmd/domaincheckbot/main.go +++ b/cmd/domaincheckbot/main.go @@ -67,7 +67,7 @@ func main() { case <-ticker.C: for d, tlds := range config.Domains { 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 } -func checkDomain(counter int, d string, query *db.Queries, client webhook.Client) { - domain := dns.CheckDomain(d) - retrievedDomainJson, dbErr := query.GetItem(context.TODO(), d) +func checkDomain(counter int, d, tld string, query *db.Queries, client webhook.Client) { + domain := dns.CheckDomain(d, tld) + fqdn := fmt.Sprintf("%s.%s", d, tld) + retrievedDomainJson, dbErr := query.GetItem(context.TODO(), fqdn) retrievedDomain := dns.Domain{} if err := json.Unmarshal(retrievedDomainJson.Data, &retrievedDomain); !errors.Is(dbErr, sql.ErrNoRows) && err != nil { logger.Error("failed unmarshalling deal", slog.Any("error", err)) } 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 } - logger.Info("domain changed", slog.String("domain", d)) + logger.Info("domain changed", slog.String("domain", fqdn)) counter += 1 if counter >= 2 { go sendWebhook(client, domain, retrievedDomain) @@ -113,7 +114,7 @@ func checkDomain(counter int, d string, query *db.Queries, client webhook.Client return } 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) { diff --git a/cmd/domainchecklist/main.go b/cmd/domainchecklist/main.go index 8fad752..8360f7a 100644 --- a/cmd/domainchecklist/main.go +++ b/cmd/domainchecklist/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "grow.rievo.dev/discordBots/cmd/domaincheckbot/config" "grow.rievo.dev/discordBots/cmd/domaincheckbot/dns" "log" @@ -12,11 +11,11 @@ func main() { for domain, tlds := range config.Domains { 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." { continue } - log.Println(dns.CheckDomain(fmt.Sprintf("%v.%v", domain, tld))) + log.Println(d) } } diff --git a/go.mod b/go.mod index 2f0faa5..27ce0d7 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22 require ( github.com/disgoorg/disgo v0.18.8 github.com/disgoorg/snowflake/v2 v2.0.1 + github.com/miekg/dns v1.1.61 golang.org/x/net v0.27.0 modernc.org/sqlite v1.30.2 ) @@ -20,7 +21,10 @@ require ( github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad // 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/tools v0.22.0 // indirect modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect modernc.org/libc v1.52.1 // indirect modernc.org/mathutil v1.6.0 // indirect diff --git a/go.sum b/go.sum index 09964bc..0d19c9c 100644 --- a/go.sum +++ b/go.sum @@ -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/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/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/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= 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= 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/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= -golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= +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/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.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= 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.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= +golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= +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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= modernc.org/cc/v4 v4.21.2 h1:dycHFB/jDc3IyacKipCNSDrjIC0Lm1hyoWOZTRR20Lk=