mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
refactor send notify logic
This commit is contained in:
parent
4c4f34c55b
commit
7a65d4e0ea
@ -13,5 +13,13 @@
|
||||
],
|
||||
"ip_url": "http://members.3322.org/dyndns/getip",
|
||||
"log_path": "./godns.log",
|
||||
"socks5_proxy": ""
|
||||
"socks5_proxy": "",
|
||||
"notify": {
|
||||
"enabled": false,
|
||||
"smtp_server": "",
|
||||
"smtp_username": "",
|
||||
"smtp_password": "",
|
||||
"smtp_port": 25,
|
||||
"send_to": "",
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,8 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<-
|
||||
|
||||
// Send mail notification if notify is enabled
|
||||
if handler.Configuration.Notify.Enabled {
|
||||
godns.SendNotify(handler.Configuration, currentIP)
|
||||
log.Print("Sending notification to:", handler.Configuration.Notify.SendTo)
|
||||
godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), currentIP)
|
||||
}
|
||||
|
||||
} else {
|
||||
|
@ -61,7 +61,8 @@ func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godn
|
||||
|
||||
// Send mail notification if notify is enabled
|
||||
if handler.Configuration.Notify.Enabled {
|
||||
godns.SendNotify(handler.Configuration, currentIP)
|
||||
log.Print("Sending notification to:", handler.Configuration.Notify.SendTo)
|
||||
godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), currentIP)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
settings.go
12
settings.go
@ -13,12 +13,12 @@ type Domain struct {
|
||||
}
|
||||
|
||||
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"`
|
||||
Enabled bool `json:"enabled"`
|
||||
SMTPServer string `json:"smtp_server"`
|
||||
SMTPUsername string `json:"smtp_username"`
|
||||
SMTPPassword string `json:"smtp_password"`
|
||||
SMTPPort int `json:"smtp_port"`
|
||||
SendTo string `json:"send_to"`
|
||||
}
|
||||
|
||||
// Settings struct
|
||||
|
105
template.go
Normal file
105
template.go
Normal file
@ -0,0 +1,105 @@
|
||||
package godns
|
||||
|
||||
var template = `
|
||||
<html>
|
||||
<body>
|
||||
<div role="section">
|
||||
<div style="background-color: #281557;">
|
||||
<div class="layout one-col" style="Margin: 0 auto;max-width: 600px;min-width: 320px; width: 320px;width: calc(28000% - 167400px);overflow-wrap: break-word;word-wrap: break-word;word-break: break-word;">
|
||||
<div class="layout__inner" style="border-collapse: collapse;display: table;width: 100%;">
|
||||
<!--[if (mso)|(IE)]><table width="100%" cellpadding="0" cellspacing="0" role="presentation"><tr class="layout-full-width" style="background-color: #281557;"><td class="layout__edges"> </td><td style="width: 600px" class="w560"><![endif]-->
|
||||
<div class="column" style="max-width: 600px;min-width: 320px; width: 320px;width: calc(28000% - 167400px);text-align: left;color: #8e959c;font-size: 14px;line-height: 21px;font-family: sans-serif;">
|
||||
|
||||
<div style="Margin-left: 20px;Margin-right: 20px;">
|
||||
<div style="mso-line-height-rule: exactly;line-height: 10px;font-size: 1px;"> </div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<!--[if (mso)|(IE)]></td><td class="layout__edges"> </td></tr></table><![endif]-->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="background-color: #281557;">
|
||||
<div class="layout one-col" style="Margin: 0 auto;max-width: 600px;min-width: 320px; width: 320px;width: calc(28000% - 167400px);overflow-wrap: break-word;word-wrap: break-word;word-break: break-word;">
|
||||
<div class="layout__inner" style="border-collapse: collapse;display: table;width: 100%;">
|
||||
<!--[if (mso)|(IE)]><table width="100%" cellpadding="0" cellspacing="0" role="presentation"><tr class="layout-full-width" style="background-color: #281557;"><td class="layout__edges"> </td><td style="width: 600px" class="w560"><![endif]-->
|
||||
<div class="column" style="max-width: 600px;min-width: 320px; width: 320px;width: calc(28000% - 167400px);text-align: left;color: #8e959c;font-size: 14px;line-height: 21px;font-family: sans-serif;">
|
||||
|
||||
<div style="Margin-left: 20px;Margin-right: 20px;">
|
||||
<div style="mso-line-height-rule: exactly;line-height: 50px;font-size: 1px;"> </div>
|
||||
</div>
|
||||
|
||||
<div style="Margin-left: 20px;Margin-right: 20px;">
|
||||
<div style="mso-line-height-rule: exactly;mso-text-raise: 4px;">
|
||||
<h1 class="size-28" style="Margin-top: 0;Margin-bottom: 0;font-style: normal;font-weight: normal;color: #000;font-size: 24px;line-height: 32px;font-family: avenir,sans-serif;text-align: center;"
|
||||
lang="x-size-28">
|
||||
<span class="font-avenir">
|
||||
<span style="color:#ffffff">Your IP address is changed to</span>
|
||||
</span>
|
||||
</h1>
|
||||
<h1 class="size-48" style="Margin-top: 20px;Margin-bottom: 0;font-style: normal;font-weight: normal;color: #000;font-size: 36px;line-height: 43px;font-family: avenir,sans-serif;text-align: center;"
|
||||
lang="x-size-48">
|
||||
<span class="font-avenir">
|
||||
<strong>
|
||||
<span style="color:#ffffff">%s</span>
|
||||
</strong>
|
||||
</span>
|
||||
</h1>
|
||||
<h2 class="size-28" style="Margin-top: 20px;Margin-bottom: 16px;font-style: normal;font-weight: normal;color: #e31212;font-size: 24px;line-height: 32px;font-family: Avenir,sans-serif;text-align: center;"
|
||||
lang="x-size-28">
|
||||
<font color="#ffffff">
|
||||
<strong>Domain: %s is updated</strong>
|
||||
</font>
|
||||
</h2>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="Margin-left: 20px;Margin-right: 20px;">
|
||||
<div style="mso-line-height-rule: exactly;line-height: 15px;font-size: 1px;"> </div>
|
||||
</div>
|
||||
|
||||
<div style="Margin-left: 20px;Margin-right: 20px;">
|
||||
<div style="mso-line-height-rule: exactly;line-height: 35px;font-size: 1px;"> </div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<!--[if (mso)|(IE)]></td><td class="layout__edges"> </td></tr></table><![endif]-->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="mso-line-height-rule: exactly;line-height: 20px;font-size: 20px;"> </div>
|
||||
|
||||
|
||||
<div style="mso-line-height-rule: exactly;" role="contentinfo">
|
||||
<div class="layout email-footer" style="Margin: 0 auto;max-width: 600px;min-width: 320px; width: 320px;width: calc(28000% - 167400px);overflow-wrap: break-word;word-wrap: break-word;word-break: break-word;">
|
||||
<div class="layout__inner" style="border-collapse: collapse;display: table;width: 100%;">
|
||||
<!--[if (mso)|(IE)]><table align="center" cellpadding="0" cellspacing="0" role="presentation"><tr class="layout-email-footer"><td style="width: 400px;" valign="top" class="w360"><![endif]-->
|
||||
<div class="column wide" style="text-align: left;font-size: 12px;line-height: 19px;color: #adb3b9;font-family: sans-serif;Float: left;max-width: 400px;min-width: 320px; width: 320px;width: calc(8000% - 47600px);">
|
||||
<div style="Margin-left: 20px;Margin-right: 20px;Margin-top: 10px;Margin-bottom: 10px;">
|
||||
|
||||
<div style="font-size: 12px;line-height: 19px;">
|
||||
|
||||
</div>
|
||||
<div style="font-size: 12px;line-height: 19px;Margin-top: 18px;">
|
||||
|
||||
</div>
|
||||
<!--[if mso]> <![endif]-->
|
||||
</div>
|
||||
</div>
|
||||
<!--[if (mso)|(IE)]></td><td style="width: 200px;" valign="top" class="w160"><![endif]-->
|
||||
<div class="column narrow" style="text-align: left;font-size: 12px;line-height: 19px;color: #adb3b9;font-family: sans-serif;Float: left;max-width: 320px;min-width: 200px; width: 320px;width: calc(72200px - 12000%);">
|
||||
<div style="Margin-left: 20px;Margin-right: 20px;Margin-top: 10px;Margin-bottom: 10px;">
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<!--[if (mso)|(IE)]></td></tr></table><![endif]-->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div style="mso-line-height-rule: exactly;line-height: 40px;font-size: 40px;"> </div>
|
||||
</body>
|
||||
</div>
|
||||
</html>
|
||||
`
|
12
utils.go
12
utils.go
@ -2,6 +2,7 @@ package godns
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
@ -105,18 +106,21 @@ func LoadCurrentIP() string {
|
||||
}
|
||||
|
||||
// SendNotify sends mail notify if IP is changed
|
||||
func SendNotify(configuration *Settings, currentIP string) error {
|
||||
func SendNotify(configuration *Settings, domain string, currentIP string) error {
|
||||
m := gomail.NewMessage()
|
||||
|
||||
m.SetHeader("From", configuration.Notify.Account)
|
||||
m.SetHeader("From", configuration.Notify.SMTPUsername)
|
||||
m.SetHeader("To", configuration.Notify.SendTo)
|
||||
m.SetHeader("Subject", "GoDNS Notification")
|
||||
m.SetBody("text/html", "")
|
||||
log.Println("currentIP:", currentIP)
|
||||
log.Println("domain:", domain)
|
||||
m.SetBody("text/html", fmt.Sprintf(template, currentIP, domain))
|
||||
|
||||
d := gomail.NewPlainDialer(configuration.Notify.SMTPServer, configuration.Notify.Port, configuration.Notify.Account, configuration.Notify.Password)
|
||||
d := gomail.NewPlainDialer(configuration.Notify.SMTPServer, configuration.Notify.SMTPPort, configuration.Notify.SMTPUsername, configuration.Notify.SMTPPassword)
|
||||
|
||||
// Send the email config by sendlist .
|
||||
if err := d.DialAndSend(m); err != nil {
|
||||
log.Println("Send email notification with error:", err.Error())
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user