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
+46 -6
View File
@@ -28,14 +28,17 @@ import (
type imapBackend struct {
stores *store.Stores
banCfg config.BanConfig
port int
}
// Login authenticates a user by email and password.
func (b *imapBackend) Login(connInfo *imap.ConnInfo, username, password string) (backend.User, error) {
clientIP := store.ClientIPFromAddr(connInfo.RemoteAddr)
now := time.Now()
// 已封禁 IP 一律拒绝认证(防协议层暴力破解)
if banned, _ := b.stores.Bans.IsBanned(clientIP); banned {
b.recordLogin(clientIP, username, false, "IP已被封禁", "认证被拒绝(IP 已封禁)", 0, now)
return nil, backend.ErrInvalidCredentials
}
@@ -43,6 +46,7 @@ func (b *imapBackend) Login(connInfo *imap.ConnInfo, username, password string)
if err != nil {
// 认证失败计数,达到阈值封禁(与 Web 登录共用 ban_entries
b.stores.RecordAuthFailure(clientIP, b.banCfg.MaxFailAttempts, b.banCfg.BanDurationMin)
b.recordLogin(clientIP, username, false, "用户名或密码错误", "LOGIN 失败", 0, now)
return nil, fmt.Errorf("invalid credentials: %w", err)
}
@@ -52,20 +56,48 @@ func (b *imapBackend) Login(connInfo *imap.ConnInfo, username, password string)
email = user.Username + "@" + domain.Name
}
logID := b.recordLogin(clientIP, username, true, "", "LOGIN 成功", 0, now)
return &imapUser{
stores: b.stores,
id: user.ID,
email: email,
stores: b.stores,
id: user.ID,
email: email,
logID: logID,
clientIP: clientIP,
startedAt: now,
}, nil
}
// recordLogin 写入一条 IMAP 登录日志,返回新记录的 ID(失败时为 0)。
func (b *imapBackend) recordLogin(ip, username string, success bool, failReason, detail string, durationMs int64, at time.Time) uint {
entry := &db.ProtocolLog{
Protocol: db.ProtocolIMAP,
Port: b.port,
ClientIP: ip,
Username: username,
Success: success,
FailReason: failReason,
Detail: detail,
DurationMs: durationMs,
CreatedAt: at,
}
if err := b.stores.ProtocolLogs.Create(entry); err != nil {
log.Printf("IMAP: 写入协议日志失败: %v", err)
return 0
}
return entry.ID
}
// ---------- imapUser ----------
// imapUser implements backend.User.
type imapUser struct {
stores *store.Stores
id uint
email string
stores *store.Stores
id uint
email string
logID uint
clientIP string
startedAt time.Time
}
// Username returns the user's email address.
@@ -146,6 +178,14 @@ func (u *imapUser) RenameMailbox(existingName, newName string) error {
// Logout is called when the user session ends.
func (u *imapUser) Logout() error {
// 回填会话时长,登录记录在 Login 时已写入
if u.logID == 0 {
return nil
}
durationMs := time.Since(u.startedAt).Milliseconds()
if err := u.stores.ProtocolLogs.UpdateDuration(u.logID, durationMs); err != nil {
log.Printf("IMAP: 更新协议日志失败: %v", err)
}
return nil
}
+16 -1
View File
@@ -4,6 +4,8 @@ import (
"crypto/tls"
"fmt"
"log"
"net"
"strconv"
"mail_go/config"
"mail_go/internal/store"
@@ -42,7 +44,7 @@ func (s *IMAPServer) tlsConfig() (*tls.Config, error) {
// newServer creates a configured imapserver.Server with the given address.
func (s *IMAPServer) newServer(addr string, tlsConfig *tls.Config) *imapserver.Server {
be := &imapBackend{stores: s.stores, banCfg: s.banCfg}
be := &imapBackend{stores: s.stores, banCfg: s.banCfg, port: portOf(addr)}
srv := imapserver.New(be)
srv.Addr = addr
srv.TLSConfig = tlsConfig
@@ -50,6 +52,19 @@ func (s *IMAPServer) newServer(addr string, tlsConfig *tls.Config) *imapserver.S
return srv
}
// portOf 从监听地址解析端口号,失败返回 0。
func portOf(addr string) int {
_, portStr, err := net.SplitHostPort(addr)
if err != nil {
return 0
}
port, err := strconv.Atoi(portStr)
if err != nil {
return 0
}
return port
}
// Start starts the IMAP server on the plain-text port.
func (s *IMAPServer) Start() error {
tlsConfig, err := s.tlsConfig()