feat: 完善后端并新增 Vue3 WebUI
后端: - 修复登录校验、DB 并发(单例+WAL+纯Go sqlite)、会话密钥持久化等核心缺陷 - 新增 Timer CRUD/签到/手动触发、执行日志、仪表盘、驱动元数据等 API - 统一驱动接口: 带 context 的执行器、真实错误返回、敏感字段掩码、连通性测试 - 补全钉钉执行器; Timer 触发/提醒去重; 配置支持默认值与环境变量覆盖 前端: - Vite+Vue3+TS+Pinia+Element Plus, 由驱动元数据动态渲染配置表单 - 登录/仪表盘(倒计时环)/定时器/规则/账号/日志/设置页面, 支持暗色模式 - 主题色 #66ccff, 信号波纹 logo, go:embed 单二进制部署 构建: Dockerfile 增加前端构建阶段; 补充各驱动配置文档 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,29 +1,33 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
"github.com/ssdomei232/goodBaby/internal/retry"
|
||||
)
|
||||
|
||||
// EmailAccountConfigValidator 邮箱账号配置验证器
|
||||
type EmailAccountConfigValidator struct{}
|
||||
|
||||
func (v *EmailAccountConfigValidator) GetType() string {
|
||||
return "email-account"
|
||||
return AccountType
|
||||
}
|
||||
|
||||
func (v *EmailAccountConfigValidator) Validate(config string) error {
|
||||
// 邮箱账号配置验证逻辑
|
||||
var cfg EmailAccountConfig
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return fmt.Errorf("解析邮箱账号配置失败: %v", err)
|
||||
cfg, err := parseAccountConfig(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cfg.SMTPServer == "" {
|
||||
return fmt.Errorf("邮箱账号配置中 SMTP 服务器不能为空")
|
||||
}
|
||||
if cfg.Port == 0 {
|
||||
return fmt.Errorf("邮箱账号配置中 SMTP 端口不能为空")
|
||||
if cfg.Port <= 0 || cfg.Port > 65535 {
|
||||
return fmt.Errorf("邮箱账号配置中 SMTP 端口不合法")
|
||||
}
|
||||
if cfg.Username == "" {
|
||||
return fmt.Errorf("邮箱账号配置中用户名不能为空")
|
||||
@@ -31,6 +35,79 @@ func (v *EmailAccountConfigValidator) Validate(config string) error {
|
||||
if cfg.Password == "" {
|
||||
return fmt.Errorf("邮箱账号配置中密码不能为空")
|
||||
}
|
||||
switch cfg.SecurityOrDefault() {
|
||||
case SecuritySSL, SecuritySTARTTLS, SecurityNone:
|
||||
default:
|
||||
return fmt.Errorf("不支持的加密方式: %s", cfg.Security)
|
||||
}
|
||||
if cfg.From != "" {
|
||||
if _, err := mail.ParseAddress(cfg.From); err != nil {
|
||||
return fmt.Errorf("发件人地址不合法: %s", cfg.From)
|
||||
}
|
||||
}
|
||||
if cfg.TestDestination != "" {
|
||||
if _, err := mail.ParseAddress(cfg.TestDestination); err != nil {
|
||||
return fmt.Errorf("测试收件地址不合法: %s", cfg.TestDestination)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Test 连接 SMTP 服务器并完成认证;填写了测试收件地址时会真的发一封测试邮件
|
||||
func (v *EmailAccountConfigValidator) Test(config string) error {
|
||||
cfg, err := parseAccountConfig(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client, err := newSMTPClient(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), retry.TestTimeout)
|
||||
defer cancel()
|
||||
|
||||
if cfg.TestDestination == "" {
|
||||
if err := client.DialWithContext(ctx); err != nil {
|
||||
return fmt.Errorf("连接 SMTP 服务器失败: %w", err)
|
||||
}
|
||||
return client.Close()
|
||||
}
|
||||
|
||||
message, err := buildMessage(cfg, cfg.TestDestination, "goodBaby 测试邮件", "这是一封来自 goodBaby 的测试邮件,收到即表示邮箱账号配置正确。")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.DialAndSendWithContext(ctx, message); err != nil {
|
||||
return fmt.Errorf("发送测试邮件失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *EmailAccountConfigValidator) Meta() meta.AccountMeta {
|
||||
return meta.AccountMeta{
|
||||
Type: AccountType,
|
||||
Label: "邮箱 (SMTP)",
|
||||
Description: "配置一个用于发信的 SMTP 账号。",
|
||||
Docs: "docs/email-config.md",
|
||||
Fields: []meta.Field{
|
||||
{Key: "smtp_server", Label: "SMTP 服务器", Type: meta.FieldString, Required: true, Placeholder: "smtp.example.com"},
|
||||
{Key: "port", Label: "端口", Type: meta.FieldNumber, Required: true, Default: 465, Help: "SSL 一般为 465,STARTTLS 一般为 587"},
|
||||
{Key: "security", Label: "加密方式", Type: meta.FieldString, Required: false, Default: SecuritySSL, Help: "可选 ssl / starttls / none,留空按 ssl 处理"},
|
||||
{Key: "username", Label: "用户名", Type: meta.FieldString, Required: true, Placeholder: "you@example.com"},
|
||||
{Key: "password", Label: "密码 / 授权码", Type: meta.FieldPassword, Required: true, Secret: true},
|
||||
{Key: "from", Label: "发件人地址", Type: meta.FieldString, Help: "留空则使用用户名作为发件人"},
|
||||
{Key: "test_destination", Label: "测试收件地址", Type: meta.FieldString, Help: "填写后点击“测试”会真的发送一封测试邮件"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func parseAccountConfig(config string) (*EmailAccountConfig, error) {
|
||||
var cfg EmailAccountConfig
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return nil, fmt.Errorf("解析邮箱账号配置失败: %v", err)
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
+38
-7
@@ -1,20 +1,51 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"log"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
// 向邮件列表发送消息
|
||||
func SendMail(rule *model.Rule) {
|
||||
// SendMail 向规则配置里的所有地址发送邮件。
|
||||
//
|
||||
// 每个地址单独重试,互不阻塞;全部失败/部分失败都会汇总成错误返回,
|
||||
// 由 runner 记录到执行日志中。
|
||||
func SendMail(ctx context.Context, rule *model.Rule) error {
|
||||
emailRule, err := GetEmailRuleFromRule(rule)
|
||||
if err != nil {
|
||||
log.Printf("获取邮件规则配置失败: %v", err)
|
||||
return
|
||||
return fmt.Errorf("获取邮件规则配置失败: %w", err)
|
||||
}
|
||||
|
||||
for _, destinations := range emailRule.Destinations {
|
||||
go sendMailMsgWithRetry(rule, destinations) // 使用 goroutine 发送邮件,避免指数退避阻塞其他地址
|
||||
accountConfig, err := GetEmailAccountFromRule(rule)
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取邮件账户配置失败: %w", err)
|
||||
}
|
||||
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
fails []string
|
||||
)
|
||||
|
||||
for _, destination := range emailRule.Destinations {
|
||||
wg.Add(1)
|
||||
go func(address string) {
|
||||
defer wg.Done()
|
||||
if err := sendMailMsgWithRetry(ctx, accountConfig, emailRule, address); err != nil {
|
||||
mu.Lock()
|
||||
fails = append(fails, fmt.Sprintf("%s: %v", address, err))
|
||||
mu.Unlock()
|
||||
}
|
||||
}(destination)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if len(fails) > 0 {
|
||||
return fmt.Errorf("%d/%d 封邮件发送失败: %s",
|
||||
len(fails), len(emailRule.Destinations), strings.Join(fails, "; "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+40
-5
@@ -1,11 +1,46 @@
|
||||
package email
|
||||
|
||||
const (
|
||||
// AccountType 邮箱账号类型标识
|
||||
AccountType = "email"
|
||||
// RuleType 发送邮件的规则类型标识
|
||||
RuleType = "email"
|
||||
)
|
||||
|
||||
// SMTP 加密方式
|
||||
const (
|
||||
SecuritySSL = "ssl" // 隐式 TLS,一般是 465 端口
|
||||
SecuritySTARTTLS = "starttls" // 显式 TLS,一般是 587 端口
|
||||
SecurityNone = "none" // 不加密,一般是 25 端口
|
||||
)
|
||||
|
||||
type EmailAccountConfig struct {
|
||||
SMTPServer string `json:"smtp_server"`
|
||||
Port int `json:"port"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
TestDestination string `json:"test_destination"` // 用于测试邮件发送的地址
|
||||
SMTPServer string `json:"smtp_server"`
|
||||
Port int `json:"port"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
// 加密方式,留空时按 ssl 处理(兼容旧配置)
|
||||
Security string `json:"security"`
|
||||
// 发件人地址,留空时使用 Username
|
||||
From string `json:"from"`
|
||||
// 用于测试邮件发送的地址
|
||||
TestDestination string `json:"test_destination"`
|
||||
}
|
||||
|
||||
// SecurityOrDefault 返回加密方式,兼容没有该字段的旧配置
|
||||
func (c *EmailAccountConfig) SecurityOrDefault() string {
|
||||
if c.Security == "" {
|
||||
return SecuritySSL
|
||||
}
|
||||
return c.Security
|
||||
}
|
||||
|
||||
// FromOrDefault 返回发件人地址
|
||||
func (c *EmailAccountConfig) FromOrDefault() string {
|
||||
if c.From == "" {
|
||||
return c.Username
|
||||
}
|
||||
return c.From
|
||||
}
|
||||
|
||||
type EmailRule struct {
|
||||
|
||||
@@ -4,13 +4,15 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
)
|
||||
|
||||
// EmailRuleValidator 邮件规则验证器
|
||||
type EmailRuleValidator struct{}
|
||||
|
||||
func (v *EmailRuleValidator) GetType() string {
|
||||
return "email"
|
||||
return RuleType
|
||||
}
|
||||
|
||||
func (v *EmailRuleValidator) Validate(configJSON string) error {
|
||||
@@ -40,3 +42,18 @@ func (v *EmailRuleValidator) Validate(configJSON string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *EmailRuleValidator) Meta() meta.RuleMeta {
|
||||
return meta.RuleMeta{
|
||||
Type: RuleType,
|
||||
Label: "发送邮件",
|
||||
Description: "触发时通过关联的 SMTP 账号向指定地址发送邮件。",
|
||||
Docs: "docs/email-config.md",
|
||||
AccountType: AccountType,
|
||||
Fields: []meta.Field{
|
||||
{Key: "title", Label: "邮件标题", Type: meta.FieldString, Required: true},
|
||||
{Key: "msg", Label: "邮件正文", Type: meta.FieldTextarea, Required: true},
|
||||
{Key: "destinations", Label: "收件人", Type: meta.FieldStringList, Required: true, Placeholder: "someone@example.com"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
@@ -10,13 +11,11 @@ import (
|
||||
type EmailExecutor struct{}
|
||||
|
||||
func (e *EmailExecutor) GetType() string {
|
||||
return "email"
|
||||
return RuleType
|
||||
}
|
||||
|
||||
func (e *EmailExecutor) Execute(rule *model.Rule) error {
|
||||
func (e *EmailExecutor) Execute(ctx context.Context, rule *model.Rule) error {
|
||||
log.Printf("执行邮件规则: %s (ID: %d)", rule.Name, rule.ID)
|
||||
|
||||
SendMail(rule)
|
||||
|
||||
return nil
|
||||
return SendMail(ctx, rule)
|
||||
}
|
||||
|
||||
+53
-78
@@ -4,113 +4,88 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/cenkalti/backoff/v5"
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/handler/db"
|
||||
"github.com/ssdomei232/goodBaby/internal/retry"
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
"github.com/wneessen/go-mail"
|
||||
)
|
||||
|
||||
// 从 Rule 中获取 EmailAccount 配置
|
||||
// GetEmailAccountFromRule 从 Rule 中获取 EmailAccount 配置
|
||||
func GetEmailAccountFromRule(rule *model.Rule) (*EmailAccountConfig, error) {
|
||||
gormDB, err := db.GetGormDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 1. 通过 account_id 获取到对应的 account 配置
|
||||
var emailAccount model.Account
|
||||
result := gormDB.Where("id = ?", rule.AccountID).First(&emailAccount)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
// 2. 通过 account 配置中的 config 字段获取到 EmailAccount
|
||||
var emailAccountConfig EmailAccountConfig
|
||||
err = json.Unmarshal([]byte(emailAccount.Config), &emailAccountConfig)
|
||||
if err != nil {
|
||||
if err := db.LoadAccountConfig(rule.AccountID, &emailAccountConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &emailAccountConfig, nil
|
||||
}
|
||||
|
||||
// 从 Rule 中获取 EmailRule 配置
|
||||
// GetEmailRuleFromRule 从 Rule 中获取 EmailRule 配置
|
||||
func GetEmailRuleFromRule(rule *model.Rule) (*EmailRule, error) {
|
||||
var emailRule EmailRule
|
||||
err := json.Unmarshal([]byte(rule.ConfigJson), &emailRule)
|
||||
if err != nil {
|
||||
if err := json.Unmarshal([]byte(rule.ConfigJson), &emailRule); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &emailRule, nil
|
||||
}
|
||||
func sendMailMsgWithRetry(rule *model.Rule, address string) {
|
||||
config, err := configs.GetConfig()
|
||||
|
||||
// newSMTPClient 按账号配置里的加密方式创建 SMTP 客户端
|
||||
func newSMTPClient(cfg *EmailAccountConfig) (*mail.Client, error) {
|
||||
options := []mail.Option{
|
||||
mail.WithPort(cfg.Port),
|
||||
mail.WithUsername(cfg.Username),
|
||||
mail.WithPassword(cfg.Password),
|
||||
mail.WithSMTPAuth(mail.SMTPAuthAutoDiscover),
|
||||
}
|
||||
|
||||
switch cfg.SecurityOrDefault() {
|
||||
case SecuritySSL:
|
||||
options = append(options, mail.WithSSL())
|
||||
case SecuritySTARTTLS:
|
||||
options = append(options, mail.WithTLSPolicy(mail.TLSMandatory))
|
||||
case SecurityNone:
|
||||
options = append(options, mail.WithTLSPolicy(mail.NoTLS))
|
||||
}
|
||||
|
||||
client, err := mail.NewClient(cfg.SMTPServer, options...)
|
||||
if err != nil {
|
||||
log.Printf("获取配置失败: %v", err)
|
||||
return
|
||||
}
|
||||
var timeout time.Duration = time.Duration(config.TimeoutDurationHours) * time.Hour
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
operation := func() (string, error) {
|
||||
err := sendMailMsg(address, rule)
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = backoff.Retry(ctx, operation, backoff.WithBackOff(backoff.NewExponentialBackOff()))
|
||||
if err != nil {
|
||||
log.Printf("发送邮件失败: %v", err)
|
||||
return nil, fmt.Errorf("创建邮件客户端失败: %w", err)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func sendMailMsg(address string, rule *model.Rule) error {
|
||||
emailAccountConfig, err := GetEmailAccountFromRule(rule)
|
||||
if err != nil {
|
||||
log.Printf("获取邮件账户配置失败: %v", err)
|
||||
return fmt.Errorf("获取邮件账户配置失败: %v", err)
|
||||
}
|
||||
|
||||
emailRule, err := GetEmailRuleFromRule(rule)
|
||||
if err != nil {
|
||||
log.Printf("获取邮件规则配置失败: %v", err)
|
||||
return fmt.Errorf("获取邮件规则配置失败: %v", err)
|
||||
}
|
||||
|
||||
client, err := mail.NewClient(
|
||||
emailAccountConfig.SMTPServer,
|
||||
mail.WithPort(emailAccountConfig.Port),
|
||||
mail.WithSSL(),
|
||||
mail.WithUsername(emailAccountConfig.Username),
|
||||
mail.WithPassword(emailAccountConfig.Password),
|
||||
mail.WithSMTPAuth(mail.SMTPAuthPlain),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建邮件客户端失败: %v", err)
|
||||
}
|
||||
|
||||
// 创建邮件
|
||||
func buildMessage(cfg *EmailAccountConfig, address, title, body string) (*mail.Msg, error) {
|
||||
message := mail.NewMsg()
|
||||
if err := message.From(emailAccountConfig.Username); err != nil {
|
||||
return fmt.Errorf("设置发件人失败: %v", err)
|
||||
if err := message.From(cfg.FromOrDefault()); err != nil {
|
||||
return nil, fmt.Errorf("设置发件人失败: %w", err)
|
||||
}
|
||||
|
||||
if err := message.To(address); err != nil {
|
||||
return fmt.Errorf("设置收件人失败: %v", err)
|
||||
return nil, fmt.Errorf("设置收件人失败: %w", err)
|
||||
}
|
||||
message.Subject(title)
|
||||
message.SetBodyString(mail.TypeTextPlain, body)
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func sendMailMsgWithRetry(ctx context.Context, cfg *EmailAccountConfig, rule *EmailRule, address string) error {
|
||||
return retry.Do(ctx, func() error {
|
||||
return sendMailMsg(ctx, cfg, rule, address)
|
||||
})
|
||||
}
|
||||
|
||||
func sendMailMsg(ctx context.Context, cfg *EmailAccountConfig, rule *EmailRule, address string) error {
|
||||
client, err := newSMTPClient(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
message.Subject(emailRule.Title)
|
||||
message.SetBodyString(mail.TypeTextPlain, emailRule.Msg)
|
||||
|
||||
// 发送邮件
|
||||
if err := client.DialAndSend(message); err != nil {
|
||||
return fmt.Errorf("发送邮件失败: %v", err)
|
||||
message, err := buildMessage(cfg, address, rule.Title, rule.Msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := client.DialAndSendWithContext(ctx, message); err != nil {
|
||||
return fmt.Errorf("发送邮件失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user