补齐三类日志的触发操作和结果并完善运行事件展示
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-09 02:48:07 +08:00
1 parent b50e5bdfa2
commit dd6258c98b
34 files changed
+734 -87

No files matched your search

+57 -2
View File
@@ -2,13 +2,47 @@ package logstore
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"net"
"net/textproto"
"strconv"
"time"
)
type MailDetails struct {
Trigger string `json:"trigger"`
Stage string `json:"stage"`
Outcome string `json:"outcome"`
AcceptedCount int `json:"acceptedCount"`
}
// MailFailureReason 使用错误类别给出原因,不保存可能携带凭据的远端响应原文。
func MailFailureReason(err error) string {
var certificate *tls.CertificateVerificationError
var dns *net.DNSError
var protocol *textproto.Error
var network net.Error
switch {
case errors.As(err, &certificate):
return "对端证书验证失败"
case errors.As(err, &dns):
return "收件域名解析失败"
case errors.As(err, &protocol):
return "对端 SMTP 拒绝,状态码 " + strconv.Itoa(protocol.Code)
case errors.As(err, &network):
if network.Timeout() {
return "投递连接超时"
}
return "投递网络连接失败"
default:
return "投递未完成,请结合运行日志排查"
}
}
// MailResult 记录投递尝试元数据,不复制正文、附件、认证信息或服务器错误原文。
func MailResult(userID int, direction string, sender string, recipientCount, size int, result string) {
func MailResult(userID int, direction string, sender string, recipientCount, size int, result string, details ...MailDetails) {
if len(sender) > 320 {
sender = "地址超出显示长度"
}
@@ -17,7 +51,8 @@ func MailResult(userID int, direction string, sender string, recipientCount, siz
Sender string `json:"sender"`
RecipientCount int `json:"recipientCount"`
Bytes int `json:"bytes"`
}{direction, sender, recipientCount, size})
MailDetails
}{direction, sender, recipientCount, size, mailDetails(direction, result, details)})
action := "接收邮件"
if direction == "outbound" {
action = "投递邮件"
@@ -29,3 +64,23 @@ func MailResult(userID int, direction string, sender string, recipientCount, siz
Kind: "mail", ActorID: strconv.Itoa(userID), CreatedAt: time.Now(), Action: action, Result: result, Payload: payload,
})
}
func mailDetails(direction, result string, details []MailDetails) MailDetails {
value := MailDetails{Trigger: "SMTP 收信", Stage: "接收处理"}
if direction == "outbound" {
value.Trigger, value.Stage = "邮件发送", "对外投递"
} else if direction == "submission" {
value.Trigger, value.Stage = "SMTP 客户端提交", "提交处理"
}
if len(details) > 0 {
value = details[0]
}
if value.Outcome == "" {
value.Outcome = map[string]string{
"success": "当前处理阶段已完成", "accepted": "对端服务器已接收,不代表收件人已阅读或最终入箱",
"failure": "当前处理阶段失败", "partial": "部分收件人已被对端接收,其余投递失败",
"discarded": "已按收信策略丢弃,未写入邮箱", "unknown": "处理未正常完成,结果待确认",
}[result]
}
return value
}
+2 -2
View File
@@ -23,7 +23,7 @@ func Record(ctx context.Context, entry Entry) {
bounded, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := store.Write(bounded, entry); err != nil {
log.WithError(err).Error("业务日志写入失败")
log.WithError(err).WithFields(log.Fields{"action": "写入业务日志", "object": entry.Kind, "result": "failure"}).Error("业务日志写入失败")
}
}
@@ -47,7 +47,7 @@ func Start(store *Store) *Runtime {
_, err := store.Cleanup(bounded, time.Now())
stop()
if err != nil && ctx.Err() == nil {
log.WithError(err).Error("过期业务日志清理失败,将在下一轮重试")
log.WithError(err).WithFields(log.Fields{"action": "清理过期业务日志", "result": "failure"}).Error("过期业务日志清理失败,将在下一轮重试")
}
select {
case <-ctx.Done():
+1 -1
View File
@@ -71,7 +71,7 @@ func Start(root string) (*Writer, error) {
err := w.Maintain(bounded, w.now())
stop()
if err != nil && ctx.Err() == nil {
log.WithError(err).Error("运行日志维护失败,源文件保留并在下一轮重试")
log.WithError(err).WithFields(log.Fields{"action": "压缩与清理运行日志", "result": "failure"}).Error("运行日志维护失败,源文件保留并在下一轮重试")
}
select {
case <-ctx.Done():
+11 -9
View File
@@ -433,7 +433,7 @@ func renewCertificate(privateKey *ecdsa.PrivateKey, cfg *config.Config) error {
Bundle: true,
}
log.Infof("wait ssl renew")
log.WithFields(log.Fields{"action": "续期 TLS 证书", "result": "started"}).Info("开始请求续期证书")
certificates, err := client.Certificate.Obtain(request)
if err != nil {
return errors.Wrap(err)
@@ -500,22 +500,23 @@ func generateCertificate(privateKey *ecdsa.PrivateKey, cfg *config.Config, newAc
as := async.New(nil)
as.Process(func(params any) {
log.Infof("wait ssl")
log.WithFields(log.Fields{"action": "申请 TLS 证书", "result": "started"}).Info("开始向证书颁发机构申请证书")
certificates, err := client.Certificate.Obtain(request)
if err != nil {
log.WithError(err).Error("certificate issuance failed")
log.WithError(err).WithFields(log.Fields{"action": "申请 TLS 证书", "result": "failure"}).Error("证书申请失败")
return
}
log.Infof("证书校验通过!")
log.WithFields(log.Fields{"action": "申请 TLS 证书", "result": "success"}).Info("已取得证书,开始校验并保存")
if err := writeCertificateFiles(cfg, certificates.PrivateKey, certificates.Certificate, certificates.IssuerCertificate); err != nil {
log.WithError(err).Error("certificate storage failed")
log.WithError(err).WithFields(log.Fields{"action": "保存 TLS 证书", "result": "failure"}).Error("证书保存失败")
return
}
if err := setup.Finish(); err != nil {
log.WithError(err).Error("finish setup after certificate issuance failed")
log.WithError(err).WithFields(log.Fields{"action": "完成证书初始化", "result": "failure"}).Error("证书已保存,但完成初始化失败")
return
}
log.WithFields(log.Fields{"action": "完成证书初始化", "result": "success"}).Info("证书已保存并完成初始化")
}, nil)
@@ -602,15 +603,16 @@ func Update(needRestart bool) {
if days < 30 || err != nil || nameMatchFail {
if err != nil {
log.Errorf("SSL Check Error, Update SSL Certificate. Error Info :%+v", err)
log.WithError(err).WithFields(log.Fields{"action": "检查 TLS 证书", "result": "failure"}).Warn("证书检查失败,准备重新申请")
} else {
log.Infof("SSL certificate remaining time is only %d days, renew SSL certificate.", days)
log.WithFields(log.Fields{"action": "检查 TLS 证书", "result": "recorded", "remainingDays": days}).Info("证书需要更新,准备重新申请")
}
err = GenSSL(true)
if err != nil {
log.Errorf("SSL Update Error! %+v", err)
log.WithError(err).WithFields(log.Fields{"action": "更新 TLS 证书", "result": "failure"}).Error("证书更新失败")
return
}
log.WithFields(log.Fields{"action": "更新 TLS 证书", "result": "success"}).Info("证书已更新并保存")
if needRestart {
// 更新完证书,重启服务
signal.RestartChan <- true
+12 -1
View File
@@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"git.pcux.cn/xuans/Smail/services/logstore"
smailsmtp "git.pcux.cn/xuans/Smail/utils/smtp"
)
@@ -58,7 +59,17 @@ func (d *SMTPDelivery) Delivery() Delivery {
}
message := buildCodeMessage(d.settings.From, challenge.Email, code, challenge.Purpose)
defer clearBytes(message)
return d.send(d.settings, challenge.Email, message)
err := d.send(d.settings, challenge.Email, message)
result := "accepted"
details := logstore.MailDetails{Trigger: "系统安全验证邮件", Stage: "SMTP 中继投递", AcceptedCount: 1}
if err != nil {
result = "failure"
details.AcceptedCount = 0
details.Outcome = logstore.MailFailureReason(err)
}
// 只记录发送元数据,不保存外部邮箱、验证码、邮件内容及中继凭据。
logstore.MailResult(0, "outbound", d.settings.From, 1, len(message), result, details)
return err
}
}