From ccbdf4e31df9858e8194b33c68e0cdcf3846491f Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 2 Nov 2017 16:45:50 +0800 Subject: [PATCH] add notify model, add send notify method --- settings.go | 10 ++++++++++ utils.go | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/settings.go b/settings.go index 6ac9904..0d35613 100644 --- a/settings.go +++ b/settings.go @@ -12,6 +12,15 @@ type Domain struct { SubDomains []string `json:"sub_domains"` } +type Notify struct { + Enabled bool `json:"enabled"` + SMTPServer string `json:"smtp_server"` + SendTo string `json:"send_to"` + Port int `json:"port"` + Account string `json:"account"` + Password string `json:"password"` +} + // Settings struct type Settings struct { Provider string `json:"provider"` @@ -22,6 +31,7 @@ type Settings struct { IPUrl string `json:"ip_url"` LogPath string `json:"log_path"` Socks5Proxy string `json:"socks5_proxy"` + Notify Notify `json:"notify"` } // LoadSettings -- Load settings from config file diff --git a/utils.go b/utils.go index 5ec76a3..4e767e7 100644 --- a/utils.go +++ b/utils.go @@ -9,6 +9,7 @@ import ( "strings" "golang.org/x/net/proxy" + "gopkg.in/gomail.v2" ) var ( @@ -102,3 +103,21 @@ func LoadCurrentIP() string { return strings.Replace(string(content), "\n", "", -1) } + +// SendNotify sends mail notify if IP is changed +func SendNotify(configuration *Settings, currentIP string) error { + m := gomail.NewMessage() + + m.SetHeader("From", configuration.Notify.Account) + m.SetHeader("To", configuration.Notify.SendTo) + m.SetHeader("Subject", "GoDNS Notification") + m.SetBody("text/html", "") + + d := gomail.NewPlainDialer(configuration.Notify.SMTPServer, configuration.Notify.Port, configuration.Notify.Account, configuration.Notify.Password) + + // Send the email config by sendlist . + if err := d.DialAndSend(m); err != nil { + return err + } + return nil +}