package main import ( "crypto/sha256" "io" "log" "net/http" "os" "strconv" "strings" "time" "github.com/joho/godotenv" "gopkg.in/gomail.v2" ) /* Get a website HTML. */ func getWebsite(url string) []byte { response, err := http.Get(url) if err != nil { log.Fatal("Error: ", err) } defer response.Body.Close() body, err := io.ReadAll(response.Body) if err != nil { log.Fatal("Error reading response body: ", err) } return body } /* Convert data to sha256 hash. */ func stringToHash(data []byte) string { hash := sha256.Sum256(data) return string(hash[:]) } /* Send an email. */ func sendEmail(identity string, username string, password string, host string, recipient []string, port int) { message := gomail.NewMessage() message.SetHeader("From", identity) message.SetHeader("To", strings.Join(recipient, ", ")) message.SetHeader("Subject", "Changes on website") message.SetBody("text/plain", "A changed has been found on the website.") dialer := gomail.NewDialer(host, port, username, password) if err := dialer.DialAndSend(message); err != nil { log.Fatal(err) } } /* Environnement variable checker */ func checkEnvVariables(keys []string) { for _, key := range keys { _, i := os.LookupEnv(key) if !i { log.Fatal(key, " doesn't exist.") } } } /* Take a map of string * string and returns a list of its values */ func valuesOfMapStringToListString(from map[string]string) []string { res := make([]string, 0, len(from)) for _, value := range from { res = append(res, value) } return res } func main() { // Loading environment variables godotenv.Load() keys := map[string]string{ "url": "WEBSITE", "smtp_host": "SMTP_HOST", "smtp_from": "SMTP_FROM", "smtp_user": "SMTP_USERNAME", "smtp_pass": "SMTP_PASSWORD", "smtp_port": "SMTP_PORT", "recipient_adress": "RECIPIENT_ADRESS", } checkEnvVariables(valuesOfMapStringToListString(keys)) url := os.Getenv(keys["url"]) smtp_from := os.Getenv(keys["smtp_from"]) smtp_user := os.Getenv(keys["smtp_user"]) smtp_pass := os.Getenv(keys["smtp_pass"]) smtp_host := os.Getenv(keys["smtp_host"]) recipient_adress := os.Getenv(keys["recipient_adress"]) smtp_port, err := strconv.Atoi(os.Getenv(keys["smtp_port"])) if err != nil { log.Fatal("SMTP port is invalid.") } last_hash := "" sleepDuration := 24 log.Default().Print("Checking: ", url) log.Default().Print("Running each ", sleepDuration, "h, starting now.") for { body := getWebsite(url) hash := stringToHash(body) if last_hash != hash && len(last_hash) > 0 { sendEmail( smtp_from, smtp_user, smtp_pass, smtp_host, strings.Split(recipient_adress, ","), smtp_port, ) } last_hash = hash /* log.Default().Print("Next loop in ", sleepDuration, "h.") */ time.Sleep(time.Duration(sleepDuration) * time.Hour) } }