feat: 新增 SMTP/IMAP/POP3 协议调用日志(含攻击分析筛选)

- 每个连接记录一条日志:协议、端口、来源 IP、用户名、成功/失败、
  失败原因(密码错误/IP封禁/中继被拒/发件人伪造/未认证发信等)、
  操作摘要、消息数与会话时长
- 管理后台新增「协议日志」页:按协议/状态/IP/用户名/时间筛选,
  今日与历史成功/失败统计卡片,分页查看,可手动清理
- 后台每 6 小时自动清理超出 protocol_log_keep_days(默认30天)
  的日志;新增 [web] protocol_log_keep_days 配置项
- 修复 POP3 认证既有 bug:handleUSER 丢弃邮箱域名导致 PASS 永远失败
- 新增 store 单测、SMTP/POP3 端到端测试与模板渲染测试
This commit is contained in:
2026-08-19 18:49:29 +08:00
parent 8ea4a623a9
commit 353bfa88f2
30 changed files with 1233 additions and 59 deletions
+30 -8
View File
@@ -302,10 +302,32 @@ func main() {
}
}()
// 11. 后台定期清理过期的协议调用日志(SMTP/IMAP/POP3
startProtocolLogCleaner(stores, cfg.Web.ProtocolLogKeepDays)
fmt.Println("MailGo 邮件系统启动完成")
select {} // Block main goroutine
}
// startProtocolLogCleaner 每 6 小时清理一次超出保留天数的协议调用日志。
// keepDays <= 0 表示不清理。
func startProtocolLogCleaner(stores *store.Stores, keepDays int) {
if keepDays <= 0 {
return
}
go func() {
for {
n, err := stores.ProtocolLogs.CleanupBefore(time.Now().AddDate(0, 0, -keepDays))
if err != nil {
log.Printf("清理协议日志失败: %v", err)
} else if n > 0 {
log.Printf("已清理 %d 条过期协议日志(保留 %d 天)", n, keepDays)
}
time.Sleep(6 * time.Hour)
}
}()
}
// ensureAdminUser checks if an admin user exists and creates one if not.
// It also ensures the default domain "example.com" exists.
func ensureAdminUser(stores *store.Stores, cfg *config.Config) {
@@ -353,14 +375,14 @@ func ensureAdminUser(stores *store.Stores, cfg *config.Config) {
// Create the admin user
adminUser := &db.User{
Username: "admin",
PasswordHash: string(hashedPassword),
DomainID: domain.ID,
QuotaBytes: 5 * 1024 * 1024 * 1024, // 5GB
UsedBytes: 0,
IsActive: true,
IsAdmin: true,
MustChangePassword: true,
Username: "admin",
PasswordHash: string(hashedPassword),
DomainID: domain.ID,
QuotaBytes: 5 * 1024 * 1024 * 1024, // 5GB
UsedBytes: 0,
IsActive: true,
IsAdmin: true,
MustChangePassword: true,
}
if createErr := stores.Users.Create(adminUser); createErr != nil {