From a31a89c1282d908d756afba3cf169c7110bc019e Mon Sep 17 00:00:00 2001 From: Laurent Le Houerou Date: Sat, 6 Apr 2019 15:56:17 +0400 Subject: [PATCH] Ajout d'une notification par email en cas de changement d'ip ou d'erreur --- main.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index afcd0eb..88d396e 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http" "io/ioutil" "net/http" + "net/smtp" "os" "os/signal" "path" @@ -103,7 +104,7 @@ func start(c *cli.Context) error { defer func() { tryLockSem <- v }() if err := update(c); err != nil { log.Println(err) - + SendStatusMail(fmt.Sprintf("Error during update process : %s", err)) } default: log.Debug("Skipped another update already running.") @@ -150,6 +151,12 @@ func update(c *cli.Context) error { configuration := Configuration{} err := decoder.Decode(&configuration) + Info("Repository : %s", configuration.RepositoryUrl) + Info("Name : %s", configuration.Username) + Info("Email : %s", configuration.Email) + Info("GitLogin : %s", configuration.GitLogin) + + err = os.RemoveAll(path.Join(configPath, "data")) if err != nil { return err @@ -219,9 +226,44 @@ func update(c *cli.Context) error { if err != nil { return err } + + SendStatusMail(fmt.Sprintf(`Home IP has changed : %s -> %s +Dns updated successfully`, currentIp, newIp)) + return nil } +func SendStatusMail(messageText string) { + // user we are authorizing as + from := "laurent@lehouerou.net" + + // use we are sending email to + to := "laurent@lehouerou.net" + + // server we are authorized to send email through + host := "smtp.fastmail.com" + + // Create the authentication for the SendMail() + // using PlainText, but other authentication methods are encouraged + auth := smtp.PlainAuth("", from, "c9bd8fb8l4bhs2f8", host) + + // NOTE: Using the backtick here ` works like a heredoc, which is why all the + // rest of the lines are forced to the beginning of the line, otherwise the + // formatting is wrong for the RFC 822 style + message := fmt.Sprintf(`To: "Laurent Le Houerou" +From: "Laurent Le Houerou" +Subject: DnsUpdater Status + +%s +`,messageText) + + if err := smtp.SendMail(host+":587", auth, from, []string{to}, []byte(message)); err != nil { + fmt.Println("Error SendMail: ", err) + } else { + fmt.Println("Email Sent!") + } +} + func GetCurrentIp() (string, error) { b, err := ioutil.ReadFile(path.Join(configPath, "data/templates/home_a.lua")) if err != nil {