mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-05-06 13:48:52 +00:00
feat: implement VPS/Domain expiry notifications and native SMTP support
This commit is contained in:
+2
-1
@@ -42,7 +42,8 @@ type ConfigDashboard struct {
|
||||
UserTemplate string `koanf:"user_template" json:"user_template,omitempty"`
|
||||
AdminTemplate string `koanf:"admin_template" json:"admin_template,omitempty"`
|
||||
|
||||
EnablePlainIPInNotification bool `koanf:"enable_plain_ip_in_notification" json:"enable_plain_ip_in_notification,omitempty"` // 通知信息IP不打码
|
||||
EnablePlainIPInNotification bool `koanf:"enable_plain_ip_in_notification" json:"enable_plain_ip_in_notification,omitempty"` // 通知信息IP不打码
|
||||
ExpiryNotificationGroupID uint64 `koanf:"expiry_notification_group_id" json:"expiry_notification_group_id"`
|
||||
|
||||
// IP变更提醒
|
||||
EnableIPChangeNotification bool `koanf:"enable_ip_change_notification" json:"enable_ip_change_notification,omitempty"`
|
||||
|
||||
+50
-4
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/smtp"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -31,14 +32,21 @@ type NotificationServerBundle struct {
|
||||
Loc *time.Location
|
||||
}
|
||||
|
||||
const (
|
||||
_ = iota
|
||||
NotificationTypeWebhook
|
||||
NotificationTypeSMTP
|
||||
)
|
||||
|
||||
type Notification struct {
|
||||
Common
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
Type uint8 `json:"type"` // 0: Webhook, 1: SMTP
|
||||
URL string `json:"url"` // SMTP: host:port, Webhook: url
|
||||
RequestMethod uint8 `json:"request_method"`
|
||||
RequestType uint8 `json:"request_type"`
|
||||
RequestHeader string `json:"request_header" gorm:"type:longtext"`
|
||||
RequestBody string `json:"request_body" gorm:"type:longtext"`
|
||||
RequestHeader string `json:"request_header" gorm:"type:longtext"` // SMTP: user, Webhook: header
|
||||
RequestBody string `json:"request_body" gorm:"type:longtext"` // SMTP: pass, Webhook: body
|
||||
VerifyTLS *bool `json:"verify_tls,omitempty"`
|
||||
FormatMetricUnits *bool `json:"format_metric_units,omitempty"`
|
||||
}
|
||||
@@ -111,8 +119,12 @@ func (n *Notification) setRequestHeader(req *http.Request) error {
|
||||
}
|
||||
|
||||
func (ns *NotificationServerBundle) Send(message string) error {
|
||||
var client *http.Client
|
||||
n := ns.Notification
|
||||
if n.Type == NotificationTypeSMTP {
|
||||
return ns.sendSMTP(message)
|
||||
}
|
||||
|
||||
var client *http.Client
|
||||
if n.VerifyTLS != nil && *n.VerifyTLS {
|
||||
client = utils.HttpClient
|
||||
} else {
|
||||
@@ -158,6 +170,40 @@ func (ns *NotificationServerBundle) Send(message string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ns *NotificationServerBundle) sendSMTP(message string) error {
|
||||
n := ns.Notification
|
||||
// RequestHeader: user:pass
|
||||
// RequestBody: to_email
|
||||
// URL: host:port
|
||||
authInfo := strings.SplitN(n.RequestHeader, ":", 2)
|
||||
if len(authInfo) < 2 {
|
||||
return errors.New("SMTP认证信息格式错误 (user:pass)")
|
||||
}
|
||||
user := authInfo[0]
|
||||
pass := authInfo[1]
|
||||
to := n.RequestBody
|
||||
|
||||
hp := strings.SplitN(n.URL, ":", 2)
|
||||
if len(hp) < 2 {
|
||||
return errors.New("SMTP服务器地址格式错误 (host:port)")
|
||||
}
|
||||
|
||||
auth := smtp.PlainAuth("", user, pass, hp[0])
|
||||
|
||||
subject := "Nezha Monitoring Alert"
|
||||
if ns.Server != nil {
|
||||
subject = fmt.Sprintf("Nezha Alert: %s", ns.Server.Name)
|
||||
}
|
||||
|
||||
body := fmt.Sprintf("To: %s\r\nSubject: %s\r\n\r\n%s", to, subject, message)
|
||||
|
||||
err := smtp.SendMail(n.URL, auth, user, []string{to}, []byte(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// replaceParamInString 替换字符串中的占位符
|
||||
func (ns *NotificationServerBundle) replaceParamsInString(str string, message string, mod func(string) string) string {
|
||||
if mod == nil {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
|
||||
pb "github.com/nezhahq/nezha/proto"
|
||||
@@ -21,6 +22,7 @@ type Server struct {
|
||||
DisplayIndex int `json:"display_index"` // 展示排序,越大越靠前
|
||||
HideForGuest bool `json:"hide_for_guest,omitempty"` // 对游客隐藏
|
||||
EnableDDNS bool `json:"enable_ddns,omitempty"` // 启用DDNS
|
||||
BillingData datatypes.JSON `gorm:"type:json" json:"billing_data,omitempty"`
|
||||
DDNSProfilesRaw string `gorm:"default:'[]';column:ddns_profiles_raw" json:"-"`
|
||||
OverrideDDNSDomainsRaw string `gorm:"default:'{}';column:override_ddns_domains_raw" json:"-"`
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ type SettingForm struct {
|
||||
AgentTLS bool `json:"tls,omitempty" validate:"optional"`
|
||||
EnableIPChangeNotification bool `json:"enable_ip_change_notification,omitempty" validate:"optional"`
|
||||
EnablePlainIPInNotification bool `json:"enable_plain_ip_in_notification,omitempty" validate:"optional"`
|
||||
ExpiryNotificationGroupID uint64 `json:"expiry_notification_group_id,omitempty" validate:"optional"`
|
||||
}
|
||||
|
||||
type Setting struct {
|
||||
|
||||
Reference in New Issue
Block a user