2023-03-09 23:26:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/dgraph-io/badger/v4"
|
|
|
|
"github.com/disgoorg/disgo"
|
|
|
|
"github.com/disgoorg/disgo/discord"
|
|
|
|
"github.com/disgoorg/disgo/rest"
|
|
|
|
"github.com/disgoorg/disgo/webhook"
|
|
|
|
"github.com/disgoorg/snowflake/v2"
|
|
|
|
"grow.rievo.dev/discordBots/cmd/domaincheckbot/config"
|
|
|
|
"grow.rievo.dev/discordBots/cmd/domaincheckbot/dns"
|
|
|
|
"grow.rievo.dev/discordBots/cmd/domaincheckbot/repository"
|
2023-11-05 16:03:52 +00:00
|
|
|
"log/slog"
|
2023-03-09 23:26:08 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"reflect"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
webhookID = snowflake.GetEnv("webhook_id")
|
|
|
|
webhookToken = os.Getenv("webhook_token")
|
|
|
|
)
|
|
|
|
|
2023-11-05 16:03:52 +00:00
|
|
|
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
|
|
|
|
2023-03-09 23:26:08 +00:00
|
|
|
// TODO: clear db from domains removed from json
|
|
|
|
|
2023-04-22 23:23:52 +00:00
|
|
|
var release string
|
|
|
|
|
2023-03-09 23:26:08 +00:00
|
|
|
func main() {
|
2023-04-22 23:23:52 +00:00
|
|
|
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Info("starting domainCheck...", slog.String("disgo version", disgo.Version))
|
2023-03-09 23:26:08 +00:00
|
|
|
|
|
|
|
client := webhook.New(webhookID, webhookToken)
|
|
|
|
defer client.Close(context.TODO())
|
|
|
|
|
|
|
|
repo := repository.InitDb()
|
|
|
|
defer repo.Close()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(10 * time.Minute)
|
|
|
|
tickerGC := time.NewTicker(15 * time.Minute)
|
|
|
|
quit := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
for _, d := range config.Domains {
|
2023-04-16 09:56:25 +00:00
|
|
|
go checkDomain(0, d, repo, client)
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-tickerGC.C:
|
|
|
|
err := repo.RunGC()
|
|
|
|
if err != nil && !errors.Is(err, badger.ErrNoRewrite) {
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Error("GC failed", slog.Any("error", err))
|
2023-03-09 23:26:08 +00:00
|
|
|
} else {
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Debug("GC successful")
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-quit:
|
|
|
|
ticker.Stop()
|
|
|
|
tickerGC.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Info("domaincheckbot is now running. Press CTRL-C to exit.", slog.String("version", release))
|
2023-04-22 23:23:52 +00:00
|
|
|
|
2023-03-09 23:26:08 +00:00
|
|
|
s := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
|
|
<-s
|
|
|
|
}
|
|
|
|
|
2023-04-16 09:56:25 +00:00
|
|
|
func checkDomain(counter int, d string, repo *repository.DomainRepository, client webhook.Client) {
|
|
|
|
domain := dns.CheckDomain(d)
|
|
|
|
retrievedDomain, _ := repo.GetValue(d)
|
|
|
|
if reflect.DeepEqual(domain, retrievedDomain) {
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Debug("domain did not change", slog.String("domain", d))
|
2023-04-16 09:56:25 +00:00
|
|
|
return
|
|
|
|
}
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Info("domain changed changed", slog.String("domain", d))
|
2023-04-16 09:56:25 +00:00
|
|
|
counter += 1
|
|
|
|
if counter >= 2 {
|
|
|
|
go sendWebhook(client, domain, retrievedDomain)
|
|
|
|
repo.SetValue(domain)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
checkDomain(counter, d, repo, client)
|
|
|
|
}
|
|
|
|
|
2023-03-09 23:26:08 +00:00
|
|
|
func sendWebhook(client webhook.Client, domain repository.Domain, oldDomain repository.Domain) {
|
|
|
|
var status string
|
|
|
|
|
|
|
|
status = fmt.Sprintf("```md\n# %v", domain.Name)
|
|
|
|
status = fmt.Sprintf("%v\n - %v", status, oldDomain.NS)
|
|
|
|
status = fmt.Sprintf("%v\n + %v", status, domain.NS)
|
|
|
|
status = fmt.Sprintf("%v```\n", status)
|
|
|
|
|
|
|
|
if _, err := client.CreateMessage(discord.NewWebhookMessageCreateBuilder().
|
|
|
|
SetContent(status).Build(),
|
|
|
|
rest.WithDelay(2*time.Second),
|
|
|
|
); err != nil {
|
2023-11-05 16:03:52 +00:00
|
|
|
logger.Error("sending message failed", slog.Any("error", err))
|
2023-03-09 23:26:08 +00:00
|
|
|
}
|
|
|
|
}
|