Author SHA1 Message Date
dsh 9607c4f023 fix(migrate): 目标库缺少 mailbox_states 表时自动建表(兼容上游模型未含 MailboxState 的场景) 2026-08-19 11:27:25 -04:00
dsh 4367f7fb92 fix(migrate): mailbox_states 改用原生 SQL 迁移,不依赖 MailboxState 模型(兼容未合并 UIDVALIDITY 特性的上游) 2026-08-19 11:27:00 -04:00
dsh af839a1378 chore: 移除误提交的 migrate 编译产物,加入 .gitignore 2026-08-19 11:26:32 -04:00
dsh 07e81fc328 feat(db): 支持 MySQL 迁移(修复模型外键冲突 + 大文本类型 + 迁移工具)
- Attachment 模型移除 Message 关联:其 foreignKey 名 MessageID 与
  Message.MessageID 字符串字段冲突,GORM AutoMigrate 会生成错误外键
  (messages.message_id → attachments.id 且强转 bigint),MySQL 下建表
  直接失败(SQLite 因动态类型侥幸可用)
- Message.TextBody/HtmlBody 改 mediumtext:MySQL TEXT 仅 64KB,
  大 HTML 邮件会写入失败
- 新增 cmd/migrate:SQLite → MySQL 一次性迁移工具(GORM 模型读源、
  批量写目标、时间统一 UTC、ban_entries 零值时间转 NULL、逐表校验)
2026-08-19 11:26:32 -04:00
kevin f8fbe1ebdb Merge pull request 'fix(imap): FETCH BODY/BODYSTRUCTURE 解析失败时服务器 panic,客户端只能取到部分邮件' (#10) from dsh/mailgo:fix/imap-fetch-body-panic into main
Reviewed-on: kevin/mailgo#10
2026-08-19 08:55:22 -04:00
dsh 3a4636c2c5 fix(imap): FETCH BODY/BODYSTRUCTURE 解析失败时服务器 panic 导致客户端只能取到部分邮件
根因:backendutil.FetchBodyStructure 对部分消息返回 nil(典型场景:
- message/rfc822 附件为 base64 编码时库内不解码,把编码文本当嵌套
  消息头解析报错(转发邮件场景,如 .mail-monitor/forward.py 转发)
- multipart 缺少结束边界(截断)时 extended 解析报错)

buildIMAPMessage 未处理 nil,go-imap 格式化 FETCH 响应时在 send()
协程 nil 指针解引用 panic,连接中断——Thunderbird 只取到崩溃前已
发送的几封邮件,手机客户端一直卡在"正在获取邮件"。

修复:
- BodyStructure 解析失败时降级为 text/plain 单段结构,杜绝 nil
- FetchBodySection 返回 nil 时跳过该 section,不再写入 nil literal
- 新增集成测试 TestFetchBodyMalformedMIME 覆盖两类畸形 MIME
2026-08-19 08:52:39 -04:00
kevin 07ba01a6f6 fix(imap): 修复 Thunderbird 未读状态丢失(SQLite 并发写 + 客户端 seq 错位)
A. SQLite 并发写失败被吞(高概率根因):
- DSN 追加 _busy_timeout=5000&_journal_mode=WAL&_synchronous=NORMAL,
  WAL 下读不阻塞写,消除瞬时 SQLITE_BUSY
- UpdateMessagesFlags 不再吞 MarkReadState/MarkFlagged 错误:
  出错记日志并返回 error(客户端收到 NO 会重试)
- Web MarkRead/Delete 同款静默丢错补日志

B. 客户端 seq 视图错位(集成测试复现后修复):
- 真实服务器+脚本客户端测试复现:客户端按日期倒序自编号发
  seq 式 STORE 时,旧排序(id ASC)把最旧邮件标为已读
- 规范排序改为 date DESC, id DESC(最新在前),与主流客户端
  默认视图一致;buildNewMessageUpdate 改用 seqOf 取真实序号
- 新增 UID STORE / 服务器下发 seq STORE / 自编号 seq STORE 集成测试

注:go-imap v1.2.1 存在库内 *conn.silent() 数据竞争(启用推送后
必然触发),集成测试以 //go:build !race 排除,-race 下由单元测试
覆盖推送逻辑
2026-08-19 20:30:06 +08:00
kevin 278c16c36a fix(imap): 修复多监听器广播更新导致 close of closed channel panic
broadcastUpdate 此前把同一个 backend.Update 对象推送到明文/TLS 两个
监听器的推送通道,两个 go-imap listenUpdates goroutine 各自对
update.Done() 执行 close,二次关闭同一 channel 触发 panic
(server.go:373 close of closed channel)。

修复:按监听器克隆 Update 对象(载荷共享、Done channel 独立),
新增回归测试:两个监听器收到的更新 Done channel 必须不同,且各自
close 不 panic;全量 -race 通过
2026-08-19 19:58:18 +08:00
kevin 6a9dcd0285 feat(imap): 多客户端状态实时同步 + 当前连接「断开并封禁」
- 推送扩展:IMAP STORE(已读/星标/\Deleted)推送 FETCH 标志更新、
  EXPUNGE 推送 ExpungeUpdate(删除前序号)、APPEND/COPY/MOVE 推送
  新邮件;POP3 QUIT 删除、Web 标已读/删除同样实时同步到 IMAP 客户端
- Pusher 接口统一 SMTP/POP3/Web 的推送入口,IMAP 内部操作经会话
  通道直接入队(非阻塞,满则丢弃)
- 当前连接页新增「断开并封禁」:connhub 支持断开回调,SMTP/POP3
  关底层连接、IMAP 经 ForEachConn 按地址断开;一键封禁 180 天并
  断开该 IP 全部在线连接,黑名单页可随时解封
- 修复:POP3 PASS 成功后保留完整邮箱(此前被裸用户名覆盖)
- 新增测试:断开/按 IP 断开、flags/expunge 推送内容、POP3 删除推送、
  Web 断开封禁处理器;全量 -race 通过
2026-08-19 19:56:04 +08:00
kevin ede85e0698 feat(imap): 新邮件实时推送(IDLE)+ 后台当前连接监控
- IMAP 推送:imapBackend 实现 backend.BackendUpdater,SMTP 本地投递
  与 Web 写信投递成功后 NotifyNewMessage,挂起 IDLE 的客户端即时收到
  新邮件 FETCH 通知(按用户名+INBOX 过滤广播,通道满非阻塞丢弃)
- 当前连接:新增 internal/connhub 连接注册中心,SMTP/IMAP/POP3 三协议
  注册/注销/用户名/TLS/活跃时间追踪;后台新增「当前连接」页
  (/admin/connections,统计卡片+连接表格,每 5 秒自动刷新)
- 新增测试:connhub 并发安全、推送内容/非阻塞/nil 安全、
  后台页面渲染;全量 -race 通过
2026-08-19 19:40:05 +08:00
kevin b158b8f1f5 feat(outbound): 外发投递多线程化(worker 池 + 每域并发上限)
- 1 个 dispatcher + N 个 worker 池(默认 4)并行投递,启动时立即
  扫描清空积压;workers=0/1 退化为串行(旧行为)
- 每收件域并发信号量(默认 2,配置 max_concurrent_per_domain),
  防对单个 MX 域并发过多被判定滥发;中继模式统一按 relay 限制
- store 新增 Claim 原子抢占(sending 状态防重复投递),并发下
  每封邮件恰好投递一次
- 新增配置 workers/batch_size(替代硬编码 50);dispatcher 改为
  抢占后投递、worker 只负责发送,退避重试/退信/状态机不变
- 新增测试:真并发、串行回退、每域上限、临时/永久失败、错误隔离、
  Claim 并发原子性与状态可抢占性;-race 无警告
2026-08-19 19:23:27 +08:00
kevin 6d73171207 fix(web): 管理后台内容被固定顶栏遮挡
.container 缺少顶部内边距,固定定位的 topbar(56px)盖住后台页面
标题/统计卡片/表格表头;为用户页面同等的 --topbar-h 下移空间,
移动端断点同步修正(50px)
2026-08-19 19:11:32 +08:00
kevin f2493da03e feat(security): IP 阶段性封禁,前3次触发不封禁、第4次起按档位递增
- 封禁规则:达到失败阈值记为一次触发,前 3 次只计数不封禁;
  第 4 次起封禁并按档位递增:30分钟(ban_duration_min)→ 3小时
  → 3个月 → 半年(上限);封禁过期后保留记录作为升档依据,
  成功登录或管理员解封清零
- BanEntry 新增 BanCount(累计触发次数),每 IP 一条记录 upsert,
  不再重复建行;RecordAuthFailure 统一 Web/LDAP/SMTP/IMAP/POP3
  五处封禁逻辑,原因带档位(如"第1次封禁:登录失败次数过多
  (第4次触发,失败5次)")
- 黑名单页修复:列表仅显示已封禁或曾封禁记录(原因/到期时间必填),
  新增封禁次数列与封禁中/已过期状态徽章,移除清理过期按钮
- 用户封禁页显示第 N 次封禁档位;新增档位升级与列表过滤单测
2026-08-19 19:07:25 +08:00
kevin 353bfa88f2 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 端到端测试与模板渲染测试
2026-08-19 18:49:29 +08:00
kevin 8ea4a623a9 fix(security): 修复 P3 低危项(开放重定向/配额TOCTOU/safeJS/会话治理)
- Referer 开放重定向:safeRedirectPath 仅放行同站相对路径,
  外部 URL/协议跳转一律回退 /inbox
- 发信配额 TOCTOU:新增 TryReserveQuota 原子预扣
  (UPDATE ... WHERE used_bytes + n <= quota_bytes),超配额即拒发;
  附件保存失败按大小补偿回退
- 移除危险模板函数 safeHTML/safeJS:新增 jsonify(json.Marshal,
  < > & 转义为 \u003c 等,无法逃出 </script>),compose 页
  quill.innerHTML 改用 jsonify;srcdoc 改回默认属性转义
- 会话治理:登录成功后 session.Clear() 清旧状态;记录 loginAt,
  绝对过期 7 天 + 滑动续期(活跃会话 12h 写回刷新)
- 确认 #15 Content-Disposition 编码随 P1 #4 已完成
- 新增 12 个测试:重定向路径矩阵、配额原子性(含超额不部分扣费)、
  jsonify 逃逸防护、会话绝对过期/有效访问(签名会话构造)

至此 16 项安全审计项(P0-P3)全部修复完成。
2026-08-19 16:56:23 +08:00
wuwenfengmi1998 8cfeb43c6a docs: 新增移动端界面截图(响应式布局) 2026-08-19 16:48:26 +08:00
kevin 3f28ec20f4 fix(security): 修复 P2 中危项(cookie/协议限速/路径遍历/默认口令/中继TLS/安全头/信息泄露)
- 会话 cookie 增加 Secure 标志;新增 [web].cookie_secure 配置
  (默认 true,仅本地 HTTP 调试关闭;缺失字段按安全默认处理)
- SMTP/IMAP/POP3 认证接入封禁体系(store.RecordAuthFailure 与 Web
  共用 ban_entries):失败计数达 ban.max_fail_attempts 即封禁 IP,
  已封禁 IP 拒绝认证,堵住协议层暴力破解
- 附件存储路径遍历防护重写:FullPath 白名单校验(UUID 文件名格式)
  + baseDir 前缀兜底,非法路径返回错误;Save 扩展名白名单化
- 初始管理员不再使用 admin/admin:密码取 MAILGO_ADMIN_PASSWORD 或
  随机生成并打印一次;新增 MustChangePassword 首登强制改密
  (管理员重置密码同样触发)
- 外发中继默认验证 TLS 证书(保护 AUTH 凭据,防 MITM),直投 MX
  保持机会式 TLS;新增 outbound.relay_tls_insecure 开关(默认 false)
- 新增安全响应头中间件:HSTS、X-Frame-Options DENY、nosniff、
  Referrer-Policy、基础 CSP(frame-ancestors 'none' 防点击劫持,
  connect-src/form-action 'self' 防数据外泄)
- LDAP/OAuth 登录错误统一为通用文案,原始错误只写日志,
  不再回显邮箱/内部细节(防用户枚举与信息泄露)
- 新增 25 个回归测试:cookie 标志、封禁阈值、路径遍历用例、
  中继 TLS 验证(自签证书 STARTTLS 集成)、安全头、OAuth 文案

部署注意:升级后所有会话失效需重新登录;若直接以 HTTP 提供
服务需显式配置 cookie_secure = false。
2026-08-19 16:45:21 +08:00
kevin c725d0b91e fix(security): 修复 P1 高危项(OAuth2 state / 伪造客户端IP / CRLF 邮件头注入)
- OAuth2: state 改为 crypto/rand 随机值并写入独立短期 cookie
  (主会话为 SameSite=Strict,跨站回调不携带,不能放主会话);
  回调用 ConstantTimeCompare 校验 state,缺失/不匹配返回 403,
  校验后立即清除保证一次性使用。原硬编码 mailgo_oauth2_state
  可被利用做授权码注入/登录 CSRF。
- 代理信任: engine.SetTrustedProxies 仅信任 127.0.0.1/::1。
  外部直连时 X-Forwarded-For 完全不可信,防止伪造客户端 IP
  绕过登录封禁或恶意封禁他人;本机 Caddy/Nginx 转发不受影响。
- CRLF 注入: Web 写信的 To/Cc/Subject 及附件文件名不再原样拼入
  MIME 头。新增 sanitizeHeaderField(strip CR/LF/NUL)、
  subject 按 RFC 2047 编码、附件名用 mime.FormatMediaType
  (RFC 2231);附件下载的 Content-Disposition 同步修复。
  消息构建抽为 buildOutgoingMessage 纯函数便于测试。
- test: 新增 13 个回归测试(trustedproxy / mail_injection /
  oauth2_state),覆盖伪造 XFF、注入载荷、state 校验全部分支。
2026-08-19 16:31:25 +08:00
kevin 551ed981de fix(security): 会话密钥改为随机生成,修复硬编码密钥可伪造管理员会话
安全审计 P0 修复:旧版会话签名密钥硬编码在源码中(源码公开即泄露),
任何人可据此伪造 isAdmin 会话接管后台。

- config: 新增 [web].secret_key,首启/升级时用 crypto/rand 自动生成
  32 字节随机密钥并持久化;旧硬编码值自动替换
- config: 支持 MAILGO_SECRET_KEY 环境变量覆盖(覆盖值不落盘)
- config: 配置文件权限收紧为 0600(同时保护 relay_password 等敏感字段)
- web: NewWebServer 校验密钥(空/旧默认值/短于16字节拒绝启动)
- test: 伪造会话拒绝、密钥生命周期(生成/持久化/重启稳定/env不落盘)等
  9 个测试
- docs: README 配置参考、security_todo.md 安全修复清单

部署注意:升级重启后所有用户需重新登录。
2026-08-19 16:21:28 +08:00
kevin 6484af7e63 Merge pull request 'fix: 返回列表后未读邮件状态不更新(BFCache 静默同步)' (#9) from dsh/mailgo:fix/unread-status-bfcache into main
Reviewed-on: kevin/mailgo#9
2026-08-17 23:18:30 -04:00
dsh bf283ba5fc fix: 返回列表后未读邮件状态不更新(BFCache 静默同步)
问题:打开未读邮件(阅读页已自动标记已读)后返回列表,浏览器从 BFCache
恢复旧 DOM,列表仍显示未读,需手动刷新。

修复:
- 三个列表页的交互脚本提取为公共模板 listjs(base.html),行为一致
- pageshow(persisted=true) 时静默 fetch 最新列表并局部更新行状态
  (未读类/圆点/已删除行/未读角标/总数),不整页刷新、保留滚动
- visibilitychange 兜底:标签页重新可见时也同步一次(离线失败静默忽略,
  仅 BFCache 场景失败才整页刷新)
- 验证:合成 pageshow 事件驱动同步,未读->已读局部更新 PASS;
  桌面/移动/平板布局回归无异常
2026-08-17 23:08:15 -04:00
kevin e3514246b0 Merge pull request 'feat: 前端移动端响应式适配' (#8) from dsh/mailgo:feature/mobile-responsive into main
Reviewed-on: kevin/mailgo#8
2026-08-17 22:48:48 -04:00
dsh f281121a4d feat: 前端移动端响应式适配(≤767px)
- 左侧文件夹栏在小屏下变为底部导航条:收件箱/草稿箱/已发送/管理后台 + 居中「写信」悬浮圆钮(带未读角标)
- 顶栏紧凑化:列表页搜索框自动下移为通栏第二行
- 邮件列表:隐藏正文摘要、缩小头像/时间列、行高加大便于触控,删除按钮常显
- 阅读页/写信页/设置页:内边距与字号收紧,配额条适配
- 管理后台:侧栏改为横向换行导航,表格横向滚动,统计卡两列排布
- 全部模板 viewport 增加 viewport-fit=cover(iOS 安全区),底部导航支持 env(safe-area-inset-bottom)
- settings.html 内联样式改为 .settings-main 类以便响应式覆盖
2026-08-17 13:21:19 -04:00
kevin fe1bf25d31 Merge pull request 'docs: README 增加界面预览截图(QQ 邮箱风格界面)' (#7) from dsh/mailgo:docs/readme-screenshots into main
Reviewed-on: kevin/mailgo#7
2026-08-17 04:51:15 -04:00
dsh df5ea7b228 docs: README 增加界面预览截图与 QQ 邮箱风格界面说明 2026-08-17 04:50:53 -04:00
kevin 9e7e8e1fb5 Merge pull request 'feat: 前端整体重写为 QQ 邮箱风格布局' (#6) from dsh/mailgo:feature/qq-mail-frontend into main
Reviewed-on: kevin/mailgo#6
2026-08-17 04:42:34 -04:00
dsh 878d31b48e feat: 前端整体重写为 QQ 邮箱风格布局
- base.html: 全新设计系统(顶部导航栏 + 左侧文件夹栏 + 内容区三栏布局,
  蓝/橙主题色),兼容管理后台原有组件类
- inbox/drafts/sent: 邮件列表页重写,支持全选、批量删除、实时搜索过滤、
  未读标记、头像圆标、QQ 式短日期、悬停行内删除、分页
- view: 邮件阅读页重写(返回/回复/删除工具条、发件人卡片、附件区)
- compose: 写信页重写(发送/附件/取消、字段行、附件 chips、配额进度条)
- settings/login/banned: 设置页(账号信息+配额条)、登录页、封禁页重写
- server.go: 新增模板函数 mailName/mailEmail/initial/truncate/shortDate/avatarStyle
- mail.go: 侧栏文件夹计数(收件箱未读红标、草稿/已发送数量)
- 新增 render_test.go 模板渲染回归测试
2026-08-17 04:42:03 -04:00
kevin 9036523d9f Merge pull request 'fix: 修复编辑域名时私钥留空仍报“TLS 私钥和公钥证书必须同时填写”' (#5) from dsh/mailgo:caddy-cert-hot-reload into main
Reviewed-on: kevin/mailgo#5
2026-08-16 23:46:57 -04:00
dsh eed0c7b95f fix: 修复编辑域名时私钥留空仍报“TLS 私钥和公钥证书必须同时填写”
浏览器提交 textarea 会把换行规范为 CRLF,而证书文件(尤其是一键从
Caddy 导入的)是 LF,导致“证书未修改”的比较失效,私钥留空(保留
现有私钥)时误报必须同时填写。

- handleDomainTLSUpdate 增加 normalizePEM:提交值与磁盘文件统一
  归一化为 LF 后再比较/写入
- 新增 3 个回归测试:CRLF 提交证书未变+私钥留空、CRLF 新证书对
  保存为 LF、证书已变但私钥留空仍应报错
2026-08-16 23:45:44 -04:00
kevin f24c939eda Merge pull request 'feat: 管理后台一键从 Caddy 获取证书 + TLS 证书热加载' (#4) from dsh/mailgo:caddy-cert-hot-reload into main
Reviewed-on: kevin/mailgo#4
2026-08-16 23:41:25 -04:00
dsh f0b9ad3e6f feat: 管理后台一键从 Caddy 获取证书 + TLS 证书热加载
- 域名编辑页新增“从 Caddy 获取证书”按钮:一键把本机 Caddy 已签发的
  证书与私钥导入该域名的 TLS 目录并自动启用 TLS,支持通配符证书
  (如 *.example.com 可匹配 mail.example.com),成功/失败均回显横幅
- 新增 internal/caddycert:搜索 Caddy 证书存储(同步镜像目录优先、
  caddy.data_dir 与常见位置兜底),校验密钥对/有效期/SAN,并给出
  可操作的中文错误提示(未找到/证书无效/权限不足)
- install.sh 新增 setup-caddy-cert:安装 root 权限的 systemd
  path+timer 同步任务(mailgo-caddy-sync),把 Caddy 证书树镜像到
  /srv/mail_go/tls/caddy(证书续期后自动更新、每日兜底),另授予
  ACL 作为直接读取兜底;install 时自动检测并配置
- 新增 [caddy] data_dir 配置节,支持自定义 Caddy 数据目录
- 新增 internal/tlsutil:TLS 证书热加载器,每次握手按需重载证书
  文件(mtime 检测),重载失败继续使用旧证书兜底并节流重试;
  应用于 SMTPS 465/IMAPS 993/POP3S 995 与 STARTTLS,导入或上传
  新证书后无需重启服务即生效
- 证书来源动态切换:协议显式配置优先,否则取首个启用 TLS 且有证书
  的域名(10 秒缓存),新域名一键导入证书后自动切换
- 更新 README 与界面文案(去掉“重启服务生效”提示)
2026-08-16 23:39:43 -04:00
kevin cead42fd69 Merge pull request 'fix: 外发优先 IPv4 + smarthost 中继 + 地址族配置(补 PR #1 合并后遗漏的提交)' (#3) from dsh/mailgo:outbound-ipv4-relay into main
Reviewed-on: kevin/mailgo#3
2026-08-16 00:13:00 -04:00
dsh 7ce8751f46 feat: 出站地址族可配置(ip_family=ipv4/ipv6/auto)+ 源地址绑定(source_ip)
- ip_family 默认 ipv4(保持 PTR/SPF 最可靠的路径);运营商为静态 IPv6
  配置 PTR 后可切换 ipv6
- source_ip 绑定出站源地址,避免内核使用轮换的 IPv6 临时隐私地址
  (临时地址无 PTR,Gmail 等会拒收)
- 无 MX 回退时按地址族偏好排序 A/AAAA
2026-08-16 00:09:53 -04:00
dsh 76c98c94f3 fix: relay_starttls 默认值始终为 true(未显式配置时),避免中继密码明文传输 2026-08-16 00:09:53 -04:00
dsh 8fb7aef052 fix: 外发优先走 IPv4(Gmail 拒收无 PTR 的 IPv6);新增 smarthost 中继支持
- 出站 SMTP 连接强制 IPv4(tcp4),无 MX 回退时 IPv4 优先:
  住宅/动态 IP 的 IPv6 临时地址通常无 PTR,Gmail 会以 5.7.25 拒收,
  而 IPv4 一般具备正反向一致的 PTR(实测 Gmail 250 OK)
- [outbound] 新增 relay_host/relay_port/relay_user/relay_password/
  relay_starttls:配置后所有外部投递经智能主机中继(AUTH PLAIN、
  465 隐式 TLS / 其他端口 STARTTLS),解决服务器 IP 被 Spamhaus PBL
  收录时 Outlook/Hotmail 拒收的问题
- 新增 smarthost 中继单元测试
2026-08-16 00:09:53 -04:00
kevin c40113d3bf Merge pull request 'fix: SMTP 收件邮件的附件未保存(Web 邮箱看不到附件)' (#2) from dsh/mailgo:fix/smtp-received-attachments into main
Reviewed-on: kevin/mailgo#2
2026-08-15 23:55:09 -04:00
dsh 31e5f94be6 fix: SMTP 收件邮件的附件未保存(Web 邮箱看不到附件)
- parseSMTPMessage 之前只提取附件元数据、丢弃内容;现在保留原始字节
- saveMessage 创建邮件后落盘附件文件(storage.Save)、写 attachments 记录
  并计入用户配额 used_bytes,Web 邮箱可正常列表/下载
- 新增单元测试:附件字节提取 + 附件落盘/记录/配额校验
- 已在生产环境实测:带附件邮件经 :25 入站后,附件文件、记录、配额、
  Web 下载(/attachment/:id)全部正确
2026-08-15 23:53:50 -04:00
kevin 79220b816d Merge pull request 'feat: 实现对外邮件投递(外发队列 + MX 直投 + DKIM 签名 + 退信 + 管理后台)' (#1) from dsh/mailgo:outbound-delivery into main
Reviewed-on: kevin/mailgo#1
2026-08-15 23:38:46 -04:00
88 changed files with 9845 additions and 870 deletions
+1
View File
@@ -40,3 +40,4 @@ win/srv/
# 临时测试文件
login_test.html
migrate
+112 -5
View File
@@ -1,18 +1,34 @@
# MailGo
Go 语言编写的轻量级邮件系统,集成 SMTP / IMAP / POP3 协议服务和 Web 管理界面。
Web 前端采用 QQ 邮箱风格的布局:顶部导航 + 左侧文件夹栏 + 邮件列表三栏设计。
## 功能特性
- **邮件协议**:SMTP(发送)、IMAP(同步)、POP3(收取),均支持 TLS 加密
- **外部投递**:认证用户可向外部邮箱(QQ/Gmail/Outlook 等)发送邮件,内置外发队列、MX 直投、STARTTLS、指数退避重试、退信通知与 DKIM 签名
- **Web 邮箱**收件箱、已发送、草稿箱、富文本编辑(Quill.js)、附件上传/下载
- **外部投递**:认证用户可向外部邮箱(QQ/Gmail/Outlook 等)发送邮件,内置外发队列、**并发 worker 池投递**(默认 4 线程 + 每收件域并发上限 2)、MX 直投、STARTTLS、指数退避重试、退信通知与 DKIM 签名
- **Web 邮箱**QQ 邮箱风格界面,支持收件箱 / 已发送 / 草稿箱、未读角标与搜索过滤、全选 / 批量删除、发件人头像、富文本编辑(Quill.js)、附件上传/下载
- **管理后台**:域名管理、用户管理、DKIM 密钥自动生成、DNS 配置提示、全量邮件查看、外发队列管理、IP 封禁管理、仪表盘统计
- **协议调用日志**SMTP / IMAP / POP3 每次连接自动记录来源 IP、用户名、成功/失败、失败原因与操作摘要,可按协议/状态/IP/用户名/时间筛选,用于分析密码爆破、中继滥用等攻击行为(默认保留 30 天,自动清理)
- **IMAP 新邮件推送**:本地投递(SMTP/Web 写信)成功后实时推送,挂起 IDLE 的客户端即时收到新邮件通知(无需轮询);其他客户端造成的已读/星标/删除变化也实时同步(IMAP STORE/EXPUNGE、POP3 删除、Web 标已读/删除)
- **当前连接监控**:管理后台实时查看 SMTP/IMAP/POP3 活动连接(来源 IP、用户名、TLS、时长),每 5 秒自动刷新;支持「断开并封禁」一键封禁该 IP 全部在线连接(封禁 180 天,可随时解封)
- **外部认证**OAuth2Google / GitHub)、LDAP(可选,默认关闭)
- **安全机制**:BCrypt 密码哈希、登录失败自动封禁 IP、外发频率限制(防滥用)、非认证禁止中继(防开放中继)、管理员可解封
- **安全机制**:BCrypt 密码哈希、登录失败自动封禁 IP(**阶段性封禁**:前 3 次达到失败阈值只计数,第 4 次起封禁并按档位递增 30分钟 → 3小时 → 3个月 → 半年,成功登录清零)、外发频率限制(防滥用)、非认证禁止中继(防开放中继)、管理员可解封
- **多数据库**:默认 SQLite,可切换 MySQL
- **跨平台**Linux 生产部署 + Windows 本地调试
## 界面预览
| 收件箱 | 邮件阅读 |
|--------|----------|
| ![收件箱](docs/screenshots/inbox.png) | ![邮件阅读](docs/screenshots/view.png) |
| 写信 | 设置 | 登录 |
|------|------|------|
| ![写信](docs/screenshots/compose.png) | ![设置](docs/screenshots/settings.png) | ![登录](docs/screenshots/login.png) |
> 截图使用演示数据渲染,实际界面以部署为准。
## 快速开始
### 编译
@@ -69,6 +85,13 @@ attach_dir = "/srv/mail_go/attachments" # 附件存储目录
[web]
addr = ":8080" # 监听地址,支持 TCP 端口或 Unix socket
secret_key = "" # Web 会话签名密钥;留空时首次启动自动生成
# 随机密钥并写入本文件(请妥善备份,泄露/丢失
# 分别意味着会话可被伪造/所有登录态失效)
cookie_secure = true # 会话 cookie 仅通过 HTTPS 传输(Secure 标志);
# 仅本地 HTTP 调试时才改为 false
protocol_log_keep_days = 30 # SMTP/IMAP/POP3 协议调用日志保留天数,
# 超出后由后台任务自动清理;0 表示不清理
[smtp]
addr = ":25" # SMTP 明文端口
@@ -106,17 +129,39 @@ ldap_use_tls = false
[ban]
max_fail_attempts = 5 # 登录失败次数阈值
ban_duration_min = 30 # 封禁时长(分钟)
ban_duration_min = 30 # 第 1 次封禁时长(分钟);之后按档位递增:
# 第 2 次 3 小时 → 第 3 次 3 个月 → 第 4 次起半年(上限)
# 前 3 次达到阈值只计数不封禁,成功登录后清零
[caddy]
data_dir = "" # Caddy 数据目录(含 certificates/ 的那个),
# 供后台一键导入证书;留空自动探测
# /var/lib/caddy/.local/share/caddy 等常见位置
[outbound]
hostname = "" # EHLO 主机名,留空使用 [smtp] domain
poll_interval = 15 # 外发队列扫描间隔(秒)
workers = 4 # 并发投递 worker 数(多线程并行发送,
# 大量邮件时吞吐提升;0/1 为串行)
batch_size = 50 # 每次扫描最多取出的待投递邮件数
max_concurrent_per_domain = 2 # 同一收件域(或中继)的最大并发连接数,
# 防被判定为滥发;0 表示不限制
max_attempts = 12 # 单封邮件最大投递尝试次数
retry_base_min = 5 # 重试退避基数(分钟),指数增长:5/10/20/40...
max_recipients = 50 # 单封邮件最大外部收件人数
max_per_min = 30 # 每用户每分钟最大外发数
max_per_day = 500 # 每用户每日最大外发数,0 表示禁用外部投递
connect_timeout = 30 # 连接远程 MX 超时(秒)
relay_host = "" # 智能主机(smarthost),留空则直投 MX
relay_port = 587 # 465 = 隐式 TLS,其他端口按需 STARTTLS
relay_user = "" # 中继认证用户名(AUTH PLAIN
relay_password = "" # 中继认证密码
relay_starttls = true # 非 465 端口是否使用 STARTTLS
relay_tls_insecure = false # 是否跳过中继服务器 TLS 证书验证;
# 默认验证证书(保护中继凭据),仅自签证书
# 内网中继且明确知晓风险时才改为 true
ip_family = "ipv4" # 出站地址族:ipv4(默认)| ipv6 | auto
source_ip = "" # 出站源地址绑定(如静态 IPv6 地址),留空由内核选择
```
---
@@ -150,6 +195,16 @@ addr = "/run/mail_go/web.sock"
`addr``/` 开头时,Gin 自动以 Unix socket 方式监听。
### 3. 指定会话密钥(容器/多实例部署)
会话签名密钥可通过环境变量 `MAILGO_SECRET_KEY` 覆盖(优先于配置文件,且不会写入磁盘):
```bash
MAILGO_SECRET_KEY="$(openssl rand -hex 32)" mail_go
```
要求:长度至少 16 字节;留空时由配置文件提供(首次启动自动生成)。更换密钥后所有已登录会话立即失效,用户需重新登录。
Nginx 反向代理配置:
```nginx
@@ -200,6 +255,32 @@ tls_key = "/etc/mail_go/certs/server.key"
> # 私钥路径: /etc/letsencrypt/live/mail.example.com/privkey.pem
> ```
#### 从 Caddy 一键导入证书
如果本机已用 [Caddy](https://caddyserver.com/) 托管该域名 HTTPS(Caddy 会自动签发并续期证书),
可在管理后台 **域名管理 → 编辑域名** 页面点击 **“从 Caddy 获取证书”** 按钮,
一键把 Caddy 存储中的证书与私钥导入邮件服务(自动启用该域名的 TLS),无需手动复制 PEM 文件。
支持通配符证书(如 `*.example.com` 可匹配 `mail.example.com`)。
证书支持**热加载**:导入(或手动上传)后立即生效,无需重启服务——SMTP/IMAP/POP3
每次 TLS 握手会自动检查并重载变化的证书文件。
由于 Caddy 的证书目录仅 `caddy` 用户可读,install.sh 会安装一个 root 权限的证书同步任务
`mailgo-caddy-sync.{path,timer}`),把 Caddy 证书树镜像到 `/srv/mail_go/tls/caddy`
证书续期后自动同步,mail_go 始终可读;另外还会授予 ACL 权限作为直接读取的兜底。
安装时自动配置,也可手动执行:
```bash
sudo ./install.sh setup-caddy-cert # 自动探测 Caddy 数据目录并配置同步 + ACL
sudo ./install.sh setup-caddy-cert /path/to/caddy/data # 或手动指定数据目录
```
若 Caddy 数据目录不在常见位置,可在配置文件中显式指定:
```toml
[caddy]
data_dir = "/var/lib/caddy/.local/share/caddy"
```
### 4. 启用 OAuth2 登录(Google 示例)
```toml
@@ -258,6 +339,30 @@ max_per_day = 500 # 设为 0 可完全禁用外部投递
每用户每分钟/每日外发数受限;失败邮件会退信到发件人收件箱;
管理员可在后台「外发队列」查看投递状态、手动重试或取消。
> **IPv4/IPv6**:默认仅使用 IPv4 出站(`ip_family = "ipv4"`),因为很多收件方
> (如 Gmail)会拒收没有 PTR 的 IPv6 地址,而 IPv4 通常具备正反向一致的 PTR。
> 如需走 IPv6:请运营商为静态地址配置 PTR(指向 `mail.example.com`),
> 然后设置 `ip_family = "ipv6"` 并把 `source_ip` 绑定到该静态地址
> (避免内核使用轮换的临时隐私地址)。
### 7. 通过智能主机(smarthost)中继外发
服务器 IP 属于家庭宽带/动态 IP 段时,常被 Spamhaus PBL 等策略列表收录,
MicrosoftOutlook/Hotmail)等收件方会直接拒收。此时建议把外发邮件交给
第三方 SMTP 中继(Mailgun / SendGrid / Amazon SES / 阿里云邮件推送等),
`[outbound]` 中配置即可,所有外部投递自动改走中继:
```toml
[outbound]
relay_host = "smtp.example-relay.com"
relay_port = 587 # 465 为隐式 TLS
relay_user = "your-api-user"
relay_password = "your-api-key"
relay_starttls = true
```
中继使用 AUTH PLAIN 认证;本地收件人仍走本地投递,不受影响。
---
## 端口速查
@@ -290,7 +395,8 @@ mailgo/
│ │ ├── domain_store.go # 域名数据操作
│ │ ├── attachment_store.go # 附件数据操作
│ │ ├── outbound_store.go # 外发队列数据操作
│ │ ── ban_store.go # 封禁数据操作
│ │ ── ban_store.go # 封禁数据操作
│ │ └── protocol_log_store.go # 协议调用日志数据操作
│ ├── smtp_server/server.go # SMTP 服务
│ ├── outbound/
│ │ ├── mailer.go # MX 查询与 SMTP 出站客户端
@@ -300,6 +406,7 @@ mailgo/
│ │ ├── server.go # IMAP 服务
│ │ └── backend.go # IMAP 后端
│ ├── pop3_server/server.go # POP3 服务
│ ├── connhub/hub.go # 协议连接注册中心(当前连接监控)
│ ├── storage/attachment.go # 附件文件存储
│ ├── dkim/keys.go # DKIM 密钥生成
│ ├── auth/
+260
View File
@@ -0,0 +1,260 @@
// migrate 一次性工具:把 SQLite 数据迁移到 MySQLmailgo 库)。
// 用法:go run ./cmd/migrate -from /srv/mail_go/mail.db -dsn "mailgo:密码@tcp(127.0.0.1:3306)/mailgo?charset=utf8mb4&parseTime=True&loc=UTC"
package main
import (
"flag"
"fmt"
"log"
"time"
"mail_go/config"
"mail_go/internal/db"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var (
fromDSN = flag.String("from", "/srv/mail_go/mail.db", "SQLite 数据库路径")
mysqlDSN = flag.String("dsn", "", "MySQL DSN(目标库,需已创建 mailgo 库与用户)")
)
func main() {
flag.Parse()
if *mysqlDSN == "" {
log.Fatal("缺少 -dsn")
}
// 目标:MySQLInitDB 内含 AutoMigrate,按当前模型建表)
mdb, err := db.InitDB(config.DatabaseConfig{Driver: "mysql", DSN: *mysqlDSN}, config.StorageConfig{BaseDir: "/srv/mail_go/"})
if err != nil {
log.Fatalf("连接 MySQL 失败: %v", err)
}
log.Println("MySQL 建表完成(AutoMigrate")
// 源:SQLite(只读)
sdb, err := db.InitDB(config.DatabaseConfig{Driver: "sqlite", DSN: *fromDSN}, config.StorageConfig{BaseDir: "/srv/mail_go/"})
if err != nil {
log.Fatalf("连接 SQLite 失败: %v", err)
}
sdb.Logger = logger.Default.LogMode(logger.Silent)
// 关闭 GORM 自动时间戳(保留原始 CreatedAt/UpdatedAt
mw := mdb.Session(&gorm.Session{SkipHooks: true})
stateWant := int64(-1) // mailbox_states 期望行数;-1 = 源库无此表不校验
// 按外键依赖顺序复制:domains → users → messages → attachments → 其余
// 所有时间统一 UTCMySQL DATETIME 无时区)。
utc := func(t time.Time) time.Time {
if t.IsZero() {
// MySQL DATETIME 最小年份 1000;零值由调用方转 NULL
return t
}
return t.UTC()
}
_ = utc
// ---- domains ----
var domains []db.Domain
if err := sdb.Order("id").Find(&domains).Error; err != nil {
log.Fatalf("读 domains: %v", err)
}
for i := range domains {
domains[i].CreatedAt = domains[i].CreatedAt.UTC()
domains[i].UpdatedAt = domains[i].UpdatedAt.UTC()
}
if err := mw.Create(&domains).Error; err != nil {
log.Fatalf("写 domains: %v", err)
}
log.Printf("domains: %d", len(domains))
// ---- users ----
var users []db.User
if err := sdb.Order("id").Find(&users).Error; err != nil {
log.Fatalf("读 users: %v", err)
}
for i := range users {
users[i].CreatedAt = users[i].CreatedAt.UTC()
users[i].UpdatedAt = users[i].UpdatedAt.UTC()
}
if err := mw.Create(&users).Error; err != nil {
log.Fatalf("写 users: %v", err)
}
log.Printf("users: %d", len(users))
// ---- messages ----
var msgs []db.Message
if err := sdb.Order("id").Find(&msgs).Error; err != nil {
log.Fatalf("读 messages: %v", err)
}
for i := range msgs {
msgs[i].Date = msgs[i].Date.UTC()
msgs[i].CreatedAt = msgs[i].CreatedAt.UTC()
}
if err := mw.Create(&msgs).Error; err != nil {
log.Fatalf("写 messages: %v", err)
}
log.Printf("messages: %d", len(msgs))
// ---- attachments ----
var atts []db.Attachment
if err := sdb.Order("id").Find(&atts).Error; err != nil {
log.Fatalf("读 attachments: %v", err)
}
for i := range atts {
atts[i].CreatedAt = atts[i].CreatedAt.UTC()
}
if err := mw.Create(&atts).Error; err != nil {
log.Fatalf("写 attachments: %v", err)
}
log.Printf("attachments: %d", len(atts))
// ---- outbound_messages(原样,含时间转 UTC----
var outs []db.OutboundMessage
if err := sdb.Order("id").Find(&outs).Error; err != nil {
log.Fatalf("读 outbound_messages: %v", err)
}
for i := range outs {
outs[i].NextAttemptAt = outs[i].NextAttemptAt.UTC()
if outs[i].CompletedAt != nil && !outs[i].CompletedAt.IsZero() {
u := outs[i].CompletedAt.UTC()
outs[i].CompletedAt = &u
}
outs[i].CreatedAt = outs[i].CreatedAt.UTC()
outs[i].UpdatedAt = outs[i].UpdatedAt.UTC()
}
if err := mw.Create(&outs).Error; err != nil {
log.Fatalf("写 outbound_messages: %v", err)
}
log.Printf("outbound_messages: %d", len(outs))
// ---- ban_entriesexpires_at 零值 → NULL----
rows, err := sdb.Raw("SELECT id, ip_address, reason, fail_count, ban_count, expires_at, created_at, updated_at FROM ban_entries ORDER BY id").Rows()
if err != nil {
log.Fatalf("读 ban_entries: %v", err)
}
defer rows.Close()
bans := 0
for rows.Next() {
var (
id uint
ip string
reason *string
failCount int
banCount int
expires *time.Time
created *time.Time
updated *time.Time
)
if err := rows.Scan(&id, &ip, &reason, &failCount, &banCount, &expires, &created, &updated); err != nil {
log.Fatalf("扫 ban_entries: %v", err)
}
norm := func(t *time.Time) *time.Time {
if t == nil || t.IsZero() {
return nil
}
u := t.UTC()
return &u
}
if err := mdb.Exec("INSERT INTO ban_entries (id, ip_address, reason, fail_count, ban_count, expires_at, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?)",
id, ip, reason, failCount, banCount, norm(expires), norm(created), norm(updated)).Error; err != nil {
log.Fatalf("写 ban_entries id=%d: %v", id, err)
}
bans++
}
log.Printf("ban_entries: %d", bans)
// ---- protocol_logs ----
var logs []db.ProtocolLog
if err := sdb.Order("id").Find(&logs).Error; err != nil {
log.Fatalf("读 protocol_logs: %v", err)
}
for i := range logs {
logs[i].CreatedAt = logs[i].CreatedAt.UTC()
}
if err := mw.Create(&logs).Error; err != nil {
log.Fatalf("写 protocol_logs: %v", err)
}
log.Printf("protocol_logs: %d", len(logs))
// ---- mailbox_states(原生 SQL:该表随 UIDVALIDITY 特性存在,旧版本源库可能没有)----
var stateCount int64
sdb.Raw("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='mailbox_states'").Scan(&stateCount)
if stateCount > 0 {
// 目标库若没有该表(上游模型未含 MailboxState 时 AutoMigrate 不会建),先建表
var tcnt int64
mdb.Raw("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'mailbox_states'").Scan(&tcnt)
if tcnt == 0 {
if err := mdb.Exec(`CREATE TABLE mailbox_states (
user_id bigint unsigned NOT NULL,
folder varchar(64) NOT NULL,
uid_validity bigint unsigned NOT NULL,
created_at datetime(3) NULL,
updated_at datetime(3) NULL,
PRIMARY KEY (user_id, folder))`).Error; err != nil {
log.Fatalf("建 mailbox_states 表: %v", err)
}
log.Println("mailbox_states: 目标库已建表")
}
srows, err := sdb.Raw("SELECT user_id, folder, uid_validity, created_at, updated_at FROM mailbox_states ORDER BY user_id, folder").Rows()
if err != nil {
log.Fatalf("读 mailbox_states: %v", err)
}
defer srows.Close()
states := 0
for srows.Next() {
var (
userID uint
folder string
validity uint32
created *time.Time
updated *time.Time
)
if err := srows.Scan(&userID, &folder, &validity, &created, &updated); err != nil {
log.Fatalf("扫 mailbox_states: %v", err)
}
norm := func(t *time.Time) *time.Time {
if t == nil || t.IsZero() {
return nil
}
u := t.UTC()
return &u
}
if err := mdb.Exec("INSERT INTO mailbox_states (user_id, folder, uid_validity, created_at, updated_at) VALUES (?,?,?,?,?)",
userID, folder, validity, norm(created), norm(updated)).Error; err != nil {
log.Fatalf("写 mailbox_states: %v", err)
}
states++
}
log.Printf("mailbox_states: %d", states)
stateWant = int64(states)
} else {
log.Println("mailbox_states: 源库无此表,跳过")
}
// ---- 校验 ----
check := func(table string, want int64) {
var got int64
if err := mdb.Table(table).Count(&got).Error; err != nil {
log.Fatalf("校验 %s: %v", table, err)
}
if got != want {
log.Fatalf("校验 %s 失败: got %d want %d", table, got, want)
}
fmt.Printf("校验 %s: %d/%d ✓\n", table, got, want)
}
check("domains", int64(len(domains)))
check("users", int64(len(users)))
check("messages", int64(len(msgs)))
check("attachments", int64(len(atts)))
check("outbound_messages", int64(len(outs)))
check("ban_entries", int64(bans))
check("protocol_logs", int64(len(logs)))
if stateWant >= 0 {
check("mailbox_states", stateWant)
}
log.Println("迁移完成 ✅")
}
+188 -11
View File
@@ -1,10 +1,14 @@
package config
import (
"crypto/rand"
"encoding/hex"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/BurntSushi/toml"
)
@@ -24,8 +28,32 @@ type StorageConfig struct {
// WebConfig holds web server settings.
type WebConfig struct {
Addr string `toml:"addr"`
// SecretKey 是 Web 会话 cookie 的签名密钥。留空时首次启动自动生成
// 随机密钥并持久化到配置文件;也可通过环境变量 MAILGO_SECRET_KEY
// 覆盖(覆盖值不落盘,适合容器部署)。
SecretKey string `toml:"secret_key"`
// CookieSecure 控制会话 cookie 是否仅通过 HTTPS 传输(Secure 标志)。
// 默认 true;仅当应用直接以 HTTP 提供服务(本地调试、内网明文)时
// 才应改为 false。
CookieSecure bool `toml:"cookie_secure"`
// ProtocolLogKeepDays SMTP/IMAP/POP3 协议调用日志保留天数,
// 超过该天数的记录会被后台任务自动清理。
ProtocolLogKeepDays int `toml:"protocol_log_keep_days"`
}
// SecretKeyEnvVar 是覆盖会话签名密钥的环境变量名。
const SecretKeyEnvVar = "MAILGO_SECRET_KEY"
// InsecureLegacySecretKey 是旧版本硬编码在源码中的会话签名密钥。
// 源码公开意味着该密钥完全不可信,任何出现都必须替换。
const InsecureLegacySecretKey = "mail-go-secret-key-change-in-production"
// MinSecretKeyLen 是允许的最短会话密钥长度(字节)。
const MinSecretKeyLen = 16
// secretKeyRandomBytes 是自动生成密钥的随机字节数(hex 编码后 64 字符)。
const secretKeyRandomBytes = 32
// SMTPConfig holds SMTP server settings.
type SMTPConfig struct {
Addr string `toml:"addr"`
@@ -78,6 +106,15 @@ type BanConfig struct {
BanDurationMin int `toml:"ban_duration_min"` // Default: 30 (minutes)
}
// CaddyConfig holds settings for importing TLS certificates from a local Caddy.
type CaddyConfig struct {
// DataDir is the Caddy data directory (the one containing the
// "certificates/" subdirectory), used by the one-click certificate
// import in the admin panel. Leave empty to auto-detect common
// locations such as /var/lib/caddy/.local/share/caddy.
DataDir string `toml:"data_dir"`
}
// OutboundConfig holds outbound (external) mail delivery settings.
type OutboundConfig struct {
Hostname string `toml:"hostname"` // EHLO 主机名,留空使用 [smtp] domain
@@ -88,6 +125,32 @@ type OutboundConfig struct {
MaxPerMin int `toml:"max_per_min"` // 每用户每分钟最大外发数
MaxPerDay int `toml:"max_per_day"` // 每用户每日最大外发数,0 表示禁用外部投递
ConnectTimeout int `toml:"connect_timeout"` // 连接远程 MX 超时(秒)
// Workers 并发投递 worker 数:多 goroutine 并行发送队列中的邮件。
// 0 或 1 表示串行(旧行为)。
Workers int `toml:"workers"`
// BatchSize 每次扫描最多取出的待投递邮件数。
BatchSize int `toml:"batch_size"`
// MaxConcurrentPerDomain 同一收件域(或中继)的最大并发连接数,
// 防止对单个 MX 域并发过多而被判定为滥发;0 表示不限制。
MaxConcurrentPerDomain int `toml:"max_concurrent_per_domain"`
// Smarthost relay: when relay_host is non-empty, all external mail is
// delivered through this relay instead of direct MX delivery. Useful when
// the server IP is listed in PBL/blocklists (residential/dynamic IPs).
RelayHost string `toml:"relay_host"` // 中继服务器地址,留空则直投 MX
RelayPort int `toml:"relay_port"` // 465 = 隐式 TLS,其他端口先尝试 STARTTLS
RelayUser string `toml:"relay_user"` // 中继认证用户名(AUTH PLAIN
RelayPassword string `toml:"relay_password"` // 中继认证密码
RelayStartTLS bool `toml:"relay_starttls"` // 非 465 端口是否使用 STARTTLS
// RelayTLSInsecure 是否跳过中继服务器的 TLS 证书验证。
// 默认 false(验证证书),避免凭据被中间人截获;仅当使用自签证书的
// 内网中继且明确知晓风险时才设为 true。
RelayTLSInsecure bool `toml:"relay_tls_insecure"`
// IP family and source address binding for outbound connections.
IPFamily string `toml:"ip_family"` // ipv4(默认,PTR/SPF 最可靠)| ipv6 | auto
SourceIP string `toml:"source_ip"` // 出站源地址绑定(如静态 IPv6),留空由内核选择
}
// Config is the top-level configuration structure.
@@ -100,6 +163,7 @@ type Config struct {
POP3 POP3Config `toml:"pop3"`
Auth AuthConfig `toml:"auth"`
Ban BanConfig `toml:"ban"`
Caddy CaddyConfig `toml:"caddy"`
Outbound OutboundConfig `toml:"outbound"`
}
@@ -145,7 +209,9 @@ func defaultConfig() *Config {
AttachDir: filepath.Join(bd, "attachments"),
},
Web: WebConfig{
Addr: DefaultWebPort,
Addr: DefaultWebPort,
CookieSecure: true,
ProtocolLogKeepDays: DefaultProtocolLogKeepDays,
},
SMTP: SMTPConfig{
Addr: fmt.Sprintf(":%d", DefaultSMTPPort),
@@ -170,14 +236,22 @@ func defaultConfig() *Config {
MaxFailAttempts: 5,
BanDurationMin: 30,
},
// Caddy: 留空则自动探测常见数据目录,无需配置
Caddy: CaddyConfig{},
Outbound: OutboundConfig{
PollInterval: 15, // 15 秒扫描一次队列
MaxAttempts: 12, // 最多尝试 12 次
RetryBaseMin: 5, // 5/10/20/40/... 分钟指数退避
MaxRecipients: 50, // 单封最多 50 个外部收件人
MaxPerMin: 30, // 每用户每分钟 30 封
MaxPerDay: 500,
ConnectTimeout: 30, // 连接远程 MX 超时 30 秒
PollInterval: 15, // 15 秒扫描一次队列
MaxAttempts: 12, // 最多尝试 12 次
RetryBaseMin: 5, // 5/10/20/40/... 分钟指数退避
MaxRecipients: 50, // 单封最多 50 个外部收件人
MaxPerMin: 30, // 每用户每分钟 30 封
MaxPerDay: 500,
ConnectTimeout: 30, // 连接远程 MX 超时 30 秒
RelayPort: 587, // smarthost 默认提交端口
RelayStartTLS: true,
IPFamily: "ipv4",
Workers: DefaultOutboundWorkers,
BatchSize: DefaultOutboundBatchSize,
MaxConcurrentPerDomain: DefaultMaxConcurrentPerDomain,
},
}
}
@@ -204,6 +278,9 @@ func mergeDefaults(cfg *Config, defaults *Config) *Config {
if cfg.Web.Addr == "" {
cfg.Web.Addr = defaults.Web.Addr
}
if cfg.Web.ProtocolLogKeepDays == 0 {
cfg.Web.ProtocolLogKeepDays = defaults.Web.ProtocolLogKeepDays
}
if cfg.SMTP.Addr == "" {
cfg.SMTP.Addr = defaults.SMTP.Addr
}
@@ -260,9 +337,81 @@ func mergeDefaults(cfg *Config, defaults *Config) *Config {
if cfg.Outbound.ConnectTimeout == 0 {
cfg.Outbound.ConnectTimeout = defaults.Outbound.ConnectTimeout
}
if cfg.Outbound.Workers == 0 {
cfg.Outbound.Workers = defaults.Outbound.Workers
}
if cfg.Outbound.BatchSize == 0 {
cfg.Outbound.BatchSize = defaults.Outbound.BatchSize
}
if cfg.Outbound.MaxConcurrentPerDomain == 0 {
cfg.Outbound.MaxConcurrentPerDomain = defaults.Outbound.MaxConcurrentPerDomain
}
if cfg.Outbound.RelayPort == 0 {
cfg.Outbound.RelayPort = defaults.Outbound.RelayPort
}
if cfg.Outbound.IPFamily == "" {
cfg.Outbound.IPFamily = defaults.Outbound.IPFamily
}
return cfg
}
// generateSecretKey generates a cryptographically random session key,
// hex-encoded (64 characters for 32 random bytes).
func generateSecretKey() (string, error) {
buf := make([]byte, secretKeyRandomBytes)
if _, err := rand.Read(buf); err != nil {
return "", fmt.Errorf("生成随机会话密钥失败: %w", err)
}
return hex.EncodeToString(buf), nil
}
// ensureSecretKey guarantees that cfg holds a trustworthy session key:
// an empty value or the known insecure legacy default is replaced with a
// freshly generated random key. The caller is responsible for persisting
// the updated config.
func ensureSecretKey(cfg *Config) error {
if cfg.Web.SecretKey != "" && cfg.Web.SecretKey != InsecureLegacySecretKey {
return nil
}
key, err := generateSecretKey()
if err != nil {
return err
}
if cfg.Web.SecretKey == InsecureLegacySecretKey {
log.Printf("检测到不安全的旧默认会话密钥,已自动更换为随机密钥(所有已登录会话将失效)")
} else {
log.Printf("已生成随机会话密钥并写入配置文件(Web 会话签名密钥,请妥善备份)")
}
cfg.Web.SecretKey = key
return nil
}
// applySecretKeyEnv lets the MAILGO_SECRET_KEY environment variable
// override the key loaded from the config file. The override value is
// never persisted to disk.
func applySecretKeyEnv(cfg *Config) *Config {
if env := os.Getenv(SecretKeyEnvVar); env != "" {
cfg.Web.SecretKey = env
}
return cfg
}
// ValidateSecretKey rejects session signing keys that are missing, too
// short, or the known insecure legacy default hardcoded in old versions.
func ValidateSecretKey(key string) error {
if key == "" {
return fmt.Errorf("Web 会话密钥为空,拒绝启动:请检查配置文件 [web].secret_key 或环境变量 %s", SecretKeyEnvVar)
}
if key == InsecureLegacySecretKey {
return fmt.Errorf("Web 会话密钥为已知不安全的旧默认值,拒绝启动:请删除配置文件 [web].secret_key 后重启以自动生成随机密钥")
}
if len(key) < MinSecretKeyLen {
return fmt.Errorf("Web 会话密钥过短(%d 字节,最少 %d):请检查 %s 或 [web].secret_key",
len(key), MinSecretKeyLen, SecretKeyEnvVar)
}
return nil
}
// writeConfig writes the configuration to the given file path.
// It creates the parent directories if they don't exist.
func writeConfig(path string, cfg *Config) error {
@@ -281,6 +430,11 @@ func writeConfig(path string, cfg *Config) error {
if err := enc.Encode(cfg); err != nil {
return fmt.Errorf("写入配置文件失败: %w", err)
}
// 配置文件包含会话密钥、中继密码等敏感信息,收紧为仅属主可读写
if err := os.Chmod(path, 0600); err != nil {
return fmt.Errorf("设置配置文件权限失败 %s: %w", path, err)
}
return nil
}
@@ -288,15 +442,23 @@ func writeConfig(path string, cfg *Config) error {
// If the configuration file does not exist, it creates one with default values.
// If the file exists but has missing fields, they are filled with defaults and the file is updated.
func LoadConfig() (*Config, error) {
path := configFilePath()
return loadConfigFrom(configFilePath())
}
// loadConfigFrom implements LoadConfig against an explicit file path so it
// can be unit-tested with temporary directories.
func loadConfigFrom(path string) (*Config, error) {
defaults := defaultConfig()
// If config file doesn't exist, create it with defaults
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := ensureSecretKey(defaults); err != nil {
return nil, err
}
if mkErr := writeConfig(path, defaults); mkErr != nil {
return nil, mkErr
}
return defaults, nil
return applySecretKeyEnv(defaults), nil
}
// Read existing config file
@@ -310,6 +472,21 @@ func LoadConfig() (*Config, error) {
return nil, fmt.Errorf("解析配置文件失败: %w", err)
}
// relay_starttls 与 web.cookie_secure 默认值为 true for safety;
// the raw file is checked because TOML decoding cannot distinguish
// an absent bool from false.
if !strings.Contains(string(data), "relay_starttls") {
cfg.Outbound.RelayStartTLS = defaults.Outbound.RelayStartTLS
}
if !strings.Contains(string(data), "cookie_secure") {
cfg.Web.CookieSecure = defaults.Web.CookieSecure
}
// 会话密钥缺失或不安全时补发随机密钥(随下面的写回一并落盘)
if err := ensureSecretKey(cfg); err != nil {
return nil, err
}
// Merge defaults for any missing fields
merged := mergeDefaults(cfg, defaults)
@@ -319,5 +496,5 @@ func LoadConfig() (*Config, error) {
return nil, writeErr
}
return merged, nil
return applySecretKeyEnv(merged), nil
}
+191
View File
@@ -0,0 +1,191 @@
package config
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestGenerateSecretKey(t *testing.T) {
key, err := generateSecretKey()
if err != nil {
t.Fatalf("generateSecretKey() error: %v", err)
}
// 32 随机字节 hex 编码 = 64 字符
if len(key) != secretKeyRandomBytes*2 {
t.Fatalf("key length = %d, want %d", len(key), secretKeyRandomBytes*2)
}
// 两次生成必须不同
key2, err := generateSecretKey()
if err != nil {
t.Fatalf("generateSecretKey() error: %v", err)
}
if key == key2 {
t.Fatal("generated keys must be unique")
}
if key == InsecureLegacySecretKey {
t.Fatal("generated key must never equal the legacy insecure default")
}
}
func TestLoadConfigFirstBootGeneratesSecretKey(t *testing.T) {
path := filepath.Join(t.TempDir(), "mail_go.toml")
cfg, err := loadConfigFrom(path)
if err != nil {
t.Fatalf("loadConfigFrom() error: %v", err)
}
if cfg.Web.SecretKey == "" {
t.Fatal("secret key should be generated on first boot")
}
// 密钥必须落盘,保证重启后会话不失效
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read config file: %v", err)
}
if !strings.Contains(string(data), "secret_key = \""+cfg.Web.SecretKey+"\"") {
t.Fatalf("generated secret key should be persisted, file content:\n%s", data)
}
// 第二次加载返回相同密钥(会话保持)
cfg2, err := loadConfigFrom(path)
if err != nil {
t.Fatalf("second loadConfigFrom() error: %v", err)
}
if cfg2.Web.SecretKey != cfg.Web.SecretKey {
t.Fatalf("secret key must be stable across restarts: %q != %q", cfg2.Web.SecretKey, cfg.Web.SecretKey)
}
// 配置文件包含敏感信息,权限必须为 0600(Windows 无 POSIX 权限,跳过)
if runtime.GOOS != "windows" {
info, err := os.Stat(path)
if err != nil {
t.Fatalf("stat config file: %v", err)
}
if perm := info.Mode().Perm(); perm != 0600 {
t.Fatalf("config file mode = %o, want 0600", perm)
}
}
}
func TestLoadConfigBackfillsSecretKey(t *testing.T) {
path := filepath.Join(t.TempDir(), "mail_go.toml")
// 模拟旧版本升级:配置文件中没有 secret_key 字段
old := "[web]\naddr = \":9090\"\n\n[smtp]\ndomain = \"example.com\"\n"
if err := os.WriteFile(path, []byte(old), 0644); err != nil {
t.Fatal(err)
}
cfg, err := loadConfigFrom(path)
if err != nil {
t.Fatalf("loadConfigFrom() error: %v", err)
}
if cfg.Web.SecretKey == "" {
t.Fatal("missing secret key should be backfilled")
}
// 原有字段保持不变
if cfg.Web.Addr != ":9090" {
t.Fatalf("existing field overwritten: addr = %q", cfg.Web.Addr)
}
if cfg.SMTP.Domain != "example.com" {
t.Fatalf("existing field overwritten: domain = %q", cfg.SMTP.Domain)
}
}
func TestLoadConfigReplacesLegacySecretKey(t *testing.T) {
path := filepath.Join(t.TempDir(), "mail_go.toml")
content := "[web]\naddr = \":8080\"\nsecret_key = \"" + InsecureLegacySecretKey + "\"\n"
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
cfg, err := loadConfigFrom(path)
if err != nil {
t.Fatalf("loadConfigFrom() error: %v", err)
}
if cfg.Web.SecretKey == InsecureLegacySecretKey {
t.Fatal("legacy insecure secret key must be replaced")
}
if cfg.Web.SecretKey == "" {
t.Fatal("replacement key must be non-empty")
}
// 替换后的密钥必须落盘
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(data), InsecureLegacySecretKey) {
t.Fatal("legacy key should be removed from the config file")
}
if !strings.Contains(string(data), cfg.Web.SecretKey) {
t.Fatal("replaced key should be persisted")
}
}
func TestSecretKeyEnvOverride(t *testing.T) {
path := filepath.Join(t.TempDir(), "mail_go.toml")
// 先正常生成一个落盘密钥
cfg1, err := loadConfigFrom(path)
if err != nil {
t.Fatalf("first loadConfigFrom() error: %v", err)
}
// 环境变量覆盖运行时密钥
envKey := "env-override-secret-key-0123456789abcdef"
t.Setenv(SecretKeyEnvVar, envKey)
cfg2, err := loadConfigFrom(path)
if err != nil {
t.Fatalf("second loadConfigFrom() error: %v", err)
}
if cfg2.Web.SecretKey != envKey {
t.Fatalf("env var should override the file key: got %q", cfg2.Web.SecretKey)
}
// 环境变量的值不能落盘
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(data), envKey) {
t.Fatal("env-provided secret must not be persisted to disk")
}
// 落盘密钥保持原值
if !strings.Contains(string(data), cfg1.Web.SecretKey) {
t.Fatal("file key should remain unchanged when env override is active")
}
}
func TestValidateSecretKey(t *testing.T) {
cases := []struct {
name string
key string
wantErr bool
}{
{"empty", "", true},
{"legacy default", InsecureLegacySecretKey, true},
{"too short", "short", true},
{"valid hex key", "0123456789abcdef0123456789abcdef", false},
{"valid env style", "env-override-secret-key-0123456789abcdef", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := ValidateSecretKey(tc.key)
if tc.wantErr && err == nil {
t.Fatalf("expected error for key %q", tc.key)
}
if !tc.wantErr && err != nil {
t.Fatalf("unexpected error for key %q: %v", tc.key, err)
}
})
}
}
+13
View File
@@ -36,5 +36,18 @@ const (
DefaultQuotaBytes int64 = 5 * 1024 * 1024 * 1024 // 5GB
)
// DefaultProtocolLogKeepDays 是 SMTP/IMAP/POP3 协议调用日志的默认保留天数。
const DefaultProtocolLogKeepDays = 30
// Outbound delivery concurrency defaults.
const (
// DefaultOutboundWorkers 并发投递 worker 数(0/1 为串行)。
DefaultOutboundWorkers = 4
// DefaultOutboundBatchSize 每次扫描最多取出的待投递邮件数。
DefaultOutboundBatchSize = 50
// DefaultMaxConcurrentPerDomain 同一收件域的最大并发连接数。
DefaultMaxConcurrentPerDomain = 2
)
// ConfigFileName is the name of the configuration file
const ConfigFileName = "mail_go.toml"
Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

+1 -1
View File
@@ -13,6 +13,7 @@ require (
github.com/gin-gonic/gin v1.12.0
github.com/go-ldap/ldap/v3 v3.4.13
github.com/google/uuid v1.6.0
github.com/gorilla/securecookie v1.1.2
golang.org/x/crypto v0.48.0
golang.org/x/oauth2 v0.36.0
golang.org/x/text v0.35.0
@@ -38,7 +39,6 @@ require (
github.com/goccy/go-json v0.10.5 // indirect
github.com/goccy/go-yaml v1.19.2 // indirect
github.com/gorilla/context v1.1.2 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.4.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
+177 -13
View File
@@ -214,6 +214,20 @@ do_install() {
# 4. 拉取代码并编译
build_binary
# 4.5 配置 Caddy 证书同步(若检测到 Caddy),供后台一键导入使用
local caddy_data
caddy_data="$(find_caddy_data_dir 2>/dev/null || true)"
if [[ -n "${caddy_data}" ]]; then
info "检测到 Caddy${caddy_data}),配置证书同步任务 ..."
if install_caddy_sync "${caddy_data}"; then
ok "Caddy 证书同步已配置(后台可一键导入证书)"
else
warn "Caddy 证书同步配置失败,可稍后手动执行: sudo $0 setup-caddy-cert"
fi
# ACL 兜底(失败不影响使用)
command -v setfacl &>/dev/null && setup_caddy_acls "${caddy_data}" || true
fi
# 5. 部署文件
deploy_files
@@ -250,6 +264,153 @@ do_install() {
warn "⚠ 请登录后立即修改默认密码!"
}
# ======================== Caddy 证书同步 ========================
# mail_go 后台有一键“从 Caddy 获取证书”功能:从 Caddy 的证书存储读取
# 域名证书与私钥并导入邮件服务。Caddy 证书目录默认仅 caddy 用户可读
# (0700/0600),且证书续期后文件会被替换(权限重置),因此安装一个
# root 权限的 systemd path/timer 同步任务,把 Caddy 证书树镜像到
# mail_go 可读的 <storage>/tls/caddy 目录,续期后自动更新;
# 同时授予 ACL 作为直接读取的兜底。用法: sudo ./install.sh setup-caddy-cert [caddy数据目录]
# 探测 Caddy 数据目录(包含 certificates/ 子目录的那个目录)
find_caddy_data_dir() {
local candidates=(
"/var/lib/caddy/.local/share/caddy"
"/root/.local/share/caddy"
"/home/caddy/.local/share/caddy"
)
local d
for d in "${candidates[@]}"; do
if [[ -d "${d}/certificates" ]]; then
echo "${d}"
return 0
fi
done
# systemd 服务可能配置了自定义 HOME
local home
home=$(systemctl show caddy -p Environment --value 2>/dev/null | grep -oP '(?<=HOME=)[^ ]+' || true)
if [[ -n "${home}" && -d "${home}/.local/share/caddy/certificates" ]]; then
echo "${home}/.local/share/caddy"
return 0
fi
return 1
}
# 用 ACL 授予 mail_go 用户读取 Caddy 证书的权限(幂等,兜底用:
# 续期后 caddy 以 0600 重建文件,ACL 可能失效,靠同步任务保障)
setup_caddy_acls() {
local data_dir="${1:-$(find_caddy_data_dir 2>/dev/null || true)}"
[[ -n "${data_dir}" ]] || return 1
local certs_dir="${data_dir}/certificates"
[[ -d "${certs_dir}" ]] || return 1
command -v setfacl &>/dev/null || return 1
# 各级父目录需要 x(遍历)权限
local p="${data_dir}"
while [[ "${p}" != "/" ]]; do
setfacl -m "u:${SERVICE_USER}:x" "${p}" 2>/dev/null
p="$(dirname "${p}")"
done
setfacl -R -m "u:${SERVICE_USER}:rX" "${certs_dir}" 2>/dev/null
setfacl -R -m "d:u:${SERVICE_USER}:rX" "${certs_dir}" 2>/dev/null
return 0
}
# 安装证书同步脚本 + systemd path/timer 单元,并立即同步一次
install_caddy_sync() {
local data_dir="$1"
local sync_script="/usr/local/sbin/mailgo-caddy-cert-sync.sh"
local sync_dir="${DATA_DIR}/tls/caddy"
local certs_dir="${data_dir}/certificates"
# 同步脚本(把数据目录固化进去)
cat > "${sync_script}" <<EOF
#!/usr/bin/env bash
# MailGo - 将 Caddy 证书存储镜像到 mail_go 可读目录(root 运行,
# 由 mailgo-caddy-sync.{path,timer} 触发),供后台一键导入使用。
set -euo pipefail
SRC="${certs_dir}"
SYNC="${sync_dir}"
[[ -d "\${SRC}" ]] || exit 0
mkdir -p "\${SYNC}"
rm -rf "\${SYNC}/.certs.tmp"
cp -a "\${SRC}" "\${SYNC}/.certs.tmp"
chown -R "${SERVICE_USER}:${SERVICE_USER}" "\${SYNC}/.certs.tmp"
chmod -R u+rwX,go-rwx "\${SYNC}/.certs.tmp"
rm -rf "\${SYNC}/certificates"
mv "\${SYNC}/.certs.tmp" "\${SYNC}/certificates"
EOF
chmod 700 "${sync_script}"
cat > /etc/systemd/system/mailgo-caddy-sync.service <<EOF
[Unit]
Description=MailGo - 同步 Caddy 证书到 mail_go TLS 目录
After=network.target
[Service]
Type=oneshot
ExecStart=${sync_script}
EOF
cat > /etc/systemd/system/mailgo-caddy-sync.path <<EOF
[Unit]
Description=MailGo - 监视 Caddy 证书目录变化并触发同步
[Path]
PathChanged=${certs_dir}
PathChanged=${certs_dir}/*/*
Unit=mailgo-caddy-sync.service
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/mailgo-caddy-sync.timer <<EOF
[Unit]
Description=MailGo - 定期同步 Caddy 证书(开机 + 每日兜底)
[Timer]
OnBootSec=1min
OnUnitActiveSec=1d
Unit=mailgo-caddy-sync.service
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable --now mailgo-caddy-sync.path mailgo-caddy-sync.timer >/dev/null
systemctl start mailgo-caddy-sync.service
return 0
}
do_setup_caddy_cert() {
check_root
local data_dir="${2:-}"
if [[ -z "${data_dir}" ]]; then
data_dir="$(find_caddy_data_dir || true)"
if [[ -z "${data_dir}" ]]; then
error "未检测到 Caddy 数据目录(/var/lib/caddy 等),请手动指定: sudo $0 setup-caddy-cert <caddy数据目录>"
fi
info "检测到 Caddy 数据目录: ${data_dir}"
elif [[ ! -d "${data_dir}/certificates" ]]; then
error "目录 ${data_dir} 下未找到 certificates/ 子目录,请确认传入的是 Caddy 数据目录"
fi
info "安装证书同步任务(systemd path + timer,续期后自动同步)..."
install_caddy_sync "${data_dir}"
ok "证书同步任务已安装并完成首次同步"
if command -v setfacl &>/dev/null && setup_caddy_acls "${data_dir}"; then
ok "已授予 ${SERVICE_USER} 用户直接读取 Caddy 证书的 ACL 权限(兜底)"
else
warn "未配置 ACL 兜底(不影响使用,同步镜像始终可读)"
fi
ok "现在可在管理后台“编辑域名”页点击“从 Caddy 获取证书”一键导入"
}
# ======================== 卸载 ========================
do_uninstall() {
info "========== 卸载 ${SERVICE_NAME} =========="
@@ -352,21 +513,24 @@ do_status() {
# ======================== 入口 ========================
case "${1:-}" in
install) do_install ;;
uninstall) do_uninstall ;;
start) do_start ;;
stop) do_stop ;;
restart) do_restart ;;
status) do_status ;;
install) do_install ;;
uninstall) do_uninstall ;;
start) do_start ;;
stop) do_stop ;;
restart) do_restart ;;
status) do_status ;;
setup-caddy-cert) do_setup_caddy_cert ;;
*)
echo "用法: sudo $0 {install|uninstall|start|stop|restart|status}"
echo "用法: sudo $0 {install|uninstall|start|stop|restart|status|setup-caddy-cert}"
echo ""
echo " install — 完整安装/更新(拉代码+编译+部署+启动+开机自启)"
echo " uninstall — 卸载服务(可选保留数据)"
echo " start — 启动服务"
echo " stop — 停止服务"
echo " restart — 重启服务"
echo " status — 查看服务状态"
echo " install — 完整安装/更新(拉代码+编译+部署+启动+开机自启)"
echo " uninstall — 卸载服务(可选保留数据)"
echo " start — 启动服务"
echo " stop — 停止服务"
echo " restart — 重启服务"
echo " status — 查看服务状态"
echo " setup-caddy-cert — 授予 mail_go 读取本机 Caddy 证书的 ACL 权限"
echo " (后台“从 Caddy 获取证书”按钮的前置条件)"
exit 1
;;
esac
+243
View File
@@ -0,0 +1,243 @@
// Package caddycert 从本机 Caddy 的证书存储中查找并读取某个域名
// 的 TLS 证书与私钥,供 MailGo 一键导入使用。
//
// Caddycertmagic)将 ACME 证书保存在其数据目录下的
//
// <data>/certificates/<CA 目录>/<域名>/<域名>.crt
// <data>/certificates/<CA 目录>/<域名>/<域名>.key
//
// 数据目录默认是 $HOME/.local/share/caddysystemd 服务通常是
// /var/lib/caddy/.local/share/caddy),可通过配置 caddy.data_dir 覆盖。
package caddycert
import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
"os/user"
"path/filepath"
"sort"
"strings"
"time"
)
// DefaultDataDirs 是未显式配置时依次探测的 Caddy 数据目录。
var DefaultDataDirs = []string{
"/var/lib/caddy/.local/share/caddy", // Debian/Ubuntu 软件包的 systemd 服务默认 HOME
"/root/.local/share/caddy", // 直接以 root 运行的 caddy
"/home/caddy/.local/share/caddy",
}
// Cert 是从 Caddy 存储中找到的一对证书与私钥(PEM 编码)。
type Cert struct {
CertPEM []byte // 证书链(含叶子证书)
KeyPEM []byte // 私钥
Source string // 来源 .crt 文件的绝对路径
}
// Fetch 在给定的 Caddy 证书数据目录中查找 domain 的证书与私钥。
//
// dataDirs 按优先级从高到低排列,每个目录都是包含 certificates/ 子目录的
// Caddy 数据目录(如 /var/lib/caddy/.local/share/caddy,或 mail_go 的同步
// 镜像目录 /srv/mail_go/tls/caddy);空字符串项被忽略。dataDirs 为空时仅
// 探测 DefaultDataDirs 及当前进程用户的数据目录。
//
// 返回的证书保证:能组成有效的密钥对、尚未过期、且证书 SAN 覆盖 domain
// (支持通配符证书,例如 *.example.com 的证书可匹配 mail.example.com)。
func Fetch(domain string, dataDirs []string) (*Cert, error) {
domain = strings.ToLower(strings.TrimSpace(domain))
if domain == "" {
return nil, fmt.Errorf("域名为空")
}
roots := dataRoots(dataDirs)
var (
permDenied []string
seen []string // 找到同名/相关文件但证书无效的来源
)
for _, root := range roots {
cert, found, invalid, err := searchRoot(domain, root)
if err != nil {
if os.IsPermission(err) {
permDenied = append(permDenied, root)
continue
}
continue
}
if found {
return cert, nil
}
seen = append(seen, invalid...)
}
msg := fmt.Sprintf("在 Caddy 证书存储中未找到域名 %q 的证书(请确认 Caddy 已为该域名签发证书)", domain)
if len(seen) > 0 {
msg += fmt.Sprintf(";发现相关文件但证书无效/已过期/不匹配域名: %s", strings.Join(seen, "、"))
}
if len(permDenied) > 0 {
msg += fmt.Sprintf(";另有目录因权限不足未能检查: %s,可运行 install.sh 的 setup-caddy-cert 授予 %s 用户读取权限",
strings.Join(permDenied, "、"), currentUsername())
}
return nil, fmt.Errorf("%s", msg)
}
// dataRoots 返回要探测的候选数据目录列表(去重,保留优先级顺序)。
func dataRoots(dataDirs []string) []string {
var roots []string
seen := map[string]bool{}
add := func(p string) {
p = strings.TrimRight(filepath.Clean(p), string(filepath.Separator))
if p == "" || seen[p] {
return
}
seen[p] = true
roots = append(roots, p)
}
for _, d := range dataDirs {
add(d)
}
for _, d := range DefaultDataDirs {
add(d)
}
if home, err := os.UserHomeDir(); err == nil && home != "" {
add(filepath.Join(home, ".local", "share", "caddy"))
}
return roots
}
// searchRoot 在单个数据目录中查找 domain 的证书。
// 返回 (证书, 是否找到, 找到但无效的来源列表, 错误)。
func searchRoot(domain, root string) (*Cert, bool, []string, error) {
// 允许把 certificates/ 目录本身当作 data_dir 传入
certsDir := root
if filepath.Base(certsDir) != "certificates" {
certsDir = filepath.Join(root, "certificates")
}
info, err := os.Stat(certsDir)
if err != nil {
if os.IsNotExist(err) {
return nil, false, nil, nil
}
return nil, false, nil, err
}
if !info.IsDir() {
return nil, false, nil, nil
}
caDirs, err := os.ReadDir(certsDir)
if err != nil {
return nil, false, nil, err
}
var invalid []string
// 1) 直接路径: certificates/<CA>/<domain>/<domain>.crt|.key
for _, ca := range caDirs {
if !ca.IsDir() {
continue
}
cert, found, bad, err := readDomainDir(filepath.Join(certsDir, ca.Name(), domain), domain)
if err != nil {
return nil, false, nil, err
}
if found {
return cert, true, nil, nil
}
invalid = append(invalid, bad...)
}
// 2) 全量扫描,处理通配符证书(如 *.example.com 目录)等情况
for _, ca := range caDirs {
if !ca.IsDir() {
continue
}
caPath := filepath.Join(certsDir, ca.Name())
domDirs, err := os.ReadDir(caPath)
if err != nil {
return nil, false, nil, err
}
for _, d := range domDirs {
if !d.IsDir() {
continue
}
cert, found, bad, err := readDomainDir(filepath.Join(caPath, d.Name()), domain)
if err != nil {
return nil, false, nil, err
}
if found {
return cert, true, nil, nil
}
invalid = append(invalid, bad...)
}
}
sort.Strings(invalid)
return nil, false, invalid, nil
}
// readDomainDir 读取 Caddy 某个域名目录下的 <name>.crt 与 <name>.key
// 校验其是否为 domain 的有效证书。bad 返回“存在但无效”的来源路径。
func readDomainDir(dirPath, domain string) (*Cert, bool, []string, error) {
name := filepath.Base(dirPath)
certPath := filepath.Join(dirPath, name+".crt")
keyPath := filepath.Join(dirPath, name+".key")
certPEM, err := os.ReadFile(certPath)
if err != nil {
if os.IsNotExist(err) {
return nil, false, nil, nil
}
return nil, false, nil, err
}
keyPEM, err := os.ReadFile(keyPath)
if err != nil {
if os.IsNotExist(err) {
return nil, false, nil, nil
}
return nil, false, nil, err
}
// 只有与目标域名相关的目录才值得报“无效”,否则静默跳过
related := strings.TrimSuffix(name, "."+domain) == domain ||
name == domain || strings.HasPrefix(name, "*.") && strings.HasSuffix(domain, name[1:])
if !validPair(certPEM, keyPEM, domain) {
if related {
return nil, false, []string{certPath}, nil
}
return nil, false, nil, nil
}
return &Cert{CertPEM: certPEM, KeyPEM: keyPEM, Source: certPath}, true, nil, nil
}
// validPair 校验证书/私钥是否组成有效密钥对、未过期且 SAN 覆盖 domain。
func validPair(certPEM, keyPEM []byte, domain string) bool {
pair, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
return false
}
if len(pair.Certificate) == 0 {
return false
}
leaf, err := x509.ParseCertificate(pair.Certificate[0])
if err != nil {
return false
}
if time.Now().After(leaf.NotAfter) {
return false
}
return leaf.VerifyHostname(domain) == nil
}
// currentUsername 返回当前进程的运行用户(错误提示用)。
func currentUsername() string {
if u, err := user.Current(); err == nil && u.Username != "" {
return u.Username
}
return os.Getenv("USER")
}
+176
View File
@@ -0,0 +1,176 @@
package caddycert
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// makeCert 生成一份自签名证书(含指定 SAN),返回 PEM 编码的证书与私钥。
func makeCert(t *testing.T, dnsNames []string, notBefore, notAfter time.Time) (certPEM, keyPEM []byte) {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("生成私钥失败: %v", err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: dnsNames[0]},
DNSNames: dnsNames,
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatalf("生成证书失败: %v", err)
}
certPEM = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
keyPEM = pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
return certPEM, keyPEM
}
// writeSite 在 Caddy 风格目录结构中写入某个域名的证书。
func writeSite(t *testing.T, dataDir, domain string, certPEM, keyPEM []byte) {
t.Helper()
dir := filepath.Join(dataDir, "certificates", "acme-v02.api.letsencrypt.org-directory", domain)
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatalf("创建目录失败: %v", err)
}
if err := os.WriteFile(filepath.Join(dir, domain+".crt"), certPEM, 0600); err != nil {
t.Fatalf("写入证书失败: %v", err)
}
if err := os.WriteFile(filepath.Join(dir, domain+".key"), keyPEM, 0600); err != nil {
t.Fatalf("写入私钥失败: %v", err)
}
}
func TestFetchExactDomain(t *testing.T) {
dataDir := t.TempDir()
certPEM, keyPEM := makeCert(t, []string{"mail.example.com"}, time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour))
writeSite(t, dataDir, "mail.example.com", certPEM, keyPEM)
got, err := Fetch("mail.example.com", []string{dataDir})
if err != nil {
t.Fatalf("Fetch 失败: %v", err)
}
if string(got.CertPEM) != string(certPEM) {
t.Error("返回的证书与写入的不一致")
}
if string(got.KeyPEM) != string(keyPEM) {
t.Error("返回的私钥与写入的不一致")
}
}
func TestFetchWildcardCoversSubdomain(t *testing.T) {
dataDir := t.TempDir()
certPEM, keyPEM := makeCert(t, []string{"*.example.com", "example.com"}, time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour))
writeSite(t, dataDir, "*.example.com", certPEM, keyPEM)
got, err := Fetch("mail.example.com", []string{dataDir})
if err != nil {
t.Fatalf("通配符证书应覆盖子域名,Fetch 失败: %v", err)
}
if got.Source == "" {
t.Error("Source 不应为空")
}
}
func TestFetchSkipsExpiredCert(t *testing.T) {
dataDir := t.TempDir()
certPEM, keyPEM := makeCert(t, []string{"mail.example.com"}, time.Now().Add(-48*time.Hour), time.Now().Add(-24*time.Hour))
writeSite(t, dataDir, "mail.example.com", certPEM, keyPEM)
_, err := Fetch("mail.example.com", []string{dataDir})
if err == nil {
t.Fatal("过期证书不应被返回")
}
if !strings.Contains(err.Error(), "无效") {
t.Errorf("错误信息应说明证书无效,实际: %v", err)
}
}
func TestFetchNotExist(t *testing.T) {
dataDir := t.TempDir()
_, err := Fetch("nobody.example.com", []string{dataDir})
if err == nil {
t.Fatal("不存在的域名应返回错误")
}
if !strings.Contains(err.Error(), "未找到") {
t.Errorf("错误信息应包含“未找到”,实际: %v", err)
}
}
func TestFetchUppercaseDomainIsLowercased(t *testing.T) {
dataDir := t.TempDir()
certPEM, keyPEM := makeCert(t, []string{"mail.example.com"}, time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour))
writeSite(t, dataDir, "mail.example.com", certPEM, keyPEM)
if _, err := Fetch("MAIL.Example.COM", []string{dataDir}); err != nil {
t.Fatalf("域名大小写应被归一化,Fetch 失败: %v", err)
}
}
func TestFetchCertificatesDirAsDataDir(t *testing.T) {
dataDir := t.TempDir()
certPEM, keyPEM := makeCert(t, []string{"mail.example.com"}, time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour))
writeSite(t, dataDir, "mail.example.com", certPEM, keyPEM)
// 把 certificates 目录本身当作 data_dir 传入
certsDir := filepath.Join(dataDir, "certificates")
if _, err := Fetch("mail.example.com", []string{certsDir}); err != nil {
t.Fatalf("data_dir 直接指向 certificates 目录时应可用: %v", err)
}
}
func TestFetchPrefersFirstDataDir(t *testing.T) {
// 模拟“同步镜像目录优先”:两个目录都有该域名证书时,应返回第一个的
dirA := t.TempDir()
dirB := t.TempDir()
certA, keyA := makeCert(t, []string{"mail.example.com"}, time.Now().Add(-time.Hour), time.Now().Add(48*time.Hour))
certB, keyB := makeCert(t, []string{"mail.example.com"}, time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour))
writeSite(t, dirA, "mail.example.com", certA, keyA)
writeSite(t, dirB, "mail.example.com", certB, keyB)
got, err := Fetch("mail.example.com", []string{dirA, dirB})
if err != nil {
t.Fatalf("Fetch 失败: %v", err)
}
if string(got.CertPEM) != string(certA) {
t.Error("应按优先级返回第一个目录中的证书")
}
}
func TestFetchPermissionDeniedHint(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("root 用户不受文件权限限制,跳过")
}
dataDir := t.TempDir()
certPEM, keyPEM := makeCert(t, []string{"mail.example.com"}, time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour))
writeSite(t, dataDir, "mail.example.com", certPEM, keyPEM)
if err := os.Chmod(dataDir, 0000); err != nil {
t.Fatalf("chmod 失败: %v", err)
}
defer os.Chmod(dataDir, 0700)
_, err := Fetch("mail.example.com", []string{dataDir})
if err == nil {
t.Fatal("无权限时应返回错误")
}
if !strings.Contains(err.Error(), "权限不足") {
t.Errorf("错误信息应提示权限不足,实际: %v", err)
}
}
+198
View File
@@ -0,0 +1,198 @@
// Package connhub 提供邮件协议(SMTP/IMAP/POP3)当前活动连接的注册中心,
// 供管理后台实时查看连接情况(来源 IP、用户、TLS、时长等)。
package connhub
import (
"sort"
"sync"
"time"
)
// Conn 表示一个活动中的协议连接。字段由 Hub 的锁保护。
type Conn struct {
ID uint64 // 自增序号
Protocol string // smtp | imap | pop3
IP string
Port int
User string // 认证后填充
TLS bool
Connected time.Time
LastActive time.Time
hub *Hub
// disconnect 强制断开底层连接的回调(由各协议服务器注册)。
// 关闭底层 socket 后协议服务器会正常走收尾清理(Logout/注销)。
disconnect func()
}
// Hub 管理所有活动连接(同一把锁保护注册表与连接字段)。
type Hub struct {
mu sync.Mutex
seq uint64
conns map[uint64]*Conn
}
// New 创建连接注册中心。
func New() *Hub {
return &Hub{conns: make(map[uint64]*Conn)}
}
// Register 注册一个新连接并返回其句柄;调用方在连接结束时调用 Close()。
func (h *Hub) Register(protocol, ip string, port int, tls bool) *Conn {
if h == nil {
return nil
}
h.mu.Lock()
defer h.mu.Unlock()
h.seq++
now := time.Now()
c := &Conn{
ID: h.seq,
Protocol: protocol,
IP: ip,
Port: port,
TLS: tls,
Connected: now,
LastActive: now,
hub: h,
}
h.conns[c.ID] = c
return c
}
// SetUser 记录认证成功的用户名(邮箱)并刷新最后活跃时间。
func (c *Conn) SetUser(u string) {
if c == nil || c.hub == nil {
return
}
c.hub.mu.Lock()
c.User = u
c.LastActive = time.Now()
c.hub.mu.Unlock()
}
// SetTLS 更新连接的 TLS 状态(如 POP3 STLS 升级之后)。
func (c *Conn) SetTLS(on bool) {
if c == nil || c.hub == nil {
return
}
c.hub.mu.Lock()
c.TLS = on
c.hub.mu.Unlock()
}
// Touch 刷新最后活跃时间。
func (c *Conn) Touch() {
if c == nil || c.hub == nil {
return
}
c.hub.mu.Lock()
c.LastActive = time.Now()
c.hub.mu.Unlock()
}
// SetDisconnect 注册强制断开底层连接的回调(管理后台「断开并封禁」用)。
func (c *Conn) SetDisconnect(fn func()) {
if c == nil || c.hub == nil {
return
}
c.hub.mu.Lock()
c.disconnect = fn
c.hub.mu.Unlock()
}
// Close 从注册中心移除该连接。
func (c *Conn) Close() {
if c == nil || c.hub == nil {
return
}
c.hub.mu.Lock()
delete(c.hub.conns, c.ID)
c.hub.mu.Unlock()
}
// Get 按 ID 查找活动连接。
func (h *Hub) Get(id uint64) (*Conn, bool) {
if h == nil {
return nil, false
}
h.mu.Lock()
defer h.mu.Unlock()
c, ok := h.conns[id]
return c, ok
}
// Disconnect 强制断开指定连接(关闭底层连接并注销)。
func (h *Hub) Disconnect(id uint64) bool {
c, ok := h.Get(id)
if !ok {
return false
}
h.mu.Lock()
fn := c.disconnect
h.mu.Unlock()
if fn != nil {
fn()
}
return true
}
// DisconnectByIP 强制断开该 IP 的全部连接,返回断开的连接数。
// 用于封禁 IP 后立即踢掉其所有在线会话。
func (h *Hub) DisconnectByIP(ip string) int {
if h == nil || ip == "" {
return 0
}
h.mu.Lock()
var fns []func()
for _, c := range h.conns {
if c.IP == ip && c.disconnect != nil {
fns = append(fns, c.disconnect)
}
}
h.mu.Unlock()
for _, fn := range fns {
fn()
}
return len(fns)
}
// List 返回当前所有活动连接(拷贝),按连接时间升序。
func (h *Hub) List() []Conn {
if h == nil {
return nil
}
h.mu.Lock()
out := make([]Conn, 0, len(h.conns))
for _, c := range h.conns {
out = append(out, Conn{
ID: c.ID,
Protocol: c.Protocol,
IP: c.IP,
Port: c.Port,
User: c.User,
TLS: c.TLS,
Connected: c.Connected,
LastActive: c.LastActive,
})
}
h.mu.Unlock()
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
return out
}
// Counts 返回按协议分组的当前连接数。
func (h *Hub) Counts() map[string]int {
if h == nil {
return nil
}
h.mu.Lock()
defer h.mu.Unlock()
counts := make(map[string]int)
for _, c := range h.conns {
counts[c.Protocol]++
}
return counts
}
+177
View File
@@ -0,0 +1,177 @@
package connhub
import (
"sync"
"sync/atomic"
"testing"
"time"
)
func TestRegisterListCountClose(t *testing.T) {
h := New()
c1 := h.Register("smtp", "10.0.0.1", 25, false)
c2 := h.Register("imap", "10.0.0.2", 993, true)
c3 := h.Register("pop3", "10.0.0.3", 110, false)
if n := len(h.List()); n != 3 {
t.Fatalf("list len = %d, want 3", n)
}
counts := h.Counts()
if counts["smtp"] != 1 || counts["imap"] != 1 || counts["pop3"] != 1 {
t.Fatalf("counts = %v", counts)
}
c2.SetUser("alice@example.com")
c1.SetTLS(true)
time.Sleep(time.Millisecond)
c3.Touch()
// 用户名 / TLS 状态生效
var imapUser string
for _, c := range h.List() {
if c.Protocol == "imap" {
imapUser = c.User
}
if c.Protocol == "smtp" && !c.TLS {
t.Fatal("smtp conn should be TLS after SetTLS(true)")
}
if c.Protocol == "pop3" && !c.LastActive.After(c.Connected) {
t.Fatal("pop3 conn LastActive should be after Connected after Touch")
}
}
if imapUser != "alice@example.com" {
t.Fatalf("imap user = %q", imapUser)
}
c1.Close()
c2.Close()
if n := len(h.List()); n != 1 {
t.Fatalf("after close, len = %d, want 1", n)
}
if h.Counts()["imap"] != 0 {
t.Fatalf("imap count after close = %d, want 0", h.Counts()["imap"])
}
}
func TestNilHubSafe(t *testing.T) {
var h *Hub
if c := h.Register("smtp", "1.2.3.4", 25, false); c != nil {
t.Fatal("nil hub Register must return nil")
}
if h.List() != nil || h.Counts() != nil {
t.Fatal("nil hub List/Counts must be nil")
}
var c *Conn
c.SetUser("x") // 不应 panic
c.Touch()
c.SetTLS(true)
c.Close()
}
func TestConcurrentRegisterClose(t *testing.T) {
h := New()
const n = 50
var wg sync.WaitGroup
conns := make([]*Conn, n)
for i := 0; i < n; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
conns[i] = h.Register("smtp", "10.0.0.1", 25, false)
conns[i].SetUser("u")
conns[i].Touch()
}(i)
}
wg.Wait()
if len(h.List()) != n {
t.Fatalf("len = %d, want %d", len(h.List()), n)
}
for i := 0; i < n; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
conns[i].Close()
}(i)
}
wg.Wait()
if len(h.List()) != 0 {
t.Fatalf("after concurrent close, len = %d, want 0", len(h.List()))
}
}
func TestListOrderedByID(t *testing.T) {
h := New()
h.Register("smtp", "1.1.1.1", 25, false)
h.Register("imap", "2.2.2.2", 143, false)
h.Register("pop3", "3.3.3.3", 110, false)
list := h.List()
for i := 1; i < len(list); i++ {
if list[i].ID <= list[i-1].ID {
t.Fatalf("list not ordered by ID: %+v", list)
}
}
}
// TestDisconnect 验证强制断开回调被调用。
func TestDisconnect(t *testing.T) {
h := New()
var closed atomic.Int32
c1 := h.Register("smtp", "10.0.0.1", 25, false)
c1.SetDisconnect(func() { closed.Add(1) })
c2 := h.Register("imap", "10.0.0.2", 993, true)
c2.SetDisconnect(func() { closed.Add(1) })
if !h.Disconnect(c1.ID) {
t.Fatal("Disconnect should report success")
}
if closed.Load() != 1 {
t.Fatalf("closed = %d, want 1", closed.Load())
}
// 已断开(未注销)仍可查到
if _, ok := h.Get(c1.ID); !ok {
t.Fatal("conn should still be registered until Close")
}
// 不存在的 ID
if h.Disconnect(99999) {
t.Fatal("Disconnect of unknown id must fail")
}
}
// TestDisconnectByIP 验证封禁时断开该 IP 全部连接。
func TestDisconnectByIP(t *testing.T) {
h := New()
var closed atomic.Int32
// 同一 IP 三个协议连接
for _, proto := range []string{"smtp", "imap", "pop3"} {
c := h.Register(proto, "203.0.113.5", 25, false)
c.SetDisconnect(func() { closed.Add(1) })
}
// 另一 IP 不受影响
other := h.Register("smtp", "203.0.113.6", 25, false)
other.SetDisconnect(func() { closed.Add(1) })
n := h.DisconnectByIP("203.0.113.5")
if n != 3 {
t.Fatalf("disconnected = %d, want 3", n)
}
if closed.Load() != 3 {
t.Fatalf("closed = %d, want 3", closed.Load())
}
}
// TestDisconnectNoCallback 验证未注册断开回调的连接安全跳过。
func TestDisconnectNoCallback(t *testing.T) {
h := New()
c := h.Register("pop3", "10.0.0.9", 110, false)
if !h.Disconnect(c.ID) {
t.Fatal("Disconnect should report success even without callback")
}
if n := h.DisconnectByIP("10.0.0.9"); n != 0 {
t.Fatalf("disconnected = %d, want 0", n)
}
}
+11 -1
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"mail_go/config"
@@ -31,6 +32,15 @@ func InitDB(cfg config.DatabaseConfig, storageCfg config.StorageConfig) (*gorm.D
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("创建数据库目录失败 %s: %w", dir, err)
}
// 多连接并发(SMTP/IMAP/POP3/Web/外发 worker/推送)下:
// - WAL 模式:读不阻塞写,消除瞬时 SQLITE_BUSY 导致写失败被吞
// - busy_timeout=5000ms:写竞争时等待而非立刻失败
// - synchronous=NORMALWAL 下安全且写入更快
sep := "?"
if strings.Contains(dsn, "?") {
sep = "&"
}
dsn = dsn + sep + "_busy_timeout=5000&_journal_mode=WAL&_synchronous=NORMAL"
dialector = sqlite.Open(dsn)
case "mysql":
dialector = mysql.Open(cfg.DSN)
@@ -46,7 +56,7 @@ func InitDB(cfg config.DatabaseConfig, storageCfg config.StorageConfig) (*gorm.D
}
// Auto-migrate all models
if err := db.AutoMigrate(&User{}, &Domain{}, &Message{}, &Attachment{}, &BanEntry{}, &OutboundMessage{}); err != nil {
if err := db.AutoMigrate(&User{}, &Domain{}, &Message{}, &Attachment{}, &BanEntry{}, &OutboundMessage{}, &ProtocolLog{}); err != nil {
return nil, fmt.Errorf("数据库迁移失败: %w", err)
}
+41 -5
View File
@@ -15,8 +15,11 @@ type User struct {
UsedBytes int64 `gorm:"default:0" json:"used_bytes"`
IsActive bool `gorm:"default:true" json:"is_active"`
IsAdmin bool `gorm:"default:false" json:"is_admin"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// MustChangePassword 为 true 时该用户(通常是初始管理员或被重置密码的
// 用户)在首次登录后必须修改密码。
MustChangePassword bool `gorm:"default:false" json:"-"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName specifies the table name for User.
@@ -57,8 +60,8 @@ type Message struct {
ToAddr string `gorm:"size:2048;not null" json:"to_addr"`
CcAddr string `gorm:"size:2048" json:"cc_addr"`
Subject string `gorm:"size:1024" json:"subject"`
TextBody string `gorm:"type:text" json:"text_body"`
HtmlBody string `gorm:"type:text" json:"html_body"`
TextBody string `gorm:"type:mediumtext" json:"text_body"`
HtmlBody string `gorm:"type:mediumtext" json:"html_body"`
RawData string `gorm:"type:mediumtext" json:"raw_data"`
IsRead bool `gorm:"default:false" json:"is_read"`
IsFlagged bool `gorm:"default:false" json:"is_flagged"`
@@ -111,6 +114,10 @@ type BanEntry struct {
IPAddress string `gorm:"size:45;index;not null" json:"ip_address"`
Reason string `gorm:"size:255" json:"reason"`
FailCount int `gorm:"default:0" json:"fail_count"`
// BanCount 是该 IP 累计达到失败阈值的次数(含未封禁的前几次)。
// 阶段封禁依据:前 3 次只计数不封禁,第 4 次起按档位递增时长。
// 成功登录或管理员解封会删除记录,次数随之清零。
BanCount int `gorm:"default:0" json:"ban_count"`
ExpiresAt time.Time `gorm:"index" json:"expires_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
@@ -119,11 +126,40 @@ type BanEntry struct {
// TableName specifies the table name for BanEntry.
func (BanEntry) TableName() string { return "ban_entries" }
// Protocol log statuses.
const (
ProtocolSMTP = "smtp"
ProtocolIMAP = "imap"
ProtocolPOP3 = "pop3"
)
// ProtocolLog records one SMTP/IMAP/POP3 connection session: auth result,
// failure reason and source IP, for admin analysis of attacks/abuse.
type ProtocolLog struct {
ID uint `gorm:"primaryKey" json:"id"`
Protocol string `gorm:"size:16;index;not null" json:"protocol"` // smtp | imap | pop3
Port int `json:"port"` // 25/465/587/143/993/110/995
ClientIP string `gorm:"size:64;index;not null" json:"client_ip"`
Username string `gorm:"size:255;index" json:"username"`
Success bool `gorm:"index" json:"success"`
FailReason string `gorm:"size:512" json:"fail_reason"`
Detail string `gorm:"size:2048" json:"detail"`
MsgCount int `json:"msg_count"`
DurationMs int64 `json:"duration_ms"`
CreatedAt time.Time `gorm:"index" json:"created_at"`
}
// TableName specifies the table name for ProtocolLog.
func (ProtocolLog) TableName() string {
return "protocol_logs"
}
// Attachment represents a file attached to an email message.
// 注意:不声明 Message 关联(避免 GORM 外键名 MessageID 与
// Message.MessageID 字符串字段冲突,导致 AutoMigrate 生成错误外键)。
type Attachment struct {
ID uint `gorm:"primaryKey" json:"id"`
MessageID uint `gorm:"index;not null" json:"message_id"`
Message Message `gorm:"foreignKey:MessageID" json:"message"`
FileName string `gorm:"size:255;not null" json:"file_name"`
FilePath string `gorm:"size:512;not null" json:"file_path"`
ContentType string `gorm:"size:128" json:"content_type"`
+261 -11
View File
@@ -10,6 +10,8 @@ import (
"strings"
"time"
"mail_go/config"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/mailutil"
"mail_go/internal/store"
@@ -23,15 +25,131 @@ import (
// ---------- imapBackend ----------
// imapBackend implements backend.Backend.
// imapBackend implements backend.Backend and backend.BackendUpdater.
type imapBackend struct {
stores *store.Stores
banCfg config.BanConfig
port int
hub *connhub.Hub
// updates 承载新邮件等后端更新,由 go-imap 服务器广播给相关客户端。
updates chan backend.Update
// disconnectAddr 强制断开指定远端地址的连接(管理后台断开封禁用)。
disconnectAddr func(addr string)
}
// Updates 实现 backend.BackendUpdater:新邮件推送通道(广播按用户名与
// 邮箱过滤,只送达已选中对应邮箱的客户端)。
func (b *imapBackend) Updates() <-chan backend.Update {
return b.updates
}
// buildNewMessageUpdate 为一条新投递到 mailbox 的邮件构造 IMAP 更新。
// seq 取该邮件在邮箱中的实际序号(最新在前,通常为 1)。
func buildNewMessageUpdate(stores *store.Stores, userEmail, mailbox string, msg *db.Message) *backend.MessageUpdate {
if stores == nil || msg == nil || userEmail == "" || mailbox == "" {
return nil
}
seq := seqOf(stores, msg.UserID, mailbox, msg.ID)
if seq == 0 {
seq = 1
}
imapMsg := imap.NewMessage(seq, []imap.FetchItem{imap.FetchUid, imap.FetchFlags, imap.FetchInternalDate, imap.FetchRFC822Size, imap.FetchEnvelope})
imapMsg.Uid = uint32(msg.ID)
imapMsg.Flags = flagsOf(msg.IsRead, msg.IsFlagged, false)
imapMsg.InternalDate = msg.Date
imapMsg.Size = uint32(len(msg.RawData))
imapMsg.Envelope = &imap.Envelope{
Date: msg.Date,
Subject: msg.Subject,
From: parseAddressList(msg.FromAddr),
Sender: parseAddressList(msg.FromAddr),
ReplyTo: parseAddressList(msg.FromAddr),
To: parseAddressList(msg.ToAddr),
Cc: parseAddressList(msg.CcAddr),
MessageId: msg.MessageID,
}
return &backend.MessageUpdate{
Update: backend.NewUpdate(userEmail, mailbox),
Message: imapMsg,
}
}
// buildFlagsUpdate 为一条消息的标志变化构造 IMAP 更新(已读/星标/删除标记)。
// deleted 为会话内 \Deleted 标记(IMAP STORE 会话状态,不入库)。
func buildFlagsUpdate(stores *store.Stores, userEmail, mailbox string, msg *db.Message, deleted bool) *backend.MessageUpdate {
if stores == nil || msg == nil || userEmail == "" || mailbox == "" {
return nil
}
imapMsg := imap.NewMessage(seqOf(stores, msg.UserID, mailbox, msg.ID),
[]imap.FetchItem{imap.FetchUid, imap.FetchFlags})
imapMsg.Uid = uint32(msg.ID)
imapMsg.Flags = flagsOf(msg.IsRead, msg.IsFlagged, deleted)
return &backend.MessageUpdate{
Update: backend.NewUpdate(userEmail, mailbox),
Message: imapMsg,
}
}
// seqOf 返回消息在文件夹中的序号(1 基),未找到返回 0。
func seqOf(stores *store.Stores, userID uint, mailbox string, msgID uint) uint32 {
msgs, err := stores.Mails.ListAllByUserAndFolder(userID, mailbox)
if err != nil {
return 0
}
for i := range msgs {
if msgs[i].ID == msgID {
return uint32(i + 1)
}
}
return 0
}
// flagsOf 按数据库状态生成 IMAP 标志列表(deleted 为会话内 \Deleted 标记)。
func flagsOf(read, flagged, deleted bool) []string {
flags := make([]string, 0, 3)
if read {
flags = append(flags, "\\Seen")
}
if flagged {
flags = append(flags, "\\Flagged")
}
if deleted {
flags = append(flags, "\\Deleted")
}
return flags
}
// pushUpdate 非阻塞地把一条后端更新送入推送通道(满则丢弃并记日志)。
func pushUpdate(ch chan backend.Update, u backend.Update) {
if ch == nil {
return
}
select {
case ch <- u:
default:
log.Printf("IMAP: 推送通道已满,丢弃更新")
}
}
// 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
}
user, err := b.stores.Users.Authenticate(username, password)
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)
}
@@ -41,20 +159,68 @@ func (b *imapBackend) Login(connInfo *imap.ConnInfo, username, password string)
email = user.Username + "@" + domain.Name
}
logID := b.recordLogin(clientIP, username, true, "", "LOGIN 成功", 0, now)
// 连接追踪:注册到当前连接中心,Logout 时注销;
// 注册强制断开回调(按远端地址匹配,断开时由 go-imap 走正常收尾)。
conn := b.hub.Register("imap", clientIP, b.port, connInfo.TLS != nil)
if conn != nil {
conn.SetUser(email)
remoteAddr := ""
if connInfo.RemoteAddr != nil {
remoteAddr = connInfo.RemoteAddr.String()
}
if b.disconnectAddr != nil && remoteAddr != "" {
addr := remoteAddr
conn.SetDisconnect(func() { b.disconnectAddr(addr) })
}
}
return &imapUser{
stores: b.stores,
id: user.ID,
email: email,
stores: b.stores,
id: user.ID,
email: email,
logID: logID,
clientIP: clientIP,
startedAt: now,
conn: conn,
updates: b.updates,
}, 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
conn *connhub.Conn
// updates 所在 backend 的推送通道(STORE/EXPUNGE 等实时同步用)。
updates chan backend.Update
}
// Username returns the user's email address.
@@ -135,6 +301,15 @@ 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)
}
u.conn.Close()
return nil
}
@@ -286,6 +461,15 @@ func (m *imapMailbox) buildIMAPMessage(dbMsg *db.Message, seqNum uint32, items [
if err == nil {
imapMsg.BodyStructure, _ = backendutil.FetchBodyStructure(hdr, body, item == imap.FetchBodyStructure)
}
// 防御:FetchBodyStructure 对部分合法/畸形 MIME 会失败并返回
// nil(典型:message/rfc822 附件为 base64 编码时库内不解码
// 直接按嵌套消息解析头;或 multipart 边界截断)。BodyStructure
// 为 nil 时 go-imap 格式化 FETCH 响应会在 send() 协程 panic
// (nil 指针解引用),连接中断导致客户端只收到部分邮件甚至
// 一直卡在同步。解析失败时降级为 text/plain 单段结构。
if imapMsg.BodyStructure == nil {
imapMsg.BodyStructure = fallbackBodyStructure(rawMsg)
}
default:
section, err := imap.ParseBodySectionName(item)
if err != nil {
@@ -296,13 +480,33 @@ func (m *imapMailbox) buildIMAPMessage(dbMsg *db.Message, seqNum uint32, items [
return nil, err
}
literal, _ := backendutil.FetchBodySection(hdr, body, section)
imapMsg.Body[section] = literal
if literal != nil {
imapMsg.Body[section] = literal
}
}
}
return imapMsg, nil
}
// fallbackBodyStructure 构造一个 text/plain 单段 BodyStructure,用于
// MIME 解析失败的消息(保证 FETCH BODY/BODYSTRUCTURE 不因 nil 崩溃)。
func fallbackBodyStructure(raw []byte) *imap.BodyStructure {
size := uint32(len(raw))
lines := uint32(bytes.Count(raw, []byte{'\n'}))
if len(raw) > 0 && raw[len(raw)-1] != '\n' {
lines++
}
return &imap.BodyStructure{
MIMEType: "text",
MIMESubType: "plain",
Params: map[string]string{"charset": "utf-8"},
Encoding: "8bit",
Size: size,
Lines: lines,
}
}
func messageRawData(msg *db.Message) []byte {
if msg.RawData != "" {
return []byte(msg.RawData)
@@ -559,6 +763,9 @@ func (m *imapMailbox) CreateMessage(flags []string, date time.Time, body imap.Li
return fmt.Errorf("failed to create message: %w", err)
}
// 新邮件(IMAP APPEND)→ 推送给同用户其他已选中该邮箱的客户端
pushUpdate(m.user.updates, buildNewMessageUpdate(m.stores, m.user.email, m.name, msg))
return nil
}
@@ -573,6 +780,10 @@ func (m *imapMailbox) UpdateMessagesFlags(uid bool, seqset *imap.SeqSet, op imap
return err
}
// 记录首个持久化错误:SQLite 忙/锁等瞬时失败必须让客户端感知
// (返回 NO 触发重试),否则已读/星标会静默丢失。
var firstErr error
for i, dbMsg := range dbMessages {
var match bool
if uid {
@@ -592,9 +803,15 @@ func (m *imapMailbox) UpdateMessagesFlags(uid bool, seqset *imap.SeqSet, op imap
applyFlag := func(flag string, enabled bool) {
switch flag {
case "\\Seen":
_ = m.stores.Mails.MarkReadState(dbMsg.ID, enabled)
if err := m.stores.Mails.MarkReadState(dbMsg.ID, enabled); err != nil && firstErr == nil {
log.Printf("IMAP: mark read state for msg %d failed: %v", dbMsg.ID, err)
firstErr = err
}
case "\\Flagged":
_ = m.stores.Mails.MarkFlagged(dbMsg.ID, enabled)
if err := m.stores.Mails.MarkFlagged(dbMsg.ID, enabled); err != nil && firstErr == nil {
log.Printf("IMAP: mark flagged for msg %d failed: %v", dbMsg.ID, err)
firstErr = err
}
case "\\Deleted":
if enabled {
m.deleted[dbMsg.ID] = true
@@ -618,9 +835,18 @@ func (m *imapMailbox) UpdateMessagesFlags(uid bool, seqset *imap.SeqSet, op imap
applyFlag(flag, false)
}
}
// 标志变化(已读/星标/删除)→ 推送给同用户其他客户端
// (重新读库取最新状态,\Deleted 取会话内状态)
fresh, err := m.stores.Mails.GetByID(dbMsg.ID)
if err != nil {
continue
}
deleted := m.deleted != nil && m.deleted[dbMsg.ID]
pushUpdate(m.user.updates, buildFlagsUpdate(m.stores, m.user.email, m.name, fresh, deleted))
}
return nil
return firstErr
}
// CopyMessages copies messages to another mailbox.
@@ -664,7 +890,10 @@ func (m *imapMailbox) CopyMessages(uid bool, seqset *imap.SeqSet, dest string) e
}
if err := m.stores.Mails.Create(copyMsg); err != nil {
log.Printf("IMAP: failed to copy message %d to %s: %v", dbMsg.ID, dest, err)
continue
}
// 目标邮箱新增 → 推送给同用户其他客户端
pushUpdate(m.user.updates, buildNewMessageUpdate(m.stores, m.user.email, dest, copyMsg))
}
return nil
@@ -694,6 +923,11 @@ func (m *imapMailbox) MoveMessages(uid bool, seqset *imap.SeqSet, dest string) e
}
if err := m.stores.Mails.MoveToFolder(dbMsg.ID, dest); err != nil {
log.Printf("IMAP: failed to move message %d to %s: %v", dbMsg.ID, dest, err)
continue
}
// 目标邮箱新增(移动后消息在 dest)→ 推送给同用户其他客户端
if moved, err := m.stores.Mails.GetByID(dbMsg.ID); err == nil {
pushUpdate(m.user.updates, buildNewMessageUpdate(m.stores, m.user.email, dest, moved))
}
}
return nil
@@ -705,12 +939,28 @@ func (m *imapMailbox) Expunge() error {
return nil
}
// 删除前计算各消息的序号(Expunge 响应序号为删除前状态下的序号)
var seqs []uint32
for msgID := range m.deleted {
if seq := seqOf(m.stores, m.user.id, m.name, msgID); seq > 0 {
seqs = append(seqs, seq)
}
}
for msgID := range m.deleted {
if err := m.stores.Mails.Delete(msgID); err != nil {
log.Printf("IMAP: failed to expunge message %d: %v", msgID, err)
}
}
m.deleted = make(map[uint]bool)
// 删除 → 推送给同用户其他客户端(每条序号一个 ExpungeUpdate
for _, seq := range seqs {
pushUpdate(m.user.updates, &backend.ExpungeUpdate{
Update: backend.NewUpdate(m.user.email, m.name),
SeqNum: seq,
})
}
return nil
}
+282
View File
@@ -0,0 +1,282 @@
//go:build !race
// 集成测试:启动真实 IMAP 监听 + 脚本客户端(go-imap client)。
// 注意:仅在非 -race 构建下运行——go-imap v1.2.1 存在库内数据竞争
// cmd_selected.go STORE 写 *conn.silent() vs listenUpdates 读),
// 启用 backend 推送(Updates != nil)时必然触发,-race 下会误报。
// 推送逻辑的竞态覆盖由单元测试(notify_test.go)承担。
package imap_server
import (
"net"
"path/filepath"
"testing"
"time"
"mail_go/config"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/store"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"golang.org/x/crypto/bcrypt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// startIntegrationServer 启动一个真实的 IMAP 监听(随机端口)供客户端测试。
func startIntegrationServer(t *testing.T) (*store.Stores, string) {
t.Helper()
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}, &db.ProtocolLog{}, &db.BanEntry{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
domain := &db.Domain{Name: "example.com"}
if err := stores.Domains.Create(domain); err != nil {
t.Fatalf("create domain: %v", err)
}
hashed, _ := bcrypt.GenerateFromPassword([]byte("secret123"), bcrypt.DefaultCost)
user := &db.User{Username: "alice", DomainID: domain.ID, PasswordHash: string(hashed), IsActive: true}
if err := stores.Users.Create(user); err != nil {
t.Fatalf("create user: %v", err)
}
srv := NewIMAPServer(config.IMAPConfig{}, stores, nil, config.BanConfig{}, connhub.New())
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
t.Cleanup(func() { ln.Close() })
imapSrv := srv.newServer(ln.Addr().String(), nil)
go imapSrv.Serve(ln)
return stores, ln.Addr().String()
}
// seedMailbox 创建 n 封按时间递增的邮件(id 与 date 顺序一致时 id ASC == date ASC)。
func seedMailbox(t *testing.T, stores *store.Stores, userID uint, n int) []uint {
t.Helper()
ids := make([]uint, 0, n)
base := time.Now().Add(-time.Duration(n) * time.Hour)
for i := 0; i < n; i++ {
msg := &db.Message{
UserID: userID,
Folder: "INBOX",
FromAddr: "x@y",
ToAddr: "alice@example.com",
Subject: "m",
Date: base.Add(time.Duration(i) * time.Hour), // 时间递增:id 越大日期越新
CreatedAt: time.Now(),
}
if err := stores.Mails.Create(msg); err != nil {
t.Fatalf("create message: %v", err)
}
ids = append(ids, msg.ID)
}
return ids
}
func loginAndSelect(t *testing.T, addr string) *client.Client {
t.Helper()
c, err := client.Dial(addr)
if err != nil {
t.Fatalf("dial: %v", err)
}
t.Cleanup(func() { c.Logout() })
if err := c.Login("alice@example.com", "secret123"); err != nil {
t.Fatalf("login: %v", err)
}
if _, err := c.Select("INBOX", false); err != nil {
t.Fatalf("select: %v", err)
}
return c
}
// uidOf returns the message id of the newest message by date.
func assertReadState(t *testing.T, stores *store.Stores, msgID uint, want bool) {
t.Helper()
msg, err := stores.Mails.GetByID(msgID)
if err != nil {
t.Fatalf("get msg: %v", err)
}
if msg.IsRead != want {
t.Fatalf("msg %d IsRead = %v, want %v", msgID, msg.IsRead, want)
}
}
// TestUidStorePersists 验证 UID STORE +FLAGS(\Seen) 持久化(RFC 标准流程)。
func TestUidStorePersists(t *testing.T) {
stores, addr := startIntegrationServer(t)
ids := seedMailbox(t, stores, 1, 3)
c := loginAndSelect(t, addr)
seqset := new(imap.SeqSet)
seqset.AddNum(uint32(ids[1])) // UID = 第二条消息
ch := make(chan *imap.Message, 1)
if err := c.UidStore(seqset, imap.AddFlags, []interface{}{imap.SeenFlag}, ch); err != nil {
t.Fatalf("uid store: %v", err)
}
<-ch
assertReadState(t, stores, ids[1], true)
assertReadState(t, stores, ids[0], false)
assertReadState(t, stores, ids[2], false)
}
// TestSeqStoreServerIssued 验证客户端用服务器下发的序号(FETCH 结果)做
// seq 式 STORE:任何排序下都应正确持久化。
func TestSeqStoreServerIssued(t *testing.T) {
stores, addr := startIntegrationServer(t)
ids := seedMailbox(t, stores, 1, 3)
c := loginAndSelect(t, addr)
// 拉取全部消息,找到 ids[2](最新一封)的服务器序号
seqsetAll := new(imap.SeqSet)
seqsetAll.AddRange(1, 3)
messages := make(chan *imap.Message, 3)
if err := c.Fetch(seqsetAll, []imap.FetchItem{imap.FetchFlags, imap.FetchUid}, messages); err != nil {
t.Fatalf("fetch: %v", err)
}
var targetSeq uint32
for m := range messages {
if m.Uid == uint32(ids[2]) {
targetSeq = m.SeqNum
}
}
if targetSeq == 0 {
t.Fatal("target message not found in fetch")
}
seqset := new(imap.SeqSet)
seqset.AddNum(targetSeq)
ch := make(chan *imap.Message, 1)
if err := c.Store(seqset, imap.AddFlags, []interface{}{imap.SeenFlag}, ch); err != nil {
t.Fatalf("store: %v", err)
}
<-ch
assertReadState(t, stores, ids[2], true)
}
// TestSeqStoreClientSelfNumbered 复现风险场景:客户端不信任服务器序号,
// 按自己的视图(日期倒序,最新在前)自行编号后发 seq 式 STORE。
// 服务器规范排序必须与常见客户端视图一致(date DESC, id DESC),
// 否则会把另一封邮件标为已读、目标邮件永远未读。
func TestSeqStoreClientSelfNumbered(t *testing.T) {
stores, addr := startIntegrationServer(t)
ids := seedMailbox(t, stores, 1, 3) // 3 封,日期递增,最新的是 ids[2]
c := loginAndSelect(t, addr)
// 客户端按日期倒序视图:最新一封 = seq 1
seqset := new(imap.SeqSet)
seqset.AddNum(1)
ch := make(chan *imap.Message, 1)
if err := c.Store(seqset, imap.AddFlags, []interface{}{imap.SeenFlag}, ch); err != nil {
t.Fatalf("store: %v", err)
}
<-ch
// 客户端意图是标记最新一封(ids[2])为已读
assertReadState(t, stores, ids[2], true)
}
// TestFetchBodyMalformedMIME 回归:消息包含无法解析的 MIME(base64 编码的
// message/rfc822 附件 / 截断的 multipart)时,FETCH BODY/BODYSTRUCTURE
// 不得因 nil BodyStructure 触发服务器 panic(否则连接中断,客户端只收到
// 部分邮件或一直卡在同步)。修复前 go-imap send() 协程会 nil 指针崩溃。
func TestFetchBodyMalformedMIME(t *testing.T) {
stores, addr := startIntegrationServer(t)
// 1) base64 编码的 message/rfc822 附件(转发邮件场景):
// backendutil.FetchBodyStructure 不解码 base64,直接把编码文本
// 当嵌套消息头解析 → "malformed MIME header line" 错误。
rfc822Body := "UmVjZWl2ZWQ6IGZyb20gb3V0Ym91bmQuY2kuaWNsb3VkLmNvbSAodW5rbm93biBbMTI3LjAuMC4yKVxuXHQgYnkgcDAwLWljbG91ZG10YS1hc210cC11cy1jZW50cmFsLTFrLTEwMC1wZXJjZW50LTggKFBvc3RmaXgpIHdpdGggRVNNVFBTIGlkIDIxRTlBMThDQURDRjM4MlxuXHQgZm9yIDxkc2hAbG12ZS5uZXQ+OyBTdW4sIDE2IEF1ZyAyMDI2IDEzOjU4OjIxICswMDAwIChVVEMpXG5YLUlDTC1SZXBJZDogRURWY1BlQ3RlWG4tZ0Z1T0xxUWhfSjZvcE9fN1B2OEtsOW1mMDg2VUFxZ29zXG5EYXRlOiBTdW4sIDE2IEF1ZyAyMDI2IDEzOjU4OjIxICswMDAwXG5Gcm9tOiBkYXZpZEB5YW5kZXguY29tXG5UbzogZHNoQGxtdmUubmV0XG5NZXNzYWdlLUlEOiA8QTIxNzBEMTEtMkI1MC00MTQwLTlEQTMtMkI3M0U2RUIwQTc4QHlhbmRleC5jb20+XG5TdWJqZWN0OiB0ZXN0XG5cbmhlbGxvXG4="
msgWithRFC822 := &db.Message{
UserID: 1,
Folder: "INBOX",
FromAddr: "alice@example.com",
ToAddr: "alice@example.com",
Subject: "fwd",
Date: time.Now().Add(-2 * time.Hour),
RawData: "From: alice@example.com\r\n" +
"To: alice@example.com\r\n" +
"Subject: fwd\r\n" +
"MIME-Version: 1.0\r\n" +
"Content-Type: multipart/mixed; boundary=\"==fwd==\"\r\n\r\n" +
"--==fwd==\r\n" +
"Content-Type: text/plain; charset=\"utf-8\"\r\n" +
"Content-Transfer-Encoding: 8bit\r\n\r\n" +
"正文\r\n\r\n" +
"--==fwd==\r\n" +
"Content-Type: message/rfc822\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"Content-Disposition: attachment; filename=\"original.eml\"\r\n" +
"MIME-Version: 1.0\r\n\r\n" +
rfc822Body + "\r\n" +
"--==fwd==--\r\n",
}
// 2) 截断的 multipart(缺少结束边界):BODYSTRUCTURE(extended) 解析报错
msgTruncated := &db.Message{
UserID: 1,
Folder: "INBOX",
FromAddr: "alice@example.com",
ToAddr: "alice@example.com",
Subject: "truncated",
Date: time.Now().Add(-1 * time.Hour),
RawData: "From: alice@example.com\r\n" +
"To: alice@example.com\r\n" +
"Subject: truncated\r\n" +
"MIME-Version: 1.0\r\n" +
"Content-Type: multipart/alternative; boundary=\"==trunc==\"\r\n\r\n" +
"--==trunc==\r\n" +
"Content-Type: text/plain\r\n\r\n" +
"hello\r\n",
// 无结束边界
}
if err := stores.Mails.Create(msgWithRFC822); err != nil {
t.Fatalf("create msg: %v", err)
}
if err := stores.Mails.Create(msgTruncated); err != nil {
t.Fatalf("create msg: %v", err)
}
c := loginAndSelect(t, addr)
seqset := new(imap.SeqSet)
seqset.AddRange(1, 2)
// BODY:历史上 message/rfc822 消息解析失败 → nil BodyStructure → panic
msgs := make(chan *imap.Message, 10)
if err := c.Fetch(seqset, []imap.FetchItem{imap.FetchBody}, msgs); err != nil {
t.Fatalf("fetch body: %v", err)
}
got := 0
for range msgs {
got++
}
if got != 2 {
t.Fatalf("FETCH BODY 返回 %d/2 封", got)
}
// BODYSTRUCTURE:截断 multipart 在 extended 解析时报错 → nil → panic
msgs2 := make(chan *imap.Message, 10)
if err := c.Fetch(seqset, []imap.FetchItem{imap.FetchBodyStructure}, msgs2); err != nil {
t.Fatalf("fetch bodystructure: %v", err)
}
got2 := 0
for range msgs2 {
got2++
}
if got2 != 2 {
t.Fatalf("FETCH BODYSTRUCTURE 返回 %d/2 封", got2)
}
}
+299
View File
@@ -0,0 +1,299 @@
package imap_server
import (
"path/filepath"
"testing"
"time"
"mail_go/config"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/store"
"github.com/emersion/go-imap/backend"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// TestPushNewMessage 验证本地投递成功后推送的 MessageUpdate 内容正确。
func TestPushNewMessage(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
domain := &db.Domain{Name: "example.com"}
if err := stores.Domains.Create(domain); err != nil {
t.Fatalf("create domain: %v", err)
}
user := &db.User{Username: "alice", DomainID: domain.ID, IsActive: true}
if err := stores.Users.Create(user); err != nil {
t.Fatalf("create user: %v", err)
}
email := "alice@example.com"
// 已有一封旧邮件(日期更早);规范排序最新在前,新邮件应为 INBOX 第 1 封
old := &db.Message{UserID: user.ID, Folder: "INBOX", FromAddr: "x@y", Subject: "old", Date: time.Now().Add(-time.Hour)}
if err := stores.Mails.Create(old); err != nil {
t.Fatalf("create old message: %v", err)
}
inboxMsg := &db.Message{
UserID: user.ID,
Folder: "INBOX",
FromAddr: "sender@other.com",
ToAddr: email,
Subject: "新邮件",
RawData: "From: sender@other.com\r\nSubject: 新邮件\r\n\r\nhello",
MessageID: "<new-1@other.com>",
Date: time.Now(),
IsRead: false,
}
if err := stores.Mails.Create(inboxMsg); err != nil {
t.Fatalf("create message: %v", err)
}
hub := connhub.New()
srv := NewIMAPServer(config.IMAPConfig{}, stores, nil, config.BanConfig{}, hub)
// 模拟明文 + TLS 两个监听器(生产环境由 Start/StartTLS 注册)
srv.newServer("127.0.0.1:143", nil)
srv.newServer("127.0.0.1:993", nil)
srv.PushNewMessage(email, inboxMsg)
// 两个监听器(明文/TLS)各有一个 backend 通道,都应收到同一更新
srv.beMu.Lock()
bes := append([]*imapBackend(nil), srv.bes...)
srv.beMu.Unlock()
if len(bes) == 0 {
t.Fatal("no backends registered")
}
for i, b := range bes {
select {
case upd := <-b.updates:
mu, ok := upd.(*backend.MessageUpdate)
if !ok {
t.Fatalf("backend %d: update type = %T, want *MessageUpdate", i, upd)
}
if mu.Username() != email {
t.Fatalf("backend %d: username = %q, want %q", i, mu.Username(), email)
}
if mu.Mailbox() != "INBOX" {
t.Fatalf("backend %d: mailbox = %q, want INBOX", i, mu.Mailbox())
}
if mu.Message.Uid != uint32(inboxMsg.ID) {
t.Fatalf("backend %d: uid = %d, want %d", i, mu.Message.Uid, inboxMsg.ID)
}
if mu.Message.SeqNum != 1 {
t.Fatalf("backend %d: seq = %d, want 1", i, mu.Message.SeqNum)
}
if mu.Message.Envelope == nil || mu.Message.Envelope.Subject != "新邮件" {
t.Fatalf("backend %d: envelope missing subject", i)
}
case <-time.After(time.Second):
t.Fatalf("backend %d: no update received", i)
}
}
}
// TestPushNewMessageChannelFull 验证通道满时推送不阻塞(非阻塞丢弃)。
func TestPushNewMessageChannelFull(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
hub := connhub.New()
srv := NewIMAPServer(config.IMAPConfig{}, stores, nil, config.BanConfig{}, hub)
srv.newServer("127.0.0.1:143", nil)
srv.newServer("127.0.0.1:993", nil)
msg := &db.Message{ID: 1, Folder: "INBOX", Date: time.Now()}
done := make(chan struct{})
go func() {
// 灌满所有 backend 通道(容量 256),再调用必须立即返回
srv.beMu.Lock()
bes := append([]*imapBackend(nil), srv.bes...)
srv.beMu.Unlock()
for _, b := range bes {
for i := 0; i < cap(b.updates); i++ {
b.updates <- backend.NewUpdate("a@b", "INBOX")
}
}
srv.PushNewMessage("a@b", msg)
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("PushNewMessage blocked on full channel")
}
}
// TestPushNewMessageNilSafe 验证空参数/空指针安全。
func TestPushNewMessageNilSafe(t *testing.T) {
var srv *IMAPServer
srv.PushNewMessage("a@b", &db.Message{ID: 1}) // 不应 panic
srv = NewIMAPServer(config.IMAPConfig{}, nil, nil, config.BanConfig{}, nil)
srv.PushNewMessage("", &db.Message{ID: 1}) // 空邮箱
srv.PushNewMessage("a@b", nil) // 空消息
}
// TestPushFlagsChanged 验证标志变化(已读/星标)推送内容正确。
func TestPushFlagsChanged(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
domain := &db.Domain{Name: "example.com"}
if err := stores.Domains.Create(domain); err != nil {
t.Fatalf("create domain: %v", err)
}
user := &db.User{Username: "alice", DomainID: domain.ID, IsActive: true}
if err := stores.Users.Create(user); err != nil {
t.Fatalf("create user: %v", err)
}
msg := &db.Message{UserID: user.ID, Folder: "INBOX", FromAddr: "x@y", Subject: "s", Date: time.Now()}
if err := stores.Mails.Create(msg); err != nil {
t.Fatalf("create message: %v", err)
}
msg.IsRead = true
msg.IsFlagged = true
hub := connhub.New()
srv := NewIMAPServer(config.IMAPConfig{}, stores, nil, config.BanConfig{}, hub)
srv.newServer("127.0.0.1:143", nil)
srv.PushFlagsChanged("alice@example.com", "INBOX", msg)
srv.beMu.Lock()
b := srv.bes[0]
srv.beMu.Unlock()
select {
case upd := <-b.updates:
mu, ok := upd.(*backend.MessageUpdate)
if !ok {
t.Fatalf("update type = %T, want *MessageUpdate", upd)
}
if mu.Username() != "alice@example.com" || mu.Mailbox() != "INBOX" {
t.Fatalf("update targeting = %s/%s", mu.Username(), mu.Mailbox())
}
if mu.Message.Uid != uint32(msg.ID) {
t.Fatalf("uid = %d, want %d", mu.Message.Uid, msg.ID)
}
got := make(map[string]bool)
for _, f := range mu.Message.Flags {
got[f] = true
}
if !got["\\Seen"] || !got["\\Flagged"] {
t.Fatalf("flags = %v, want \\Seen and \\Flagged", mu.Message.Flags)
}
case <-time.After(time.Second):
t.Fatal("no flags update received")
}
}
// TestPushExpunged 验证删除推送:每条序号一个 ExpungeUpdate。
func TestPushExpunged(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.Message{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
hub := connhub.New()
srv := NewIMAPServer(config.IMAPConfig{}, stores, nil, config.BanConfig{}, hub)
srv.newServer("127.0.0.1:143", nil)
srv.PushExpunged("alice@example.com", "INBOX", []uint32{2, 5})
srv.beMu.Lock()
b := srv.bes[0]
srv.beMu.Unlock()
var seqs []uint32
for i := 0; i < 2; i++ {
select {
case upd := <-b.updates:
eu, ok := upd.(*backend.ExpungeUpdate)
if !ok {
t.Fatalf("update type = %T, want *ExpungeUpdate", upd)
}
if eu.Username() != "alice@example.com" || eu.Mailbox() != "INBOX" {
t.Fatalf("update targeting = %s/%s", eu.Username(), eu.Mailbox())
}
seqs = append(seqs, eu.SeqNum)
case <-time.After(time.Second):
t.Fatal("no expunge update received")
}
}
if seqs[0] != 2 || seqs[1] != 5 {
t.Fatalf("seqs = %v, want [2 5]", seqs)
}
}
// TestBroadcastUpdateIsolatedPerListener 回归测试:同一更新广播到多个监听器
// 时,每个监听器必须持有独立的 Update 对象(独立 Done channel),否则
// 多个 listenUpdates 会对同一 channel 二次 close 导致
// panic: close of closed channel。
func TestBroadcastUpdateIsolatedPerListener(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.Message{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
hub := connhub.New()
srv := NewIMAPServer(config.IMAPConfig{}, stores, nil, config.BanConfig{}, hub)
srv.newServer("127.0.0.1:143", nil)
srv.newServer("127.0.0.1:993", nil)
srv.PushExpunged("alice@example.com", "INBOX", []uint32{1})
srv.beMu.Lock()
bes := append([]*imapBackend(nil), srv.bes...)
srv.beMu.Unlock()
// 每个监听器各收到一条更新
var updates []backend.Update
for i, b := range bes {
select {
case upd := <-b.updates:
updates = append(updates, upd)
case <-time.After(time.Second):
t.Fatalf("backend %d: no update received", i)
}
}
if len(updates) != 2 {
t.Fatalf("updates = %d, want 2", len(updates))
}
// 关键断言:两条更新必须拥有独立的 Done channel
if updates[0].Done() == updates[1].Done() {
t.Fatal("listeners share the same Done channel: double close would panic")
}
// 模拟两个 listenUpdates 各自执行 close(update.Done()):修复前必 panic
for _, upd := range updates {
close(upd.Done())
}
}
+174 -15
View File
@@ -4,49 +4,208 @@ import (
"crypto/tls"
"fmt"
"log"
"net"
"strconv"
"sync"
"mail_go/config"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/store"
"mail_go/internal/tlsutil"
"github.com/emersion/go-imap/backend"
imapserver "github.com/emersion/go-imap/server"
)
// IMAPServer wraps a go-imap Server and provides mailbox access capability.
type IMAPServer struct {
stores *store.Stores
cfg config.IMAPConfig
// Pusher 是 IMAP 实时推送接口:SMTP/POP3/Web 在邮件状态变化后调用,
// 由 go-imap 广播给相关客户端(按用户名+邮箱过滤,IDLE 时即时送达)。
type Pusher interface {
// PushNewMessage 推送新邮件(本地投递成功)。
PushNewMessage(userEmail string, msg *db.Message)
// PushFlagsChanged 推送已读/星标等标志变化(MessageUpdate)。
PushFlagsChanged(userEmail, mailbox string, msg *db.Message)
// PushExpunged 推送邮件被删除(ExpungeUpdateseqNums 为删除前序号)。
PushExpunged(userEmail, mailbox string, seqNums []uint32)
}
// NewIMAPServer creates a new IMAP server instance.
func NewIMAPServer(cfg config.IMAPConfig, stores *store.Stores) *IMAPServer {
// IMAPServer wraps a go-imap Server and provides mailbox access capability.
type IMAPServer struct {
stores *store.Stores
cfg config.IMAPConfig
banCfg config.BanConfig
tlsLoader *tlsutil.Loader
hub *connhub.Hub
beMu sync.Mutex
bes []*imapBackend // 各监听器(明文/TLS)的 backend,用于新邮件推送
srvs []*imapserver.Server // 各监听器实例,用于强制断开连接
}
// NewIMAPServer creates a new IMAP server instance. tlsLoader may be nil
// when TLS is not configured.
func NewIMAPServer(cfg config.IMAPConfig, stores *store.Stores, tlsLoader *tlsutil.Loader, banCfg config.BanConfig, hub *connhub.Hub) *IMAPServer {
return &IMAPServer{
stores: stores,
cfg: cfg,
stores: stores,
cfg: cfg,
banCfg: banCfg,
tlsLoader: tlsLoader,
hub: hub,
}
}
// NotifyNewMessage 向所有 IMAP 监听器推送新邮件通知(go-imap 广播时按
// 用户名+邮箱过滤,只送达已选中 INBOX 的客户端,IDLE 挂起时实时收到
// FETCH 响应)。由 SMTP/Web 本地投递成功时调用;channel 满时非阻塞丢弃。
func (s *IMAPServer) PushNewMessage(userEmail string, msg *db.Message) {
if s == nil || userEmail == "" || msg == nil {
return
}
update := buildNewMessageUpdate(s.stores, userEmail, "INBOX", msg)
if update == nil {
return
}
s.broadcastUpdate(update, userEmail, msg.ID)
}
// PushFlagsChanged 推送邮件标志(已读/星标等)变化给同用户的其他客户端。
func (s *IMAPServer) PushFlagsChanged(userEmail, mailbox string, msg *db.Message) {
if s == nil || userEmail == "" || mailbox == "" || msg == nil {
return
}
update := buildFlagsUpdate(s.stores, userEmail, mailbox, msg, false)
if update == nil {
return
}
s.broadcastUpdate(update, userEmail, msg.ID)
}
// PushExpunged 推送邮件被删除(每条序号一个 ExpungeUpdate)。
func (s *IMAPServer) PushExpunged(userEmail, mailbox string, seqNums []uint32) {
if s == nil || userEmail == "" || mailbox == "" || len(seqNums) == 0 {
return
}
for _, seq := range seqNums {
update := &backend.ExpungeUpdate{
Update: backend.NewUpdate(userEmail, mailbox),
SeqNum: seq,
}
s.broadcastUpdate(update, userEmail, 0)
}
}
// broadcastUpdate 把一条更新非阻塞地投递到所有监听器的推送通道。
// 每个监听器必须收到独立的 Update 对象(各自独立的 Done channel):
// 每个监听器的 listenUpdates 都会对 update.Done() 执行 close,共享
// 同一对象会导致对同一 channel 二次 close 而 panic。
func (s *IMAPServer) broadcastUpdate(update backend.Update, userEmail string, msgID uint) {
s.beMu.Lock()
bes := append([]*imapBackend(nil), s.bes...)
s.beMu.Unlock()
for _, b := range bes {
select {
case b.updates <- cloneUpdate(update):
default:
log.Printf("IMAP: 推送通道已满,丢弃 %s 的更新 (msg=%d)", userEmail, msgID)
}
}
}
// cloneUpdate 按类型复制一条 backend.Update:载荷(消息/序号)共享,
// 但 Username/Mailbox/Done channel 重置为独立实例。
func cloneUpdate(u backend.Update) backend.Update {
switch u := u.(type) {
case *backend.MessageUpdate:
return &backend.MessageUpdate{
Update: backend.NewUpdate(u.Username(), u.Mailbox()),
Message: u.Message,
}
case *backend.ExpungeUpdate:
return &backend.ExpungeUpdate{
Update: backend.NewUpdate(u.Username(), u.Mailbox()),
SeqNum: u.SeqNum,
}
default:
// 防御:未知类型原样传递(当前不存在此类更新)
return u
}
}
// registerBackend 记录新建的 backend(用于新邮件推送)。
func (s *IMAPServer) registerBackend(be *imapBackend) {
s.beMu.Lock()
s.bes = append(s.bes, be)
s.beMu.Unlock()
}
// registerServer 记录监听器实例(用于强制断开连接)。
func (s *IMAPServer) registerServer(srv *imapserver.Server) {
s.beMu.Lock()
s.srvs = append(s.srvs, srv)
s.beMu.Unlock()
}
// DisconnectByAddr 强制断开指定远端地址的连接(管理后台「断开并封禁」)。
// 关闭连接会触发 go-imap 的收尾流程(user.Logout、协议日志回填、hub 注销)。
func (s *IMAPServer) DisconnectByAddr(remoteAddr string) {
if s == nil || remoteAddr == "" {
return
}
s.beMu.Lock()
srvs := append([]*imapserver.Server(nil), s.srvs...)
s.beMu.Unlock()
for _, srv := range srvs {
srv.ForEachConn(func(conn imapserver.Conn) {
info := conn.Info()
if info != nil && info.RemoteAddr != nil && info.RemoteAddr.String() == remoteAddr {
_ = conn.Close()
}
})
}
}
func (s *IMAPServer) tlsConfig() (*tls.Config, error) {
if s.cfg.TLSCert == "" || s.cfg.TLSKey == "" {
if s.tlsLoader == nil {
return nil, fmt.Errorf("IMAP TLS certificate or key not configured")
}
cert, err := tls.LoadX509KeyPair(s.cfg.TLSCert, s.cfg.TLSKey)
if err != nil {
return nil, fmt.Errorf("failed to load IMAP TLS certificate: %w", err)
}
return &tls.Config{Certificates: []tls.Certificate{cert}}, nil
// GetCertificate 每次握手按需重载证书,证书更新后无需重启服务
return &tls.Config{GetCertificate: s.tlsLoader.GetCertificate}, nil
}
// 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}
be := &imapBackend{
stores: s.stores,
banCfg: s.banCfg,
port: portOf(addr),
hub: s.hub,
updates: make(chan backend.Update, 256),
disconnectAddr: s.DisconnectByAddr,
}
s.registerBackend(be)
srv := imapserver.New(be)
srv.Addr = addr
srv.TLSConfig = tlsConfig
srv.AllowInsecureAuth = tlsConfig == nil
s.registerServer(srv)
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()
+177 -25
View File
@@ -3,12 +3,14 @@
// Messages queued for external recipients are stored in the outbound_messages
// table and delivered by the Manager's background worker: MX lookup, SMTP
// transaction over port 25 with opportunistic STARTTLS, exponential backoff
// retries, permanent-failure bounces and DKIM signing.
// retries, permanent-failure bounces and DKIM signing. A smarthost relay can
// be configured for servers whose own IP is blocklisted (e.g. PBL).
package outbound
import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"net"
@@ -45,10 +47,23 @@ func newPermError(format string, args ...interface{}) *DeliveryError {
return &DeliveryError{Permanent: true, Msg: fmt.Sprintf(format, args...)}
}
// Mailer performs direct MX delivery of a single message.
// RelayConfig describes a smarthost through which all external mail is sent.
type RelayConfig struct {
Host string
Port int // 465 = implicit TLS; other ports may use STARTTLS
Username string // AUTH PLAIN credentials (empty = no authentication)
Password string
StartTLS bool // use STARTTLS on non-465 ports
TLSInsecure bool // skip certificate verification (test-only, credentials leak risk)
}
// Mailer performs direct MX delivery (or smarthost relay) of a single message.
type Mailer struct {
Hostname string // EHLO hostname presented to remote servers
Port int // destination port, 0 means the default SMTP port 25
Relay *RelayConfig
IPFamily string // "ipv4" (default), "ipv6" or "auto"
SourceIP string // optional source address to bind (e.g. a static IPv6)
ConnectTimeout time.Duration
}
@@ -68,17 +83,22 @@ func (m *Mailer) port() int {
return m.Port
}
// Deliver sends one message to one recipient via the recipient domain's MX.
// It returns the final SMTP response text on success and a *DeliveryError on
// failure.
// Deliver sends one message to one recipient. When a relay is configured the
// message goes through the smarthost; otherwise the recipient domain's MX is
// used. It returns the final SMTP response text on success and a
// *DeliveryError on failure.
func (m *Mailer) Deliver(from, to string, data []byte) (string, error) {
if m.Relay != nil && m.Relay.Host != "" {
return m.deliverViaRelay(from, to, data)
}
at := strings.LastIndex(to, "@")
if at < 0 || at == len(to)-1 {
return "", newPermError("invalid recipient address: %s", to)
}
domain := strings.ToLower(strings.TrimSpace(to[at+1:]))
mxHosts, err := lookupMX(domain)
mxHosts, err := lookupMX(domain, m.IPFamily)
if err != nil {
var de *DeliveryError
if errors.As(err, &de) {
@@ -111,6 +131,21 @@ func (m *Mailer) Deliver(from, to string, data []byte) (string, error) {
return "", lastErr
}
// deliverViaRelay sends the message through the configured smarthost.
// The relay carries AUTH credentials, so its TLS certificate is verified
// unless RelayTLSInsecure is explicitly enabled.
func (m *Mailer) deliverViaRelay(from, to string, data []byte) (string, error) {
port := m.Relay.Port
if port == 0 {
port = 587
}
implicitTLS := port == 465
return m.smtpTransaction(m.Relay.Host, port, implicitTLS,
m.Relay.StartTLS && !implicitTLS,
m.Relay.Username, m.Relay.Password, from, to, data,
m.Relay.TLSInsecure)
}
// smtpClient wraps a textproto connection to a remote SMTP server.
type smtpClient struct {
conn net.Conn
@@ -191,15 +226,41 @@ func (c *smtpClient) hello(hostname string) error {
return nil
}
// authPlain performs AUTH PLAIN with the initial-response form, falling back
// to the two-step form when the server asks for credentials separately.
func (c *smtpClient) authPlain(username, password string) error {
b64 := base64.StdEncoding.EncodeToString([]byte("\x00" + username + "\x00" + password))
code, msg, err := c.cmd(235, "AUTH PLAIN %s", b64)
if err != nil {
if code == 334 {
_, _, err = c.cmd(235, "%s", b64)
}
if err != nil {
return err
}
}
_ = msg
return nil
}
// deliverToHost performs a full SMTP transaction with a single MX host.
// Direct MX delivery is opportunistic TLS: certificates are not verified
// because most MX certificates cannot be validated over a cold connection.
func (m *Mailer) deliverToHost(host, from, to string, data []byte) (string, error) {
addr := net.JoinHostPort(host, strconv.Itoa(m.port()))
return m.smtpTransaction(host, m.port(), false, false, "", "", from, to, data, true)
}
// smtpTransaction performs one complete SMTP session: connect, greeting,
// optional implicit TLS / STARTTLS, optional AUTH PLAIN, MAIL/RCPT/DATA/QUIT.
// tlsInsecure 控制 TLS 握手时是否跳过证书验证:直投 MX 用 true(机会式
// TLS),relay 用配置值(默认 false,保护中继凭据)。
func (m *Mailer) smtpTransaction(host string, port int, implicitTLS, startTLS bool, username, password, from, to string, data []byte, tlsInsecure bool) (string, error) {
addr := net.JoinHostPort(host, strconv.Itoa(port))
ctx, cancel := context.WithTimeout(context.Background(), m.ConnectTimeout)
defer cancel()
dialer := &net.Dialer{Timeout: m.ConnectTimeout}
conn, err := dialer.DialContext(ctx, "tcp", addr)
conn, err := m.dialSMTP(ctx, addr)
if err != nil {
return "", newTempError("connect to %s failed: %v", addr, err)
}
@@ -212,26 +273,51 @@ func (m *Mailer) deliverToHost(host, from, to string, data []byte) (string, erro
return "", classifyResponse(err, msg)
}
if err := c.hello(m.Hostname); err != nil {
return "", err
tlsServerName := host
if ip := net.ParseIP(host); ip != nil {
// IP literal 同样作为 ServerName:证书验证模式下校验其 IP SAN
tlsServerName = ip.String()
}
// Opportunistic STARTTLS (RFC 3207): only when the server advertises it.
if _, ok := c.exts["STARTTLS"]; ok {
if _, _, err := c.cmd(220, "STARTTLS"); err != nil {
if implicitTLS {
tlsConn, err := tlsClientHandshake(ctx, conn, tlsServerName, host, tlsInsecure)
if err != nil {
return "", err
}
tlsConn := tls.Client(conn, &tls.Config{
ServerName: host,
InsecureSkipVerify: true, // remote MX certificates often cannot be verified
})
if err := tlsConn.HandshakeContext(ctx); err != nil {
return "", newTempError("TLS handshake with %s failed: %v", host, err)
}
c.txt = textproto.NewConn(tlsConn)
if err := c.hello(m.Hostname); err != nil {
return "", err
}
} else {
if err := c.hello(m.Hostname); err != nil {
return "", err
}
// Opportunistic STARTTLS: only when the server advertises it, unless
// startTLS is explicitly requested (smarthost), in which case a
// non-advertising server is an error.
_, adv := c.exts["STARTTLS"]
if adv || startTLS {
if !adv && startTLS {
return "", newTempError("%s does not advertise STARTTLS", host)
}
if _, _, err := c.cmd(220, "STARTTLS"); err != nil {
return "", err
}
tlsConn, err := tlsClientHandshake(ctx, conn, tlsServerName, host, tlsInsecure)
if err != nil {
return "", err
}
c.txt = textproto.NewConn(tlsConn)
if err := c.hello(m.Hostname); err != nil {
return "", err
}
}
}
if username != "" {
if err := c.authPlain(username, password); err != nil {
return "", fmt.Errorf("AUTH PLAIN with %s failed: %w", host, err)
}
}
// MAIL FROM with BODY=8BITMIME when the message contains 8-bit bytes and
@@ -276,6 +362,61 @@ func (m *Mailer) deliverToHost(host, from, to string, data []byte) (string, erro
return fmt.Sprintf("%d %s", code, msg), nil
}
// tlsClientHandshake upgrades a plain connection to TLS.
// Direct MX delivery passes insecure=true (opportunistic TLS: remote MX
// certificates often cannot be verified). Relays with AUTH credentials must
// pass insecure=false so the connection cannot be MITM'd.
func tlsClientHandshake(ctx context.Context, conn net.Conn, serverName, host string, insecure bool) (net.Conn, error) {
tlsConn := tls.Client(conn, &tls.Config{
ServerName: serverName,
InsecureSkipVerify: insecure,
})
if err := tlsConn.HandshakeContext(ctx); err != nil {
return nil, newTempError("TLS handshake with %s failed: %v", host, err)
}
return tlsConn, nil
}
// dialSMTP connects to a remote SMTP server, honoring the configured IP
// family and optional source address binding.
//
// The default is IPv4-only: many receiving systems (e.g. Gmail) reject mail
// from IPv6 addresses without PTR records, and the IPv4 address of a mail
// host usually has a forward-confirmed PTR and a matching SPF entry. Switch
// IPFamily to "ipv6"/"auto" after the ISP has configured a PTR for the
// source address and SourceIP binds the connection to that static address.
func (m *Mailer) dialSMTP(ctx context.Context, addr string) (net.Conn, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
network := "tcp4"
if ip := net.ParseIP(host); ip != nil {
// Literal destination: pick the matching family.
if ip.To4() == nil {
network = "tcp6"
}
} else {
switch strings.ToLower(m.IPFamily) {
case "ipv6":
network = "tcp6"
case "auto":
network = "tcp"
default: // "ipv4" and anything unrecognized
network = "tcp4"
}
}
dialer := &net.Dialer{Timeout: m.ConnectTimeout}
if m.SourceIP != "" {
if ip := net.ParseIP(m.SourceIP); ip != nil {
dialer.LocalAddr = &net.TCPAddr{IP: ip}
}
}
return dialer.DialContext(ctx, network, net.JoinHostPort(host, port))
}
// is8Bit reports whether the data contains any byte >= 0x80.
func is8Bit(data []byte) bool {
for _, b := range data {
@@ -288,8 +429,9 @@ func is8Bit(data []byte) bool {
// lookupMX resolves the MX hosts for a domain, sorted by preference.
// Per RFC 5321 section 5.1, when no MX record exists the domain itself is
// used as an implicit MX with preference 0.
func lookupMX(domain string) ([]string, error) {
// used as an implicit MX with preference 0. ipFamily controls the ordering
// of the A/AAAA fallback ("ipv6" puts IPv6 first, otherwise IPv4 first).
func lookupMX(domain, ipFamily string) ([]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
@@ -308,9 +450,19 @@ func lookupMX(domain string) ([]string, error) {
if err != nil {
return nil, err
}
hosts := make([]string, 0, len(ips))
var hosts []string
var v6 []string
for _, ip := range ips {
hosts = append(hosts, ip.String())
if ip.IP.To4() != nil {
hosts = append(hosts, ip.IP.String())
} else {
v6 = append(v6, ip.IP.String())
}
}
if strings.EqualFold(ipFamily, "ipv6") {
hosts = append(v6, hosts...)
} else {
hosts = append(hosts, v6...)
}
if len(hosts) == 0 {
return nil, fmt.Errorf("no MX or A records for %s", domain)
+292
View File
@@ -2,7 +2,16 @@ package outbound
import (
"bufio"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/pem"
"math/big"
"net"
"strconv"
"strings"
"testing"
"time"
@@ -225,3 +234,286 @@ func TestMailerPermanentFailure(t *testing.T) {
}
<-done
}
func TestMailerSmarthostRelay(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
defer ln.Close()
type result struct {
gotData []byte
authLine string
}
ch := make(chan result, 1)
go func() {
conn, err := ln.Accept()
if err != nil {
ch <- result{}
return
}
defer conn.Close()
r := bufio.NewReader(conn)
w := bufio.NewWriter(conn)
_, _ = w.WriteString("220 relay.test ESMTP\r\n")
_ = w.Flush()
var authLine string
var got []byte
for {
line, err := r.ReadString('\n')
if err != nil {
break
}
trimmed := strings.TrimRight(line, "\r\n")
up := strings.ToUpper(trimmed)
switch {
case strings.HasPrefix(up, "EHLO"):
_, _ = w.WriteString("250-relay.test\r\n250-8BITMIME\r\n250 AUTH PLAIN\r\n")
_ = w.Flush()
case strings.HasPrefix(up, "AUTH PLAIN"):
authLine = trimmed
_, _ = w.WriteString("235 2.0.0 ok\r\n")
_ = w.Flush()
case strings.HasPrefix(up, "MAIL FROM"):
if authLine == "" {
_, _ = w.WriteString("530 5.7.0 auth required\r\n")
_ = w.Flush()
break
}
_, _ = w.WriteString("250 ok\r\n")
_ = w.Flush()
case strings.HasPrefix(up, "RCPT TO"):
_, _ = w.WriteString("250 ok\r\n")
_ = w.Flush()
case strings.HasPrefix(up, "DATA"):
_, _ = w.WriteString("354 go\r\n")
_ = w.Flush()
for {
dl, err := r.ReadString('\n')
if err != nil {
break
}
if strings.TrimRight(dl, "\r\n") == "." {
break
}
if strings.HasPrefix(dl, "..") {
dl = dl[1:]
}
got = append(got, []byte(dl)...)
}
_, _ = w.WriteString("250 queued\r\n")
_ = w.Flush()
case strings.HasPrefix(up, "QUIT"):
_, _ = w.WriteString("221 bye\r\n")
_ = w.Flush()
ch <- result{gotData: got, authLine: authLine}
return
}
}
ch <- result{}
}()
m := NewMailer("mail.lmve.net", 10*time.Second)
m.Relay = &RelayConfig{
Host: "127.0.0.1",
Port: ln.Addr().(*net.TCPAddr).Port,
Username: "relay-user",
Password: "relay-pass",
StartTLS: false,
}
// The recipient domain does not even exist — with a relay configured,
// no MX lookup happens and the relay still receives the message.
input := []byte("From: a@lmve.net\r\nTo: b@bogus-domain.invalid\r\nSubject: relay\r\n\r\nbody\r\n")
resp, err := m.Deliver("a@lmve.net", "b@bogus-domain.invalid", input)
if err != nil {
t.Fatalf("Deliver via relay: %v", err)
}
if !strings.HasPrefix(resp, "250") {
t.Fatalf("unexpected relay response: %q", resp)
}
res := <-ch
if res.authLine == "" {
t.Fatal("relay did not receive AUTH PLAIN")
}
wantAuth := "AUTH PLAIN " + base64.StdEncoding.EncodeToString([]byte("\x00relay-user\x00relay-pass"))
if res.authLine != wantAuth {
t.Fatalf("auth line mismatch: got %q want %q", res.authLine, wantAuth)
}
if string(res.gotData) != string(input) {
t.Fatalf("relay data mismatch.\ngot: %q\nwant: %q", res.gotData, input)
}
}
// startTLSSMTPServer 起一个支持 STARTTLS 的 SMTP 服务器(自签证书),
// 供 relay TLS 验证测试使用:未升级 TLS 时广告 STARTTLS 能力,
// 收到 STARTTLS 后升级为 TLS 并重新 EHLO。
func startTLSSMTPServer(t *testing.T) (addr string, cleanup func()) {
t.Helper()
cert, err := tls.X509KeyPair(makeSelfSignedCertPEM(t))
if err != nil {
t.Fatalf("load self-signed cert: %v", err)
}
tlsCfg := &tls.Config{Certificates: []tls.Certificate{cert}}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
go func() {
for {
conn, err := ln.Accept()
if err != nil {
return
}
go func() {
defer conn.Close()
r := bufio.NewReader(conn)
w := bufio.NewWriter(conn)
_, _ = w.WriteString("220 relay.test ESMTP ready\r\n")
_ = w.Flush()
for {
line, err := r.ReadString('\n')
if err != nil {
return
}
up := strings.ToUpper(strings.TrimRight(line, "\r\n"))
switch {
case strings.HasPrefix(up, "STARTTLS"):
_, _ = w.WriteString("220 2.0.0 ready to start TLS\r\n")
_ = w.Flush()
tlsConn := tls.Server(conn, tlsCfg)
if err := tlsConn.Handshake(); err != nil {
return
}
conn = tlsConn
r = bufio.NewReader(conn)
w = bufio.NewWriter(conn)
case strings.HasPrefix(up, "EHLO"), strings.HasPrefix(up, "HELO"):
_, _ = w.WriteString("250-relay.test\r\n250-STARTTLS\r\n250 8BITMIME\r\n")
_ = w.Flush()
case strings.HasPrefix(up, "AUTH PLAIN"):
_, _ = w.WriteString("235 2.0.0 ok\r\n")
_ = w.Flush()
case strings.HasPrefix(up, "DATA"):
_, _ = w.WriteString("354 go ahead\r\n")
_ = w.Flush()
for {
dl, err := r.ReadString('\n')
if err != nil {
return
}
if strings.TrimRight(dl, "\r\n") == "." {
break
}
}
_, _ = w.WriteString("250 2.0.0 queued\r\n")
_ = w.Flush()
case strings.HasPrefix(up, "QUIT"):
_, _ = w.WriteString("221 bye\r\n")
_ = w.Flush()
return
default:
_, _ = w.WriteString("250 ok\r\n")
_ = w.Flush()
}
}
}()
}
}()
addr = ln.Addr().String()
return addr, func() { ln.Close() }
}
// makeSelfSignedCertPEM 生成一对自签证书(CN=relay.test)。
func makeSelfSignedCertPEM(t *testing.T) (certPEM, keyPEM []byte) {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("generate key: %v", err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "relay.test"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
DNSNames: []string{"relay.test"},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatalf("create certificate: %v", err)
}
certPEM = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
keyPEM = pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
return certPEM, keyPEM
}
// TestMailerRelayRejectsUntrustedCert 中继使用自签证书时默认必须拒绝
// (证书验证开启,防止凭据被中间人截获)。
func TestMailerRelayRejectsUntrustedCert(t *testing.T) {
addr, cleanup := startTLSSMTPServer(t)
defer cleanup()
m := NewMailer("mail.lmve.net", 5*time.Second)
m.Relay = &RelayConfig{
Host: "127.0.0.1",
Port: mustPort(t, addr),
Username: "relay-user",
Password: "relay-pass",
TLSInsecure: false,
}
input := []byte("From: a@lmve.net\r\nTo: b@bogus-domain.invalid\r\nSubject: relay\r\n\r\nbody\r\n")
_, err := m.Deliver("a@lmve.net", "b@bogus-domain.invalid", input)
if err == nil {
t.Fatal("relay with untrusted self-signed cert should be rejected")
}
if !strings.Contains(err.Error(), "certificate") {
t.Fatalf("expected certificate verification error, got: %v", err)
}
}
// TestMailerRelayInsecureSkipsVerification 显式开启 relay_tls_insecure
// 后,自签证书的中继可以完成 TLS 握手并进入 SMTP 会话。
func TestMailerRelayInsecureSkipsVerification(t *testing.T) {
addr, cleanup := startTLSSMTPServer(t)
defer cleanup()
m := NewMailer("mail.lmve.net", 5*time.Second)
m.Relay = &RelayConfig{
Host: "127.0.0.1",
Port: mustPort(t, addr),
Username: "relay-user",
Password: "relay-pass",
TLSInsecure: true,
}
input := []byte("From: a@lmve.net\r\nTo: b@bogus-domain.invalid\r\nSubject: relay\r\n\r\nbody\r\n")
resp, err := m.Deliver("a@lmve.net", "b@bogus-domain.invalid", input)
if err != nil {
t.Fatalf("relay with TLSInsecure should proceed: %v", err)
}
if !strings.HasPrefix(resp, "250") {
t.Fatalf("unexpected response: %q", resp)
}
}
func mustPort(t *testing.T, addr string) int {
t.Helper()
_, portStr, err := net.SplitHostPort(addr)
if err != nil {
t.Fatalf("split %q: %v", addr, err)
}
port, err := strconv.Atoi(portStr)
if err != nil {
t.Fatalf("port %q: %v", portStr, err)
}
return port
}
+125 -18
View File
@@ -16,8 +16,13 @@ import (
)
// Manager orchestrates the outbound delivery queue: enqueueing messages,
// background delivery worker, exponential backoff retries, DKIM signing,
// per-user rate limits and failure bounces.
// concurrent background delivery workers, exponential backoff retries, DKIM
// signing, per-user rate limits and failure bounces.
//
// 并发模型:一个 dispatcher goroutine 周期性扫描队列并原子抢占(Claim)
// 待投递项,投递给 worker 池(默认 4 个 goroutine)并行发送;同一收件域
// (或中继)的连接数受 max_concurrent_per_domain 限制。workers=0/1 时退
// 化为串行投递(旧行为)。
type Manager struct {
cfg config.OutboundConfig
hostname string // EHLO hostname
@@ -31,7 +36,12 @@ type Manager struct {
wg sync.WaitGroup
mu sync.Mutex
lim map[uint]*userWindow
batch int
jobs chan *db.OutboundMessage
domMu sync.Mutex
dom map[string]chan struct{} // 每域并发信号量
// deliver 执行单封投递,默认走 m.mailer.Deliver;测试可注入替换。
deliver func(from, to string, data []byte) (string, error)
}
// userWindow tracks a user's sending rate within fixed windows.
@@ -45,6 +55,14 @@ type userWindow struct {
// NewManager creates an outbound delivery Manager.
// hostname is the EHLO name presented to remote servers (defaults to "localhost").
func NewManager(cfg config.OutboundConfig, hostname string, stores *store.Stores) *Manager {
batchSize := cfg.BatchSize
if batchSize <= 0 {
batchSize = 50
}
workers := cfg.Workers
if workers <= 1 {
workers = 1
}
m := &Manager{
cfg: cfg,
hostname: hostname,
@@ -54,24 +72,48 @@ func NewManager(cfg config.OutboundConfig, hostname string, stores *store.Stores
stop: make(chan struct{}),
done: make(chan struct{}),
lim: make(map[uint]*userWindow),
batch: 50,
jobs: make(chan *db.OutboundMessage, batchSize),
dom: make(map[string]chan struct{}),
}
m.deliver = m.mailer.Deliver
m.mailer.IPFamily = cfg.IPFamily
m.mailer.SourceIP = cfg.SourceIP
if cfg.SourceIP != "" {
log.Printf("outbound: binding source address %s (ip_family=%s)", cfg.SourceIP, cfg.IPFamily)
}
if cfg.RelayHost != "" {
m.mailer.Relay = &RelayConfig{
Host: cfg.RelayHost,
Port: cfg.RelayPort,
Username: cfg.RelayUser,
Password: cfg.RelayPassword,
StartTLS: cfg.RelayStartTLS,
TLSInsecure: cfg.RelayTLSInsecure,
}
log.Printf("outbound: using smarthost relay %s:%d", cfg.RelayHost, cfg.RelayPort)
}
log.Printf("outbound: %d delivery workers, batch=%d, per-domain concurrency=%d",
workers, batchSize, cfg.MaxConcurrentPerDomain)
return m
}
// Start launches the background delivery worker.
// Start launches the dispatcher and the delivery worker pool.
func (m *Manager) Start() {
interval := time.Duration(m.cfg.PollInterval) * time.Second
if interval <= 0 {
interval = 15 * time.Second
}
// 调度者:启动时立即扫描一次(清空积压),之后按周期扫描 +
// 原子抢占待投递项,投递给 worker 池。
m.wg.Add(1)
go func() {
defer m.wg.Done()
ticker := time.NewTicker(interval)
defer ticker.Stop()
log.Printf("outbound: delivery worker started (interval=%s, max_attempts=%d)", interval, m.cfg.MaxAttempts)
log.Printf("outbound: dispatcher started (interval=%s, max_attempts=%d)", interval, m.cfg.MaxAttempts)
m.processDue()
for {
select {
case <-ticker.C:
@@ -79,14 +121,30 @@ func (m *Manager) Start() {
case <-m.kick:
m.processDue()
case <-m.stop:
close(m.jobs)
close(m.done)
return
}
}
}()
// Worker 池:并行投递。
workers := m.cfg.Workers
if workers <= 1 {
workers = 1
}
for i := 0; i < workers; i++ {
m.wg.Add(1)
go func() {
defer m.wg.Done()
for job := range m.jobs {
m.deliverOne(job)
}
}()
}
}
// Stop gracefully stops the delivery worker.
// Stop gracefully stops the dispatcher and workers.
func (m *Manager) Stop() {
m.once.Do(func() {
close(m.stop)
@@ -226,28 +284,77 @@ func (m *Manager) checkRateLimit(userID uint) error {
return nil
}
// processDue attempts delivery of all due queue items.
// processDue scans the queue and dispatches due items to the worker pool.
// Each item is atomically claimed (status -> sending) before dispatch so
// that concurrent workers never deliver the same message twice. When all
// workers are busy the dispatcher blocks here, naturally throttling claims;
// remaining due items are picked up on the next scan.
func (m *Manager) processDue() {
items, err := m.stores.Outbound.ListDue(time.Now(), m.batch)
batchSize := m.cfg.BatchSize
if batchSize <= 0 {
batchSize = 50
}
items, err := m.stores.Outbound.ListDue(time.Now(), batchSize)
if err != nil {
log.Printf("outbound: loading due queue failed: %v", err)
return
}
for i := range items {
m.deliverOne(&items[i])
claimed, err := m.stores.Outbound.Claim(items[i].ID)
if err != nil {
log.Printf("outbound: claim item %d failed: %v", items[i].ID, err)
continue
}
if !claimed {
// 已被其他调度周期抢占(并发下应不会发生,防御性跳过)
continue
}
item := items[i]
item.Status = db.OutboundStatusSending
m.jobs <- &item
}
}
// deliverOne performs a single delivery attempt for a queue item.
func (m *Manager) deliverOne(item *db.OutboundMessage) {
// Mark as sending to avoid concurrent workers double-delivering.
item.Status = db.OutboundStatusSending
if err := m.stores.Outbound.Update(item); err != nil {
log.Printf("outbound: update item %d to sending failed: %v", item.ID, err)
return
// acquireDomain 获取收件域(或中继)的并发信号量,限制对同一目标域同时
// 打开的 SMTP 连接数。limit <= 0 表示不限制。
func (m *Manager) acquireDomain(domain string) func() {
limit := m.cfg.MaxConcurrentPerDomain
if limit <= 0 {
return func() {}
}
resp, err := m.mailer.Deliver(item.FromAddr, item.ToAddr, []byte(item.RawData))
m.domMu.Lock()
sem := m.dom[domain]
if sem == nil {
sem = make(chan struct{}, limit)
m.dom[domain] = sem
}
m.domMu.Unlock()
sem <- struct{}{}
return func() { <-sem }
}
// deliverKey 返回并发限制使用的目标标识:配置了中继时所有连接都打向同一
// smarthost,统一按 "relay" 限制;否则按收件域名限制。
func (m *Manager) deliverKey(to string) string {
if m.mailer.Relay != nil && m.mailer.Relay.Host != "" {
return "relay"
}
at := strings.LastIndex(to, "@")
if at < 0 || at == len(to)-1 {
return ""
}
return strings.ToLower(to[at+1:])
}
// deliverOne performs a single delivery attempt for a queue item.
// 调用前该项已被原子抢占为 sending,此处不再重复置位。
func (m *Manager) deliverOne(item *db.OutboundMessage) {
release := m.acquireDomain(item.ToAddr)
defer release()
resp, err := m.deliver(item.FromAddr, item.ToAddr, []byte(item.RawData))
now := time.Now()
item.Attempts++
+332
View File
@@ -0,0 +1,332 @@
package outbound
import (
"errors"
"path/filepath"
"sync"
"sync/atomic"
"testing"
"time"
"mail_go/config"
"mail_go/internal/db"
"mail_go/internal/store"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// newTestManagerStores 创建带 outbound_messages 表的测试数据库。
func newTestManagerStores(t *testing.T) *store.Stores {
t.Helper()
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.OutboundMessage{}); err != nil {
t.Fatalf("migrate: %v", err)
}
return store.NewStores(gdb)
}
// seedPending 写入 n 条立即可投递的 pending 队列项。
func seedPending(t *testing.T, stores *store.Stores, n int) []uint {
t.Helper()
ids := make([]uint, 0, n)
for i := 0; i < n; i++ {
item := &db.OutboundMessage{
MessageID: "<seed@test>",
FromAddr: "sender@test.local",
ToAddr: "rcpt@fake.test",
RecipientDom: "fake.test",
RawData: "From: sender@test.local\r\nTo: rcpt@fake.test\r\nSubject: t\r\n\r\nbody\r\n",
Status: db.OutboundStatusPending,
Attempts: 0,
NextAttemptAt: time.Now(),
}
if err := stores.Outbound.Create(item); err != nil {
t.Fatalf("create item: %v", err)
}
ids = append(ids, item.ID)
}
return ids
}
// countByStatus 统计队列中指定状态的项数。
func countByStatus(t *testing.T, stores *store.Stores, status string) int64 {
t.Helper()
n, err := stores.Outbound.CountByStatus(status)
if err != nil {
t.Fatalf("count %s: %v", status, err)
}
return n
}
// waitFor 轮询等待条件满足或超时。
func waitFor(t *testing.T, timeout time.Duration, desc string, cond func() bool) {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if cond() {
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatalf("timed out waiting for %s", desc)
}
// concurrencyTracker 统计并发调用数(用于验证 worker 池与每域信号量)。
type concurrencyTracker struct {
mu sync.Mutex
active int
peak int
deliveries int
err error
}
// deliverWithDelay 返回一个带固定延迟的注入投递函数,并统计并发峰值。
func deliverWithDelay(tr *concurrencyTracker, delay time.Duration) func(string, string, []byte) (string, error) {
return func(from, to string, data []byte) (string, error) {
tr.mu.Lock()
tr.active++
if tr.active > tr.peak {
tr.peak = tr.active
}
tr.mu.Unlock()
time.Sleep(delay)
tr.mu.Lock()
tr.active--
tr.deliveries++
tr.mu.Unlock()
return "250 2.0.0 queued", nil
}
}
// TestManagerConcurrentDelivery 验证 worker 池真并发投递且每封只投一次。
func TestManagerConcurrentDelivery(t *testing.T) {
stores := newTestManagerStores(t)
seedPending(t, stores, 12)
tr := &concurrencyTracker{}
cfg := config.OutboundConfig{
PollInterval: 1,
MaxAttempts: 5,
RetryBaseMin: 1,
MaxPerDay: 10000,
Workers: 4,
BatchSize: 50,
ConnectTimeout: 10,
}
m := NewManager(cfg, "test.local", stores)
m.deliver = deliverWithDelay(tr, 150*time.Millisecond)
m.Start()
t.Cleanup(m.Stop)
waitFor(t, 15*time.Second, "all items sent", func() bool {
return countByStatus(t, stores, db.OutboundStatusSent) == 12
})
if tr.peak < 2 {
t.Fatalf("expected concurrent deliveries (peak=%d), got serial behavior", tr.peak)
}
if tr.deliveries != 12 {
t.Fatalf("deliveries = %d, want 12 (each message exactly once)", tr.deliveries)
}
if n := countByStatus(t, stores, db.OutboundStatusPending) + countByStatus(t, stores, db.OutboundStatusDeferred); n != 0 {
t.Fatalf("%d items still pending/deferred", n)
}
}
// TestManagerSerialFallback 验证 workers=1 时退化为串行(旧行为)。
func TestManagerSerialFallback(t *testing.T) {
stores := newTestManagerStores(t)
seedPending(t, stores, 6)
tr := &concurrencyTracker{}
cfg := config.OutboundConfig{
PollInterval: 1,
MaxAttempts: 5,
RetryBaseMin: 1,
MaxPerDay: 10000,
Workers: 1,
BatchSize: 50,
ConnectTimeout: 10,
}
m := NewManager(cfg, "test.local", stores)
m.deliver = deliverWithDelay(tr, 50*time.Millisecond)
m.Start()
t.Cleanup(m.Stop)
waitFor(t, 15*time.Second, "all items sent", func() bool {
return countByStatus(t, stores, db.OutboundStatusSent) == 6
})
if tr.peak > 1 {
t.Fatalf("workers=1 must be serial, peak=%d", tr.peak)
}
}
// TestManagerDomainLimit 验证同一收件域的并发连接数不超过上限。
func TestManagerDomainLimit(t *testing.T) {
stores := newTestManagerStores(t)
seedPending(t, stores, 8)
tr := &concurrencyTracker{}
cfg := config.OutboundConfig{
PollInterval: 1,
MaxAttempts: 5,
RetryBaseMin: 1,
MaxPerDay: 10000,
Workers: 8,
BatchSize: 50,
MaxConcurrentPerDomain: 1,
ConnectTimeout: 10,
}
m := NewManager(cfg, "test.local", stores)
m.deliver = deliverWithDelay(tr, 100*time.Millisecond)
m.Start()
t.Cleanup(m.Stop)
waitFor(t, 15*time.Second, "all items sent", func() bool {
return countByStatus(t, stores, db.OutboundStatusSent) == 8
})
if tr.peak > 1 {
t.Fatalf("per-domain limit 1 violated: peak=%d", tr.peak)
}
if tr.deliveries != 8 {
t.Fatalf("deliveries = %d, want 8", tr.deliveries)
}
}
// TestManagerRetriesTemporaryFailure 验证临时失败进入退避重试(deferred)。
func TestManagerRetriesTemporaryFailure(t *testing.T) {
stores := newTestManagerStores(t)
seedPending(t, stores, 1)
cfg := config.OutboundConfig{
PollInterval: 1,
MaxAttempts: 3,
RetryBaseMin: 1,
MaxPerDay: 10000,
Workers: 2,
BatchSize: 50,
ConnectTimeout: 10,
}
m := NewManager(cfg, "test.local", stores)
m.deliver = func(from, to string, data []byte) (string, error) {
return "", newTempError("connection refused")
}
m.Start()
t.Cleanup(m.Stop)
waitFor(t, 15*time.Second, "item deferred", func() bool {
return countByStatus(t, stores, db.OutboundStatusDeferred) == 1
})
items, _, err := stores.Outbound.List(1, 10, db.OutboundStatusDeferred)
if err != nil || len(items) != 1 {
t.Fatalf("list deferred: %v (n=%d)", err, len(items))
}
if items[0].Attempts != 1 {
t.Fatalf("attempts = %d, want 1", items[0].Attempts)
}
if items[0].LastError == "" {
t.Fatal("expected last error recorded")
}
}
// TestManagerPermanentFailureBouncesAndFails 验证永久失败直接标记 failed。
func TestManagerPermanentFailureBouncesAndFails(t *testing.T) {
stores := newTestManagerStores(t)
seedPending(t, stores, 1)
cfg := config.OutboundConfig{
PollInterval: 1,
MaxAttempts: 3,
RetryBaseMin: 1,
MaxPerDay: 10000,
Workers: 2,
BatchSize: 50,
ConnectTimeout: 10,
}
m := NewManager(cfg, "test.local", stores)
m.deliver = func(from, to string, data []byte) (string, error) {
return "", newPermError("550 recipient rejected")
}
m.Start()
t.Cleanup(m.Stop)
waitFor(t, 15*time.Second, "item failed", func() bool {
return countByStatus(t, stores, db.OutboundStatusFailed) == 1
})
if n := countByStatus(t, stores, db.OutboundStatusDeferred); n != 0 {
t.Fatalf("permanent failure must not defer: %d deferred", n)
}
}
// TestManagerDomainLimitNoLimit 验证 max_concurrent_per_domain=0 不限制并发。
func TestManagerDomainLimitNoLimit(t *testing.T) {
stores := newTestManagerStores(t)
seedPending(t, stores, 8)
tr := &concurrencyTracker{}
cfg := config.OutboundConfig{
PollInterval: 1,
MaxAttempts: 5,
RetryBaseMin: 1,
MaxPerDay: 10000,
Workers: 8,
BatchSize: 50,
ConnectTimeout: 10,
}
m := NewManager(cfg, "test.local", stores)
m.deliver = deliverWithDelay(tr, 80*time.Millisecond)
m.Start()
t.Cleanup(m.Stop)
waitFor(t, 15*time.Second, "all items sent", func() bool {
return countByStatus(t, stores, db.OutboundStatusSent) == 8
})
if tr.peak < 2 {
t.Fatalf("expected concurrent deliveries without domain limit, peak=%d", tr.peak)
}
}
// TestManagerDeliverErrorsPropagate 防御:投递函数报错不影响 worker 存活,
// 错误项进入重试(deferred)。
func TestManagerDeliverErrorsPropagate(t *testing.T) {
stores := newTestManagerStores(t)
seedPending(t, stores, 3)
var calls atomic.Int32
cfg := config.OutboundConfig{
PollInterval: 1,
MaxAttempts: 2,
RetryBaseMin: 1,
MaxPerDay: 10000,
Workers: 2,
BatchSize: 50,
ConnectTimeout: 10,
}
m := NewManager(cfg, "test.local", stores)
m.deliver = func(from, to string, data []byte) (string, error) {
n := calls.Add(1)
if n%2 == 0 {
return "", errors.New("boom")
}
return "250 ok", nil
}
m.Start()
t.Cleanup(m.Stop)
waitFor(t, 15*time.Second, "queue settled", func() bool {
return countByStatus(t, stores, db.OutboundStatusSent)+countByStatus(t, stores, db.OutboundStatusDeferred) == 3
})
if calls.Load() != 3 {
t.Fatalf("deliver calls = %d, want 3 (once per item)", calls.Load())
}
if n := countByStatus(t, stores, db.OutboundStatusFailed); n != 0 {
t.Fatalf("unexpected failed items: %d", n)
}
}
+164 -25
View File
@@ -12,32 +12,37 @@ import (
"time"
"mail_go/config"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/imap_server"
"mail_go/internal/store"
"mail_go/internal/tlsutil"
)
// POP3Server implements a simple POP3 mail server over TCP.
type POP3Server struct {
listener net.Listener
stores *store.Stores
cfg config.POP3Config
wg sync.WaitGroup
listener net.Listener
stores *store.Stores
cfg config.POP3Config
banCfg config.BanConfig
tlsLoader *tlsutil.Loader
hub *connhub.Hub
pusher imap_server.Pusher // 邮件删除推送(IMAP 客户端同步),可空
wg sync.WaitGroup
}
// NewPOP3Server creates a new POP3 server instance.
func NewPOP3Server(cfg config.POP3Config, stores *store.Stores) *POP3Server {
return &POP3Server{stores: stores, cfg: cfg}
// NewPOP3Server creates a new POP3 server instance. tlsLoader may be nil
// when TLS is not configured.
func NewPOP3Server(cfg config.POP3Config, stores *store.Stores, tlsLoader *tlsutil.Loader, banCfg config.BanConfig, hub *connhub.Hub, pusher imap_server.Pusher) *POP3Server {
return &POP3Server{stores: stores, cfg: cfg, banCfg: banCfg, tlsLoader: tlsLoader, hub: hub, pusher: pusher}
}
func (s *POP3Server) tlsConfig() (*tls.Config, error) {
if s.cfg.TLSCert == "" || s.cfg.TLSKey == "" {
if s.tlsLoader == nil {
return nil, fmt.Errorf("POP3 TLS certificate or key not configured")
}
cert, err := tls.LoadX509KeyPair(s.cfg.TLSCert, s.cfg.TLSKey)
if err != nil {
return nil, fmt.Errorf("load POP3 TLS certificate failed: %w", err)
}
return &tls.Config{Certificates: []tls.Certificate{cert}}, nil
// GetCertificate 每次握手按需重载证书,证书更新后无需重启服务
return &tls.Config{GetCertificate: s.tlsLoader.GetCertificate}, nil
}
// Start starts the POP3 server on the configured plain-text port.
@@ -47,6 +52,7 @@ func (s *POP3Server) Start() error {
if err != nil {
return fmt.Errorf("POP3 listen failed: %w", err)
}
port := parseAddrPort(s.cfg.Addr)
log.Printf("POP3 server listening on %s", s.cfg.Addr)
@@ -61,7 +67,7 @@ func (s *POP3Server) Start() error {
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.handleConn(conn)
s.handleConn(conn, port)
}()
}
}()
@@ -80,6 +86,7 @@ func (s *POP3Server) StartTLS() error {
if err != nil {
return fmt.Errorf("POP3 TLS listen failed: %w", err)
}
port := parseAddrPort(s.cfg.TLSAddr)
log.Printf("POP3 TLS server listening on %s", s.cfg.TLSAddr)
@@ -94,7 +101,7 @@ func (s *POP3Server) StartTLS() error {
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.handleConn(conn)
s.handleConn(conn, port)
}()
}
}()
@@ -102,23 +109,67 @@ func (s *POP3Server) StartTLS() error {
return nil
}
// parseAddrPort 从监听地址解析端口号,失败返回 0。
func parseAddrPort(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
}
// handleConn handles a single POP3 client connection.
func (s *POP3Server) handleConn(conn net.Conn) {
func (s *POP3Server) handleConn(conn net.Conn, port int) {
defer conn.Close()
conn.SetDeadline(time.Now().Add(10 * time.Minute))
clientIP := store.ClientIPFromAddr(conn.RemoteAddr())
startedAt := time.Now()
// 已封禁 IP 直接拒绝(防协议层暴力破解)
if banned, _ := s.stores.Bans.IsBanned(clientIP); banned {
sendResponse(conn, "-ERR access denied")
s.writeProtocolLog(port, clientIP, "", false, "IP已被封禁", "连接被拒绝", 0, 0, startedAt)
return
}
// 连接追踪:注册到当前连接中心,连接结束时注销;
// 强制断开:关闭底层连接(STLS 后 conn 变量已指向 tlsConn,同样生效)。
activeConn := s.hub.Register("pop3", clientIP, port, false)
if activeConn != nil {
activeConn.SetDisconnect(func() { _ = conn.Close() })
}
// 会话状态(供协议日志汇总)
var (
authUser *db.User
authUsername string
authFailReason string
commandCount = make(map[string]int)
deletedCount int
)
reader := bufio.NewReader(conn)
var user *db.User
var messages []pop3Message
var deleted map[int]bool
tlsActive := false
defer func() {
if activeConn != nil {
activeConn.Close()
}
}()
sendResponse(conn, "+OK MailGo POP3 server ready")
for {
line, err := reader.ReadString('\n')
if err != nil {
return
break
}
line = strings.TrimSpace(line)
@@ -128,12 +179,14 @@ func (s *POP3Server) handleConn(conn net.Conn) {
parts := strings.SplitN(line, " ", 2)
cmd := strings.ToUpper(parts[0])
commandCount[cmd]++
activeConn.Touch()
arg := ""
if len(parts) > 1 {
arg = strings.TrimSpace(parts[1])
}
authenticated := user != nil && user.ID != 0
authenticated := authUser != nil && authUser.ID != 0
if !authenticated && requiresAuth(cmd) {
sendResponse(conn, "-ERR authentication required")
continue
@@ -141,9 +194,18 @@ func (s *POP3Server) handleConn(conn net.Conn) {
switch cmd {
case "USER":
user, messages, deleted = s.handleUSER(conn, arg, user)
authUsername = arg
authUser, messages, deleted = s.handleUSER(conn, arg, authUser)
case "PASS":
user, messages, deleted = s.handlePASS(conn, arg, user)
authUser, messages, deleted = s.handlePASS(conn, arg, authUser)
if authUser == nil || authUser.ID == 0 {
if authFailReason == "" {
authFailReason = "用户名或密码错误"
}
} else {
authFailReason = ""
activeConn.SetUser(authUsername)
}
case "STAT":
s.handleSTAT(conn, messages, deleted)
case "LIST":
@@ -158,8 +220,11 @@ func (s *POP3Server) handleConn(conn net.Conn) {
deleted = make(map[int]bool)
sendResponse(conn, "+OK")
case "QUIT":
s.expungeDeleted(messages, deleted, user)
deletedCount = s.expungeDeleted(messages, deleted, authUser)
sendResponse(conn, "+OK MailGo POP3 server signing off")
s.writeProtocolLog(port, clientIP, authUsername, authUser != nil && authUser.ID != 0, authFailReason,
pop3CommandDetail(commandCount, deletedCount), deletedCount,
time.Since(startedAt).Milliseconds(), time.Now())
return
case "CAPA":
s.handleCAPA(conn, tlsActive)
@@ -180,11 +245,14 @@ func (s *POP3Server) handleConn(conn net.Conn) {
sendResponse(conn, "+OK Begin TLS negotiation")
tlsConn := tls.Server(conn, tlsConfig)
if err := tlsConn.Handshake(); err != nil {
s.writeProtocolLog(port, clientIP, authUsername, false, "TLS 握手失败",
pop3CommandDetail(commandCount, 0), 0, time.Since(startedAt).Milliseconds(), time.Now())
return
}
conn = tlsConn
reader = bufio.NewReader(conn)
tlsActive = true
activeConn.SetTLS(true)
case "TOP":
s.handleTOP(conn, arg, messages, deleted)
case "UIDL":
@@ -193,6 +261,56 @@ func (s *POP3Server) handleConn(conn net.Conn) {
sendResponse(conn, "-ERR unknown command")
}
}
// 连接异常结束(未 QUIT
success := authUser != nil && authUser.ID != 0 && authFailReason == ""
if success && authFailReason == "" && authUsername == "" && len(commandCount) == 0 {
success = true
}
s.writeProtocolLog(port, clientIP, authUsername, success, authFailReason,
pop3CommandDetail(commandCount, 0), 0, time.Since(startedAt).Milliseconds(), time.Now())
}
// pop3CommandDetail 汇总会话中执行的命令为可读摘要(计数,忽略 NOOP/CAPA)。
func pop3CommandDetail(counts map[string]int, deletedCount int) string {
var parts []string
for _, c := range []string{"USER", "PASS", "STAT", "LIST", "RETR", "TOP", "UIDL", "DELE", "RSET", "STLS", "QUIT"} {
n := counts[c]
if n == 0 {
continue
}
if n == 1 {
parts = append(parts, c)
} else {
parts = append(parts, fmt.Sprintf("%s×%d", c, n))
}
}
if deletedCount > 0 {
parts = append(parts, fmt.Sprintf("删除%d", deletedCount))
}
if len(parts) == 0 {
return "连接建立,无命令"
}
return strings.Join(parts, " ")
}
// writeProtocolLog 写入一条 POP3 协议调用日志。
func (s *POP3Server) writeProtocolLog(port int, ip, username string, success bool, failReason, detail string, msgCount int, durationMs int64, at time.Time) {
entry := &db.ProtocolLog{
Protocol: db.ProtocolPOP3,
Port: port,
ClientIP: ip,
Username: username,
Success: success,
FailReason: failReason,
Detail: detail,
MsgCount: msgCount,
DurationMs: durationMs,
CreatedAt: at,
}
if err := s.stores.ProtocolLogs.Create(entry); err != nil {
log.Printf("POP3: 写入协议日志失败: %v", err)
}
}
func requiresAuth(cmd string) bool {
@@ -261,6 +379,9 @@ func (s *POP3Server) handleUSER(conn net.Conn, username string, currentUser *db.
return &db.User{Username: username}, nil, nil
}
// 保留完整的邮箱地址作为登录标识(PASS 阶段用 Authenticate 校验),
// user.ID 用于后续加载邮件。
user.Username = username
sendResponse(conn, "+OK")
return user, nil, nil
}
@@ -272,12 +393,19 @@ func (s *POP3Server) handlePASS(conn net.Conn, password string, user *db.User) (
return nil, nil, nil
}
clientIP := store.ClientIPFromAddr(conn.RemoteAddr())
authUser, err := s.stores.Users.Authenticate(user.Username, password)
if err != nil {
// 认证失败计数,达到阈值按档位封禁(与 Web 登录共用 ban_entries
s.stores.RecordAuthFailure(clientIP, s.banCfg.MaxFailAttempts, s.banCfg.BanDurationMin, "邮件协议认证失败次数过多")
sendResponse(conn, "-ERR authentication failed")
return nil, nil, nil
}
// 保留完整邮箱作为登录标识(与 handleUSER 一致),便于推送/日志使用
authUser.Username = user.Username
messages := s.loadMessages(authUser)
deleted := make(map[int]bool)
sendResponse(conn, fmt.Sprintf("+OK authenticated, %d messages", len(messages)))
@@ -412,18 +540,29 @@ func (s *POP3Server) handleUIDL(conn net.Conn, arg string, messages []pop3Messag
sendResponse(conn, fmt.Sprintf("+OK %d %d", num, messages[num-1].id))
}
// expungeDeleted actually deletes messages that were marked for deletion.
func (s *POP3Server) expungeDeleted(messages []pop3Message, deleted map[int]bool, user *db.User) {
// expungeDeleted actually deletes messages that were marked for deletion,
// returning the number of messages deleted. 删除成功后向 IMAP 客户端推送
// Expunge 通知(序号为删除前在 INBOX 中的位置)。
func (s *POP3Server) expungeDeleted(messages []pop3Message, deleted map[int]bool, user *db.User) int {
if deleted == nil || user == nil || user.ID == 0 {
return
return 0
}
count := 0
var seqs []uint32
for seqNum, msgDeleted := range deleted {
if msgDeleted && seqNum >= 1 && seqNum <= len(messages) {
if err := s.stores.Mails.Delete(messages[seqNum-1].id); err != nil {
log.Printf("POP3: failed to delete message %d: %v", messages[seqNum-1].id, err)
continue
}
count++
seqs = append(seqs, uint32(seqNum))
}
}
if s.pusher != nil && len(seqs) > 0 && user.Username != "" {
s.pusher.PushExpunged(user.Username, "INBOX", seqs)
}
return count
}
// sendResponse writes a POP3 response line to the connection.
+249
View File
@@ -0,0 +1,249 @@
package pop3_server
import (
"bufio"
"net"
"strings"
"testing"
"time"
"mail_go/config"
"mail_go/internal/db"
"mail_go/internal/store"
"golang.org/x/crypto/bcrypt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func newTestServer(t *testing.T) *POP3Server {
t.Helper()
gdb, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}, &db.Attachment{}, &db.BanEntry{}, &db.OutboundMessage{}, &db.ProtocolLog{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
return &POP3Server{
stores: stores,
cfg: config.POP3Config{},
banCfg: config.BanConfig{MaxFailAttempts: 5, BanDurationMin: 30},
}
}
// TestHandleConnLogsAuthFailure 验证认证失败的 POP3 会话写入协议日志。
func TestHandleConnLogsAuthFailure(t *testing.T) {
s := newTestServer(t)
server, client := net.Pipe()
defer server.Close()
defer client.Close()
done := make(chan struct{})
go func() {
defer close(done)
s.handleConn(server, 110)
}()
br := bufio.NewReader(client)
// 等待 greeting
if _, err := br.ReadString('\n'); err != nil {
t.Fatalf("greeting: %v", err)
}
client.Write([]byte("USER no-such-user\r\n"))
br.ReadString('\n')
client.Write([]byte("PASS wrong-pass\r\n"))
br.ReadString('\n')
client.Write([]byte("QUIT\r\n"))
br.ReadString('\n')
client.Close()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("handleConn did not return")
}
logs, total, err := s.stores.ProtocolLogs.List(1, 10, store.ProtocolLogFilter{})
if err != nil {
t.Fatalf("list: %v", err)
}
if total != 1 {
t.Fatalf("expected 1 log, got %d", total)
}
log := logs[0]
if log.Protocol != db.ProtocolPOP3 || log.Port != 110 {
t.Fatalf("unexpected log: %+v", log)
}
if log.Success {
t.Fatalf("expected failure, got %+v", log)
}
if log.Username != "no-such-user" {
t.Fatalf("username = %q, want no-such-user", log.Username)
}
if !strings.Contains(log.Detail, "USER") || !strings.Contains(log.Detail, "PASS") {
t.Fatalf("detail missing commands: %q", log.Detail)
}
}
// TestHandleConnLogsSuccess 验证认证成功的 POP3 会话写入成功日志。
func TestHandleConnLogsSuccess(t *testing.T) {
s := newTestServer(t)
domain := &db.Domain{Name: "example.com"}
if err := s.stores.Domains.Create(domain); err != nil {
t.Fatalf("create domain: %v", err)
}
user := &db.User{Username: "alice", DomainID: domain.ID, IsActive: true}
hashed, err := bcrypt.GenerateFromPassword([]byte("secret123"), bcrypt.DefaultCost)
if err != nil {
t.Fatalf("hash: %v", err)
}
user.PasswordHash = string(hashed)
if err := s.stores.Users.Create(user); err != nil {
t.Fatalf("create user: %v", err)
}
server, client := net.Pipe()
defer server.Close()
defer client.Close()
done := make(chan struct{})
go func() {
defer close(done)
s.handleConn(server, 110)
}()
br := bufio.NewReader(client)
br.ReadString('\n')
client.Write([]byte("USER alice@example.com\r\n"))
br.ReadString('\n')
client.Write([]byte("PASS secret123\r\n"))
br.ReadString('\n')
client.Write([]byte("STAT\r\n"))
br.ReadString('\n')
client.Write([]byte("QUIT\r\n"))
br.ReadString('\n')
client.Close()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("handleConn did not return")
}
logs, total, err := s.stores.ProtocolLogs.List(1, 10, store.ProtocolLogFilter{})
if err != nil {
t.Fatalf("list: %v", err)
}
if total != 1 {
t.Fatalf("expected 1 log, got %d", total)
}
if !logs[0].Success {
t.Fatalf("expected success, got %+v", logs[0])
}
if logs[0].Username != "alice@example.com" {
t.Fatalf("username = %q", logs[0].Username)
}
}
// TestPop3CommandDetail 验证命令摘要生成。
func TestPop3CommandDetail(t *testing.T) {
counts := map[string]int{"USER": 1, "PASS": 1, "RETR": 3, "DELE": 1, "NOOP": 2}
got := pop3CommandDetail(counts, 2)
if got != "USER PASS RETR×3 DELE 删除2" {
t.Fatalf("detail = %q", got)
}
if empty := pop3CommandDetail(nil, 0); empty != "连接建立,无命令" {
t.Fatalf("empty detail = %q", empty)
}
}
// mockPusher 记录推送调用的测试桩。
type mockPusher struct {
expunged []struct {
Email string
Mailbox string
Seqs []uint32
}
}
func (m *mockPusher) PushNewMessage(string, *db.Message) {}
func (m *mockPusher) PushFlagsChanged(string, string, *db.Message) {}
func (m *mockPusher) PushExpunged(email, mailbox string, seqs []uint32) {
m.expunged = append(m.expunged, struct {
Email string
Mailbox string
Seqs []uint32
}{email, mailbox, seqs})
}
// TestExpungePushesIMAPUpdate 验证 POP3 删除邮件后向 IMAP 推送 Expunge。
func TestExpungePushesIMAPUpdate(t *testing.T) {
s := newTestServer(t)
pusher := &mockPusher{}
s.pusher = pusher
domain := &db.Domain{Name: "example.com"}
if err := s.stores.Domains.Create(domain); err != nil {
t.Fatalf("create domain: %v", err)
}
user := &db.User{Username: "alice", DomainID: domain.ID, IsActive: true}
hashed, _ := bcrypt.GenerateFromPassword([]byte("secret123"), bcrypt.DefaultCost)
user.PasswordHash = string(hashed)
if err := s.stores.Users.Create(user); err != nil {
t.Fatalf("create user: %v", err)
}
for i := 0; i < 3; i++ {
msg := &db.Message{UserID: user.ID, Folder: "INBOX", FromAddr: "x@y", Subject: "m", Date: time.Now()}
if err := s.stores.Mails.Create(msg); err != nil {
t.Fatalf("create message %d: %v", i, err)
}
}
server, client := net.Pipe()
defer server.Close()
defer client.Close()
done := make(chan struct{})
go func() {
defer close(done)
s.handleConn(server, 110)
}()
br := bufio.NewReader(client)
br.ReadString('\n')
client.Write([]byte("USER alice@example.com\r\n"))
br.ReadString('\n')
client.Write([]byte("PASS secret123\r\n"))
br.ReadString('\n')
// 删除第 1 封后退出
client.Write([]byte("DELE 1\r\n"))
br.ReadString('\n')
client.Write([]byte("QUIT\r\n"))
br.ReadString('\n')
client.Close()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("handleConn did not return")
}
if len(pusher.expunged) != 1 {
t.Fatalf("expunge pushes = %d, want 1", len(pusher.expunged))
}
p := pusher.expunged[0]
if p.Email != "alice@example.com" || p.Mailbox != "INBOX" {
t.Fatalf("push target = %s/%s", p.Email, p.Mailbox)
}
if len(p.Seqs) != 1 || p.Seqs[0] != 1 {
t.Fatalf("seqs = %v, want [1]", p.Seqs)
}
// 邮件确实已删除
if n, _ := s.stores.Mails.CountByUserAndFolder(user.ID, "INBOX"); n != 2 {
t.Fatalf("inbox count = %d, want 2", n)
}
}
+219 -26
View File
@@ -6,15 +6,20 @@ import (
"fmt"
"io"
"log"
"net"
"strconv"
"strings"
"time"
"mail_go/config"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/imap_server"
"mail_go/internal/mailutil"
"mail_go/internal/outbound"
"mail_go/internal/storage"
"mail_go/internal/store"
"mail_go/internal/tlsutil"
"github.com/emersion/go-message/mail"
"github.com/emersion/go-sasl"
@@ -31,27 +36,28 @@ const (
// SMTPServer wraps go-smtp servers and provides local mail delivery.
type SMTPServer struct {
stores *store.Stores
storage *storage.AttachmentStorage
outbound *outbound.Manager
cfg config.SMTPConfig
stores *store.Stores
storage *storage.AttachmentStorage
outbound *outbound.Manager
cfg config.SMTPConfig
banCfg config.BanConfig
tlsLoader *tlsutil.Loader
hub *connhub.Hub
pusher imap_server.Pusher // 本地投递成功推送(IMAP 新邮件),可空
}
// NewSMTPServer creates a new SMTP server instance.
func NewSMTPServer(cfg config.SMTPConfig, stores *store.Stores, attStorage *storage.AttachmentStorage, ob *outbound.Manager) *SMTPServer {
return &SMTPServer{stores: stores, storage: attStorage, outbound: ob, cfg: cfg}
// NewSMTPServer creates a new SMTP server instance. tlsLoader may be nil
// when TLS is not configured.
func NewSMTPServer(cfg config.SMTPConfig, stores *store.Stores, attStorage *storage.AttachmentStorage, ob *outbound.Manager, tlsLoader *tlsutil.Loader, banCfg config.BanConfig, hub *connhub.Hub, pusher imap_server.Pusher) *SMTPServer {
return &SMTPServer{stores: stores, storage: attStorage, outbound: ob, cfg: cfg, banCfg: banCfg, tlsLoader: tlsLoader, hub: hub, pusher: pusher}
}
func (s *SMTPServer) tlsConfig() (*tls.Config, error) {
if s.cfg.TLSCert == "" || s.cfg.TLSKey == "" {
if s.tlsLoader == nil {
return nil, fmt.Errorf("SMTP TLS certificate or key not configured")
}
cert, err := tls.LoadX509KeyPair(s.cfg.TLSCert, s.cfg.TLSKey)
if err != nil {
return nil, fmt.Errorf("failed to load SMTP TLS certificate: %w", err)
}
return &tls.Config{Certificates: []tls.Certificate{cert}}, nil
// GetCertificate 每次握手按需重载证书,证书更新后无需重启服务
return &tls.Config{GetCertificate: s.tlsLoader.GetCertificate}, nil
}
func (s *SMTPServer) newServer(addr string, mode smtpMode, tlsConfig *tls.Config) *smtp.Server {
@@ -108,13 +114,50 @@ type smtpBackend struct {
// NewSession creates a new SMTP session for the incoming connection.
func (be *smtpBackend) NewSession(c *smtp.Conn) (smtp.Session, error) {
clientIP := store.ClientIPFromAddr(c.Conn().RemoteAddr())
conn := be.server.hub.Register("smtp", clientIP, be.server.sessionPort(be.mode), be.server.tlsActive(c))
if conn != nil {
// 强制断开:关闭底层连接后 go-smtp 读到 EOF,正常走 Logout 收尾
raw := c.Conn()
conn.SetDisconnect(func() { _ = raw.Close() })
}
return &smtpSession{
backend: be,
mode: be.mode,
rcpts: make([]string, 0),
backend: be,
mode: be.mode,
rcpts: make([]string, 0),
clientIP: clientIP,
startedAt: time.Now(),
port: be.server.sessionPort(be.mode),
conn: conn,
}, nil
}
// tlsActive 判断当前连接是否处于 TLS 加密状态(implicit TLS 或 STARTTLS)。
func (s *SMTPServer) tlsActive(c *smtp.Conn) bool {
_, ok := c.TLSConnectionState()
return ok
}
// sessionPort 返回该会话监听的端口号(区分明文/TLS/提交端口),解析失败返回 0。
func (s *SMTPServer) sessionPort(mode smtpMode) int {
addr := s.cfg.Addr
switch mode {
case smtpModeSubmission:
addr = s.cfg.SubmissionAddr
case smtpModeImplicitTLS:
addr = s.cfg.TLSAddr
}
_, portStr, err := net.SplitHostPort(addr)
if err != nil {
return 0
}
port, err := strconv.Atoi(portStr)
if err != nil {
return 0
}
return port
}
// smtpSession implements the smtp.Session interface for handling a single connection.
type smtpSession struct {
backend *smtpBackend
@@ -127,6 +170,20 @@ type smtpSession struct {
userID uint
email string
user *db.User
clientIP string
// 会话日志累积状态
startedAt time.Time
port int
authTried bool
authOK bool
authUsername string
failReason string // 首个失败原因
msgCount int // 成功处理的邮件数(本地投递 + 外发队列)
detailParts []string
// 连接追踪
conn *connhub.Conn
}
// AuthMechanisms returns supported SMTP AUTH mechanisms.
@@ -134,14 +191,44 @@ func (s *smtpSession) AuthMechanisms() []string {
return []string{sasl.Plain}
}
// recordFail 记录会话中第一个失败原因(日志用途,不改变协议行为)。
func (s *smtpSession) recordFail(reason string) {
if s.failReason == "" {
s.failReason = reason
}
}
// recordDetail 追加一条操作摘要。
func (s *smtpSession) recordDetail(part string) {
s.detailParts = append(s.detailParts, part)
}
// Auth authenticates the user with SASL PLAIN credentials.
func (s *smtpSession) Auth(mech string) (sasl.Server, error) {
if mech != sasl.Plain {
s.recordFail("不支持的认证机制")
return nil, smtp.ErrAuthUnknownMechanism
}
return sasl.NewPlainServer(func(identity, username, password string) error {
s.authTried = true
s.authUsername = username
s.conn.Touch()
// 已封禁 IP 一律拒绝认证(防协议层暴力破解)
if banned, _ := s.backend.server.stores.Bans.IsBanned(s.clientIP); banned {
s.recordFail("IP已被封禁")
return smtp.ErrAuthFailed
}
user, err := s.backend.server.stores.Users.Authenticate(username, password)
if err != nil {
// 认证失败计数,达到阈值按档位封禁(与 Web 登录共用 ban_entries
s.backend.server.stores.RecordAuthFailure(
s.clientIP,
s.backend.server.banCfg.MaxFailAttempts,
s.backend.server.banCfg.BanDurationMin,
"邮件协议认证失败次数过多",
)
s.recordFail("用户名或密码错误")
return smtp.ErrAuthFailed
}
@@ -153,13 +240,18 @@ func (s *smtpSession) Auth(mech string) (sasl.Server, error) {
}
}
if domainName == "" {
s.recordFail("用户名或密码错误")
return smtp.ErrAuthFailed
}
s.authenticated = true
s.authOK = true
s.userID = user.ID
s.user = user
s.email = user.Username + "@" + domainName
if s.conn != nil {
s.conn.SetUser(s.email)
}
return nil
}), nil
}
@@ -167,10 +259,12 @@ func (s *smtpSession) Auth(mech string) (sasl.Server, error) {
// Mail records the sender address (MAIL FROM command).
func (s *smtpSession) Mail(from string, opts *smtp.MailOptions) error {
if s.mode != smtpModeInbound && !s.authenticated {
s.recordFail("未认证用户尝试发信")
return smtp.ErrAuthRequired
}
// Authenticated users may only send as themselves, preventing spoofing.
if s.authenticated && !strings.EqualFold(strings.TrimSpace(from), s.email) {
s.recordFail("发件人地址与登录用户不一致")
return fmt.Errorf("sender address must match authenticated user")
}
@@ -188,6 +282,7 @@ func (s *smtpSession) Mail(from string, opts *smtp.MailOptions) error {
func (s *smtpSession) Rcpt(to string, opts *smtp.RcptOptions) error {
to = strings.TrimSpace(to)
if to == "" {
s.recordFail("无效的收件人地址")
return fmt.Errorf("invalid recipient address: %s", to)
}
@@ -199,12 +294,14 @@ func (s *smtpSession) Rcpt(to string, opts *smtp.RcptOptions) error {
// External recipient: only authenticated local users may relay.
if !s.authenticated {
s.recordFail("中继访问被拒绝")
return fmt.Errorf("relay access denied: %s", to)
}
// Sender verification must have been enforced in Mail() already.
ob := s.backend.server.outbound
if ob == nil || !ob.Enabled() {
s.recordFail("外部投递未启用")
return fmt.Errorf("external delivery is disabled: %s", to)
}
@@ -221,58 +318,78 @@ func (s *smtpSession) localUserByEmail(email string) (*db.User, error) {
// External recipients (authenticated sessions only) are queued for
// outbound delivery.
func (s *smtpSession) Data(r io.Reader) error {
s.conn.Touch()
if len(s.rcpts) == 0 {
s.recordFail("未指定收件人")
return fmt.Errorf("no accepted recipients")
}
data, err := io.ReadAll(r)
if err != nil {
s.recordFail("读取邮件数据失败")
return fmt.Errorf("failed to read message data: %w", err)
}
parsed, err := parseSMTPMessage(data)
if err != nil {
s.recordFail("邮件格式解析失败")
return err
}
// Local recipients: deliver to INBOX.
localDelivered := 0
for _, rcpt := range s.localRcpts {
user, err := s.localUserByEmail(rcpt)
if err != nil {
log.Printf("SMTP: recipient not found %s, skipping", rcpt)
continue
}
if err := s.saveMessage(user.ID, "INBOX", parsed, data, false); err != nil {
msg, err := s.saveMessage(user.ID, "INBOX", parsed, data, false)
if err != nil {
log.Printf("SMTP: failed to create message for %s: %v", rcpt, err)
continue
}
log.Printf("SMTP: message delivered to %s", rcpt)
localDelivered++
// 本地投递成功 → IMAP 新邮件推送(IDLE 客户端实时收到通知)
if pusher := s.backend.server.pusher; pusher != nil && msg != nil {
pusher.PushNewMessage(user.Username+"@"+user.Domain.Name, msg)
}
}
s.msgCount += localDelivered
// External recipients: queue for outbound delivery.
externalQueued := 0
if len(s.externalRcpts) > 0 {
ob := s.backend.server.outbound
if ob == nil {
s.recordFail("外部投递服务不可用")
return fmt.Errorf("outbound delivery is unavailable")
}
maxRcpt := ob.MaxRecipients()
if maxRcpt > 0 && len(s.externalRcpts) > maxRcpt {
s.recordFail("外部收件人数量超出限制")
return fmt.Errorf("too many external recipients: %d (max %d)", len(s.externalRcpts), maxRcpt)
}
for _, rcpt := range s.externalRcpts {
if _, err := ob.Enqueue(s.user, s.email, rcpt, data); err != nil {
s.recordFail("外发队列投递失败")
return fmt.Errorf("failed to queue external recipient %s: %v", rcpt, err)
}
log.Printf("SMTP: external message queued for %s", rcpt)
externalQueued++
}
}
s.msgCount += externalQueued
if s.authenticated && s.userID != 0 && s.mode != smtpModeInbound {
if err := s.saveMessage(s.userID, "Sent", parsed, data, true); err != nil {
if _, err := s.saveMessage(s.userID, "Sent", parsed, data, true); err != nil {
log.Printf("SMTP: failed to save sent copy for %s: %v", s.email, err)
}
}
s.recordDetail(fmt.Sprintf("MAIL FROM:<%s> RCPT×%d 本地投递%d 外发%d",
s.from, len(s.rcpts), localDelivered, externalQueued))
return nil
}
@@ -285,7 +402,14 @@ type parsedSMTPMessage struct {
textBody string
htmlBody string
date time.Time
attachments []*db.Attachment
attachments []*parsedAttachment
}
// parsedAttachment holds an extracted MIME attachment part.
type parsedAttachment struct {
fileName string
contentType string
data []byte
}
func parseSMTPMessage(data []byte) (*parsedSMTPMessage, error) {
@@ -346,10 +470,10 @@ func parseSMTPMessage(data []byte) (*parsedSMTPMessage, error) {
log.Printf("SMTP: error reading attachment part: %v", readErr)
continue
}
msg.attachments = append(msg.attachments, &db.Attachment{
FileName: filename,
ContentType: contentType,
FileSize: int64(len(buf)),
msg.attachments = append(msg.attachments, &parsedAttachment{
fileName: filename,
contentType: contentType,
data: buf,
})
}
}
@@ -360,7 +484,7 @@ func parseSMTPMessage(data []byte) (*parsedSMTPMessage, error) {
return msg, nil
}
func (s *smtpSession) saveMessage(userID uint, folder string, parsed *parsedSMTPMessage, data []byte, read bool) error {
func (s *smtpSession) saveMessage(userID uint, folder string, parsed *parsedSMTPMessage, data []byte, read bool) (*db.Message, error) {
msg := &db.Message{
UserID: userID,
MessageID: parsed.messageID,
@@ -376,7 +500,32 @@ func (s *smtpSession) saveMessage(userID uint, folder string, parsed *parsedSMTP
IsFlagged: false,
Date: parsed.date,
}
return s.backend.server.stores.Mails.Create(msg)
if err := s.backend.server.stores.Mails.Create(msg); err != nil {
return nil, err
}
// Persist attachments to disk and link them to the message so that the
// Web mail UI can list/download them and quota accounting stays correct.
for _, att := range parsed.attachments {
relPath, err := s.backend.server.storage.Save(att.fileName, att.data)
if err != nil {
log.Printf("SMTP: failed to save attachment %s: %v", att.fileName, err)
continue
}
rec := &db.Attachment{
MessageID: msg.ID,
FileName: att.fileName,
FilePath: relPath,
ContentType: att.contentType,
FileSize: int64(len(att.data)),
}
if err := s.backend.server.stores.Attachments.Create(rec); err != nil {
log.Printf("SMTP: failed to create attachment record: %v", err)
continue
}
_ = s.backend.server.stores.Users.UpdateUsedBytes(userID, rec.FileSize)
}
return msg, nil
}
// Reset clears the session state for the next message on the same connection.
@@ -389,5 +538,49 @@ func (s *smtpSession) Reset() {
// Logout is called when the SMTP connection is closed.
func (s *smtpSession) Logout() error {
s.writeProtocolLog()
if s.conn != nil {
s.conn.Close()
}
return nil
}
// writeProtocolLog 汇总本会话状态写入协议调用日志(供后台分析攻击/滥用)。
func (s *smtpSession) writeProtocolLog() {
success := s.failReason == ""
detail := strings.Join(s.detailParts, "; ")
username := s.authUsername
if username == "" && s.email != "" {
username = s.email
}
if detail == "" {
if s.authTried {
if s.authOK {
detail = "AUTH 成功"
} else {
detail = "AUTH 失败"
}
} else if success {
detail = "连接建立,无邮件操作"
}
}
if success && s.authTried && !s.authOK {
success = false
}
entry := &db.ProtocolLog{
Protocol: db.ProtocolSMTP,
Port: s.port,
ClientIP: s.clientIP,
Username: username,
Success: success,
FailReason: s.failReason,
Detail: detail,
MsgCount: s.msgCount,
DurationMs: time.Since(s.startedAt).Milliseconds(),
CreatedAt: time.Now(),
}
if err := s.backend.server.stores.ProtocolLogs.Create(entry); err != nil {
log.Printf("SMTP: 写入协议日志失败: %v", err)
}
}
+214
View File
@@ -0,0 +1,214 @@
package smtp_server
import (
"bytes"
"fmt"
"testing"
"time"
"mail_go/config"
"mail_go/internal/db"
"mail_go/internal/storage"
"mail_go/internal/store"
"github.com/emersion/go-sasl"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// testMultipartMessage builds an RFC 5322 message with one text part and one
// base64 attachment.
func testMultipartMessage() []byte {
const boundary = "X"
return []byte(fmt.Sprintf(
"From: sender@example.com\r\n"+
"To: rcpt@lmve.net\r\n"+
"Subject: with attachment\r\n"+
"MIME-Version: 1.0\r\n"+
"Content-Type: multipart/mixed; boundary=\"%s\"\r\n"+
"\r\n"+
"--%s\r\n"+
"Content-Type: text/plain; charset=utf-8\r\n"+
"\r\n"+
"hello body\r\n"+
"--%s\r\n"+
"Content-Type: text/plain; name=\"test.txt\"\r\n"+
"Content-Transfer-Encoding: base64\r\n"+
"Content-Disposition: attachment; filename=\"test.txt\"\r\n"+
"\r\n"+
"aGVsbG8gd29ybGQ=\r\n"+
"--%s--\r\n",
boundary, boundary, boundary, boundary))
}
func TestParseSMTPMessageExtractsAttachmentData(t *testing.T) {
parsed, err := parseSMTPMessage(testMultipartMessage())
if err != nil {
t.Fatalf("parseSMTPMessage: %v", err)
}
if parsed.textBody != "hello body" {
t.Fatalf("unexpected text body: %q", parsed.textBody)
}
if len(parsed.attachments) != 1 {
t.Fatalf("expected 1 attachment, got %d", len(parsed.attachments))
}
att := parsed.attachments[0]
if att.fileName != "test.txt" {
t.Fatalf("unexpected filename: %q", att.fileName)
}
if string(att.data) != "hello world" {
t.Fatalf("unexpected attachment data: %q", att.data)
}
}
func TestSaveMessagePersistsAttachments(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}, &db.Attachment{}, &db.BanEntry{}, &db.OutboundMessage{}, &db.ProtocolLog{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
attStorage := storage.NewAttachmentStorage(t.TempDir())
srv := &SMTPServer{stores: stores, storage: attStorage}
sess := &smtpSession{backend: &smtpBackend{server: srv}}
data := testMultipartMessage()
parsed, err := parseSMTPMessage(data)
if err != nil {
t.Fatalf("parseSMTPMessage: %v", err)
}
user := &db.User{Username: "rcpt", PasswordHash: "x", DomainID: 0, IsActive: true}
if err := stores.Users.Create(user); err != nil {
t.Fatalf("create user: %v", err)
}
if _, err := sess.saveMessage(user.ID, "INBOX", parsed, data, false); err != nil {
t.Fatalf("saveMessage: %v", err)
}
msgs, err := stores.Mails.ListAllByUserAndFolder(user.ID, "INBOX")
if err != nil || len(msgs) != 1 {
t.Fatalf("expected 1 inbox message, got %d (err=%v)", len(msgs), err)
}
atts, err := stores.Attachments.ListByMessage(msgs[0].ID)
if err != nil {
t.Fatalf("ListByMessage: %v", err)
}
if len(atts) != 1 {
t.Fatalf("expected 1 attachment record, got %d", len(atts))
}
att := atts[0]
if att.FileName != "test.txt" || att.FileSize != int64(len("hello world")) {
t.Fatalf("unexpected attachment record: %+v", att)
}
// The file must exist on disk with the original content.
content, err := attStorage.Read(att.FilePath)
if err != nil {
t.Fatalf("read attachment from disk: %v", err)
}
if !bytes.Equal(content, []byte("hello world")) {
t.Fatalf("attachment content mismatch: %q", content)
}
}
// TestSessionLoggingRecordsAuthFailure 验证认证失败的会话在 Logout 时写入协议日志。
func TestSessionLoggingRecordsAuthFailure(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}, &db.Attachment{}, &db.BanEntry{}, &db.OutboundMessage{}, &db.ProtocolLog{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
srv := &SMTPServer{stores: stores, banCfg: config.BanConfig{MaxFailAttempts: 5, BanDurationMin: 30}}
sess := &smtpSession{
backend: &smtpBackend{server: srv, mode: smtpModeSubmission},
clientIP: "203.0.113.7",
startedAt: time.Now(),
port: 587,
}
// 触发一次认证(用户名不存在 → 失败)
mech, err := sess.Auth(sasl.Plain)
if err != nil {
t.Fatalf("Auth: %v", err)
}
// SASL PLAIN 凭据格式: authzid\0authcid\0passwd
if _, _, err := mech.Next([]byte("\x00no-such-user\x00wrong-pass")); err == nil {
t.Fatal("expected auth failure for unknown user")
}
// 直接调用 Logout 模拟连接结束
if err := sess.Logout(); err != nil {
t.Fatalf("Logout: %v", err)
}
logs, total, err := stores.ProtocolLogs.List(1, 10, store.ProtocolLogFilter{})
if err != nil {
t.Fatalf("list: %v", err)
}
if total != 1 {
t.Fatalf("expected 1 log, got %d", total)
}
log := logs[0]
if log.Protocol != db.ProtocolSMTP || log.Port != 587 || log.ClientIP != "203.0.113.7" {
t.Fatalf("unexpected log: %+v", log)
}
if log.Success {
t.Fatalf("expected failure, got %+v", log)
}
if log.FailReason == "" {
t.Fatal("expected fail reason")
}
if log.Username != "no-such-user" {
t.Fatalf("username = %q", log.Username)
}
}
// TestSessionLoggingRecordsDelivery 验证投递成功的会话写入成功日志。
func TestSessionLoggingRecordsDelivery(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}, &db.Attachment{}, &db.BanEntry{}, &db.OutboundMessage{}, &db.ProtocolLog{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
attStorage := storage.NewAttachmentStorage(t.TempDir())
srv := &SMTPServer{stores: stores, storage: attStorage}
sess := &smtpSession{
backend: &smtpBackend{server: srv, mode: smtpModeInbound},
clientIP: "203.0.113.8",
startedAt: time.Now(),
port: 25,
rcpts: make([]string, 0),
}
if err := sess.Mail("sender@example.com", nil); err != nil {
t.Fatalf("Mail: %v", err)
}
if err := sess.Logout(); err != nil {
t.Fatalf("Logout: %v", err)
}
logs, total, err := stores.ProtocolLogs.List(1, 10, store.ProtocolLogFilter{})
if err != nil {
t.Fatalf("list: %v", err)
}
if total != 1 {
t.Fatalf("expected 1 log, got %d", total)
}
if !logs[0].Success {
t.Fatalf("expected success, got %+v", logs[0])
}
}
+47 -11
View File
@@ -4,11 +4,16 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/google/uuid"
)
// savedFileRe 匹配 Save 生成的文件名:UUID(小写十六进制)+ 可选白名单扩展名。
// 只允许这种格式的路径进入文件系统,杜绝路径遍历(../)、绝对路径等。
var savedFileRe = regexp.MustCompile(`^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}(\.[A-Za-z0-9._-]{1,32})?$`)
// AttachmentStorage handles file operations for email attachments on disk.
type AttachmentStorage struct {
baseDir string // cfg.Storage.AttachDir
@@ -19,6 +24,24 @@ func NewAttachmentStorage(baseDir string) *AttachmentStorage {
return &AttachmentStorage{baseDir: baseDir}
}
// safeExt 提取并白名单化文件扩展名:只保留字母数字与 ._-,最长 32 字符。
// 非法字符(含 CR/LF、路径分隔符)直接丢弃扩展名。
func safeExt(filename string) string {
ext := filepath.Ext(filename)
if len(ext) > 33 {
return ""
}
for _, r := range ext {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9':
case r == '.', r == '_', r == '-':
default:
return ""
}
}
return ext
}
// Save writes attachment data to disk and returns the relative file path.
// The filename is generated as {uuid}{ext} to avoid collisions.
func (s *AttachmentStorage) Save(filename string, data []byte) (string, error) {
@@ -27,8 +50,8 @@ func (s *AttachmentStorage) Save(filename string, data []byte) (string, error) {
return "", fmt.Errorf("创建附件目录失败: %w", err)
}
// Generate a unique filename with the original extension
ext := filepath.Ext(filename)
// Generate a unique filename with a sanitized extension
ext := safeExt(filename)
uniqueName := uuid.New().String() + ext
fullPath := filepath.Join(s.baseDir, uniqueName)
@@ -41,7 +64,10 @@ func (s *AttachmentStorage) Save(filename string, data []byte) (string, error) {
// Read reads attachment data from disk given a relative path.
func (s *AttachmentStorage) Read(relPath string) ([]byte, error) {
fullPath := s.FullPath(relPath)
fullPath, err := s.FullPath(relPath)
if err != nil {
return nil, err
}
data, err := os.ReadFile(fullPath)
if err != nil {
return nil, fmt.Errorf("读取附件文件失败: %w", err)
@@ -51,19 +77,29 @@ func (s *AttachmentStorage) Read(relPath string) ([]byte, error) {
// Delete removes an attachment file from disk given a relative path.
func (s *AttachmentStorage) Delete(relPath string) error {
fullPath := s.FullPath(relPath)
fullPath, err := s.FullPath(relPath)
if err != nil {
return err
}
if err := os.Remove(fullPath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("删除附件文件失败: %w", err)
}
return nil
}
// FullPath returns the absolute path for a given relative path.
func (s *AttachmentStorage) FullPath(relPath string) string {
// Prevent directory traversal attacks
cleanRel := filepath.Clean(relPath)
if strings.HasPrefix(cleanRel, "..") {
cleanRel = strings.TrimPrefix(cleanRel, "../")
// FullPath returns the absolute path for a relative path produced by Save.
// Paths that do not match the saved-file format (traversal attempts,
// absolute paths, unrelated names) are rejected with an error so they can
// never escape baseDir.
func (s *AttachmentStorage) FullPath(relPath string) (string, error) {
if !savedFileRe.MatchString(relPath) {
return "", fmt.Errorf("非法的附件路径: %q", relPath)
}
return filepath.Join(s.baseDir, cleanRel)
// 兜底校验:解析后的路径必须仍在 baseDir 内
fullPath := filepath.Join(s.baseDir, relPath)
if !strings.HasPrefix(fullPath, filepath.Clean(s.baseDir)+string(os.PathSeparator)) {
return "", fmt.Errorf("附件路径越界: %q", relPath)
}
return fullPath, nil
}
+97
View File
@@ -0,0 +1,97 @@
package storage
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/uuid"
)
// TestFullPathRejectsTraversal 验证路径遍历/绝对路径等恶意输入被拒绝。
func TestFullPathRejectsTraversal(t *testing.T) {
s := NewAttachmentStorage(filepath.Join(t.TempDir(), "attachments"))
valid := uuid.New().String() + ".pdf"
bad := []string{
"../secret.txt",
"../../etc/passwd",
"..",
"....//x",
"/etc/passwd",
"a/../b.txt",
"sub/file.png",
"",
".", "..\\..\\x", // windows style
"00000000-0000-0000-0000-000000000000.exe\r\nBcc: x@y.com",
"garbage",
"00000000-0000-0000-0000-000000000000.%2e%2e",
}
for _, p := range bad {
if _, err := s.FullPath(p); err == nil {
t.Errorf("FullPath(%q) should be rejected", p)
}
}
// 合法文件名必须通过
full, err := s.FullPath(valid)
if err != nil {
t.Fatalf("FullPath(%q) rejected: %v", valid, err)
}
if !strings.HasPrefix(full, s.baseDir+string(os.PathSeparator)) {
t.Fatalf("FullPath(%q) = %q escapes baseDir", valid, full)
}
}
// TestSaveSanitizesExtension 验证恶意扩展名不会进入文件名。
func TestSaveSanitizesExtension(t *testing.T) {
s := NewAttachmentStorage(filepath.Join(t.TempDir(), "attachments"))
// 换行/路径分隔符等非法字符的扩展名应被丢弃
rel, err := s.Save("evil.pdf\r\nBcc: x@y.com", []byte("data"))
if err != nil {
t.Fatalf("Save: %v", err)
}
if strings.ContainsAny(rel, "\r\n/\\") {
t.Fatalf("saved name contains dangerous chars: %q", rel)
}
if !savedFileRe.MatchString(rel) {
t.Fatalf("saved name %q does not match allowed pattern", rel)
}
// 后续 Read 应能按返回的路径读取
if _, err := s.Read(rel); err != nil {
t.Fatalf("Read after Save: %v", err)
}
// 正常扩展名保留
rel2, err := s.Save("report.pdf", []byte("data"))
if err != nil {
t.Fatalf("Save: %v", err)
}
if !strings.HasSuffix(rel2, ".pdf") {
t.Fatalf("extension lost: %q", rel2)
}
}
// TestReadDeleteRoundTrip 正常读写删流程。
func TestReadDeleteRoundTrip(t *testing.T) {
dir := t.TempDir()
s := NewAttachmentStorage(filepath.Join(dir, "attachments"))
rel, err := s.Save("a.txt", []byte("hello"))
if err != nil {
t.Fatalf("Save: %v", err)
}
data, err := s.Read(rel)
if err != nil || string(data) != "hello" {
t.Fatalf("Read = %q, %v", data, err)
}
if err := s.Delete(rel); err != nil {
t.Fatalf("Delete: %v", err)
}
// 删除后路径仍然合法(删除不存在文件不算错误)
if err := s.Delete(rel); err != nil {
t.Fatalf("Delete again: %v", err)
}
}
+68
View File
@@ -0,0 +1,68 @@
package store
import (
"fmt"
"net"
"time"
)
// ClientIPFromAddr 从 net.Addr 提取客户端 IP 字符串(去掉端口)。
// 解析失败返回空字符串,调用方应据此跳过封禁逻辑(不误封)。
func ClientIPFromAddr(addr net.Addr) string {
if addr == nil {
return ""
}
host, _, err := net.SplitHostPort(addr.String())
if err != nil {
return addr.String()
}
return host
}
// RecordAuthFailure 记录一次登录/认证失败(Web 表单、LDAP 与 SMTP/IMAP/POP3
// 协议层统一入口):
// - 失败计数累加(每 IP 一条记录,upsert);
// - 达到 maxFail 阈值时触发次数 BanCount+1
// 前 freeTriggers3)次只计数不封禁;
// 从第 4 次起封禁,时长按档位递增(stageDuration),上限半年;
// - reason 为失败场景描述(如“登录失败次数过多”),封禁原因会带上档位。
//
// 返回 (是否触发封禁, 当前失败计数)。成功登录后调用 ResetFail 清零。
func (s *Stores) RecordAuthFailure(ip string, maxFail int, firstBanMin int, reason string) (banned bool, failCount int) {
if ip == "" || maxFail <= 0 {
return false, 0
}
failCount, _ = s.Bans.IncrementFail(ip)
if failCount < maxFail {
return false, failCount
}
entry, err := s.Bans.GetByIP(ip)
if err != nil || entry == nil {
return false, failCount
}
// 已处于封禁中(例如并发请求竞态)不重复触发、不重设档位
if entry.ExpiresAt.After(time.Now()) {
return true, failCount
}
banCount := entry.BanCount + 1
entry.BanCount = banCount
entry.FailCount = failCount
// 前 3 次只计数,不封禁(保留零到期时间与空原因)
if banCount <= freeTriggers {
if err := s.Bans.Update(entry); err != nil {
return false, failCount
}
return false, failCount
}
banNum := banCount - freeTriggers
entry.Reason = fmt.Sprintf("第%d次封禁:%s(第%d次触发,失败%d次)", banNum, reason, banCount, failCount)
entry.ExpiresAt = time.Now().Add(stageDuration(banNum, firstBanMin))
if err := s.Bans.Update(entry); err != nil {
return false, failCount
}
return true, failCount
}
+352
View File
@@ -0,0 +1,352 @@
package store
import (
"net"
"path/filepath"
"strings"
"testing"
"time"
"mail_go/internal/db"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func newTestStores(t *testing.T) *Stores {
t.Helper()
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}, &db.Attachment{}, &db.BanEntry{}, &db.OutboundMessage{}, &db.ProtocolLog{}); err != nil {
t.Fatalf("migrate: %v", err)
}
return NewStores(gdb)
}
// TestRecordAuthFailureFreeTriggers 验证前 3 次达到阈值只计数不封禁,
// 第 4 次起封禁(第 1 次封禁 = 配置时长)。
func TestRecordAuthFailureFreeTriggers(t *testing.T) {
s := newTestStores(t)
const ip = "203.0.113.10"
const maxFail = 2
failOnce := func() bool {
banned, _ := s.RecordAuthFailure(ip, maxFail, 30, "登录失败次数过多")
return banned
}
// 第 1 次触发需要 maxFail 次失败
for f := 0; f < maxFail; f++ {
if failOnce() {
t.Fatalf("trigger 1 (fail %d) should not ban yet", f+1)
}
}
// 达到阈值后失败计数持续累计,之后每次失败都会再次触发
for i := 2; i <= 3; i++ {
if failOnce() {
t.Fatalf("trigger %d should not ban yet", i)
}
}
entry, err := s.Bans.GetByIP(ip)
if err != nil {
t.Fatalf("get entry: %v", err)
}
if entry.BanCount != 3 {
t.Fatalf("ban_count = %d, want 3", entry.BanCount)
}
if !entry.ExpiresAt.IsZero() {
t.Fatal("observation record must not have expiry")
}
// 第 4 次触发封禁,时长 = firstBanMin30 分钟)
if !failOnce() {
t.Fatal("4th trigger should ban the IP")
}
entry, err = s.Bans.GetByIP(ip)
if err != nil {
t.Fatalf("get entry: %v", err)
}
wantExpiry := time.Now().Add(30 * time.Minute)
if entry.ExpiresAt.Before(wantExpiry.Add(-time.Minute)) || entry.ExpiresAt.After(wantExpiry.Add(time.Minute)) {
t.Fatalf("ban expiry = %v, want ~%v", entry.ExpiresAt, wantExpiry)
}
if !strings.Contains(entry.Reason, "第1次封禁") {
t.Fatalf("reason = %q, want 第1次封禁", entry.Reason)
}
if entry.BanCount != 4 {
t.Fatalf("ban_count = %d, want 4", entry.BanCount)
}
// IP 现在处于封禁状态
if banned, _ := s.Bans.IsBanned(ip); !banned {
t.Fatal("IP should be banned")
}
}
// TestStagedBanEscalation 验证封禁档位递增:30分钟 → 3小时 → 3个月 → 半年(上限)。
func TestStagedBanEscalation(t *testing.T) {
s := newTestStores(t)
const ip = "203.0.113.11"
const maxFail = 2
failOnce := func() bool {
banned, _ := s.RecordAuthFailure(ip, maxFail, 30, "登录失败次数过多")
return banned
}
// 第 1 次触发需要 maxFail 次失败;此后每次失败即触发下一轮
if failOnce() {
t.Fatal("fail 1 must not trigger")
}
if failOnce() { // 触发 1
t.Fatal("trigger 1 must not ban")
}
if failOnce() { // 触发 2
t.Fatal("trigger 2 must not ban")
}
if failOnce() { // 触发 3
t.Fatal("trigger 3 must not ban")
}
// 第 4 次触发:30 分钟
if !failOnce() {
t.Fatal("trigger 4 should ban")
}
expectBanDuration(t, s, ip, 4, 30*time.Minute)
// 第 5 次:3 小时
expireBan(t, s, ip)
if !failOnce() {
t.Fatal("trigger 5 should ban")
}
expectBanDuration(t, s, ip, 5, 3*time.Hour)
// 第 6 次:3 个月
expireBan(t, s, ip)
if !failOnce() {
t.Fatal("trigger 6 should ban")
}
expectBanDuration(t, s, ip, 6, 90*24*time.Hour)
// 第 7 次:半年
expireBan(t, s, ip)
if !failOnce() {
t.Fatal("trigger 7 should ban")
}
expectBanDuration(t, s, ip, 7, 180*24*time.Hour)
// 第 8 次:仍为半年(上限)
expireBan(t, s, ip)
if !failOnce() {
t.Fatal("trigger 8 should ban")
}
expectBanDuration(t, s, ip, 8, 180*24*time.Hour)
entry, _ := s.Bans.GetByIP(ip)
if !strings.Contains(entry.Reason, "第5次封禁") {
t.Fatalf("reason = %q, want 第5次封禁", entry.Reason)
}
}
// expectBanDuration 断言该 IP 当前封禁时长约为 min(允许 2 分钟误差)。
func expectBanDuration(t *testing.T, s *Stores, ip string, trigger int, min time.Duration) {
t.Helper()
entry, err := s.Bans.GetByIP(ip)
if err != nil {
t.Fatalf("trigger %d: %v", trigger, err)
}
diff := entry.ExpiresAt.Sub(time.Now())
if diff < min-2*time.Minute || diff > min+2*time.Minute {
t.Fatalf("trigger %d: ban duration = %v, want ~%v", trigger, diff, min)
}
}
// expireBan 把该 IP 的封禁记录改成已过期(模拟时间流逝)。
func expireBan(t *testing.T, s *Stores, ip string) {
t.Helper()
entry, err := s.Bans.GetByIP(ip)
if err != nil {
t.Fatalf("get entry: %v", err)
}
entry.ExpiresAt = time.Now().Add(-time.Minute)
if err := s.Bans.Update(entry); err != nil {
t.Fatalf("update entry: %v", err)
}
}
// TestBanListOnlyBannedOrExpired 验证列表只返回封禁(含已过期)记录,
// 仅计数的观察记录不出现。
func TestBanListOnlyBannedOrExpired(t *testing.T) {
s := newTestStores(t)
// 观察记录:失败计数,未封禁(无到期时间)
if _, err := s.Bans.IncrementFail("203.0.113.20"); err != nil {
t.Fatalf("increment: %v", err)
}
// 当前生效的封禁
if err := s.Bans.Create(&db.BanEntry{
IPAddress: "198.51.100.21",
Reason: "第1次封禁:登录失败次数过多(第4次触发,失败5次)",
FailCount: 5,
BanCount: 4,
ExpiresAt: time.Now().Add(30 * time.Minute),
}); err != nil {
t.Fatalf("create ban: %v", err)
}
// 已过期的封禁(历史)
if err := s.Bans.Create(&db.BanEntry{
IPAddress: "198.51.100.22",
Reason: "第2次封禁:登录失败次数过多(第5次触发,失败6次)",
FailCount: 6,
BanCount: 5,
ExpiresAt: time.Now().Add(-24 * time.Hour),
}); err != nil {
t.Fatalf("create expired ban: %v", err)
}
entries, total, err := s.Bans.List(1, 10)
if err != nil {
t.Fatalf("list: %v", err)
}
if total != 2 {
t.Fatalf("total = %d, want 2", total)
}
if len(entries) != 2 {
t.Fatalf("len = %d, want 2", len(entries))
}
for _, e := range entries {
if e.ExpiresAt.Before(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) {
t.Fatalf("observation record leaked into list: %+v", e)
}
}
}
// TestRecordAuthFailureEmptyIPSafe 空 IP 不应产生副作用。
func TestRecordAuthFailureEmptyIPSafe(t *testing.T) {
s := newTestStores(t)
banned, count := s.RecordAuthFailure("", 3, 30, "登录失败次数过多")
if banned || count != 0 {
t.Fatalf("empty IP must be a no-op: banned=%v count=%d", banned, count)
}
if _, err := s.Bans.GetByIP(""); err == nil {
t.Fatal("empty IP should not be recorded")
}
}
// TestRecordAuthFailureWebAndProtocolShared 协议层与 Web 层共用封禁记录。
func TestRecordAuthFailureWebAndProtocolShared(t *testing.T) {
s := newTestStores(t)
const ip = "198.51.100.20"
// Web 层已封禁(直接建记录模拟),协议层认证必须被拒绝
s.Bans.Create(&db.BanEntry{
IPAddress: ip,
Reason: "web login failures",
FailCount: 5,
ExpiresAt: time.Now().Add(30 * time.Minute),
})
if banned, _ := s.Bans.IsBanned(ip); !banned {
t.Fatal("IP should be banned for both web and protocol auth")
}
}
func TestClientIPFromAddr(t *testing.T) {
cases := []struct {
addr net.Addr
want string
}{
{nil, ""},
{addrMock("203.0.113.5:12345"), "203.0.113.5"},
{addrMock("[2001:db8::1]:993"), "2001:db8::1"},
{addrMock("bad-format"), "bad-format"},
}
for _, tc := range cases {
if got := ClientIPFromAddr(tc.addr); got != tc.want {
t.Errorf("ClientIPFromAddr(%v) = %q, want %q", tc.addr, got, tc.want)
}
}
}
// addrMock 实现 net.Addr 的最小桩。
type addrMock string
func (a addrMock) Network() string { return "tcp" }
func (a addrMock) String() string { return string(a) }
// P3 #13:配额原子预扣——并发/超额场景下不得绕过配额。
func TestTryReserveQuota(t *testing.T) {
s := newTestStores(t)
// 用户配额 1000
user := &db.User{
Username: "quota_user",
PasswordHash: "x",
DomainID: 0,
QuotaBytes: 1000,
IsActive: true,
}
if err := s.Users.Create(user); err != nil {
t.Fatalf("create user: %v", err)
}
if user.ID == 0 {
t.Fatal("user ID must be assigned")
}
// 预扣 600 成功
ok, err := s.Users.TryReserveQuota(user.ID, 600)
if err != nil || !ok {
t.Fatalf("reserve 600: ok=%v err=%v", ok, err)
}
// 再扣 400 正好用完
ok, err = s.Users.TryReserveQuota(user.ID, 400)
if err != nil || !ok {
t.Fatalf("reserve 400: ok=%v err=%v", ok, err)
}
// 超出配额被拒且不改变 used_bytes
ok, err = s.Users.TryReserveQuota(user.ID, 1)
if err != nil {
t.Fatalf("reserve beyond quota: %v", err)
}
if ok {
t.Fatal("reserve beyond quota must fail")
}
got, _ := s.Users.GetByID(user.ID)
if got.UsedBytes != 1000 {
t.Fatalf("used_bytes = %d, want 1000 (no partial charge)", got.UsedBytes)
}
// 释放后可以再次预扣
if err := s.Users.UpdateUsedBytes(user.ID, -500); err != nil {
t.Fatalf("release: %v", err)
}
ok, err = s.Users.TryReserveQuota(user.ID, 500)
if err != nil || !ok {
t.Fatalf("reserve after release: ok=%v err=%v", ok, err)
}
}
// P3 #13:非正 delta 不允许(防御)。
func TestTryReserveQuotaNonPositiveDelta(t *testing.T) {
s := newTestStores(t)
user := &db.User{Username: "u", PasswordHash: "x", QuotaBytes: 100}
if err := s.Users.Create(user); err != nil {
t.Fatal(err)
}
for _, delta := range []int64{0, -10} {
ok, err := s.Users.TryReserveQuota(user.ID, delta)
if err != nil {
t.Fatalf("delta %d: %v", delta, err)
}
if ok {
t.Fatalf("delta %d must not reserve", delta)
}
}
got, _ := s.Users.GetByID(user.ID)
if got.UsedBytes != 0 {
t.Fatalf("used_bytes = %d, want 0", got.UsedBytes)
}
}
+51 -12
View File
@@ -8,16 +8,48 @@ import (
"gorm.io/gorm"
)
// 阶段封禁档位(分钟/天),从第 4 次触发阈值开始封禁:
// 第 4 次 = ban_duration_min(默认 30 分钟)→ 第 5 次 = 3 小时 →
// 第 6 次 = 3 个月 → 第 7 次起 = 半年(上限)。
const (
// freeTriggers 达到失败阈值但暂不封禁的触发次数(前 3 次只计数)。
freeTriggers = 3
banStage2Min = 3 * 60 // 3 小时
banStage3Day = 90 // 3 个月
banStage4Day = 180 // 半年(上限)
banMaxDay = banStage4Day
)
// stageDuration 返回第 banCount 次封禁(banCount 从 1 开始)的时长。
// firstBanMin 是第一次封禁的分钟数(来自配置 [ban] ban_duration_min)。
func stageDuration(banCount int, firstBanMin int) time.Duration {
switch banCount {
case 1:
if firstBanMin <= 0 {
firstBanMin = 30
}
return time.Duration(firstBanMin) * time.Minute
case 2:
return time.Duration(banStage2Min) * time.Minute
case 3:
return time.Duration(banStage3Day) * 24 * time.Hour
default:
return time.Duration(banMaxDay) * 24 * time.Hour
}
}
// BanStore defines the interface for IP ban operations.
type BanStore interface {
Create(entry *db.BanEntry) error
GetByIP(ip string) (*db.BanEntry, error)
Update(entry *db.BanEntry) error
Delete(id uint) error
// List 返回已封禁或曾封禁的记录(不含仅计数未封禁的观察记录)。
List(page, size int) ([]db.BanEntry, int64, error)
IsBanned(ip string) (bool, *db.BanEntry)
// IncrementFail 累加该 IP 的失败次数(无记录时创建),保留 BanCount。
IncrementFail(ip string) (int, error)
ResetFail(ip string) error
Cleanup() error
}
// banStoreGorm implements BanStore using GORM.
@@ -44,22 +76,33 @@ func (s *banStoreGorm) GetByIP(ip string) (*db.BanEntry, error) {
return &entry, nil
}
// Update saves changes to an existing ban entry record.
func (s *banStoreGorm) Update(entry *db.BanEntry) error {
return s.db.Save(entry).Error
}
// Delete removes a ban entry by ID.
func (s *banStoreGorm) Delete(id uint) error {
return s.db.Delete(&db.BanEntry{}, id).Error
}
// List retrieves a paginated list of ban entries.
// banEpochSentinel 用于区分“未封禁的计数记录”(expires_at 为零值):
// 所有实际封禁记录的到期时间都晚于 2000 年。
var banEpochSentinel = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
// List retrieves a paginated list of ban entries that are or have been
// banned (expires_at set). 仅计数的观察记录(零到期时间、无原因)不返回。
func (s *banStoreGorm) List(page, size int) ([]db.BanEntry, int64, error) {
var entries []db.BanEntry
var total int64
if err := s.db.Model(&db.BanEntry{}).Count(&total).Error; err != nil {
query := s.db.Model(&db.BanEntry{}).Where("expires_at > ?", banEpochSentinel)
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
offset := (page - 1) * size
if err := s.db.Order("id DESC").Offset(offset).Limit(size).Find(&entries).Error; err != nil {
if err := query.Order("id DESC").Offset(offset).Limit(size).Find(&entries).Error; err != nil {
return nil, 0, err
}
return entries, total, nil
@@ -76,7 +119,8 @@ func (s *banStoreGorm) IsBanned(ip string) (bool, *db.BanEntry) {
}
// IncrementFail increments the fail count for an IP address.
// If no record exists, it creates one with fail_count=1 and a zero expires_at.
// If no record exists, it creates one with fail_count=1, ban_count=0 and a
// zero expires_at (not yet banned). Existing BanCount is preserved.
// Returns the updated fail count.
func (s *banStoreGorm) IncrementFail(ip string) (int, error) {
var entry db.BanEntry
@@ -86,6 +130,7 @@ func (s *banStoreGorm) IncrementFail(ip string) (int, error) {
entry = db.BanEntry{
IPAddress: ip,
FailCount: 1,
BanCount: 0,
ExpiresAt: time.Time{}, // Zero time, not yet banned
}
if createErr := s.db.Create(&entry).Error; createErr != nil {
@@ -103,13 +148,7 @@ func (s *banStoreGorm) IncrementFail(ip string) (int, error) {
}
// ResetFail resets the fail count for an IP address by deleting its record.
// 成功登录(或管理员解封)后调用:封禁档位历史随之清零。
func (s *banStoreGorm) ResetFail(ip string) error {
return s.db.Where("ip_address = ?", ip).Delete(&db.BanEntry{}).Error
}
// Cleanup removes expired ban entries.
// It deletes records where expires_at is in the past and is not zero
// (preserving records that have fail counts but are not yet banned).
func (s *banStoreGorm) Cleanup() error {
return s.db.Where("expires_at < ? AND expires_at > ?", time.Now(), time.Time{}).Delete(&db.BanEntry{}).Error
}
+5 -2
View File
@@ -118,11 +118,14 @@ func (s *mailStoreGorm) CountUnread(userID uint, folder string) (int64, error) {
}
// ListAllByUserAndFolder retrieves all messages for a user in a folder without pagination.
// Messages are ordered by ID ascending so that sequence numbers are stable.
// 按 date DESC, id DESC 排序(最新在前):与主流邮件客户端(Thunderbird、
// 手机客户端等)默认视图一致,客户端自行按日期编号的 seq 式 STORE 不会
// 错位标错邮件。所有 IMAP 序号相关路径(Status/ListMessages/推送/seqOf
// 共用本排序,保证序号全链路一致。
func (s *mailStoreGorm) ListAllByUserAndFolder(userID uint, folder string) ([]db.Message, error) {
var messages []db.Message
if err := s.db.Where("user_id = ? AND folder = ?", userID, folder).
Order("id ASC").Find(&messages).Error; err != nil {
Order("date DESC, id DESC").Find(&messages).Error; err != nil {
return nil, err
}
return messages, nil
+12
View File
@@ -15,6 +15,9 @@ type OutboundStore interface {
ListDue(now time.Time, limit int) ([]db.OutboundMessage, error)
List(page, size int, status string) ([]db.OutboundMessage, int64, error)
Update(msg *db.OutboundMessage) error
// Claim 原子地将一项待投递邮件置为 sending;仅当该项仍处于
// pending/deferred 时成功(并发 worker 抢占,防重复投递)。
Claim(id uint) (bool, error)
Delete(id uint) error
CountByStatus(status string) (int64, error)
}
@@ -85,6 +88,15 @@ func (s *outboundStoreGorm) Update(msg *db.OutboundMessage) error {
return s.db.Save(msg).Error
}
// Claim 原子抢占:把 pending/deferred 项置为 sending。
// 返回是否抢占成功(false 表示已被其他 worker 抢先或状态已变化)。
func (s *outboundStoreGorm) Claim(id uint) (bool, error) {
res := s.db.Model(&db.OutboundMessage{}).
Where("id = ? AND status IN (?, ?)", id, db.OutboundStatusPending, db.OutboundStatusDeferred).
Update("status", db.OutboundStatusSending)
return res.RowsAffected == 1, res.Error
}
// Delete removes an outbound queue record by ID.
func (s *outboundStoreGorm) Delete(id uint) error {
return s.db.Delete(&db.OutboundMessage{}, id).Error
+102
View File
@@ -0,0 +1,102 @@
package store
import (
"sync"
"sync/atomic"
"testing"
"time"
"mail_go/internal/db"
)
// TestOutboundClaimAtomic 验证并发抢占同一队列项恰好只有一次成功。
func TestOutboundClaimAtomic(t *testing.T) {
s := newTestStores(t)
item := &db.OutboundMessage{
MessageID: "<t@test>",
FromAddr: "a@test.local",
ToAddr: "b@fake.test",
RecipientDom: "fake.test",
RawData: "raw",
Status: db.OutboundStatusPending,
NextAttemptAt: time.Now(),
}
if err := s.Outbound.Create(item); err != nil {
t.Fatalf("create: %v", err)
}
const n = 10
var wins int64
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
defer wg.Done()
ok, err := s.Outbound.Claim(item.ID)
if err != nil {
t.Errorf("claim: %v", err)
return
}
if ok {
atomic.AddInt64(&wins, 1)
}
}()
}
wg.Wait()
if wins != 1 {
t.Fatalf("claims won = %d, want exactly 1", wins)
}
got, err := s.Outbound.GetByID(item.ID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.Status != db.OutboundStatusSending {
t.Fatalf("status = %s, want sending", got.Status)
}
}
// TestOutboundClaimStatuses 验证只有 pending/deferred 可被抢占。
func TestOutboundClaimStatuses(t *testing.T) {
s := newTestStores(t)
cases := []struct {
status string
want bool
}{
{db.OutboundStatusPending, true},
{db.OutboundStatusDeferred, true},
{db.OutboundStatusSending, false},
{db.OutboundStatusSent, false},
{db.OutboundStatusFailed, false},
{db.OutboundStatusCanceled, false},
}
for _, tc := range cases {
item := &db.OutboundMessage{
MessageID: "<t@test>",
FromAddr: "a@test.local",
ToAddr: "b@fake.test",
RecipientDom: "fake.test",
RawData: "raw",
Status: tc.status,
NextAttemptAt: time.Now(),
}
if err := s.Outbound.Create(item); err != nil {
t.Fatalf("create %s: %v", tc.status, err)
}
ok, err := s.Outbound.Claim(item.ID)
if err != nil {
t.Fatalf("claim %s: %v", tc.status, err)
}
if ok != tc.want {
t.Fatalf("claim %s = %v, want %v", tc.status, ok, tc.want)
}
if tc.want {
got, _ := s.Outbound.GetByID(item.ID)
if got.Status != db.OutboundStatusSending {
t.Fatalf("claimed %s item must become sending", tc.status)
}
}
}
}
+139
View File
@@ -0,0 +1,139 @@
package store
import (
"time"
"mail_go/internal/db"
"gorm.io/gorm"
)
// ProtocolLogFilter holds optional filters for listing protocol logs.
type ProtocolLogFilter struct {
Protocol string // smtp | imap | pop3,空表示全部
Success *bool // nil 表示全部
IP string // 客户端 IP 模糊匹配
Username string // 用户名模糊匹配
From time.Time // 起(含)
To time.Time // 止(含)
}
// ProtocolLogStore defines the interface for protocol call log operations.
type ProtocolLogStore interface {
Create(log *db.ProtocolLog) error
// UpdateDuration 回填会话时长(登录后连接关闭时调用)。
UpdateDuration(id uint, durationMs int64) error
List(page, size int, filter ProtocolLogFilter) ([]db.ProtocolLog, int64, error)
// CountStats 汇总各协议的失败/成功记录数(用于页面统计卡片)。
CountStats(from time.Time) (map[string]map[string]int64, error)
// CleanupBefore 删除 created_at 早于 before 的记录。
CleanupBefore(before time.Time) (int64, error)
}
// protocolLogStoreGorm implements ProtocolLogStore using GORM.
type protocolLogStoreGorm struct {
db *gorm.DB
}
// newProtocolLogStore creates a new GORM-backed ProtocolLogStore.
func newProtocolLogStore(database *gorm.DB) ProtocolLogStore {
return &protocolLogStoreGorm{db: database}
}
// Create inserts a new protocol log record.
func (s *protocolLogStoreGorm) Create(log *db.ProtocolLog) error {
return s.db.Create(log).Error
}
// UpdateDuration 回填会话时长,仅更新 duration_ms 字段。
func (s *protocolLogStoreGorm) UpdateDuration(id uint, durationMs int64) error {
return s.db.Model(&db.ProtocolLog{}).Where("id = ?", id).Update("duration_ms", durationMs).Error
}
// List retrieves a paginated list of protocol logs, newest first.
func (s *protocolLogStoreGorm) List(page, size int, filter ProtocolLogFilter) ([]db.ProtocolLog, int64, error) {
var logs []db.ProtocolLog
var total int64
query := s.db.Model(&db.ProtocolLog{})
query = s.applyFilter(query, filter)
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
offset := (page - 1) * size
if err := query.Order("id DESC").Offset(offset).Limit(size).Find(&logs).Error; err != nil {
return nil, 0, err
}
return logs, total, nil
}
func (s *protocolLogStoreGorm) applyFilter(query *gorm.DB, filter ProtocolLogFilter) *gorm.DB {
if filter.Protocol != "" {
query = query.Where("protocol = ?", filter.Protocol)
}
if filter.Success != nil {
query = query.Where("success = ?", *filter.Success)
}
if filter.IP != "" {
query = query.Where("client_ip LIKE ?", "%"+filter.IP+"%")
}
if filter.Username != "" {
query = query.Where("username LIKE ?", "%"+filter.Username+"%")
}
if !filter.From.IsZero() {
query = query.Where("created_at >= ?", filter.From)
}
if !filter.To.IsZero() {
query = query.Where("created_at <= ?", filter.To)
}
return query
}
// CountStats 返回自 from 以来的记录数,按 protocol 再按 success 分组:
// map[protocol]map[successKey]count。successKey 为 "success"/"fail"。
func (s *protocolLogStoreGorm) CountStats(from time.Time) (map[string]map[string]int64, error) {
stats := make(map[string]map[string]int64)
for _, proto := range []string{db.ProtocolSMTP, db.ProtocolIMAP, db.ProtocolPOP3} {
stats[proto] = map[string]int64{"success": 0, "fail": 0}
}
type row struct {
Protocol string
Success bool
Count int64
}
var rows []row
query := s.db.Model(&db.ProtocolLog{}).
Select("protocol, success, COUNT(*) AS count").
Group("protocol, success")
if !from.IsZero() {
query = query.Where("created_at >= ?", from)
}
if err := query.Scan(&rows).Error; err != nil {
return nil, err
}
for _, r := range rows {
proto := stats[r.Protocol]
if proto == nil {
proto = map[string]int64{"success": 0, "fail": 0}
stats[r.Protocol] = proto
}
key := "fail"
if r.Success {
key = "success"
}
proto[key] = r.Count
}
return stats, nil
}
// CleanupBefore deletes records older than the given time and returns the
// number of deleted rows.
func (s *protocolLogStoreGorm) CleanupBefore(before time.Time) (int64, error) {
res := s.db.Where("created_at < ?", before).Delete(&db.ProtocolLog{})
return res.RowsAffected, res.Error
}
+171
View File
@@ -0,0 +1,171 @@
package store
import (
"testing"
"time"
"mail_go/internal/db"
)
func TestProtocolLogCreateAndUpdateDuration(t *testing.T) {
s := newTestStores(t)
entry := &db.ProtocolLog{
Protocol: db.ProtocolIMAP,
Port: 143,
ClientIP: "203.0.113.9",
Username: "alice",
Success: true,
Detail: "LOGIN 成功",
CreatedAt: time.Now(),
}
if err := s.ProtocolLogs.Create(entry); err != nil {
t.Fatalf("create: %v", err)
}
if entry.ID == 0 {
t.Fatal("expected generated ID")
}
if err := s.ProtocolLogs.UpdateDuration(entry.ID, 3210); err != nil {
t.Fatalf("update duration: %v", err)
}
logs, total, err := s.ProtocolLogs.List(1, 10, ProtocolLogFilter{})
if err != nil {
t.Fatalf("list: %v", err)
}
if total != 1 || len(logs) != 1 {
t.Fatalf("expected 1 log, got total=%d len=%d", total, len(logs))
}
if logs[0].DurationMs != 3210 {
t.Fatalf("duration = %d, want 3210", logs[0].DurationMs)
}
if logs[0].Success != true || logs[0].Username != "alice" {
t.Fatalf("unexpected log: %+v", logs[0])
}
}
func TestProtocolLogListFilters(t *testing.T) {
s := newTestStores(t)
now := time.Now()
entries := []*db.ProtocolLog{
{Protocol: db.ProtocolSMTP, Port: 25, ClientIP: "10.0.0.1", Username: "", Success: true, FailReason: "", Detail: "投递", CreatedAt: now.Add(-3 * time.Hour)},
{Protocol: db.ProtocolSMTP, Port: 25, ClientIP: "10.0.0.2", Username: "admin", Success: false, FailReason: "中继访问被拒绝", Detail: "RCPT", CreatedAt: now.Add(-2 * time.Hour)},
{Protocol: db.ProtocolIMAP, Port: 993, ClientIP: "10.0.0.2", Username: "admin", Success: false, FailReason: "用户名或密码错误", Detail: "LOGIN 失败", CreatedAt: now.Add(-1 * time.Hour)},
{Protocol: db.ProtocolPOP3, Port: 110, ClientIP: "10.0.0.3", Username: "bob", Success: true, FailReason: "", Detail: "STAT", CreatedAt: now},
}
for _, e := range entries {
if err := s.ProtocolLogs.Create(e); err != nil {
t.Fatalf("create: %v", err)
}
}
cases := []struct {
name string
filter ProtocolLogFilter
want int64
}{
{"全部", ProtocolLogFilter{}, 4},
{"按协议", ProtocolLogFilter{Protocol: db.ProtocolSMTP}, 2},
{"按失败", ProtocolLogFilter{Success: boolPtr(false)}, 2},
{"按成功", ProtocolLogFilter{Success: boolPtr(true)}, 2},
{"协议+失败", ProtocolLogFilter{Protocol: db.ProtocolIMAP, Success: boolPtr(false)}, 1},
{"按IP模糊", ProtocolLogFilter{IP: "10.0.0.2"}, 2},
{"按用户名", ProtocolLogFilter{Username: "admin"}, 2},
{"按时间起", ProtocolLogFilter{From: now.Add(-90 * time.Minute)}, 2},
{"按时间止", ProtocolLogFilter{To: now.Add(-2 * time.Hour)}, 2},
{"无匹配", ProtocolLogFilter{Protocol: db.ProtocolPOP3, Success: boolPtr(false)}, 0},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, total, err := s.ProtocolLogs.List(1, 50, tc.filter)
if err != nil {
t.Fatalf("list: %v", err)
}
if total != tc.want {
t.Fatalf("total = %d, want %d", total, tc.want)
}
})
}
}
func TestProtocolLogListPagination(t *testing.T) {
s := newTestStores(t)
for i := 0; i < 5; i++ {
if err := s.ProtocolLogs.Create(&db.ProtocolLog{
Protocol: db.ProtocolSMTP, ClientIP: "10.0.0.1",
Success: true, CreatedAt: time.Now(),
}); err != nil {
t.Fatalf("create: %v", err)
}
}
page1, total, err := s.ProtocolLogs.List(1, 2, ProtocolLogFilter{})
if err != nil {
t.Fatalf("list: %v", err)
}
if total != 5 || len(page1) != 2 {
t.Fatalf("page1 total=%d len=%d", total, len(page1))
}
// 新记录在前
if page1[0].ID < page1[1].ID {
t.Fatal("expected newest first")
}
page3, _, err := s.ProtocolLogs.List(3, 2, ProtocolLogFilter{})
if err != nil {
t.Fatalf("list page3: %v", err)
}
if len(page3) != 1 {
t.Fatalf("page3 len = %d, want 1", len(page3))
}
}
func TestProtocolLogCountStatsAndCleanup(t *testing.T) {
s := newTestStores(t)
now := time.Now()
entries := []*db.ProtocolLog{
{Protocol: db.ProtocolSMTP, ClientIP: "a", Success: true, CreatedAt: now.Add(-10 * time.Minute)},
{Protocol: db.ProtocolSMTP, ClientIP: "b", Success: false, CreatedAt: now.Add(-20 * time.Minute)},
{Protocol: db.ProtocolIMAP, ClientIP: "c", Success: false, CreatedAt: now.Add(-30 * time.Minute)},
{Protocol: db.ProtocolIMAP, ClientIP: "d", Success: false, CreatedAt: now.AddDate(0, 0, -40)},
}
for _, e := range entries {
if err := s.ProtocolLogs.Create(e); err != nil {
t.Fatalf("create: %v", err)
}
}
stats, err := s.ProtocolLogs.CountStats(now.Add(-24 * time.Hour))
if err != nil {
t.Fatalf("stats: %v", err)
}
if stats[db.ProtocolSMTP]["success"] != 1 || stats[db.ProtocolSMTP]["fail"] != 1 {
t.Fatalf("smtp stats: %+v", stats[db.ProtocolSMTP])
}
if stats[db.ProtocolIMAP]["fail"] != 1 {
t.Fatalf("imap fail stats: %+v", stats[db.ProtocolIMAP])
}
// 清理 30 天前的记录
n, err := s.ProtocolLogs.CleanupBefore(now.AddDate(0, 0, -30))
if err != nil {
t.Fatalf("cleanup: %v", err)
}
if n != 1 {
t.Fatalf("deleted = %d, want 1", n)
}
_, total, err := s.ProtocolLogs.List(1, 50, ProtocolLogFilter{})
if err != nil {
t.Fatalf("list: %v", err)
}
if total != 3 {
t.Fatalf("total = %d, want 3", total)
}
}
func boolPtr(v bool) *bool {
return &v
}
+15 -12
View File
@@ -8,23 +8,25 @@ import (
// Stores aggregates all store interfaces for convenient access.
type Stores struct {
Users UserStore
Mails MailStore
Domains DomainStore
Attachments AttachmentStore
Bans BanStore
Outbound OutboundStore
Users UserStore
Mails MailStore
Domains DomainStore
Attachments AttachmentStore
Bans BanStore
Outbound OutboundStore
ProtocolLogs ProtocolLogStore
}
// NewStores creates a new Stores instance with all GORM-backed implementations.
func NewStores(database *gorm.DB) *Stores {
return &Stores{
Users: newUserStore(database),
Mails: newMailStore(database),
Domains: newDomainStore(database),
Attachments: newAttachmentStore(database),
Bans: newBanStore(database),
Outbound: newOutboundStore(database),
Users: newUserStore(database),
Mails: newMailStore(database),
Domains: newDomainStore(database),
Attachments: newAttachmentStore(database),
Bans: newBanStore(database),
Outbound: newOutboundStore(database),
ProtocolLogs: newProtocolLogStore(database),
}
}
@@ -34,3 +36,4 @@ var _ = db.Domain{}
var _ = db.Message{}
var _ = db.Attachment{}
var _ = db.BanEntry{}
var _ = db.ProtocolLog{}
+26 -2
View File
@@ -22,6 +22,9 @@ type UserStore interface {
ListAll(page, size int) ([]db.User, int64, error)
UpdateUsedBytes(id uint, delta int64) error
UpdatePassword(userID uint, hashedPassword string) error
// TryReserveQuota 原子预扣 delta 字节:仅在不超过配额时生效并返回 true,
// 否则不做任何修改返回 false。防止并发提交绕过配额检查(TOCTOU)。
TryReserveQuota(userID uint, delta int64) (bool, error)
}
// userStoreGorm implements UserStore using GORM.
@@ -124,9 +127,30 @@ func (s *userStoreGorm) UpdateUsedBytes(id uint, delta int64) error {
Update("used_bytes", gorm.Expr("used_bytes + ?", delta)).Error
}
// UpdatePassword updates the password hash for a user.
// TryReserveQuota atomically reserves delta bytes for a user within quota.
// The reservation is applied (used_bytes incremented) only when it does not
// exceed quota_bytes; otherwise no change is made and false is returned.
func (s *userStoreGorm) TryReserveQuota(userID uint, delta int64) (bool, error) {
if delta <= 0 {
return false, nil
}
res := s.db.Model(&db.User{}).
Where("id = ? AND used_bytes + ? <= quota_bytes", userID, delta).
Update("used_bytes", gorm.Expr("used_bytes + ?", delta))
if res.Error != nil {
return false, res.Error
}
return res.RowsAffected == 1, nil
}
// UpdatePassword updates the password hash for a user and clears the
// must-change-password flag (the user has now set their own password).
func (s *userStoreGorm) UpdatePassword(userID uint, hashedPassword string) error {
return s.db.Model(&db.User{}).Where("id = ?", userID).Update("password_hash", hashedPassword).Error
return s.db.Model(&db.User{}).Where("id = ?", userID).
Updates(map[string]interface{}{
"password_hash": hashedPassword,
"must_change_password": false,
}).Error
}
// ListAll retrieves a paginated list of all users across all domains.
+127
View File
@@ -0,0 +1,127 @@
// Package tlsutil 提供 TLS 证书热加载:每次 TLS 握手时按需检查
// 证书路径与文件内容是否变化,变化则自动重载,证书更新后无需重启
// 服务即可生效。
package tlsutil
import (
"crypto/tls"
"fmt"
"os"
"sync"
"time"
)
// retryInterval 是重载失败后的最小重试间隔,避免证书文件损坏时
// 每个握手都重复做无意义的磁盘读取。
const retryInterval = 5 * time.Second
// Source 返回当前应使用的证书路径。路径可能随时间变化(例如管理后台
// 一键导入证书后切换到新的域名证书);返回空路径表示暂无可用证书。
type Source func() (certPath, keyPath string)
// Loader 管理一对可热加载的证书。所有方法均并发安全。
type Loader struct {
mu sync.Mutex
source Source
certPath string
keyPath string
cert *tls.Certificate
certMod time.Time
keyMod time.Time
lastTry time.Time
logf func(format string, args ...interface{})
}
// NewLoader 立即加载并校验证书,失败返回错误(保持启动时 fail-fast)。
// source 为 nil 时证书路径固定不变,仅检测文件内容变化。
func NewLoader(certPath, keyPath string, source Source, logf func(string, ...interface{})) (*Loader, error) {
if certPath == "" || keyPath == "" {
return nil, fmt.Errorf("TLS 证书路径为空")
}
l := &Loader{
source: source,
certPath: certPath,
keyPath: keyPath,
logf: logf,
}
if err := l.load(); err != nil {
return nil, err
}
return l, nil
}
// GetCertificate 实现 tls.Config.GetCertificate
// 每次 TLS 握手时检查证书路径与文件是否有变化,有则自动重载;
// 重载失败时继续使用上一次成功加载的证书,避免中断现有服务。
func (l *Loader) GetCertificate(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
l.mu.Lock()
defer l.mu.Unlock()
certPath, keyPath := l.certPath, l.keyPath
if l.source != nil {
certPath, keyPath = l.source()
}
if certPath == "" || keyPath == "" {
// 暂无证书可用:继续使用旧证书(若有)
return l.current()
}
changed := certPath != l.certPath || keyPath != l.keyPath
if !changed {
changed = l.filesChanged(certPath, keyPath)
}
// 仅在重载失败后节流(避免证书文件损坏时每个握手都重复读盘);
// 成功后清零节流,保证正常的连续更新立即生效。
if changed && time.Since(l.lastTry) >= retryInterval {
l.lastTry = time.Now()
l.certPath, l.keyPath = certPath, keyPath
if err := l.load(); err != nil {
if l.logf != nil {
l.logf("TLS 证书重载失败 (%s, %s): %v,继续使用旧证书", certPath, keyPath, err)
}
} else {
l.lastTry = time.Time{}
if l.logf != nil {
l.logf("TLS 证书已热加载: %s", certPath)
}
}
}
return l.current()
}
// current 返回当前已加载的证书。
func (l *Loader) current() (*tls.Certificate, error) {
if l.cert == nil {
return nil, fmt.Errorf("TLS 证书不可用")
}
return l.cert, nil
}
// filesChanged 判断证书/私钥文件自上次加载后是否被修改。
// 文件暂时不可读(如正在原子替换)时视为已变化,触发重载尝试。
func (l *Loader) filesChanged(certPath, keyPath string) bool {
stC, errC := os.Stat(certPath)
stK, errK := os.Stat(keyPath)
if errC != nil || errK != nil {
return true
}
return !stC.ModTime().Equal(l.certMod) || !stK.ModTime().Equal(l.keyMod)
}
// load 从当前路径加载证书对并记录文件修改时间。
func (l *Loader) load() error {
cert, err := tls.LoadX509KeyPair(l.certPath, l.keyPath)
if err != nil {
return err
}
if stC, err := os.Stat(l.certPath); err == nil {
l.certMod = stC.ModTime()
}
if stK, err := os.Stat(l.keyPath); err == nil {
l.keyMod = stK.ModTime()
}
l.cert = &cert
return nil
}
+241
View File
@@ -0,0 +1,241 @@
package tlsutil
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"os"
"path/filepath"
"sync"
"testing"
"time"
)
// writeCertPair 生成一对自签名证书并写入文件,返回叶子证书序列号。
func writeCertPair(t *testing.T, certPath, keyPath string, serial int64) {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("生成私钥失败: %v", err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(serial),
Subject: pkix.Name{CommonName: "test"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
DNSNames: []string{"localhost"},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatalf("生成证书失败: %v", err)
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
if err := os.MkdirAll(filepath.Dir(certPath), 0700); err != nil {
t.Fatalf("创建目录失败: %v", err)
}
if err := os.WriteFile(certPath, certPEM, 0600); err != nil {
t.Fatalf("写入证书失败: %v", err)
}
if err := os.WriteFile(keyPath, keyPEM, 0600); err != nil {
t.Fatalf("写入私钥失败: %v", err)
}
}
func leafSerial(t *testing.T, cert *x509.Certificate) *big.Int {
t.Helper()
return cert.SerialNumber
}
func TestNewLoaderFailsFast(t *testing.T) {
dir := t.TempDir()
certPath := filepath.Join(dir, "cert.pem")
keyPath := filepath.Join(dir, "key.pem")
os.WriteFile(certPath, []byte("garbage"), 0600)
os.WriteFile(keyPath, []byte("garbage"), 0600)
if _, err := NewLoader(certPath, keyPath, nil, nil); err == nil {
t.Fatal("无效证书应返回错误")
}
}
func TestReloadOnFileChange(t *testing.T) {
dir := t.TempDir()
certPath := filepath.Join(dir, "cert.pem")
keyPath := filepath.Join(dir, "key.pem")
writeCertPair(t, certPath, keyPath, 1)
l, err := NewLoader(certPath, keyPath, nil, nil)
if err != nil {
t.Fatalf("NewLoader 失败: %v", err)
}
c1, err := l.GetCertificate(nil)
if err != nil {
t.Fatalf("GetCertificate 失败: %v", err)
}
if c1.Leaf == nil {
if parsed, err := x509.ParseCertificate(c1.Certificate[0]); err == nil {
c1.Leaf = parsed
}
}
if leafSerial(t, c1.Leaf).Int64() != 1 {
t.Fatalf("初始证书序列号应为 1")
}
// 替换文件内容(模拟证书更新),无需重启
time.Sleep(10 * time.Millisecond) // 确保 mtime 变化
writeCertPair(t, certPath, keyPath, 2)
c2, err := l.GetCertificate(nil)
if err != nil {
t.Fatalf("更新后 GetCertificate 失败: %v", err)
}
if parsed, err := x509.ParseCertificate(c2.Certificate[0]); err == nil {
c2.Leaf = parsed
}
if leafSerial(t, c2.Leaf).Int64() != 2 {
t.Fatal("文件更新后应自动加载新证书(序列号 2)")
}
}
func TestStaleCertOnInvalidReload(t *testing.T) {
dir := t.TempDir()
certPath := filepath.Join(dir, "cert.pem")
keyPath := filepath.Join(dir, "key.pem")
writeCertPair(t, certPath, keyPath, 1)
l, err := NewLoader(certPath, keyPath, nil, nil)
if err != nil {
t.Fatalf("NewLoader 失败: %v", err)
}
// 写入损坏的证书:重载失败时应继续使用旧证书
time.Sleep(10 * time.Millisecond)
os.WriteFile(certPath, []byte("broken"), 0600)
c, err := l.GetCertificate(nil)
if err != nil {
t.Fatalf("重载失败时不应返回错误: %v", err)
}
if parsed, err := x509.ParseCertificate(c.Certificate[0]); err == nil {
c.Leaf = parsed
}
if leafSerial(t, c.Leaf).Int64() != 1 {
t.Fatal("重载失败时应继续使用旧证书")
}
}
func TestSourcePathSwitch(t *testing.T) {
dir := t.TempDir()
certA := filepath.Join(dir, "a", "cert.pem")
keyA := filepath.Join(dir, "a", "key.pem")
certB := filepath.Join(dir, "b", "cert.pem")
keyB := filepath.Join(dir, "b", "key.pem")
writeCertPair(t, certA, keyA, 1)
writeCertPair(t, certB, keyB, 2)
// 初始用 A,source 后续切换到 B(模拟后台导入新域名证书)
source := func() (string, string) { return certB, keyB }
l, err := NewLoader(certA, keyA, source, nil)
if err != nil {
t.Fatalf("NewLoader 失败: %v", err)
}
c, err := l.GetCertificate(nil)
if err != nil {
t.Fatalf("GetCertificate 失败: %v", err)
}
if parsed, err := x509.ParseCertificate(c.Certificate[0]); err == nil {
c.Leaf = parsed
}
if leafSerial(t, c.Leaf).Int64() != 2 {
t.Fatal("source 切换路径后应自动加载新证书")
}
}
func TestRapidSuccessiveChanges(t *testing.T) {
dir := t.TempDir()
certPath := filepath.Join(dir, "cert.pem")
keyPath := filepath.Join(dir, "key.pem")
writeCertPair(t, certPath, keyPath, 1)
l, err := NewLoader(certPath, keyPath, nil, nil)
if err != nil {
t.Fatalf("NewLoader 失败: %v", err)
}
serialOf := func() int64 {
t.Helper()
c, err := l.GetCertificate(nil)
if err != nil {
t.Fatalf("GetCertificate 失败: %v", err)
}
parsed, err := x509.ParseCertificate(c.Certificate[0])
if err != nil {
t.Fatalf("解析证书失败: %v", err)
}
return parsed.SerialNumber.Int64()
}
// 5 秒内连续两次更新,两次都应立即生效(节流只针对失败重载)
time.Sleep(10 * time.Millisecond)
writeCertPair(t, certPath, keyPath, 2)
if got := serialOf(); got != 2 {
t.Fatalf("第一次更新后应加载序列号 2,实际 %d", got)
}
time.Sleep(10 * time.Millisecond)
writeCertPair(t, certPath, keyPath, 3)
if got := serialOf(); got != 3 {
t.Fatalf("第二次快速更新后应立即加载序列号 3,实际 %d", got)
}
}
func TestConcurrentGetCertificate(t *testing.T) {
dir := t.TempDir()
certPath := filepath.Join(dir, "cert.pem")
keyPath := filepath.Join(dir, "key.pem")
writeCertPair(t, certPath, keyPath, 1)
l, err := NewLoader(certPath, keyPath, nil, nil)
if err != nil {
t.Fatalf("NewLoader 失败: %v", err)
}
var wg sync.WaitGroup
stop := make(chan struct{})
// 并发读
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-stop:
return
default:
if _, err := l.GetCertificate(nil); err != nil {
t.Errorf("并发 GetCertificate 失败: %v", err)
return
}
}
}
}()
}
// 同时反复替换证书文件(模拟续期)
for i := int64(2); i < 6; i++ {
writeCertPair(t, certPath, keyPath, i)
time.Sleep(20 * time.Millisecond)
}
close(stop)
wg.Wait()
}
+293 -17
View File
@@ -5,12 +5,15 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"mail_go/internal/caddycert"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/dkim"
"mail_go/internal/outbound"
@@ -23,16 +26,81 @@ import (
// AdminHandler handles admin-related routes (dashboard, domain/user management).
type AdminHandler struct {
stores *store.Stores
storage *storage.AttachmentStorage
tlsDir string
outbound *outbound.Manager
stores *store.Stores
storage *storage.AttachmentStorage
tlsDir string
caddyDataDir string
outbound *outbound.Manager
// protocolLogKeepDays SMTP/IMAP/POP3 协议日志保留天数(配置文件 [web])
protocolLogKeepDays int
// hub 当前协议连接注册中心(「当前连接」页)
hub *connhub.Hub
}
// NewAdminHandler creates a new AdminHandler with the given stores, attachment
// storage, TLS directory and outbound delivery manager.
func NewAdminHandler(stores *store.Stores, attStorage *storage.AttachmentStorage, tlsDir string, ob *outbound.Manager) *AdminHandler {
return &AdminHandler{stores: stores, storage: attStorage, tlsDir: tlsDir, outbound: ob}
// storage, TLS directory, Caddy data directory and outbound delivery manager.
func NewAdminHandler(stores *store.Stores, attStorage *storage.AttachmentStorage, tlsDir string, caddyDataDir string, ob *outbound.Manager, protocolLogKeepDays int, hub *connhub.Hub) *AdminHandler {
return &AdminHandler{stores: stores, storage: attStorage, tlsDir: tlsDir, caddyDataDir: caddyDataDir, outbound: ob, protocolLogKeepDays: protocolLogKeepDays, hub: hub}
}
// manualBanDuration 管理员手动封禁时长(180 天,与自动封禁档位上限制一致)。
const manualBanDuration = 180 * 24 * time.Hour
// DisconnectConnection 强制断开指定连接并封禁其 IP(管理后台「断开并封禁」)。
// 封禁后该 IP 的所有在线连接一并断开。
func (h *AdminHandler) DisconnectConnection(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "无效的连接ID")
return
}
conn, ok := h.hub.Get(id)
if !ok {
c.String(http.StatusNotFound, "连接不存在或已断开")
return
}
// 加入黑名单:180 天封禁(管理员可随时解封)
if err := h.stores.Bans.Create(&db.BanEntry{
IPAddress: conn.IP,
Reason: "管理员手动封禁(连接断开)",
FailCount: 0,
BanCount: 0,
ExpiresAt: time.Now().Add(manualBanDuration),
}); err != nil {
c.String(http.StatusInternalServerError, "封禁失败: %v", err)
return
}
// 断开该 IP 的全部连接(含本连接与其他协议连接)
n := h.hub.DisconnectByIP(conn.IP)
log.Printf("admin: 已封禁并断开 IP %s 的 %d 个连接", conn.IP, n)
c.Redirect(http.StatusFound, "/admin/connections")
}
// ListConnections 渲染当前协议连接页面(SMTP/IMAP/POP3 实时连接)。
func (h *AdminHandler) ListConnections(c *gin.Context) {
conns := h.hub.List()
counts := h.hub.Counts()
total := len(conns)
smtpCount := counts["smtp"]
imapCount := counts["imap"]
pop3Count := counts["pop3"]
currentUser, _ := c.Get("currentUser")
c.HTML(200, "admin_connections", gin.H{
"currentUser": currentUser,
"conns": conns,
"total": total,
"smtpCount": smtpCount,
"imapCount": imapCount,
"pop3Count": pop3Count,
"now": time.Now(),
"activeFolder": "connections",
})
}
// Dashboard renders the admin dashboard with summary statistics.
@@ -209,6 +277,16 @@ func (h *AdminHandler) EditDomain(c *gin.Context) {
}
currentUser, _ := c.Get("currentUser")
caddyMsg, caddyMsgType := "", ""
if c.Query("caddy_err") != "" {
caddyMsg = c.Query("caddy_err")
caddyMsgType = "error"
} else if c.Query("caddy_ok") == "1" {
caddyMsg = "✅ 已从 Caddy 获取证书并保存到域名 TLS 目录,同时已启用该域名的 TLS;证书已热加载,无需重启服务。"
caddyMsgType = "success"
}
c.HTML(200, "admin_domain_form", gin.H{
"currentUser": currentUser,
"activeFolder": "domains",
@@ -217,6 +295,8 @@ func (h *AdminHandler) EditDomain(c *gin.Context) {
"domain": domain,
"tlsPublicCert": readTLSCert(domain.TlsCertPath),
"tlsCertConfigured": domain.TlsCertPath != "" && domain.TlsKeyPath != "",
"caddyMsg": caddyMsg,
"caddyMsgType": caddyMsgType,
})
}
@@ -264,6 +344,78 @@ func (h *AdminHandler) UpdateDomain(c *gin.Context) {
c.Redirect(http.StatusFound, "/admin/domains")
}
// FetchCaddyCert 尝试从本机 Caddy 的证书存储中获取该域名的证书与私钥,
// 保存到 MailGo 的域名 TLS 目录并更新数据库记录。结果通过查询参数回显到
// 编辑页面(caddy_ok=1 成功 / caddy_err=<消息> 失败)。
func (h *AdminHandler) FetchCaddyCert(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "无效的域名ID")
return
}
domain, err := h.stores.Domains.GetByID(uint(id))
if err != nil {
c.String(http.StatusNotFound, "域名不存在")
return
}
editURL := fmt.Sprintf("/admin/domains/%d/edit", domain.ID)
fail := func(msg string) {
log.Printf("从 Caddy 获取证书失败 domain=%s: %s", domain.Name, msg)
c.Redirect(http.StatusFound, editURL+"?caddy_err="+url.QueryEscape(msg))
}
cert, err := caddycert.Fetch(domain.Name, h.caddyCertRoots())
if err != nil {
fail(fmt.Sprintf("从 Caddy 获取证书失败: %v", err))
return
}
// 保存到域名 TLS 目录(与手动上传证书的位置一致)
domainTLSDir := filepath.Join(h.tlsDir, strconv.FormatUint(uint64(domain.ID), 10))
if err := os.MkdirAll(domainTLSDir, 0700); err != nil {
fail(fmt.Sprintf("创建 TLS 证书目录失败: %v", err))
return
}
certPath := filepath.Join(domainTLSDir, "cert.pem")
keyPath := filepath.Join(domainTLSDir, "key.pem")
if err := os.WriteFile(certPath, cert.CertPEM, 0644); err != nil {
fail(fmt.Sprintf("保存 TLS 公钥证书失败: %v", err))
return
}
if err := os.WriteFile(keyPath, cert.KeyPEM, 0600); err != nil {
fail(fmt.Sprintf("保存 TLS 私钥失败: %v", err))
return
}
domain.TlsCertPath = certPath
domain.TlsKeyPath = keyPath
domain.TlsEnabled = true
if err := h.stores.Domains.Update(domain); err != nil {
fail(fmt.Sprintf("更新域名记录失败: %v", err))
return
}
log.Printf("已从 Caddy 导入域名 %s 的证书 (%s),热加载生效", domain.Name, cert.Source)
c.Redirect(http.StatusFound, editURL+"?caddy_ok=1")
}
// caddyCertRoots 返回按优先级排列的证书来源目录:
// 1. MailGo 的同步镜像目录 <storage>/tls/caddy —— 由 install.sh 安装的
// systemd path 同步任务以 root 权限从 Caddy 证书存储镜像而来,
// mail_go 始终可读,证书续期后自动更新;
// 2. 配置文件 caddy.data_dir 指定的目录(可选);
//
// 其余默认位置由 caddycert.Fetch 自行探测。
func (h *AdminHandler) caddyCertRoots() []string {
return []string{
filepath.Join(filepath.Dir(h.tlsDir), "caddy"),
h.caddyDataDir,
}
}
func readTLSCert(path string) string {
if path == "" {
return ""
@@ -276,7 +428,20 @@ func readTLSCert(path string) string {
return string(data)
}
// normalizePEM 统一 PEM 文本的换行为 LF:浏览器提交 textarea 时会把
// 换行规范为 CRLF,而证书文件里通常是 LF,直接比较会误判“证书已修改”
// (表现为:私钥留空保留现有私钥时仍报“必须同时填写”)。
func normalizePEM(s string) string {
s = strings.ReplaceAll(s, "\r\n", "\n")
s = strings.ReplaceAll(s, "\r", "\n")
return s
}
func (h *AdminHandler) handleDomainTLSUpdate(domain *db.Domain, publicCert, privateKey string) error {
// 归一化换行,保证与磁盘文件一致,避免表单往返时被误判为已修改
publicCert = normalizePEM(strings.TrimSpace(publicCert))
privateKey = normalizePEM(strings.TrimSpace(privateKey))
if !domain.TlsEnabled {
return nil
}
@@ -288,7 +453,7 @@ func (h *AdminHandler) handleDomainTLSUpdate(domain *db.Domain, publicCert, priv
}
return fmt.Errorf("启用 TLS 时必须填写 TLS 私钥和公钥证书")
}
if hasExistingCert && privateKey == "" && strings.TrimSpace(readTLSCert(domain.TlsCertPath)) == publicCert {
if hasExistingCert && privateKey == "" && normalizePEM(strings.TrimSpace(readTLSCert(domain.TlsCertPath))) == publicCert {
return nil
}
if publicCert == "" || privateKey == "" {
@@ -596,6 +761,8 @@ func (h *AdminHandler) UpdateUser(c *gin.Context) {
return
}
user.PasswordHash = string(hashedPassword)
// 管理员重置的密码必须由用户本人修改后才能正常使用
user.MustChangePassword = true
}
if err := h.stores.Users.Update(user); err != nil {
@@ -617,9 +784,6 @@ func (h *AdminHandler) UpdateUser(c *gin.Context) {
// ListBans renders the IP ban list page.
func (h *AdminHandler) ListBans(c *gin.Context) {
// Clean up expired entries first
h.stores.Bans.Cleanup()
page := getPageParam(c, "page", 1)
bans, total, err := h.stores.Bans.List(page, 20)
@@ -628,6 +792,13 @@ func (h *AdminHandler) ListBans(c *gin.Context) {
return
}
// 标记当前是否仍处于封禁中
now := time.Now()
rows := make([]banRow, 0, len(bans))
for _, b := range bans {
rows = append(rows, banRow{BanEntry: b, Active: b.ExpiresAt.After(now)})
}
currentUser, _ := c.Get("currentUser")
totalPages := int(total) / 20
@@ -640,7 +811,7 @@ func (h *AdminHandler) ListBans(c *gin.Context) {
c.HTML(200, "admin_bans", gin.H{
"currentUser": currentUser,
"bans": bans,
"rows": rows,
"total": total,
"page": page,
"pageSize": 20,
@@ -649,6 +820,12 @@ func (h *AdminHandler) ListBans(c *gin.Context) {
})
}
// banRow 是黑名单列表行:附带了当前是否封禁中的标记。
type banRow struct {
db.BanEntry
Active bool
}
// UnbanIP removes a ban entry by ID.
func (h *AdminHandler) UnbanIP(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
@@ -665,10 +842,109 @@ func (h *AdminHandler) UnbanIP(c *gin.Context) {
c.Redirect(http.StatusFound, "/admin/bans")
}
// CleanupBans removes all expired ban entries.
func (h *AdminHandler) CleanupBans(c *gin.Context) {
h.stores.Bans.Cleanup()
c.Redirect(http.StatusFound, "/admin/bans")
// ListProtocolLogs 渲染协议调用日志页(SMTP/IMAP/POP3 调用记录,支持筛选)。
func (h *AdminHandler) ListProtocolLogs(c *gin.Context) {
// 页面访问时顺带清理过期日志,避免日志表无限增长
h.stores.ProtocolLogs.CleanupBefore(time.Now().AddDate(0, 0, -h.protocolLogKeepDays))
page := getPageParam(c, "page", 1)
pageSize := 50
var success *bool
switch c.Query("success") {
case "success":
v := true
success = &v
case "fail":
v := false
success = &v
}
from := parseDateQuery(c.Query("from"))
to := parseDateQuery(c.Query("to"))
// 日期选择到天,含当天
if !to.IsZero() {
to = to.AddDate(0, 0, 1)
}
filter := store.ProtocolLogFilter{
Protocol: c.Query("protocol"),
Success: success,
IP: strings.TrimSpace(c.Query("ip")),
Username: strings.TrimSpace(c.Query("username")),
From: from,
To: to,
}
logs, total, err := h.stores.ProtocolLogs.List(page, pageSize, filter)
if err != nil {
c.String(http.StatusInternalServerError, "加载协议日志失败: %v", err)
return
}
// 统计卡片:今日 + 全部成功/失败数(按协议),int64 → int 供模板 add 使用
dayStart := time.Now().Truncate(24 * time.Hour)
todayStats, _ := h.stores.ProtocolLogs.CountStats(dayStart)
allStats, _ := h.stores.ProtocolLogs.CountStats(time.Time{})
normStats := func(m map[string]map[string]int64) map[string]map[string]int {
out := make(map[string]map[string]int, len(m))
for proto, counts := range m {
out[proto] = map[string]int{"success": int(counts["success"]), "fail": int(counts["fail"])}
}
return out
}
currentUser, _ := c.Get("currentUser")
totalPages := int(total) / pageSize
if int(total)%pageSize > 0 {
totalPages++
}
if totalPages < 1 {
totalPages = 0
}
// 分页/筛选链接保留当前筛选条件(URL 编码防止特殊字符破坏链接)
query := map[string]string{
"protocol": url.QueryEscape(filter.Protocol),
"success": url.QueryEscape(c.Query("success")),
"ip": url.QueryEscape(filter.IP),
"username": url.QueryEscape(filter.Username),
"from": url.QueryEscape(c.Query("from")),
"to": url.QueryEscape(c.Query("to")),
}
c.HTML(200, "admin_protocol_logs", gin.H{
"currentUser": currentUser,
"logs": logs,
"total": total,
"page": page,
"pageSize": pageSize,
"totalPages": totalPages,
"filter": query,
"todayStats": normStats(todayStats),
"allStats": normStats(allStats),
"keepDays": h.protocolLogKeepDays,
"activeFolder": "protocol-logs",
})
}
// CleanupProtocolLogs 手动清理超出保留天数的协议日志。
func (h *AdminHandler) CleanupProtocolLogs(c *gin.Context) {
_, _ = h.stores.ProtocolLogs.CleanupBefore(time.Now().AddDate(0, 0, -h.protocolLogKeepDays))
c.Redirect(http.StatusFound, "/admin/protocol-logs")
}
// parseDateQuery 解析 YYYY-MM-DD 日期,失败返回零值。
func parseDateQuery(s string) time.Time {
if s == "" {
return time.Time{}
}
t, err := time.ParseInLocation("2006-01-02", s, time.Local)
if err != nil {
return time.Time{}
}
return t
}
// ListMails renders the admin mail list page showing all messages across all users.
@@ -760,7 +1036,7 @@ func (h *AdminHandler) AdminDownloadAttachment(c *gin.Context) {
return
}
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", att.FileName))
c.Header("Content-Disposition", formatContentDisposition(att.FileName))
c.Data(http.StatusOK, att.ContentType, data)
}
+126
View File
@@ -0,0 +1,126 @@
package handlers
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"os"
"path/filepath"
"strings"
"testing"
"time"
"mail_go/internal/db"
)
// makeTestCertPair 生成一对自签名证书(PEM),可选 LF/CRLF 换行。
func makeTestCertPair(t *testing.T, lineEnding string) (certPEM, keyPEM string) {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("生成私钥失败: %v", err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "test"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
DNSNames: []string{"mail.example.com"},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatalf("生成证书失败: %v", err)
}
certPEM = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}))
if lineEnding == "crlf" {
certPEM = strings.ReplaceAll(certPEM, "\n", "\r\n")
}
keyPEM = string(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}))
if lineEnding == "crlf" {
keyPEM = strings.ReplaceAll(keyPEM, "\n", "\r\n")
}
return certPEM, keyPEM
}
// writeExistingCert 模拟已有证书文件(LF 换行)并返回 domain。
func writeExistingCert(t *testing.T, tlsDir string, certPEM, keyPEM string) *db.Domain {
t.Helper()
dir := filepath.Join(tlsDir, "1")
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatalf("创建目录失败: %v", err)
}
certPath := filepath.Join(dir, "cert.pem")
keyPath := filepath.Join(dir, "key.pem")
if err := os.WriteFile(certPath, []byte(certPEM+"\n"), 0644); err != nil {
t.Fatalf("写入证书失败: %v", err)
}
if err := os.WriteFile(keyPath, []byte(keyPEM+"\n"), 0600); err != nil {
t.Fatalf("写入私钥失败: %v", err)
}
return &db.Domain{
ID: 1,
Name: "mail.example.com",
TlsEnabled: true,
TlsCertPath: certPath,
TlsKeyPath: keyPath,
}
}
// TestHandleDomainTLSUpdateUnchangedCertCRLF 复现用户报告的 bug
// 浏览器把 textarea 的 LF 换成 CRLF 提交,私钥留空(保留现有私钥),
// 证书内容未变,此时保存不应报“必须同时填写”。
func TestHandleDomainTLSUpdateUnchangedCertCRLF(t *testing.T) {
certLF, keyLF := makeTestCertPair(t, "lf")
h := &AdminHandler{tlsDir: t.TempDir()}
domain := writeExistingCert(t, h.tlsDir, certLF, keyLF)
submittedCert := strings.ReplaceAll(certLF, "\n", "\r\n") // 模拟浏览器提交
if err := h.handleDomainTLSUpdate(domain, submittedCert, ""); err != nil {
t.Fatalf("证书未修改且私钥留空时应保留现有私钥,实际报错: %v", err)
}
}
// TestHandleDomainTLSUpdateNewPairCRLF 新证书+私钥(CRLF 提交)应正常保存,
// 且写入文件为 LF 换行、能组成有效密钥对。
func TestHandleDomainTLSUpdateNewPairCRLF(t *testing.T) {
certCRLF, keyCRLF := makeTestCertPair(t, "crlf")
h := &AdminHandler{tlsDir: t.TempDir()}
domain := &db.Domain{ID: 1, Name: "mail.example.com", TlsEnabled: true}
if err := h.handleDomainTLSUpdate(domain, certCRLF, keyCRLF); err != nil {
t.Fatalf("CRLF 提交的新证书应保存成功,实际报错: %v", err)
}
data, err := os.ReadFile(domain.TlsCertPath)
if err != nil {
t.Fatalf("读取保存的证书失败: %v", err)
}
if strings.Contains(string(data), "\r") {
t.Error("保存的证书文件不应包含 CR")
}
if _, err := tls.LoadX509KeyPair(domain.TlsCertPath, domain.TlsKeyPath); err != nil {
t.Fatalf("保存的证书对无效: %v", err)
}
}
// TestHandleDomainTLSUpdateChangedCertWithoutKey 证书确实被修改但私钥留空,
// 应报“必须同时填写”(防止用不匹配的私钥)。
func TestHandleDomainTLSUpdateChangedCertWithoutKey(t *testing.T) {
certA, keyA := makeTestCertPair(t, "lf")
certB, _ := makeTestCertPair(t, "lf")
h := &AdminHandler{tlsDir: t.TempDir()}
domain := writeExistingCert(t, h.tlsDir, certA, keyA)
err := h.handleDomainTLSUpdate(domain, certB, "")
if err == nil || !strings.Contains(err.Error(), "必须同时填写") {
t.Fatalf("修改证书但私钥留空应报“必须同时填写”,实际: %v", err)
}
}
+80 -35
View File
@@ -1,6 +1,9 @@
package handlers
import (
"crypto/rand"
"crypto/subtle"
"encoding/hex"
"fmt"
"log"
"net/http"
@@ -8,7 +11,6 @@ import (
"mail_go/config"
"mail_go/internal/auth"
"mail_go/internal/db"
"mail_go/internal/store"
"github.com/gin-contrib/sessions"
@@ -71,19 +73,10 @@ func (h *AuthHandler) DoLogin(c *gin.Context) {
user, err := h.stores.Users.Authenticate(email, password)
if err != nil {
failCount, _ := h.stores.Bans.IncrementFail(ip)
if failCount >= h.banCfg.MaxFailAttempts {
banDuration := time.Duration(h.banCfg.BanDurationMin) * time.Minute
banEntry := &db.BanEntry{
IPAddress: ip,
Reason: fmt.Sprintf("登录失败次数过多 (%d次)", failCount),
FailCount: failCount,
ExpiresAt: time.Now().Add(banDuration),
}
h.stores.Bans.Create(banEntry)
c.HTML(http.StatusForbidden, "banned", gin.H{"entry": banEntry})
banned, failCount := h.stores.RecordAuthFailure(ip, h.banCfg.MaxFailAttempts, h.banCfg.BanDurationMin, "登录失败次数过多")
if banned {
entry, _ := h.stores.Bans.GetByIP(ip)
c.HTML(http.StatusForbidden, "banned", gin.H{"entry": entry})
return
}
@@ -100,11 +93,14 @@ func (h *AuthHandler) DoLogin(c *gin.Context) {
// Login successful: reset fail count
h.stores.Bans.ResetFail(ip)
// Set session values
// Set session values(先清空旧会话状态,防止残留值;记录登录时间
// 供中间件做绝对过期与滑动续期)
session := sessions.Default(c)
session.Clear()
session.Set("userID", user.ID)
session.Set("userEmail", user.Username+"@"+user.Domain.Name)
session.Set("isAdmin", user.IsAdmin)
session.Set("loginAt", time.Now().Unix())
if err := session.Save(); err != nil {
c.HTML(200, "login", gin.H{
"error": "会话保存失败,请重试",
@@ -150,24 +146,16 @@ func (h *AuthHandler) LDAPLogin(c *gin.Context) {
if err != nil {
log.Printf("LDAP 认证失败: %v", err)
failCount, _ := h.stores.Bans.IncrementFail(ip)
if failCount >= h.banCfg.MaxFailAttempts {
banDuration := time.Duration(h.banCfg.BanDurationMin) * time.Minute
banEntry := &db.BanEntry{
IPAddress: ip,
Reason: fmt.Sprintf("登录失败次数过多 (%d次)", failCount),
FailCount: failCount,
ExpiresAt: time.Now().Add(banDuration),
}
h.stores.Bans.Create(banEntry)
c.HTML(http.StatusForbidden, "banned", gin.H{"entry": banEntry})
banned, failCount := h.stores.RecordAuthFailure(ip, h.banCfg.MaxFailAttempts, h.banCfg.BanDurationMin, "LDAP 登录失败次数过多")
if banned {
entry, _ := h.stores.Bans.GetByIP(ip)
c.HTML(http.StatusForbidden, "banned", gin.H{"entry": entry})
return
}
remaining := h.banCfg.MaxFailAttempts - failCount
c.HTML(200, "login", gin.H{
"error": fmt.Sprintf("LDAP 认证失败,还剩 %d 次尝试机会: %v", remaining, err),
"error": fmt.Sprintf("LDAP 认证失败,还剩 %d 次尝试机会", remaining),
"oauth2Enabled": h.authCfg.OAuth2Enabled,
"ldapEnabled": h.authCfg.LDAPEnabled,
"oauth2Provider": h.authCfg.OAuth2Provider,
@@ -179,7 +167,7 @@ func (h *AuthHandler) LDAPLogin(c *gin.Context) {
user, err := h.stores.Users.GetByEmail(email)
if err != nil {
c.HTML(200, "login", gin.H{
"error": fmt.Sprintf("LDAP 用户 %s 在系统中不存在", email),
"error": "LDAP 账号未接入本系统,请联系管理员",
"oauth2Enabled": h.authCfg.OAuth2Enabled,
"ldapEnabled": h.authCfg.LDAPEnabled,
"oauth2Provider": h.authCfg.OAuth2Provider,
@@ -200,11 +188,14 @@ func (h *AuthHandler) LDAPLogin(c *gin.Context) {
// Login successful: reset fail count
h.stores.Bans.ResetFail(ip)
// Set session values
// Set session values(先清空旧会话状态,防止残留值;记录登录时间
// 供中间件做绝对过期与滑动续期)
session := sessions.Default(c)
session.Clear()
session.Set("userID", user.ID)
session.Set("userEmail", user.Username+"@"+user.Domain.Name)
session.Set("isAdmin", user.IsAdmin)
session.Set("loginAt", time.Now().Unix())
if err := session.Save(); err != nil {
c.HTML(200, "login", gin.H{
"error": "会话保存失败,请重试",
@@ -218,6 +209,36 @@ func (h *AuthHandler) LDAPLogin(c *gin.Context) {
c.Redirect(302, "/inbox")
}
// OAuth2 state cookie 配置。state 用于防止登录 CSRF / 授权码注入:
// 发起授权时下发随机值,回调时必须原样带回。
//
// 注意 state 不能放进主会话 cookie:主会话是 SameSite=Strict
// OAuth2 回调是从 IdP 发起的跨站顶级导航,浏览器不会携带 Strict
// cookie,因此使用独立的短期 SameSite=Lax cookie。
const (
oauth2StateCookie = "mail_go_oauth2_state"
oauth2StateMaxAge = 600 // 秒,10 分钟内完成授权流程
oauth2StateRandLen = 16 // 随机字节数(hex 编码后 32 字符)
)
// randomOAuth2State generates a hex-encoded cryptographically random state.
func randomOAuth2State() (string, error) {
buf := make([]byte, oauth2StateRandLen)
if _, err := rand.Read(buf); err != nil {
return "", fmt.Errorf("生成 OAuth2 state 失败: %w", err)
}
return hex.EncodeToString(buf), nil
}
// oauth2LoginVars 是登录模板所需的公共变量。
func (h *AuthHandler) oauth2LoginVars() gin.H {
return gin.H{
"oauth2Enabled": h.authCfg.OAuth2Enabled,
"ldapEnabled": h.authCfg.LDAPEnabled,
"oauth2Provider": h.authCfg.OAuth2Provider,
}
}
// OAuth2Start redirects to the OAuth2 provider's authorization page.
func (h *AuthHandler) OAuth2Start(c *gin.Context) {
if !h.authCfg.OAuth2Enabled {
@@ -226,8 +247,13 @@ func (h *AuthHandler) OAuth2Start(c *gin.Context) {
}
provider := auth.NewOAuth2Provider(h.authCfg)
// Use a simple state for CSRF protection (in production, use a random token)
state := "mailgo_oauth2_state"
state, err := randomOAuth2State()
if err != nil {
log.Printf("生成 OAuth2 state 失败: %v", err)
c.String(http.StatusInternalServerError, "OAuth2 登录暂不可用,请稍后重试")
return
}
c.SetCookie(oauth2StateCookie, state, oauth2StateMaxAge, "/auth/oauth2", "", true, true)
c.Redirect(http.StatusFound, provider.GetAuthURL(state))
}
@@ -238,6 +264,22 @@ func (h *AuthHandler) OAuth2Callback(c *gin.Context) {
return
}
// 校验 state:必须与发起授权时下发的随机值一致(常量时间比较)。
// 缺失或不匹配视为登录 CSRF / 授权码注入,直接拒绝。
cookieState, cookieErr := c.Cookie(oauth2StateCookie)
reqState := c.Query("state")
if cookieErr != nil || reqState == "" ||
subtle.ConstantTimeCompare([]byte(cookieState), []byte(reqState)) != 1 {
c.HTML(http.StatusForbidden, "login", func() gin.H {
v := h.oauth2LoginVars()
v["error"] = "OAuth2 state 校验失败,请重新发起登录"
return v
}())
return
}
// state 一次性使用:无论后续成败都立即失效
c.SetCookie(oauth2StateCookie, "", -1, "/auth/oauth2", "", true, true)
code := c.Query("code")
if code == "" {
c.HTML(200, "login", gin.H{
@@ -254,7 +296,7 @@ func (h *AuthHandler) OAuth2Callback(c *gin.Context) {
if err != nil {
log.Printf("OAuth2 回调失败: %v", err)
c.HTML(200, "login", gin.H{
"error": fmt.Sprintf("OAuth2 认证失败: %v", err),
"error": "OAuth2 认证失败,请重试或联系管理员",
"oauth2Enabled": h.authCfg.OAuth2Enabled,
"ldapEnabled": h.authCfg.LDAPEnabled,
"oauth2Provider": h.authCfg.OAuth2Provider,
@@ -266,7 +308,7 @@ func (h *AuthHandler) OAuth2Callback(c *gin.Context) {
user, err := h.stores.Users.GetByEmail(email)
if err != nil {
c.HTML(200, "login", gin.H{
"error": fmt.Sprintf("OAuth2 用户 %s 在系统中不存在", email),
"error": "OAuth2 账号未接入本系统,请联系管理员",
"oauth2Enabled": h.authCfg.OAuth2Enabled,
"ldapEnabled": h.authCfg.LDAPEnabled,
"oauth2Provider": h.authCfg.OAuth2Provider,
@@ -284,11 +326,14 @@ func (h *AuthHandler) OAuth2Callback(c *gin.Context) {
return
}
// Set session values
// Set session values(先清空旧会话状态,防止残留值;记录登录时间
// 供中间件做绝对过期与滑动续期)
session := sessions.Default(c)
session.Clear()
session.Set("userID", user.ID)
session.Set("userEmail", user.Username+"@"+user.Domain.Name)
session.Set("isAdmin", user.IsAdmin)
session.Set("loginAt", time.Now().Unix())
if err := session.Save(); err != nil {
c.HTML(200, "login", gin.H{
"error": "会话保存失败,请重试",
+101
View File
@@ -0,0 +1,101 @@
package handlers
import (
"net/http/httptest"
"path/filepath"
"sync/atomic"
"testing"
"time"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/store"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// performPost 发送 POST 请求并返回响应(用于处理器测试)。
func performPost(r *gin.Engine, path string) *httptest.ResponseRecorder {
req := httptest.NewRequest("POST", path, nil)
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
return rec
}
// TestDisconnectConnection 验证「断开并封禁」:创建黑名单记录并断开该 IP 全部连接。
func TestDisconnectConnection(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.BanEntry{}); err != nil {
t.Fatalf("migrate: %v", err)
}
stores := store.NewStores(gdb)
hub := connhub.New()
var closed atomic.Int32
// 目标 IP 两个连接(模拟多协议在线)
c1 := hub.Register("smtp", "203.0.113.77", 25, false)
c1.SetDisconnect(func() { closed.Add(1) })
c2 := hub.Register("imap", "203.0.113.77", 993, true)
c2.SetDisconnect(func() { closed.Add(1) })
// 其他 IP 不应受影响
c3 := hub.Register("pop3", "203.0.113.78", 110, false)
c3.SetDisconnect(func() { closed.Add(1) })
h := &AdminHandler{stores: stores, hub: hub}
gin.SetMode(gin.TestMode)
r := gin.New()
r.POST("/admin/connections/:id/disconnect", h.DisconnectConnection)
rec := performPost(r, "/admin/connections/1/disconnect")
if rec.Code != 302 {
t.Fatalf("status = %d, want 302", rec.Code)
}
// 该 IP 的两个连接都被断开,其他连接不受影响
if closed.Load() != 2 {
t.Fatalf("closed = %d, want 2", closed.Load())
}
if n := hub.Counts()["pop3"]; n != 1 {
t.Fatalf("pop3 count = %d, want 1 (unaffected)", n)
}
// 黑名单记录:180 天封禁
banned, entry := stores.Bans.IsBanned("203.0.113.77")
if !banned {
t.Fatal("IP should be banned")
}
if entry.Reason != "管理员手动封禁(连接断开)" {
t.Fatalf("reason = %q", entry.Reason)
}
wantExpiry := time.Now().Add(180 * 24 * time.Hour)
if entry.ExpiresAt.Before(wantExpiry.Add(-time.Minute)) || entry.ExpiresAt.After(wantExpiry.Add(time.Minute)) {
t.Fatalf("expiry = %v, want ~180 days", entry.ExpiresAt)
}
}
// TestDisconnectConnectionNotFound 验证不存在的连接返回 404。
func TestDisconnectConnectionNotFound(t *testing.T) {
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.BanEntry{}); err != nil {
t.Fatalf("migrate: %v", err)
}
h := &AdminHandler{stores: store.NewStores(gdb), hub: connhub.New()}
gin.SetMode(gin.TestMode)
r := gin.New()
r.POST("/admin/connections/:id/disconnect", h.DisconnectConnection)
rec := performPost(r, "/admin/connections/999/disconnect")
if rec.Code != 404 {
t.Fatalf("status = %d, want 404", rec.Code)
}
}
+246 -94
View File
@@ -4,6 +4,8 @@ import (
"encoding/base64"
"fmt"
"io"
"log"
"mime"
"net/http"
"path/filepath"
"strconv"
@@ -11,6 +13,7 @@ import (
"time"
"mail_go/internal/db"
"mail_go/internal/imap_server"
"mail_go/internal/outbound"
"mail_go/internal/storage"
"mail_go/internal/store"
@@ -48,12 +51,22 @@ type MailHandler struct {
stores *store.Stores
storage *storage.AttachmentStorage
outbound *outbound.Manager
// pusher 邮件状态变化推送(IMAP 客户端实时同步),可空
pusher imap_server.Pusher
}
// NewMailHandler creates a new MailHandler with the given stores, attachment
// storage and outbound delivery manager.
func NewMailHandler(stores *store.Stores, attStorage *storage.AttachmentStorage, ob *outbound.Manager) *MailHandler {
return &MailHandler{stores: stores, storage: attStorage, outbound: ob}
func NewMailHandler(stores *store.Stores, attStorage *storage.AttachmentStorage, ob *outbound.Manager, pusher imap_server.Pusher) *MailHandler {
return &MailHandler{stores: stores, storage: attStorage, outbound: ob, pusher: pusher}
}
// folderCounts returns sidebar badge counts for the current user.
func (h *MailHandler) folderCounts(userID uint) (inboxUnread, draftsTotal, sentTotal int64) {
inboxUnread, _ = h.stores.Mails.CountUnread(userID, "INBOX")
draftsTotal, _ = h.stores.Mails.CountByUserAndFolder(userID, "Drafts")
sentTotal, _ = h.stores.Mails.CountByUserAndFolder(userID, "Sent")
return
}
// Inbox renders the inbox page showing all messages in the user's INBOX folder.
@@ -67,7 +80,7 @@ func (h *MailHandler) Inbox(c *gin.Context) {
return
}
unreadCount, _ := h.stores.Mails.CountUnread(userID, "INBOX")
inboxUnread, draftsTotal, sentTotal := h.folderCounts(userID)
currentUser, _ := c.Get("currentUser")
@@ -83,12 +96,14 @@ func (h *MailHandler) Inbox(c *gin.Context) {
"currentUser": currentUser,
"messages": messages,
"total": total,
"unreadCount": unreadCount,
"page": page,
"pageSize": 20,
"totalPages": totalPages,
"folder": "INBOX",
"activeFolder": "inbox",
"inboxUnread": inboxUnread,
"draftsTotal": draftsTotal,
"sentTotal": sentTotal,
})
}
@@ -118,17 +133,23 @@ func (h *MailHandler) View(c *gin.Context) {
// Auto mark as read
if !msg.IsRead {
_ = h.stores.Mails.MarkRead(uint(id))
if err := h.stores.Mails.MarkRead(uint(id)); err != nil {
log.Printf("web: 标记已读失败 msg=%d: %v", id, err)
}
msg.IsRead = true
}
currentUser, _ := c.Get("currentUser")
inboxUnread, draftsTotal, sentTotal := h.folderCounts(userID)
c.HTML(200, "view", gin.H{
"currentUser": currentUser,
"message": msg,
"attachments": attachments,
"activeFolder": resolveActiveFolder(msg.Folder),
"inboxUnread": inboxUnread,
"draftsTotal": draftsTotal,
"sentTotal": sentTotal,
})
}
@@ -146,6 +167,8 @@ func (h *MailHandler) Compose(c *gin.Context) {
quotaBytes = user.QuotaBytes
}
inboxUnread, draftsTotal, sentTotal := h.folderCounts(userID)
c.HTML(200, "compose", gin.H{
"currentUser": currentUser,
"activeFolder": "compose",
@@ -155,6 +178,9 @@ func (h *MailHandler) Compose(c *gin.Context) {
"bodyContent": "",
"usedBytes": usedBytes,
"quotaBytes": quotaBytes,
"inboxUnread": inboxUnread,
"draftsTotal": draftsTotal,
"sentTotal": sentTotal,
})
}
@@ -192,38 +218,60 @@ func (h *MailHandler) DoSend(c *gin.Context) {
if multipartErr == nil {
files := form.File["attachments"]
if len(files) > 0 {
// Check attachment quota before saving
user, _ := h.stores.Users.GetByID(userID)
if user != nil {
var totalNewSize int64
for _, file := range files {
totalNewSize += file.Size
}
if user.UsedBytes+totalNewSize > user.QuotaBytes {
c.HTML(http.StatusBadRequest, "compose", gin.H{
"currentUser": currentUser,
"activeFolder": "compose",
"error": fmt.Sprintf("附件超出配额限制。已用 %s / 总配额 %s", formatBytes(user.UsedBytes), formatBytes(user.QuotaBytes)),
"to": to,
"subject": subject,
"cc": cc,
"bodyContent": htmlBody,
"usedBytes": user.UsedBytes,
"quotaBytes": user.QuotaBytes,
})
return
}
// 原子预扣附件配额(单条 SQLused_bytes + n <= quota_bytes 才生效),
// 防止并发提交绕过配额检查(TOCTOU)。后续保存失败会补偿回退。
var totalNewSize int64
for _, file := range files {
totalNewSize += file.Size
}
reserved, err := h.stores.Users.TryReserveQuota(userID, totalNewSize)
if err != nil {
c.HTML(http.StatusInternalServerError, "compose", gin.H{
"currentUser": currentUser,
"activeFolder": "compose",
"error": "配额检查失败,请稍后重试",
"to": to,
"subject": subject,
"cc": cc,
"bodyContent": htmlBody,
"usedBytes": currentUser.UsedBytes,
"quotaBytes": currentUser.QuotaBytes,
})
return
}
if !reserved {
user, _ := h.stores.Users.GetByID(userID)
usedBytes, quotaBytes := currentUser.UsedBytes, currentUser.QuotaBytes
if user != nil {
usedBytes, quotaBytes = user.UsedBytes, user.QuotaBytes
}
c.HTML(http.StatusBadRequest, "compose", gin.H{
"currentUser": currentUser,
"activeFolder": "compose",
"error": fmt.Sprintf("附件超出配额限制。已用 %s / 总配额 %s", formatBytes(usedBytes), formatBytes(quotaBytes)),
"to": to,
"subject": subject,
"cc": cc,
"bodyContent": htmlBody,
"usedBytes": usedBytes,
"quotaBytes": quotaBytes,
})
return
}
// Read all attachment files into memory once (used for both the
// MIME message body and the stored attachment records).
// 读取失败的文件回退已预扣的配额。
for _, file := range files {
f, err := file.Open()
if err != nil {
_ = h.stores.Users.UpdateUsedBytes(userID, -file.Size)
continue
}
buf, readErr := io.ReadAll(f)
f.Close()
if readErr != nil {
_ = h.stores.Users.UpdateUsedBytes(userID, -file.Size)
continue
}
@@ -245,62 +293,8 @@ func (h *MailHandler) DoSend(c *gin.Context) {
// Build the email content
fromAddr := fmt.Sprintf("%s@%s", currentUser.Username, currentUser.Domain.Name)
messageID, rawMessage := buildOutgoingMessage(fromAddr, to, cc, subject, body, htmlBody, attachments)
now := time.Now()
messageID := fmt.Sprintf("<%s@mail_go>", uuid.New().String())
// Construct the raw email message
var sb strings.Builder
sb.WriteString(fmt.Sprintf("From: %s\r\n", fromAddr))
sb.WriteString(fmt.Sprintf("To: %s\r\n", to))
if cc != "" {
sb.WriteString(fmt.Sprintf("Cc: %s\r\n", cc))
}
sb.WriteString(fmt.Sprintf("Subject: %s\r\n", subject))
sb.WriteString(fmt.Sprintf("Message-ID: %s\r\n", messageID))
sb.WriteString(fmt.Sprintf("Date: %s\r\n", now.Format(time.RFC1123Z)))
sb.WriteString("MIME-Version: 1.0\r\n")
// Attachments are wrapped in an outer multipart/mixed container.
outerBoundary := ""
hasAttachments := len(attachments) > 0
if hasAttachments {
outerBoundary = fmt.Sprintf("----=_Mixed_%s", uuid.New().String())
sb.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=\"%s\"\r\n", outerBoundary))
sb.WriteString("\r\n")
sb.WriteString(fmt.Sprintf("--%s\r\n", outerBoundary))
}
// Build message body with multipart/alternative if HTML is present
if htmlBody != "" {
boundary := fmt.Sprintf("----=_Part_%s", uuid.New().String())
sb.WriteString(fmt.Sprintf("Content-Type: multipart/alternative; boundary=\"%s\"\r\n", boundary))
sb.WriteString("\r\n")
sb.WriteString(fmt.Sprintf("--%s\r\n", boundary))
sb.WriteString("Content-Type: text/plain; charset=utf-8\r\n\r\n")
sb.WriteString(body)
sb.WriteString(fmt.Sprintf("\r\n--%s\r\n", boundary))
sb.WriteString("Content-Type: text/html; charset=utf-8\r\n\r\n")
sb.WriteString(htmlBody)
sb.WriteString(fmt.Sprintf("\r\n--%s--\r\n", boundary))
} else {
sb.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
sb.WriteString("\r\n")
sb.WriteString(body)
sb.WriteString("\r\n")
}
// Append attachment parts to the multipart/mixed container.
for _, att := range attachments {
sb.WriteString(fmt.Sprintf("--%s\r\n", outerBoundary))
sb.WriteString(fmt.Sprintf("Content-Type: %s; name=\"%s\"\r\n", att.contentType, att.filename))
sb.WriteString("Content-Transfer-Encoding: base64\r\n")
sb.WriteString(fmt.Sprintf("Content-Disposition: attachment; filename=\"%s\"\r\n\r\n", att.filename))
sb.WriteString(base64LineWrap(att.data))
sb.WriteString("\r\n")
}
if hasAttachments {
sb.WriteString(fmt.Sprintf("--%s--\r\n", outerBoundary))
}
allRecipients := append(parseAddressInput(to), parseAddressInput(cc)...)
localUsers := make([]*db.User, 0, len(allRecipients))
@@ -348,7 +342,7 @@ func (h *MailHandler) DoSend(c *gin.Context) {
return
}
for _, rcpt := range externalRecipients {
if _, err := ob.Enqueue(currentUser, fromAddr, rcpt, []byte(sb.String())); err != nil {
if _, err := ob.Enqueue(currentUser, fromAddr, rcpt, []byte(rawMessage)); err != nil {
c.HTML(http.StatusBadRequest, "compose", gin.H{
"currentUser": currentUser,
"activeFolder": "compose",
@@ -376,7 +370,7 @@ func (h *MailHandler) DoSend(c *gin.Context) {
Subject: subject,
TextBody: body,
HtmlBody: htmlBody,
RawData: sb.String(),
RawData: rawMessage,
Date: now,
IsRead: false,
}
@@ -394,6 +388,10 @@ func (h *MailHandler) DoSend(c *gin.Context) {
})
return
}
// 本地投递成功 → IMAP 新邮件推送(IDLE 客户端实时收到通知)
if h.pusher != nil {
h.pusher.PushNewMessage(rcptUser.Username+"@"+rcptUser.Domain.Name, inboxMsg)
}
}
// Save to Sent folder
@@ -407,7 +405,7 @@ func (h *MailHandler) DoSend(c *gin.Context) {
Subject: subject,
TextBody: body,
HtmlBody: htmlBody,
RawData: sb.String(),
RawData: rawMessage,
Date: now,
IsRead: true,
}
@@ -428,10 +426,12 @@ func (h *MailHandler) DoSend(c *gin.Context) {
}
// Save attachment records linked to the Sent copy (bytes were already
// read during message construction).
// read during message construction). 配额已在前面原子预扣,
// 保存/落库失败的附件需要补偿回退。
for _, att := range attachments {
relPath, err := h.storage.Save(att.filename, att.data)
if err != nil {
_ = h.stores.Users.UpdateUsedBytes(userID, -int64(len(att.data)))
continue
}
@@ -442,9 +442,10 @@ func (h *MailHandler) DoSend(c *gin.Context) {
ContentType: att.contentType,
FileSize: int64(len(att.data)),
}
_ = h.stores.Attachments.Create(attRecord)
// Update user used bytes
_ = h.stores.Users.UpdateUsedBytes(userID, attRecord.FileSize)
if err := h.stores.Attachments.Create(attRecord); err != nil {
_ = h.stores.Users.UpdateUsedBytes(userID, -attRecord.FileSize)
continue
}
}
c.Redirect(http.StatusFound, "/sent")
@@ -464,6 +465,94 @@ func parseAddressInput(input string) []string {
return addresses
}
// sanitizeHeaderField removes CR/LF/NUL from a value destined for an RFC 5322
// message header, preventing header injection (e.g. smuggling a Bcc or
// Reply-To header via a crafted subject or address list).
func sanitizeHeaderField(s string) string {
s = strings.ReplaceAll(s, "\r", "")
s = strings.ReplaceAll(s, "\n", "")
s = strings.ReplaceAll(s, "\x00", "")
return s
}
// encodeSubject prepares a subject for safe inclusion as a message header:
// header injection characters are stripped and non-ASCII content is encoded
// per RFC 2047.
func encodeSubject(s string) string {
return mime.QEncoding.Encode("utf-8", sanitizeHeaderField(s))
}
// formatContentDisposition builds a Content-Disposition header value for the
// given filename, quoting/encoding it per RFC 2183/2231 (also neutralizes
// CR/LF injection through crafted filenames).
func formatContentDisposition(filename string) string {
return mime.FormatMediaType("attachment", map[string]string{"filename": filename})
}
// buildOutgoingMessage constructs the raw RFC 5322 message for the web
// compose form and returns its Message-ID. All header values derived from
// user input are sanitized to prevent CRLF header injection.
func buildOutgoingMessage(from, to, cc, subject, body, htmlBody string, attachments []pendingAttachment) (messageID, raw string) {
now := time.Now()
messageID = fmt.Sprintf("<%s@mail_go>", uuid.New().String())
var sb strings.Builder
sb.WriteString(fmt.Sprintf("From: %s\r\n", sanitizeHeaderField(from)))
sb.WriteString(fmt.Sprintf("To: %s\r\n", sanitizeHeaderField(to)))
if cc != "" {
sb.WriteString(fmt.Sprintf("Cc: %s\r\n", sanitizeHeaderField(cc)))
}
sb.WriteString(fmt.Sprintf("Subject: %s\r\n", encodeSubject(subject)))
sb.WriteString(fmt.Sprintf("Message-ID: %s\r\n", messageID))
sb.WriteString(fmt.Sprintf("Date: %s\r\n", now.Format(time.RFC1123Z)))
sb.WriteString("MIME-Version: 1.0\r\n")
// Attachments are wrapped in an outer multipart/mixed container.
outerBoundary := ""
hasAttachments := len(attachments) > 0
if hasAttachments {
outerBoundary = fmt.Sprintf("----=_Mixed_%s", uuid.New().String())
sb.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=\"%s\"\r\n", outerBoundary))
sb.WriteString("\r\n")
sb.WriteString(fmt.Sprintf("--%s\r\n", outerBoundary))
}
// Build message body with multipart/alternative if HTML is present
if htmlBody != "" {
boundary := fmt.Sprintf("----=_Part_%s", uuid.New().String())
sb.WriteString(fmt.Sprintf("Content-Type: multipart/alternative; boundary=\"%s\"\r\n", boundary))
sb.WriteString("\r\n")
sb.WriteString(fmt.Sprintf("--%s\r\n", boundary))
sb.WriteString("Content-Type: text/plain; charset=utf-8\r\n\r\n")
sb.WriteString(body)
sb.WriteString(fmt.Sprintf("\r\n--%s\r\n", boundary))
sb.WriteString("Content-Type: text/html; charset=utf-8\r\n\r\n")
sb.WriteString(htmlBody)
sb.WriteString(fmt.Sprintf("\r\n--%s--\r\n", boundary))
} else {
sb.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
sb.WriteString("\r\n")
sb.WriteString(body)
sb.WriteString("\r\n")
}
// Append attachment parts to the multipart/mixed container.
for _, att := range attachments {
contentType := mime.FormatMediaType(att.contentType, map[string]string{"name": att.filename})
sb.WriteString(fmt.Sprintf("--%s\r\n", outerBoundary))
sb.WriteString(fmt.Sprintf("Content-Type: %s\r\n", contentType))
sb.WriteString("Content-Transfer-Encoding: base64\r\n")
sb.WriteString(fmt.Sprintf("Content-Disposition: %s\r\n\r\n", formatContentDisposition(att.filename)))
sb.WriteString(base64LineWrap(att.data))
sb.WriteString("\r\n")
}
if hasAttachments {
sb.WriteString(fmt.Sprintf("--%s--\r\n", outerBoundary))
}
return messageID, sb.String()
}
// mimeTypes maps common file extensions to MIME types.
var mimeTypes = map[string]string{
".txt": "text/plain",
@@ -495,6 +584,7 @@ func (h *MailHandler) Sent(c *gin.Context) {
}
currentUser, _ := c.Get("currentUser")
inboxUnread, draftsTotal, sentTotal := h.folderCounts(userID)
totalPages := int(total) / 20
if int(total)%20 > 0 {
@@ -513,9 +603,22 @@ func (h *MailHandler) Sent(c *gin.Context) {
"totalPages": totalPages,
"folder": "Sent",
"activeFolder": "sent",
"inboxUnread": inboxUnread,
"draftsTotal": draftsTotal,
"sentTotal": sentTotal,
})
}
// safeRedirectPath 仅接受同站相对路径(以 / 开头且非 //),
// 防止把用户重定向到外部站点(开放重定向)。非法值返回空串,
// 调用方应回退到默认路径。
func safeRedirectPath(referer string) string {
if referer == "" || !strings.HasPrefix(referer, "/") || strings.HasPrefix(referer, "//") {
return ""
}
return referer
}
// Delete removes a message by ID after verifying ownership.
func (h *MailHandler) Delete(c *gin.Context) {
userID := c.GetUint("userID")
@@ -537,11 +640,37 @@ func (h *MailHandler) Delete(c *gin.Context) {
_ = h.storage.Delete(att.FilePath)
_ = h.stores.Users.UpdateUsedBytes(userID, -att.FileSize)
}
_ = h.stores.Attachments.DeleteByMessage(uint(id))
_ = h.stores.Mails.Delete(uint(id))
if err := h.stores.Attachments.DeleteByMessage(uint(id)); err != nil {
log.Printf("web: 删除附件记录失败 msg=%d: %v", id, err)
}
// Redirect back based on the folder
referer := c.GetHeader("Referer")
// 删除前计算消息在所属文件夹中的序号(用于 Expunge 推送)
var seq uint32
if msgs, err := h.stores.Mails.ListAllByUserAndFolder(userID, msg.Folder); err == nil {
for i := range msgs {
if msgs[i].ID == uint(id) {
seq = uint32(i + 1)
break
}
}
}
if err := h.stores.Mails.Delete(uint(id)); err != nil {
log.Printf("web: 删除邮件失败 msg=%d: %v", id, err)
}
// 删除 → 推送给该用户的其他 IMAP 客户端
if h.pusher != nil && seq > 0 {
userEmail := ""
if cu, ok := c.Get("currentUser"); ok {
if u, ok := cu.(*db.User); ok {
userEmail = u.Username + "@" + u.Domain.Name
}
}
h.pusher.PushExpunged(userEmail, msg.Folder, []uint32{seq})
}
// Redirect back based on the folder(仅同站相对路径,防开放重定向)
referer := safeRedirectPath(c.GetHeader("Referer"))
if referer == "" {
referer = "/inbox"
}
@@ -565,7 +694,20 @@ func (h *MailHandler) MarkRead(c *gin.Context) {
_ = h.stores.Mails.MarkRead(uint(id))
referer := c.GetHeader("Referer")
// 已读变化 → 推送给该用户的其他 IMAP 客户端
if h.pusher != nil {
msg.IsRead = true
userEmail := ""
if cu, ok := c.Get("currentUser"); ok {
if u, ok := cu.(*db.User); ok {
userEmail = u.Username + "@" + u.Domain.Name
}
}
h.pusher.PushFlagsChanged(userEmail, msg.Folder, msg)
}
// Redirect back based on the folder(仅同站相对路径,防开放重定向)
referer := safeRedirectPath(c.GetHeader("Referer"))
if referer == "" {
referer = "/inbox"
}
@@ -600,7 +742,7 @@ func (h *MailHandler) DownloadAttachment(c *gin.Context) {
return
}
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", att.FileName))
c.Header("Content-Disposition", formatContentDisposition(att.FileName))
c.Data(http.StatusOK, att.ContentType, data)
}
@@ -630,6 +772,7 @@ func (h *MailHandler) Drafts(c *gin.Context) {
}
currentUser, _ := c.Get("currentUser")
inboxUnread, draftsTotal, sentTotal := h.folderCounts(userID)
totalPages := int(total) / 20
if int(total)%20 > 0 {
@@ -648,17 +791,26 @@ func (h *MailHandler) Drafts(c *gin.Context) {
"totalPages": totalPages,
"folder": "Drafts",
"activeFolder": "drafts",
"inboxUnread": inboxUnread,
"draftsTotal": draftsTotal,
"sentTotal": sentTotal,
})
}
// Settings renders the user settings page.
func (h *MailHandler) Settings(c *gin.Context) {
currentUser, _ := c.Get("currentUser")
userID := c.GetUint("userID")
inboxUnread, draftsTotal, sentTotal := h.folderCounts(userID)
c.HTML(200, "settings", gin.H{
"currentUser": currentUser,
"activeFolder": "settings",
"error": "",
"success": "",
"mustChange": c.Query("force") == "1",
"inboxUnread": inboxUnread,
"draftsTotal": draftsTotal,
"sentTotal": sentTotal,
})
}
@@ -0,0 +1,129 @@
package handlers
// P1 #4 回归测试:Web 写信的邮件头不可被 CRLF 注入。
// 旧实现把 to/cc/subject/附件名原样拼进 MIME 头,攻击者可通过
// subject 注入 Reply-To/Bcc 等任意头用于钓鱼。
import (
"strings"
"testing"
)
func TestSanitizeHeaderField(t *testing.T) {
cases := []struct {
in, want string
}{
{"normal value", "normal value"},
{"with\r\ninjected: header", "withinjected: header"},
{"lf\nonly", "lfonly"},
{"cr\ronly", "cronly"},
{"nul\x00byte", "nulbyte"},
{"mixed\r\n\x00all", "mixedall"},
{"中文主题", "中文主题"},
}
for _, tc := range cases {
if got := sanitizeHeaderField(tc.in); got != tc.want {
t.Errorf("sanitizeHeaderField(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestBuildOutgoingMessageBlocksHeaderInjection(t *testing.T) {
_, raw := buildOutgoingMessage(
"alice@example.com",
"bob@example.com\r\nBcc: victim@evil.com",
"carol@example.com\r\nReply-To: attacker@evil.com",
"Hi\r\nBcc: victim@evil.com\r\nReply-To: attacker@evil.com",
"body",
"",
nil,
)
// 注入的头不允许以独立头形式出现
for _, injected := range []string{
"Bcc:", "Reply-To:",
} {
if strings.Contains(raw, "\r\n"+injected) || strings.HasPrefix(raw, injected) {
t.Fatalf("injected header %q found in message:\n%s", injected, raw)
}
}
// 注入的邮箱地址本身允许以折叠形式残留在原头值中,
// 但绝不能成为独立的一行头。
lines := strings.Split(raw, "\r\n")
for _, line := range lines[1:] { // 跳过 From
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "Bcc:") || strings.HasPrefix(trimmed, "Reply-To:") {
t.Fatalf("injected header line %q found in message:\n%s", line, raw)
}
}
}
func TestBuildOutgoingMessageAttFilenameInjection(t *testing.T) {
atts := []pendingAttachment{
{filename: "evil.png\r\nBcc: victim@evil.com", contentType: "image/png", data: []byte("x")},
{filename: `quote".png`, contentType: "image/png", data: []byte("x")},
}
_, raw := buildOutgoingMessage("a@b.com", "c@d.com", "", "t", "body", "", atts)
lines := strings.Split(raw, "\r\n")
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "Bcc:") {
t.Fatalf("filename header injection found: %q\nmessage:\n%s", line, raw)
}
}
// 含引号/换行的文件名必须被正确编码,不能破坏头结构
if !strings.Contains(raw, "Content-Disposition: attachment;") {
t.Fatalf("Content-Disposition missing in message:\n%s", raw)
}
}
func TestBuildOutgoingMessageEncodesNonASCIISubject(t *testing.T) {
_, raw := buildOutgoingMessage("a@b.com", "c@d.com", "", "中文主题测试", "body", "", nil)
// 非 ASCII 主题应按 RFC 2047 编码为 =?utf-8?...?= 形式
if !strings.Contains(raw, "Subject: =?utf-8?") && !strings.Contains(raw, "Subject: =?UTF-8?") {
t.Fatalf("non-ASCII subject should be RFC 2047 encoded, got:\n%s", raw)
}
// 头部不应再包含裸中文(应被编码)
for _, line := range strings.Split(raw, "\r\n") {
if strings.HasPrefix(line, "Subject:") && strings.ContainsAny(line, "中文测试") {
t.Fatalf("raw non-ASCII in Subject header: %q", line)
}
}
}
func TestFormatContentDisposition(t *testing.T) {
if got := formatContentDisposition("report.pdf"); got != "attachment; filename=report.pdf" {
t.Fatalf("simple filename: got %q", got)
}
// 特殊字符需要安全编码而不是原样嵌入
got := formatContentDisposition("a\"b\\c\r\nd.png")
if strings.ContainsAny(got, "\r\n") {
t.Fatalf("CRLF leaked into Content-Disposition: %q", got)
}
}
// P3 #12Referer 开放重定向防护。
func TestSafeRedirectPath(t *testing.T) {
cases := []struct {
in string
want string
}{
{"", ""},
{"/inbox", "/inbox"},
{"/mail/delete/5", "/mail/delete/5"},
{"/sent?page=2", "/sent?page=2"},
{"https://evil.com/", ""},
{"//evil.com/inbox", ""},
{"http://mail.lmve.net/inbox", ""},
{"javascript:alert(1)", ""},
{"/\\evil.com", "/\\evil.com"}, // 浏览器对 /\\ 的处理不一致,但不涉及外部协议跳转
}
for _, tc := range cases {
if got := safeRedirectPath(tc.in); got != tc.want {
t.Errorf("safeRedirectPath(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
+211
View File
@@ -0,0 +1,211 @@
package handlers
// P1 #2 回归测试:OAuth2 state 必须随机、回调必须校验。
// 旧实现 state 为硬编码常量且回调完全不校验(登录 CSRF / 授权码注入)。
import (
"encoding/json"
"html/template"
"math"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strings"
"testing"
"time"
"mail_go/config"
"mail_go/internal/mailutil"
"github.com/gin-gonic/gin"
)
// testTemplateFuncs 提供模板解析所需的自定义函数(与 web 包的
// templateFuncs 等价,但 handlers 包无法反向依赖 web 包)。
func testTemplateFuncs() template.FuncMap {
return template.FuncMap{
"add": func(a, b int) int { return a + b },
"sub": func(a, b int) int { return a - b },
"mul": func(a, b int) int { return a * b },
"div": func(a, b int) int { return a / b },
"mod": func(a, b int) int { return a % b },
"ceilDiv": func(a, b int) int { return int(math.Ceil(float64(a) / float64(b))) },
"seq": func(n int) []int { r := make([]int, n); for i := range r { r[i] = i + 1 }; return r },
"domainName": func(domainID uint, domains []interface{}) string { return "Domain #1" },
"jsonify": func(v interface{}) template.JS {
b, _ := json.Marshal(v)
return template.JS(b)
},
"formatBytes": func(b int64) string {
return "1 KB"
},
"decodeHeader": mailutil.DecodeRFC2047,
"mailName": func(s string) string { return s },
"mailEmail": func(s string) string { return s },
"initial": func(s string) string { return "?" },
"truncate": func(s string, n int) string { return s },
"shortDate": func(t time.Time) string { return t.Format("2006-01-02") },
"avatarStyle": func(s string) string { return "background:#eee;color:#333" },
}
}
func newOAuth2TestContext(t *testing.T) (*gin.Context, *AuthHandler, *httptest.ResponseRecorder) {
t.Helper()
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, engine := gin.CreateTestContext(w)
// 回调的错误分支渲染 login 模板,需要加载模板及自定义函数
tmpl := template.Must(template.New("").Funcs(testTemplateFuncs()).ParseGlob(filepath.Join("..", "templates", "*.html")))
engine.SetHTMLTemplate(tmpl)
c.Request = httptest.NewRequest(http.MethodGet, "/auth/oauth2", nil)
authCfg := config.AuthConfig{
OAuth2Enabled: true,
// 使用本地拒绝连接的地址作为 provider,token 交换快速失败,
// 测试不依赖外部网络。
OAuth2Provider: "127.0.0.1:1",
OAuth2ClientID: "test-client-id",
OAuth2ClientSecret: "test-client-secret",
OAuth2RedirectURL: "https://mail.example.com/auth/oauth2/callback",
}
h := NewAuthHandler(nil, authCfg, config.BanConfig{MaxFailAttempts: 100})
return c, h, w
}
func TestRandomOAuth2State(t *testing.T) {
s1, err := randomOAuth2State()
if err != nil {
t.Fatalf("randomOAuth2State() error: %v", err)
}
if len(s1) != oauth2StateRandLen*2 {
t.Fatalf("state length = %d, want %d (hex)", len(s1), oauth2StateRandLen*2)
}
s2, _ := randomOAuth2State()
if s1 == s2 {
t.Fatal("state must be unique per request")
}
if s1 == "mailgo_oauth2_state" {
t.Fatal("state must not be the old hardcoded constant")
}
}
func TestOAuth2StartSetsRandomStateCookie(t *testing.T) {
c, h, w := newOAuth2TestContext(t)
h.OAuth2Start(c)
if w.Code != http.StatusFound {
t.Fatalf("status = %d, want 302", w.Code)
}
loc := w.Header().Get("Location")
if !strings.Contains(loc, "state=") {
t.Fatalf("redirect URL should carry state: %s", loc)
}
// state cookie 必须存在且与 URL 中的一致
cookies := w.Result().Cookies()
var stateVal string
found := false
for _, ck := range cookies {
if ck.Name == oauth2StateCookie {
found = true
stateVal = ck.Value
if !ck.HttpOnly {
t.Error("state cookie must be HttpOnly")
}
if !ck.Secure {
t.Error("state cookie must be Secure")
}
if ck.MaxAge <= 0 || ck.MaxAge > oauth2StateMaxAge {
t.Errorf("state cookie MaxAge = %d, want in (0, %d]", ck.MaxAge, oauth2StateMaxAge)
}
}
}
if !found {
t.Fatal("OAuth2Start should set state cookie")
}
u, err := url.Parse(loc)
if err != nil {
t.Fatalf("parse location: %v", err)
}
if u.Query().Get("state") != stateVal {
t.Fatalf("cookie state %q != URL state %q", stateVal, u.Query().Get("state"))
}
// 两次发起的 state 不同
c2, h2, w2 := newOAuth2TestContext(t)
h2.OAuth2Start(c2)
u2, _ := url.Parse(w2.Header().Get("Location"))
if u2.Query().Get("state") == stateVal {
t.Fatal("state must differ between sessions")
}
}
func TestOAuth2CallbackRejectsMissingOrMismatchedState(t *testing.T) {
cases := []struct {
name string
cookieState string
queryState string
}{
{"no cookie", "", "abc"},
{"no query state", "abc", ""},
{"mismatch", "abc", "xyz"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
c, h, w := newOAuth2TestContext(t)
q := url.Values{}
q.Set("code", "test-code")
if tc.queryState != "" {
q.Set("state", tc.queryState)
}
c.Request = httptest.NewRequest(http.MethodGet, "/auth/oauth2/callback?"+q.Encode(), nil)
if tc.cookieState != "" {
c.Request.AddCookie(&http.Cookie{Name: oauth2StateCookie, Value: tc.cookieState})
}
h.OAuth2Callback(c)
if w.Code != http.StatusForbidden {
t.Fatalf("status = %d, want 403", w.Code)
}
})
}
}
func TestOAuth2CallbackAcceptsValidState(t *testing.T) {
// 模拟完整流程:Start 下发 state -> Callback 带回同一 state。
// state 校验通过后应进入后续流程(本测试无真实 IdP,
// code 交换会失败并渲染登录错误页,但这证明 state 关卡已通过)。
c, h, w := newOAuth2TestContext(t)
h.OAuth2Start(c)
var stateVal string
for _, ck := range w.Result().Cookies() {
if ck.Name == oauth2StateCookie {
stateVal = ck.Value
}
}
w2 := httptest.NewRecorder()
c2, engine2 := gin.CreateTestContext(w2)
tmpl2 := template.Must(template.New("").Funcs(testTemplateFuncs()).ParseGlob(filepath.Join("..", "templates", "*.html")))
engine2.SetHTMLTemplate(tmpl2)
q := url.Values{}
q.Set("code", "test-code")
q.Set("state", stateVal)
c2.Request = httptest.NewRequest(http.MethodGet, "/auth/oauth2/callback?"+q.Encode(), nil)
c2.Request.AddCookie(&http.Cookie{Name: oauth2StateCookie, Value: stateVal})
h.OAuth2Callback(c2)
// state 校验失败返回 403;此处应为非 403(进入 token 交换失败分支)
if w2.Code == http.StatusForbidden {
t.Fatalf("valid state was rejected")
}
if !strings.Contains(w2.Body.String(), "OAuth2") {
body := w2.Body.String()
if len(body) > 200 {
body = body[:200]
}
t.Fatalf("expected OAuth2 error page after state check passed, body: %s", body)
}
}
+43
View File
@@ -0,0 +1,43 @@
package web
// P3 #14jsonify 模板函数在 <script> 上下文中必须能阻止
// </script> 逃逸(encoding/json 默认转义 < > &)。
import (
"html/template"
"strings"
"testing"
)
func TestJsonifyEscapesScriptBreakout(t *testing.T) {
jsonify, ok := templateFuncs()["jsonify"].(func(interface{}) template.JS)
if !ok {
t.Fatal("template funcs must include jsonify")
}
payloads := []string{
`x</script><script>alert(1)</script>`,
`"><img src=x onerror=alert(1)>`,
"line1\nline2\ttab",
"中文内容",
`quill "quotes" 'single'`,
}
for _, p := range payloads {
out := jsonify(p)
if !strings.HasPrefix(string(out), `"`) || !strings.HasSuffix(string(out), `"`) {
t.Errorf("jsonify(%q) = %s, want a quoted JS string literal", p, out)
}
if strings.Contains(string(out), "</script>") || strings.Contains(string(out), "</SCRIPT>") {
t.Errorf("jsonify(%q) must not emit raw </script>: %s", p, out)
}
if strings.ContainsAny(string(out), "\r\n") {
t.Errorf("jsonify(%q) must escape control chars: %q", p, out)
}
}
// 特殊值:nil -> null
out := jsonify(nil)
if string(out) != "null" {
t.Errorf("jsonify(nil) = %s, want null", out)
}
}
+52
View File
@@ -1,12 +1,40 @@
package middleware
import (
"time"
"mail_go/internal/store"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
const (
// sessionAbsoluteMaxAge 会话绝对过期时间:超过后强制重新登录。
sessionAbsoluteMaxAge = 7 * 24 * time.Hour
// sessionSlidingRefresh 滑动续期阈值:距上次刷新超过该时长则更新
// loginAt 并写回 cookie,保持活跃用户不中断(约 12 小时写回一次)。
sessionSlidingRefresh = 12 * time.Hour
)
// sessionInt64 兼容不同底层 session store 解码出的整数类型。
func sessionInt64(v interface{}) (int64, bool) {
switch n := v.(type) {
case int64:
return n, true
case int:
return int64(n), true
case uint:
return int64(n), true
case uint64:
return int64(n), true
case float64:
return int64(n), true
default:
return 0, false
}
}
// AuthMiddleware checks for a valid session and loads the current user
// into the Gin context. If no valid session exists, it redirects to /login.
func AuthMiddleware(stores *store.Stores) gin.HandlerFunc {
@@ -19,6 +47,23 @@ func AuthMiddleware(stores *store.Stores) gin.HandlerFunc {
return
}
// 会话绝对过期:登录超过 7 天强制重新登录;
// 滑动续期:活跃会话每 12 小时刷新一次 loginAt。
if loginAt, ok := sessionInt64(session.Get("loginAt")); ok {
elapsed := time.Since(time.Unix(loginAt, 0))
if elapsed > sessionAbsoluteMaxAge {
session.Clear()
session.Save()
c.Redirect(302, "/login")
c.Abort()
return
}
if elapsed > sessionSlidingRefresh {
session.Set("loginAt", time.Now().Unix())
session.Save()
}
}
// userID is stored as uint in session, but sessions.Get returns interface{}
// which may be stored as int or uint depending on the underlying store.
var id uint
@@ -48,6 +93,13 @@ func AuthMiddleware(stores *store.Stores) gin.HandlerFunc {
return
}
// 首次登录/密码被重置的用户必须先修改密码才能使用其他功能
if user.MustChangePassword && c.Request.URL.Path != "/settings" && c.Request.URL.Path != "/logout" {
c.Redirect(302, "/settings?force=1")
c.Abort()
return
}
c.Set("currentUser", user)
c.Set("userID", id)
c.Next()
+36
View File
@@ -0,0 +1,36 @@
package middleware
import (
"github.com/gin-gonic/gin"
)
// securityCSP 是本应用的基础 CSP。
//
// 说明:
// - 模板大量使用内联脚本/样式(Quill 初始化、行内事件处理、
// avatarStyle 内联 CSS),故 script-src / style-src 需要
// 'unsafe-inline'
// - 邮件正文在 srcdoc iframe 中渲染,可能引用远程图片(https:),
// 因此 img-src 放行 https,同时仍阻止 data: 以外的自定义协议;
// - frame-ancestors 'none' 与 X-Frame-Options 共同防护点击劫持;
// - connect-src 'self' / form-action 'self' 阻止页面数据外泄到
// 外部域名。
const securityCSP = "default-src 'self'; " +
"script-src 'self' 'unsafe-inline'; " +
"style-src 'self' 'unsafe-inline'; " +
"img-src 'self' data: https:; " +
"connect-src 'self'; object-src 'none'; base-uri 'self'; " +
"form-action 'self'; frame-ancestors 'none'"
// SecurityHeaders 为所有响应设置基础安全头:HSTS、点击劫持防护、
// MIME 嗅探防护、Referrer 策略与基础 CSP。
func SecurityHeaders() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Strict-Transport-Security", "max-age=31536000; includeSubDomains")
c.Header("X-Frame-Options", "DENY")
c.Header("X-Content-Type-Options", "nosniff")
c.Header("Referrer-Policy", "strict-origin-when-cross-origin")
c.Header("Content-Security-Policy", securityCSP)
c.Next()
}
}
+45
View File
@@ -0,0 +1,45 @@
package middleware
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
// TestSecurityHeaders 验证所有基础安全响应头都存在。
func TestSecurityHeaders(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(SecurityHeaders())
r.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "ok") })
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
r.ServeHTTP(w, req)
for _, h := range []string{
"Strict-Transport-Security",
"X-Frame-Options",
"X-Content-Type-Options",
"Referrer-Policy",
"Content-Security-Policy",
} {
if v := w.Header().Get(h); v == "" {
t.Errorf("missing security header %q", h)
}
}
// 关键头内容抽查
if got := w.Header().Get("X-Frame-Options"); got != "DENY" {
t.Errorf("X-Frame-Options = %q, want DENY", got)
}
if got := w.Header().Get("Content-Security-Policy"); !strings.Contains(got, "frame-ancestors 'none'") {
t.Errorf("CSP should include frame-ancestors 'none', got %q", got)
}
if got := w.Header().Get("Content-Security-Policy"); !strings.Contains(got, "connect-src 'self'") {
t.Errorf("CSP should include connect-src 'self', got %q", got)
}
}
+143
View File
@@ -0,0 +1,143 @@
package web
// Temporary render test used to validate the rewritten frontend templates.
// Renders every page template with realistic dummy data and writes the
// output to /tmp/mailgo_preview for visual verification.
import (
"html/template"
"os"
"path/filepath"
"strings"
"testing"
"time"
"mail_go/internal/db"
)
func TestRenderAllPages(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
tmpl := template.Must(template.New("").Funcs(templateFuncs()).ParseGlob(filepath.Join(wd, "templates", "*.html")))
template.Must(tmpl.ParseGlob(filepath.Join(wd, "templates", "admin", "*.html")))
now := time.Now()
user := &db.User{
ID: 1,
Username: "admin",
IsAdmin: true,
Domain: db.Domain{Name: "lmve.net"},
UsedBytes: 5 * 1024 * 1024,
QuotaBytes: 5 * 1024 * 1024 * 1024,
}
messages := []db.Message{
{ID: 1, Folder: "INBOX", FromAddr: "=?UTF-8?B?5byg5LiJ?= <zhangsan@lmve.net>", ToAddr: "admin@lmve.net", Subject: "邮件系统部署完成通知", TextBody: "您好!您的 MailGo 邮件系统已成功部署,本邮件为测试邮件。", Date: now, IsRead: false},
{ID: 2, Folder: "INBOX", FromAddr: "alice@example.com", ToAddr: "admin@lmve.net", Subject: "Re: 项目进度同步", TextBody: "好的,我们下周一上午十点开会同步一下进度。", Date: now.Add(-3 * time.Hour), IsRead: true},
{ID: 3, Folder: "INBOX", FromAddr: "=?UTF-8?B?6ZmI5rKz?= <wangwu@lmve.net>", ToAddr: "admin@lmve.net", Subject: "服务器巡检报告(8 月)", TextBody: "本月巡检完成,磁盘使用率 62%,内存使用正常。", Date: now.Add(-48 * time.Hour), IsRead: false},
{ID: 4, Folder: "INBOX", FromAddr: "bob@other.com", ToAddr: "admin@lmve.net", Subject: "Newsletter #42", TextBody: "这是本周的资讯摘要,共 5 篇文章。", Date: now.Add(-10 * 24 * time.Hour), IsRead: true},
{ID: 5, Folder: "INBOX", FromAddr: "=?UTF-8?B?6ZmI5rKz?= <wangwu@lmve.net>", ToAddr: "admin@lmve.net", Subject: "DNS 记录更新", TextBody: "已按文档更新 SPF 与 DKIM 记录,请验证。", Date: now.Add(-100 * 24 * time.Hour), IsRead: true},
}
attachments := []db.Attachment{
{ID: 1, FileName: "部署文档.pdf", FileSize: 1024 * 1024},
{ID: 2, FileName: "logo.png", FileSize: 128 * 1024},
}
cases := []struct {
name string
data ginH
}{
{"login", ginH{"error": ""}},
{"banned", ginH{"entry": &db.BanEntry{IPAddress: "1.2.3.4", Reason: "登录失败次数过多", FailCount: 8, ExpiresAt: now.Add(20 * time.Minute)}}},
{"inbox", ginH{"currentUser": user, "messages": messages, "total": 5, "page": 1, "totalPages": 1, "activeFolder": "inbox", "inboxUnread": int64(2), "draftsTotal": int64(1), "sentTotal": int64(3)}},
{"drafts", ginH{"currentUser": user, "messages": messages, "total": 1, "page": 1, "totalPages": 1, "activeFolder": "drafts", "inboxUnread": int64(2), "draftsTotal": int64(1), "sentTotal": int64(3)}},
{"sent", ginH{"currentUser": user, "messages": messages, "total": 3, "page": 1, "totalPages": 1, "activeFolder": "sent", "inboxUnread": int64(2), "draftsTotal": int64(1), "sentTotal": int64(3)}},
{"view", ginH{
"currentUser": user, "activeFolder": "inbox",
"message": &db.Message{ID: 1, Folder: "INBOX", FromAddr: "=?UTF-8?B?5byg5LiJ?= <zhangsan@lmve.net>", ToAddr: "admin@lmve.net", Subject: "邮件系统部署完成通知", TextBody: "您好!您的 MailGo 邮件系统已成功部署。", HtmlBody: "", Date: now, IsRead: false},
"attachments": attachments, "inboxUnread": int64(2), "draftsTotal": int64(1), "sentTotal": int64(3),
}},
{"compose", ginH{
"currentUser": user, "activeFolder": "compose", "error": "",
"to": "zhangsan@lmve.net", "subject": "Re: 邮件系统部署完成通知", "bodyContent": "",
"usedBytes": int64(5 * 1024 * 1024), "quotaBytes": int64(5 * 1024 * 1024 * 1024),
"inboxUnread": int64(2), "draftsTotal": int64(1), "sentTotal": int64(3),
}},
{"settings", ginH{"currentUser": user, "activeFolder": "settings", "error": "", "success": "", "inboxUnread": int64(2), "draftsTotal": int64(1), "sentTotal": int64(3)}},
{"admin_dashboard", ginH{"currentUser": user, "activeFolder": "admin", "domainCount": 2, "userCount": 5, "totalMails": 100, "banCount": 1, "inboxCount": 50, "sentCount": 30, "draftsCount": 10, "trashCount": 5, "inboxSize": int64(1024), "sentSize": int64(512), "totalSize": int64(2048), "todayReceived": 3, "todaySent": 2, "weekReceived": 20, "weekSent": 15}},
{"admin_bans", ginH{
"currentUser": user, "activeFolder": "bans",
"rows": []struct {
db.BanEntry
Active bool
}{
{BanEntry: db.BanEntry{IPAddress: "203.0.113.7", BanCount: 4, FailCount: 5, Reason: "第1次封禁:登录失败次数过多(第4次触发,失败5次)", ExpiresAt: now.Add(20 * time.Minute)}, Active: true},
{BanEntry: db.BanEntry{IPAddress: "203.0.113.9", BanCount: 5, FailCount: 6, Reason: "第2次封禁:邮件协议认证失败次数过多(第5次触发,失败6次)", ExpiresAt: now.Add(-24 * time.Hour)}, Active: false},
{BanEntry: db.BanEntry{IPAddress: "10.0.0.2", BanCount: 1, FailCount: 5, Reason: "", ExpiresAt: time.Time{}}, Active: false},
},
"total": 3, "page": 1, "pageSize": 20, "totalPages": 1,
}},
{"admin_protocol_logs", ginH{
"currentUser": user, "activeFolder": "protocol-logs",
"logs": []db.ProtocolLog{
{ID: 1, Protocol: db.ProtocolSMTP, Port: 25, ClientIP: "203.0.113.7", Username: "", Success: true, FailReason: "", Detail: "MAIL FROM:<spam@evil.example> RCPT×1 本地投递1", MsgCount: 1, DurationMs: 1234, CreatedAt: now},
{ID: 2, Protocol: db.ProtocolIMAP, Port: 993, ClientIP: "203.0.113.9", Username: "admin", Success: false, FailReason: "用户名或密码错误", Detail: "LOGIN 失败", DurationMs: 88, CreatedAt: now.Add(-time.Minute)},
{ID: 3, Protocol: db.ProtocolPOP3, Port: 110, ClientIP: "10.0.0.2", Username: "alice", Success: true, FailReason: "", Detail: "USER PASS STAT RETR×3 QUIT", MsgCount: 3, DurationMs: 500, CreatedAt: now.Add(-2 * time.Minute)},
},
"total": 3, "page": 1, "pageSize": 50, "totalPages": 1,
"filter": map[string]string{"protocol": "smtp", "success": "fail", "ip": "203.0.113", "username": "", "from": "2026-08-01", "to": "2026-08-19"},
"todayStats": map[string]map[string]int{
db.ProtocolSMTP: {"success": 10, "fail": 2},
db.ProtocolIMAP: {"success": 5, "fail": 7},
db.ProtocolPOP3: {"success": 3, "fail": 4},
},
"allStats": map[string]map[string]int{
db.ProtocolSMTP: {"success": 100, "fail": 20},
db.ProtocolIMAP: {"success": 50, "fail": 70},
db.ProtocolPOP3: {"success": 30, "fail": 40},
},
"keepDays": 30,
}},
{"admin_connections", ginH{
"currentUser": user, "activeFolder": "connections",
"conns": []struct {
ID uint64
Protocol string
IP string
Port int
User string
TLS bool
Connected time.Time
LastActive time.Time
}{
{ID: 1, Protocol: "smtp", IP: "203.0.113.7", Port: 25, TLS: true, Connected: now.Add(-2 * time.Minute), LastActive: now},
{ID: 2, Protocol: "imap", IP: "203.0.113.9", Port: 993, User: "admin", TLS: true, Connected: now.Add(-30 * time.Minute), LastActive: now.Add(-10 * time.Second)},
{ID: 3, Protocol: "pop3", IP: "10.0.0.2", Port: 110, User: "alice", TLS: false, Connected: now.Add(-time.Minute), LastActive: now.Add(-30 * time.Second)},
},
"total": 3, "smtpCount": 1, "imapCount": 1, "pop3Count": 1, "now": now,
}},
}
outDir := os.Getenv("MAILGO_PREVIEW_DIR")
if outDir == "" {
outDir = filepath.Join(os.TempDir(), "mailgo_preview")
}
os.MkdirAll(outDir, 0755)
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var buf strings.Builder
if err := tmpl.ExecuteTemplate(&buf, tc.name, tc.data); err != nil {
t.Fatalf("render %s: %v", tc.name, err)
}
os.WriteFile(filepath.Join(outDir, tc.name+".html"), []byte(buf.String()), 0644)
t.Logf("%s -> %d bytes", tc.name, buf.Len())
})
}
}
// ginH mimics gin.H so the test does not need the gin dependency surface.
type ginH map[string]interface{}
+159 -35
View File
@@ -1,15 +1,21 @@
package web
import (
"encoding/json"
"fmt"
"html/template"
"math"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"unicode/utf8"
"mail_go/config"
"mail_go/internal/connhub"
"mail_go/internal/imap_server"
"mail_go/internal/mailutil"
"mail_go/internal/outbound"
"mail_go/internal/storage"
@@ -38,25 +44,31 @@ func formatBytes(b int64) string {
// WebServer wraps the Gin engine and its dependencies.
type WebServer struct {
engine *gin.Engine
stores *store.Stores
storage *storage.AttachmentStorage
cfg config.WebConfig
storageCfg config.StorageConfig
authCfg config.AuthConfig
banCfg config.BanConfig
outbound *outbound.Manager
engine *gin.Engine
stores *store.Stores
storage *storage.AttachmentStorage
cfg config.WebConfig
storageCfg config.StorageConfig
authCfg config.AuthConfig
banCfg config.BanConfig
caddyDataDir string
outbound *outbound.Manager
hub *connhub.Hub
// pusher 邮件状态变化推送(IMAP 客户端实时同步),可空
pusher imap_server.Pusher
}
// templateFuncs returns custom template functions for rendering.
func templateFuncs() template.FuncMap {
return template.FuncMap{
"add": func(a, b int) int { return a + b },
"sub": func(a, b int) int { return a - b },
"mul": func(a, b int) int { return a * b },
"div": func(a, b int) int { return a / b },
"mod": func(a, b int) int { return a % b },
"ceilDiv": func(a, b int) int { return int(math.Ceil(float64(a) / float64(b))) },
"add": func(a, b int) int { return a + b },
"sub": func(a, b int) int { return a - b },
"mul": func(a, b int) int { return a * b },
"div": func(a, b int64) int64 { return a / b },
// durationSeconds 将 time.Duration 转为整秒(模板中无法做类型转换)。
"durationSeconds": func(d time.Duration) int64 { return int64(d / time.Second) },
"mod": func(a, b int) int { return a % b },
"ceilDiv": func(a, b int) int { return int(math.Ceil(float64(a) / float64(b))) },
"seq": func(n int) []int {
result := make([]int, n)
for i := 0; i < n; i++ {
@@ -67,11 +79,15 @@ func templateFuncs() template.FuncMap {
"domainName": func(domainID uint, domains []interface{}) string {
return fmt.Sprintf("Domain #%d", domainID)
},
"safeHTML": func(s string) template.HTML {
return template.HTML(s)
},
"safeJS": func(s string) template.JS {
return template.JS(s)
// jsonify 把任意值序列化为安全的 JS 字面量(JSON 字符串),
// 用于在 <script> 上下文中注入数据。encoding/json 默认转义
// < > &\u003c 等),无法逃出 </script>,杜绝 script 注入。
"jsonify": func(v interface{}) template.JS {
b, err := json.Marshal(v)
if err != nil {
return template.JS("null")
}
return template.JS(b)
},
"formatBytes": func(b int64) string {
return formatBytes(b)
@@ -79,22 +95,115 @@ func templateFuncs() template.FuncMap {
"decodeHeader": func(s string) string {
return mailutil.DecodeRFC2047(s)
},
// mailName 从 "Name <addr>" 中提取显示名;无显示名时退回邮箱地址。
"mailName": mailName,
// mailEmail 从 "Name <addr>" 中提取邮箱地址部分。
"mailEmail": mailEmail,
// initial 返回字符串的首字符(用于头像占位)。
"initial": initial,
// truncate 折叠空白并截断到 n 个字符(用于列表摘要)。
"truncate": truncate,
// shortDate 按 QQ 邮箱习惯格式化:今天显示 HH:mm,今年显示 MM-DD,更早显示 YYYY-MM-DD。
"shortDate": shortDate,
// avatarStyle 根据字符串哈希生成头像背景/前景色。
"avatarStyle": avatarStyle,
}
}
// mailName extracts the display name from an RFC 5322 address.
func mailName(s string) string {
s = strings.TrimSpace(s)
if i := strings.IndexByte(s, '<'); i >= 0 {
name := strings.Trim(strings.TrimSpace(s[:i]), `"' `)
if name != "" {
return name
}
if j := strings.IndexByte(s, '>'); j > i {
return s[i+1 : j]
}
}
return s
}
// mailEmail extracts the bare email address from an RFC 5322 address.
func mailEmail(s string) string {
if i := strings.IndexByte(s, '<'); i >= 0 {
if j := strings.IndexByte(s, '>'); j > i {
return s[i+1 : j]
}
}
return strings.TrimSpace(s)
}
// initial returns the first rune of a string, upper-cased.
func initial(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return "?"
}
r, _ := utf8.DecodeRuneInString(s)
return strings.ToUpper(string(r))
}
// truncate collapses whitespace and cuts the string to n runes.
func truncate(s string, n int) string {
s = strings.Join(strings.Fields(s), " ")
r := []rune(s)
if len(r) <= n {
return s
}
return string(r[:n]) + "…"
}
// shortDate formats a time like QQ Mail does: today -> HH:mm,
// this year -> MM-DD, otherwise -> YYYY-MM-DD.
func shortDate(t time.Time) string {
now := time.Now()
if t.Year() == now.Year() && t.YearDay() == now.YearDay() {
return t.Format("15:04")
}
if t.Year() == now.Year() {
return t.Format("01-02")
}
return t.Format("2006-01-02")
}
// avatarStyle returns inline CSS colors derived from a string hash.
func avatarStyle(s string) string {
h := 0
for _, r := range s {
h = (h*31 + int(r)) % 360
}
return fmt.Sprintf("background:hsl(%d,78%%,92%%);color:hsl(%d,72%%,36%%)", h, h)
}
// NewWebServer creates a new WebServer, initializes the Gin engine,
// configures sessions, middleware, and registers all routes.
func NewWebServer(cfg config.WebConfig, stores *store.Stores, attStorage *storage.AttachmentStorage, storageCfg config.StorageConfig, authCfg config.AuthConfig, banCfg config.BanConfig, ob *outbound.Manager) *WebServer {
func NewWebServer(cfg config.WebConfig, stores *store.Stores, attStorage *storage.AttachmentStorage, storageCfg config.StorageConfig, authCfg config.AuthConfig, banCfg config.BanConfig, caddyCfg config.CaddyConfig, ob *outbound.Manager, hub *connhub.Hub, pusher imap_server.Pusher) (*WebServer, error) {
if err := config.ValidateSecretKey(cfg.SecretKey); err != nil {
return nil, err
}
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
engine.Use(gin.Logger())
engine.Use(gin.Recovery())
// Session store (cookie-based)
cookieStore := cookie.NewStore([]byte("mail-go-secret-key-change-in-production"))
// 仅信任本机回环上的反向代理(Caddy/Nginx)。外部直连时
// X-Forwarded-For 不可信,防止伪造客户端 IP 绕过登录封禁或
// 恶意封禁他人 IP。gin 对 Unix socket 监听无条件信任转发头,
// 因此 socket 必须保持仅本机可达。
if err := engine.SetTrustedProxies([]string{"127.0.0.1", "::1"}); err != nil {
return nil, fmt.Errorf("设置可信代理失败: %w", err)
}
// Session store (cookie-based). The signing key comes from the config
// file (auto-generated random key) or the MAILGO_SECRET_KEY env var.
cookieStore := cookie.NewStore([]byte(cfg.SecretKey))
cookieStore.Options(sessions.Options{
HttpOnly: true,
SameSite: 3, // SameSiteLaxMode
SameSite: 3, // SameSiteStrictMode(比 Lax 更严格)
Secure: cfg.CookieSecure,
MaxAge: 86400,
Path: "/",
})
@@ -107,28 +216,33 @@ func NewWebServer(cfg config.WebConfig, stores *store.Stores, attStorage *storag
engine.SetHTMLTemplate(tmpl)
ws := &WebServer{
engine: engine,
stores: stores,
storage: attStorage,
cfg: cfg,
storageCfg: storageCfg,
authCfg: authCfg,
banCfg: banCfg,
outbound: ob,
engine: engine,
stores: stores,
storage: attStorage,
cfg: cfg,
storageCfg: storageCfg,
authCfg: authCfg,
banCfg: banCfg,
caddyDataDir: caddyCfg.DataDir,
outbound: ob,
hub: hub,
pusher: pusher,
}
ws.registerRoutes()
return ws
return ws, nil
}
// registerRoutes sets up all HTTP routes with their handlers and middleware.
func (ws *WebServer) registerRoutes() {
authHandler := handlers.NewAuthHandler(ws.stores, ws.authCfg, ws.banCfg)
mailHandler := handlers.NewMailHandler(ws.stores, ws.storage, ws.outbound)
adminHandler := handlers.NewAdminHandler(ws.stores, ws.storage, filepath.Join(ws.storageCfg.BaseDir, "tls", "domains"), ws.outbound)
mailHandler := handlers.NewMailHandler(ws.stores, ws.storage, ws.outbound, ws.pusher)
adminHandler := handlers.NewAdminHandler(ws.stores, ws.storage, filepath.Join(ws.storageCfg.BaseDir, "tls", "domains"), ws.caddyDataDir, ws.outbound, ws.cfg.ProtocolLogKeepDays, ws.hub)
// Apply BanMiddleware globally before public routes
ws.engine.Use(middleware.BanMiddleware(ws.stores))
// Security headers on every response
ws.engine.Use(middleware.SecurityHeaders())
// Public routes (no auth required)
ws.engine.GET("/login", authHandler.ShowLogin)
@@ -175,6 +289,7 @@ func (ws *WebServer) registerRoutes() {
admin.GET("/domains/:id/edit", adminHandler.EditDomain)
admin.POST("/domains/:id", adminHandler.UpdateDomain)
admin.POST("/domains/:id/delete", adminHandler.DeleteDomain)
admin.POST("/domains/:id/fetch-caddy-cert", adminHandler.FetchCaddyCert)
admin.GET("/domains/:id/dns", adminHandler.DNSHint)
admin.GET("/users", adminHandler.ListUsers)
admin.GET("/users/new", adminHandler.NewUser)
@@ -190,10 +305,19 @@ func (ws *WebServer) registerRoutes() {
admin.POST("/outbound/:id/cancel", adminHandler.CancelOutbound)
admin.GET("/bans", adminHandler.ListBans)
admin.POST("/bans/:id/unban", adminHandler.UnbanIP)
admin.POST("/bans/cleanup", adminHandler.CleanupBans)
admin.GET("/protocol-logs", adminHandler.ListProtocolLogs)
admin.POST("/protocol-logs/cleanup", adminHandler.CleanupProtocolLogs)
admin.GET("/connections", adminHandler.ListConnections)
admin.POST("/connections/:id/disconnect", adminHandler.DisconnectConnection)
}
}
// Handler returns the underlying Gin engine as an http.Handler, useful for
// integration tests and for embedding behind a reverse proxy.
func (ws *WebServer) Handler() http.Handler {
return ws.engine
}
// Start launches the HTTP server on the configured address.
// Supports both TCP (e.g. ":8080") and Unix socket (e.g. "/run/mail_go/web.sock").
func (ws *WebServer) Start() error {
+270
View File
@@ -0,0 +1,270 @@
package web
// P0 回归测试:验证会话 cookie 由配置中的 secret_key 签名,
// 且旧版硬编码密钥(源码公开,视为已泄露)无法再伪造有效会话。
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
"mail_go/config"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/storage"
"mail_go/internal/store"
"github.com/gorilla/securecookie"
"golang.org/x/crypto/bcrypt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func chdirRepoRoot(t *testing.T) {
t.Helper()
// NewWebServer 以相对路径加载 internal/web/templates/
// 测试进程的 CWD 是 internal/web,需要切到仓库根目录。
if err := os.Chdir(filepath.Join("..", "..")); err != nil {
t.Fatalf("chdir to repo root: %v", err)
}
t.Cleanup(func() { _ = os.Chdir(filepath.Join("internal", "web")) })
}
func newTestStores(t *testing.T) *store.Stores {
t.Helper()
gdb, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "test.db")), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := gdb.AutoMigrate(&db.User{}, &db.Domain{}, &db.Message{}, &db.Attachment{}, &db.BanEntry{}, &db.OutboundMessage{}); err != nil {
t.Fatalf("migrate: %v", err)
}
return store.NewStores(gdb)
}
func newTestWebServer(t *testing.T, secretKey string) (*WebServer, *store.Stores) {
t.Helper()
chdirRepoRoot(t)
stores := newTestStores(t)
domain := &db.Domain{Name: "example.com", SmtpPort: 25, ImapPort: 143, Pop3Port: 110}
if err := stores.Domains.Create(domain); err != nil {
t.Fatalf("create domain: %v", err)
}
hash, err := bcrypt.GenerateFromPassword([]byte("test-password-123"), bcrypt.MinCost)
if err != nil {
t.Fatal(err)
}
if err := stores.Users.Create(&db.User{
Username: "alice",
PasswordHash: string(hash),
DomainID: domain.ID,
IsActive: true,
}); err != nil {
t.Fatalf("create user: %v", err)
}
baseDir := t.TempDir()
attStorage := storage.NewAttachmentStorage(filepath.Join(baseDir, "attachments"))
cfg := config.WebConfig{Addr: "127.0.0.1:0", SecretKey: secretKey, CookieSecure: true}
ws, err := NewWebServer(cfg, stores, attStorage, config.StorageConfig{BaseDir: baseDir},
config.AuthConfig{}, config.BanConfig{MaxFailAttempts: 100}, config.CaddyConfig{}, nil, connhub.New(), nil)
if err != nil {
t.Fatalf("NewWebServer: %v", err)
}
return ws, stores
}
func TestSessionSignedWithConfiguredSecretKey(t *testing.T) {
ws, _ := newTestWebServer(t, "0123456789abcdef0123456789abcdef")
srv := httptest.NewServer(ws.Handler())
defer srv.Close()
// 登录成功 -> 返回会话 cookie(禁用自动重定向以获取原始 302 响应)
form := url.Values{"email": {"alice@example.com"}, "password": {"test-password-123"}}
loginReq, _ := http.NewRequest(http.MethodPost, srv.URL+"/login", strings.NewReader(form.Encode()))
loginReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}
resp, err := client.Do(loginReq)
if err != nil {
t.Fatalf("login request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusFound {
t.Fatalf("login status = %d, want 302", resp.StatusCode)
}
var sessionCookie string
for _, c := range resp.Cookies() {
if c.Name == "mail_go_session" {
sessionCookie = c.Value
if !c.HttpOnly {
t.Error("session cookie must be HttpOnly")
}
if !c.Secure {
t.Error("session cookie must be Secure")
}
if c.SameSite != http.SameSiteStrictMode {
t.Errorf("session cookie SameSite = %v, want Strict", c.SameSite)
}
}
}
if sessionCookie == "" {
t.Fatal("login should set mail_go_session cookie")
}
// 合法会话可以访问收件箱
req, _ := http.NewRequest(http.MethodGet, srv.URL+"/inbox", nil)
req.AddCookie(&http.Cookie{Name: "mail_go_session", Value: sessionCookie})
resp2, err := client.Do(req)
if err != nil {
t.Fatalf("inbox request: %v", err)
}
defer resp2.Body.Close()
if resp2.StatusCode != http.StatusOK {
t.Fatalf("inbox with valid session: status = %d, want 200", resp2.StatusCode)
}
}
func TestLegacyHardcodedKeyCannotForgeSession(t *testing.T) {
// 服务端使用随机生成的新密钥
ws, _ := newTestWebServer(t, "9f8e7d6c5b4a39281706f5e4d3c2b1a09f8e7d6c5b4a39281706f5e4d3c2b1a0")
srv := httptest.NewServer(ws.Handler())
defer srv.Close()
// 攻击者用旧硬编码密钥(源码中公开)伪造管理员会话
forger := securecookie.New([]byte(config.InsecureLegacySecretKey), nil)
forged, err := forger.Encode("mail_go_session", map[interface{}]interface{}{
"userID": uint(1),
"userEmail": "admin@example.com",
"isAdmin": true,
})
if err != nil {
t.Fatalf("forge cookie: %v", err)
}
req, _ := http.NewRequest(http.MethodGet, srv.URL+"/inbox", nil)
req.AddCookie(&http.Cookie{Name: "mail_go_session", Value: forged})
client := &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("request with forged cookie: %v", err)
}
defer resp.Body.Close()
// 签名校验失败 -> 未认证,必须被重定向到登录页
if resp.StatusCode != http.StatusFound {
t.Fatalf("forged legacy-key session must be rejected: status = %d, want 302 redirect to /login", resp.StatusCode)
}
if loc := resp.Header.Get("Location"); !strings.HasPrefix(loc, "/login") {
t.Fatalf("forged session should redirect to /login, got Location: %q", loc)
}
}
func TestNewWebServerRejectsBadSecretKeys(t *testing.T) {
chdirRepoRoot(t)
stores := newTestStores(t)
baseDir := t.TempDir()
attStorage := storage.NewAttachmentStorage(filepath.Join(baseDir, "attachments"))
cases := []struct {
name string
key string
}{
{"empty", ""},
{"legacy default", config.InsecureLegacySecretKey},
{"too short", "short-key"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := NewWebServer(config.WebConfig{Addr: "127.0.0.1:0", SecretKey: tc.key},
stores, attStorage, config.StorageConfig{BaseDir: baseDir},
config.AuthConfig{}, config.BanConfig{}, config.CaddyConfig{}, nil, connhub.New(), nil)
if err == nil {
t.Fatalf("NewWebServer should reject secret key %q", tc.key)
}
})
}
}
// encodeSessionCookie 用配置密钥伪造一个签名合法的会话 cookie。
// 仅用于测试会话治理逻辑(生产密钥不会泄露)。
func encodeSessionCookie(t *testing.T, secretKey string, values map[interface{}]interface{}) string {
t.Helper()
sc := securecookie.New([]byte(secretKey), nil)
enc, err := sc.Encode("mail_go_session", values)
if err != nil {
t.Fatalf("encode session: %v", err)
}
return enc
}
// authCookieValues 构造 AuthMiddleware 可识别的最小会话内容。
func authCookieValues(userID uint, loginAt int64) map[interface{}]interface{} {
return map[interface{}]interface{}{
"userID": userID,
"userEmail": "alice@example.com",
"isAdmin": false,
"loginAt": loginAt,
}
}
// P3 #16:会话绝对过期(7 天)后强制重新登录。
func TestSessionAbsoluteExpiryForcesRelogin(t *testing.T) {
const key = "0123456789abcdef0123456789abcdef"
ws, _ := newTestWebServer(t, key)
srv := httptest.NewServer(ws.Handler())
defer srv.Close()
expired := time.Now().Add(-8 * 24 * time.Hour).Unix()
cookie := encodeSessionCookie(t, key, authCookieValues(1, expired))
req, _ := http.NewRequest(http.MethodGet, srv.URL+"/inbox", nil)
req.AddCookie(&http.Cookie{Name: "mail_go_session", Value: cookie})
client := &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusFound || !strings.HasPrefix(resp.Header.Get("Location"), "/login") {
t.Fatalf("expired session should redirect to /login, got %d Location=%q",
resp.StatusCode, resp.Header.Get("Location"))
}
}
// P3 #16:未过期会话(含滑动续期窗口内)正常访问。
func TestSessionWithinExpiryWorks(t *testing.T) {
const key = "0123456789abcdef0123456789abcdef"
ws, _ := newTestWebServer(t, key)
srv := httptest.NewServer(ws.Handler())
defer srv.Close()
cookie := encodeSessionCookie(t, key, authCookieValues(1, time.Now().Add(-time.Hour).Unix()))
req, _ := http.NewRequest(http.MethodGet, srv.URL+"/inbox", nil)
req.AddCookie(&http.Cookie{Name: "mail_go_session", Value: cookie})
client := &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("fresh session should access inbox, got %d", resp.StatusCode)
}
}
+23 -15
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>IP黑名单 - MailGo</title>
{{template "styles" .}}
</head>
@@ -18,24 +18,22 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div class="card">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
<h2>IP 黑名单</h2>
<form method="POST" action="/admin/bans/cleanup" style="display:inline;">
<button type="submit" class="btn btn-primary">清理过期记录</button>
</form>
<span style="color:#7f8c8d;font-size:13px;">阶段性封禁:第 4 次触发起封禁,30分钟 → 3小时 → 3个月 → 半年(上限)</span>
</div>
{{if not .bans}}
<p style="color:#7f8c8d;text-align:center;padding:40px 0;">暂无被封禁的 IP</p>
{{else}}
<table>
<thead>
<tr>
<th>ID</th>
<th>IP 地址</th>
<th>状态</th>
<th>封禁次数</th>
<th>失败次数</th>
<th>原因</th>
<th>到期时间</th>
@@ -43,16 +41,24 @@
</tr>
</thead>
<tbody>
{{range .bans}}
{{range .rows}}
<tr>
<td>{{.ID}}</td>
<td>{{.IPAddress}}</td>
<td>
{{if .Active}}<span class="badge" style="background:#e74c3c;color:#fff;">封禁中</span>
{{else}}<span class="badge" style="background:#95a5a6;color:#fff;">已过期</span>{{end}}
</td>
<td>
{{if .BanCount}}
{{if gt .BanCount 3}}第{{sub .BanCount 3}}次封禁{{else}}仅计数{{end}}
{{else}}—{{end}}
</td>
<td>{{.FailCount}}</td>
<td>{{.Reason}}</td>
<td>{{.ExpiresAt.Format "2006-01-02 15:04:05"}}</td>
<td>{{if .Reason}}{{.Reason}}{{else}}—{{end}}</td>
<td>{{.ExpiresAt.Format "2006-01-02 15:04:05"}}{{if not .Active}}(已过期){{end}}</td>
<td>
<form method="POST" action="/admin/bans/{{.ID}}/unban" style="display:inline;"
onsubmit="return confirm('确定要解封 IP {{.IPAddress}} 吗?');">
onsubmit="return confirm('确定要解封 IP {{.IPAddress}} 吗?解封后该 IP 的封禁档位将清零。');">
<button type="submit" class="btn btn-primary btn-sm">解封</button>
</form>
</td>
@@ -60,6 +66,8 @@
{{end}}
</tbody>
</table>
{{if not .rows}}
<p style="color:#7f8c8d;text-align:center;padding:40px 0;">暂无封禁记录</p>
{{end}}
</div>
{{if .totalPages}}
@@ -67,7 +75,7 @@
{{if gt .page 1}}
<a href="/admin/bans?page={{sub .page 1}}">上一页</a>
{{end}}
<span>第 {{.page}} / {{.totalPages}} 页</span>
<span>第 {{.page}} / {{.totalPages}} 页(共 {{.total}} 条)</span>
{{if lt .page .totalPages}}
<a href="/admin/bans?page={{add .page 1}}">下一页</a>
{{end}}
@@ -0,0 +1,101 @@
{{define "admin_connections"}}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>当前连接 - MailGo</title>
<meta http-equiv="refresh" content="5">
{{template "styles" .}}
</head>
<body>
{{template "navbar" .}}
<div class="container">
<div class="clearfix">
<div class="sidebar">
<a href="/inbox">返回邮箱</a>
<a href="/admin" {{if eq .activeFolder "admin"}}class="active"{{end}}>控制面板</a>
<a href="/admin/domains" {{if eq .activeFolder "domains"}}class="active"{{end}}>域名管理</a>
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
<h2>当前连接(SMTP / IMAP / POP3</h2>
<span style="color:#7f8c8d;font-size:13px;">每 5 秒自动刷新</span>
</div>
<div style="margin-bottom:24px;">
<div class="stat-card">
<h3>{{.total}}</h3>
<p>当前连接总数</p>
</div>
<div class="stat-card">
<h3>{{.smtpCount}}</h3>
<p>SMTP</p>
</div>
<div class="stat-card">
<h3>{{.imapCount}}</h3>
<p>IMAP</p>
</div>
<div class="stat-card">
<h3>{{.pop3Count}}</h3>
<p>POP3</p>
</div>
</div>
<div class="card">
<table>
<thead>
<tr>
<th>ID</th>
<th>协议</th>
<th>来源 IP</th>
<th>端口</th>
<th>用户名</th>
<th>TLS</th>
<th>连接时间</th>
<th>时长</th>
<th>最后活跃</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{{range .conns}}
<tr>
<td>{{.ID}}</td>
<td>
{{if eq .Protocol "smtp"}}<span class="badge" style="background:#3498db;color:#fff;">SMTP</span>
{{else if eq .Protocol "imap"}}<span class="badge" style="background:#9b59b6;color:#fff;">IMAP</span>
{{else}}<span class="badge" style="background:#16a085;color:#fff;">POP3</span>{{end}}
</td>
<td>{{.IP}}</td>
<td>{{.Port}}</td>
<td>{{if .User}}{{.User}}{{else}}—{{end}}</td>
<td>{{if .TLS}}<span class="badge" style="background:#27ae60;color:#fff;">TLS</span>{{else}}<span class="badge" style="background:#95a5a6;color:#fff;">明文</span>{{end}}</td>
<td>{{.Connected.Format "2006-01-02 15:04:05"}}</td>
<td>{{durationSeconds ($.now.Sub .Connected)}}s</td>
<td>{{.LastActive.Format "2006-01-02 15:04:05"}}</td>
<td>
<form method="POST" action="/admin/connections/{{.ID}}/disconnect" style="display:inline;"
onsubmit="return confirm('确定要断开 IP {{.IP}} 的所有连接并加入黑名单(180 天)吗?');">
<button type="submit" class="btn btn-sm btn-danger">断开并封禁</button>
</form>
</td>
</tr>
{{else}}
<tr><td colspan="10" style="text-align:center;color:#7f8c8d;">当前没有活动连接</td></tr>
{{end}}
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
{{end}}
+4 -2
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>管理后台 - MailGo</title>
{{template "styles" .}}
</head>
@@ -18,7 +18,9 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<h2 style="margin-bottom:24px;">管理后台</h2>
+4 -2
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>DNS配置 - MailGo</title>
{{template "styles" .}}
</head>
@@ -18,7 +18,9 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div class="card">
+34 -3
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>{{if .isEdit}}编辑域名{{else}}新增域名{{end}} - MailGo</title>
{{template "styles" .}}
</head>
@@ -18,12 +18,15 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div class="card">
<h2 style="margin-bottom:16px;">{{if .isEdit}}编辑域名{{else}}新增域名{{end}}</h2>
{{if .error}}<div class="alert alert-error">{{.error}}</div>{{end}}
{{if .caddyMsg}}<div class="alert {{if eq .caddyMsgType "success"}}alert-success{{else}}alert-error{{end}}">{{.caddyMsg}}</div>{{end}}
<form method="POST" action="{{if .isEdit}}/admin/domains/{{.domain.ID}}{{else}}/admin/domains{{end}}">
<div class="form-group">
<label>域名</label>
@@ -63,12 +66,40 @@
<label>TLS 公钥证书 PEM</label>
<textarea name="tls_public_cert" rows="8" placeholder="-----BEGIN CERTIFICATE-----&#10;...&#10;-----END CERTIFICATE-----" style="font-family:monospace;">{{.tlsPublicCert}}</textarea>
{{if .tlsCertConfigured}}
<p style="color:#27ae60;font-size:12px;margin-top:4px;">✅ TLS 证书已配置;上传新证书后需重启服务生效。</p>
<p style="color:#27ae60;font-size:12px;margin-top:4px;">✅ TLS 证书已配置;上传新证书后自动热加载生效。</p>
{{else}}
<p style="color:#e67e22;font-size:12px;margin-top:4px;">⚠️ TLS 证书未配置,启用 TLS 时必须同时填写私钥和证书。</p>
{{end}}
</div>
</div>
<div class="form-group" style="margin-top:4px;">
<label>从 Caddy 获取证书</label>
<p style="color:#7f8c8d;font-size:12px;margin-top:2px;margin-bottom:8px;">若该域名已由本机 Caddy 托管 HTTPS(自动签发证书),可一键导入其证书与私钥,并自动启用 TLS;证书热加载,无需重启服务。</p>
<button type="button" class="btn" id="btn_fetch_caddy" onclick="fetchCaddyCert()" style="background:#2e86de;color:#fff;">🔒 从 Caddy 获取证书</button>
<span id="caddy_fetch_msg" style="margin-left:10px;font-size:12px;color:#7f8c8d;"></span>
<script>
async function fetchCaddyCert() {
var btn = document.getElementById('btn_fetch_caddy');
var msg = document.getElementById('caddy_fetch_msg');
btn.disabled = true;
var oldText = btn.textContent;
btn.textContent = '获取中…';
msg.textContent = '';
try {
var resp = await fetch('/admin/domains/{{.domain.ID}}/fetch-caddy-cert', { method: 'POST' });
if (resp.redirected) {
window.location.href = resp.url;
return;
}
msg.textContent = '获取失败: ' + await resp.text();
} catch (e) {
msg.textContent = '请求失败: ' + e;
}
btn.disabled = false;
btn.textContent = oldText;
}
</script>
</div>
{{end}}
<script>
function togglePorts() {
+4 -2
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>域名管理 - MailGo</title>
{{template "styles" .}}
</head>
@@ -18,7 +18,9 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div class="card">
+5 -3
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>查看邮件 - MailGo 管理后台</title>
{{template "styles" .}}
<style>
@@ -27,7 +27,9 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" class="active">所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div class="card">
@@ -45,7 +47,7 @@
</div>
<div class="mail-body">
{{if .message.HtmlBody}}
<iframe class="mail-body-iframe" srcdoc="{{.message.HtmlBody | safeJS}}" sandbox="allow-same-origin" onload="this.style.height=this.contentDocument.body.scrollHeight+20+'px'"></iframe>
<iframe class="mail-body-iframe" srcdoc="{{.message.HtmlBody}}" sandbox="allow-same-origin" onload="this.style.height=this.contentDocument.body.scrollHeight+20+'px'"></iframe>
{{else}}
<pre style="white-space:pre-wrap;font-family:inherit;">{{.message.TextBody}}</pre>
{{end}}
+4 -2
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>所有邮件 - MailGo</title>
{{template "styles" .}}
</head>
@@ -18,7 +18,9 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div class="card">
+4 -2
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>外发队列 - MailGo</title>
{{template "styles" .}}
</head>
@@ -18,7 +18,9 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<h2 style="margin-bottom:24px;">外发队列</h2>
@@ -0,0 +1,132 @@
{{define "admin_protocol_logs"}}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>协议日志 - MailGo</title>
{{template "styles" .}}
</head>
<body>
{{template "navbar" .}}
<div class="container">
<div class="clearfix">
<div class="sidebar">
<a href="/inbox">返回邮箱</a>
<a href="/admin" {{if eq .activeFolder "admin"}}class="active"{{end}}>控制面板</a>
<a href="/admin/domains" {{if eq .activeFolder "domains"}}class="active"{{end}}>域名管理</a>
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
<h2>协议日志(SMTP / IMAP / POP3</h2>
<form method="POST" action="/admin/protocol-logs/cleanup" style="display:inline;"
onsubmit="return confirm('确认清理 {{.keepDays}} 天前的协议日志?');">
<button type="submit" class="btn btn-primary">清理旧日志</button>
</form>
</div>
<div style="margin-bottom:24px;">
<div class="stat-card">
<h3>{{index .todayStats "smtp" "fail"}}</h3>
<p>今日 SMTP 失败</p>
</div>
<div class="stat-card">
<h3>{{index .todayStats "imap" "fail"}}</h3>
<p>今日 IMAP 失败</p>
</div>
<div class="stat-card">
<h3>{{index .todayStats "pop3" "fail"}}</h3>
<p>今日 POP3 失败</p>
</div>
<div class="stat-card">
<h3>{{add (add (index .allStats "smtp" "fail") (index .allStats "imap" "fail")) (index .allStats "pop3" "fail")}}</h3>
<p>历史失败(全部)</p>
</div>
</div>
<div class="card">
<form method="GET" action="/admin/protocol-logs" style="margin-bottom:16px;">
<div style="display:flex;flex-wrap:wrap;gap:12px;align-items:center;">
<select name="protocol" style="padding:6px 10px;">
<option value="">全部协议</option>
<option value="smtp" {{if eq .filter.protocol "smtp"}}selected{{end}}>SMTP</option>
<option value="imap" {{if eq .filter.protocol "imap"}}selected{{end}}>IMAP</option>
<option value="pop3" {{if eq .filter.protocol "pop3"}}selected{{end}}>POP3</option>
</select>
<select name="success" style="padding:6px 10px;">
<option value="">全部状态</option>
<option value="success" {{if eq .filter.success "success"}}selected{{end}}>成功</option>
<option value="fail" {{if eq .filter.success "fail"}}selected{{end}}>失败</option>
</select>
<input type="text" name="ip" placeholder="来源 IP(模糊)" value="{{.filter.ip}}" style="padding:6px 10px;width:160px;">
<input type="text" name="username" placeholder="用户名(模糊)" value="{{.filter.username}}" style="padding:6px 10px;width:160px;">
<input type="date" name="from" value="{{.filter.from}}" style="padding:6px 10px;">
<span></span>
<input type="date" name="to" value="{{.filter.to}}" style="padding:6px 10px;">
<button type="submit" class="btn btn-sm btn-primary">筛选</button>
<a href="/admin/protocol-logs" class="btn btn-sm">重置</a>
</div>
</form>
<table>
<thead>
<tr>
<th>时间</th>
<th>协议</th>
<th>端口</th>
<th>来源 IP</th>
<th>用户名</th>
<th>状态</th>
<th>失败原因</th>
<th>操作摘要</th>
<th>消息数</th>
<th>时长</th>
</tr>
</thead>
<tbody>
{{range .logs}}
<tr>
<td style="white-space:nowrap;">{{.CreatedAt.Format "2006-01-02 15:04:05"}}</td>
<td>
{{if eq .Protocol "smtp"}}<span class="badge" style="background:#3498db;color:#fff;">SMTP</span>
{{else if eq .Protocol "imap"}}<span class="badge" style="background:#9b59b6;color:#fff;">IMAP</span>
{{else}}<span class="badge" style="background:#16a085;color:#fff;">POP3</span>{{end}}
</td>
<td>{{.Port}}</td>
<td>{{.ClientIP}}</td>
<td>{{if .Username}}{{.Username}}{{else}}—{{end}}</td>
<td>
{{if .Success}}<span class="badge" style="background:#27ae60;color:#fff;">成功</span>
{{else}}<span class="badge badge-unread">失败</span>{{end}}
</td>
<td style="max-width:200px;">{{if .FailReason}}{{.FailReason}}{{else}}—{{end}}</td>
<td style="max-width:320px;word-break:break-all;font-size:13px;color:#555;">{{.Detail}}</td>
<td>{{if .MsgCount}}{{.MsgCount}}{{else}}—{{end}}</td>
<td>{{if .DurationMs}}{{div .DurationMs 1000}}s{{else}}—{{end}}</td>
</tr>
{{else}}
<tr><td colspan="10" style="text-align:center;color:#7f8c8d;">暂无记录</td></tr>
{{end}}
</tbody>
</table>
{{if gt .totalPages 1}}
<div class="pagination">
{{if gt .page 1}}<a href="/admin/protocol-logs?page={{sub .page 1}}&protocol={{.filter.protocol}}&success={{.filter.success}}&ip={{.filter.ip}}&username={{.filter.username}}&from={{.filter.from}}&to={{.filter.to}}">上一页</a>{{end}}
<span class="current">第 {{.page}} / {{.totalPages}} 页(共 {{.total}} 条)</span>
{{if lt .page .totalPages}}<a href="/admin/protocol-logs?page={{add .page 1}}&protocol={{.filter.protocol}}&success={{.filter.success}}&ip={{.filter.ip}}&username={{.filter.username}}&from={{.filter.from}}&to={{.filter.to}}">下一页</a>{{end}}
</div>
{{end}}
</div>
</div>
</div>
</div>
</body>
</html>
{{end}}
+4 -2
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>{{if .isEdit}}编辑用户{{else}}新增用户{{end}} - MailGo</title>
{{template "styles" .}}
</head>
@@ -18,7 +18,9 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div class="card">
+4 -2
View File
@@ -3,7 +3,7 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>用户管理 - MailGo</title>
{{template "styles" .}}
</head>
@@ -18,7 +18,9 @@
<a href="/admin/users" {{if eq .activeFolder "users"}}class="active"{{end}}>用户管理</a>
<a href="/admin/mails" {{if eq .activeFolder "mails"}}class="active"{{end}}>所有邮件</a>
<a href="/admin/outbound" {{if eq .activeFolder "outbound"}}class="active"{{end}}>外发队列</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
<a href="/admin/protocol-logs" {{if eq .activeFolder "protocol-logs"}}class="active"{{end}}>协议日志</a>
<a href="/admin/connections" {{if eq .activeFolder "connections"}}class="active"{{end}}>当前连接</a>
<a href="/admin/bans" {{if eq .activeFolder "bans"}}class="active"{{end}}>IP黑名单</a>
</div>
<div class="content">
<div class="card">
+34 -11
View File
@@ -3,21 +3,40 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>访问被禁止 - MailGo</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: #f5f5f5; color: #333; display: flex; justify-content: center; align-items: center; min-height: 100vh; }
.banned-card { background: #fff; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); padding: 48px; text-align: center; max-width: 480px; width: 100%; }
.banned-icon { font-size: 64px; margin-bottom: 16px; }
h1 { font-size: 24px; color: #c0392b; margin-bottom: 12px; }
p { color: #7f8c8d; line-height: 1.6; margin-bottom: 8px; }
.detail { background: #f8f9fa; border-radius: 6px; padding: 16px; margin: 16px 0; text-align: left; }
.detail-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid #eee; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
"Hiragino Sans GB", "Microsoft YaHei", sans-serif;
background: linear-gradient(165deg, #fdecec 0%, #f6f8fc 55%, #eaf2ff 100%);
color: #1f2329; display: flex; justify-content: center; align-items: center; min-height: 100vh;
}
.banned-card {
background: #fff; border-radius: 14px;
box-shadow: 0 10px 40px rgba(227, 77, 89, 0.10);
padding: 48px; text-align: center; max-width: 480px; width: 100%;
}
.banned-icon {
width: 72px; height: 72px; margin: 0 auto 18px; border-radius: 50%;
background: #fde8e8; display: flex; align-items: center; justify-content: center;
font-size: 34px;
}
h1 { font-size: 22px; color: #e34d59; margin-bottom: 12px; }
p { color: #646a73; line-height: 1.7; margin-bottom: 8px; font-size: 14px; }
.detail {
background: #f8f9fb; border-radius: 10px; padding: 16px 18px;
margin: 18px 0; text-align: left;
}
.detail-row { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px solid #f0f1f3; }
.detail-row:last-child { border-bottom: none; }
.detail-label { color: #7f8c8d; font-size: 13px; }
.detail-value { color: #2c3e50; font-weight: 600; font-size: 13px; }
.back-link { display: inline-block; margin-top: 20px; color: #3498db; text-decoration: none; }
.detail-label { color: #8f959e; font-size: 13px; }
.detail-value { color: #1f2329; font-weight: 600; font-size: 13px; }
.back-link {
display: inline-block; margin-top: 22px; color: #1677ff;
text-decoration: none; font-size: 14px;
}
.back-link:hover { text-decoration: underline; }
</style>
</head>
@@ -32,6 +51,10 @@
<span class="detail-label">IP 地址</span>
<span class="detail-value">{{.entry.IPAddress}}</span>
</div>
<div class="detail-row">
<span class="detail-label">封禁档位</span>
<span class="detail-value">{{if gt .entry.BanCount 3}}第 {{sub .entry.BanCount 3}} 次封禁{{else}}—{{end}}</span>
</div>
<div class="detail-row">
<span class="detail-label">原因</span>
<span class="detail-value">{{.entry.Reason}}</span>
+604 -55
View File
@@ -1,66 +1,615 @@
{{define "styles"}}
<style>
* { margin:0; padding:0; box-sizing:border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background:#f5f5f5; color:#333; }
.navbar { background:#2c3e50; padding:0 20px; height:50px; display:flex; align-items:center; }
.navbar a { color:#ecf0f1; text-decoration:none; margin-right:20px; font-size:14px; }
.navbar a:hover { color:#3498db; }
.navbar .right { margin-left:auto; }
.container { max-width:1200px; margin:20px auto; padding:0 20px; }
.card { background:#fff; border-radius:8px; box-shadow:0 2px 4px rgba(0,0,0,0.1); padding:20px; margin-bottom:20px; }
table { width:100%; border-collapse:collapse; }
th, td { padding:10px 12px; text-align:left; border-bottom:1px solid #eee; }
th { background:#f8f9fa; font-weight:600; }
.btn { display:inline-block; padding:8px 16px; border-radius:4px; text-decoration:none; font-size:14px; cursor:pointer; border:none; }
.btn-primary { background:#3498db; color:#fff; }
.btn-primary:hover { background:#2980b9; }
.btn-danger { background:#e74c3c; color:#fff; }
.btn-danger:hover { background:#c0392b; }
.btn-sm { padding:4px 10px; font-size:12px; }
.alert { padding:12px 16px; border-radius:4px; margin-bottom:16px; }
.alert-error { background:#fde8e8; color:#c0392b; }
.alert-success { background:#e8fde8; color:#27ae60; }
.form-group { margin-bottom:16px; }
.form-group label { display:block; margin-bottom:6px; font-weight:600; }
.form-group input, .form-group textarea, .form-group select { width:100%; padding:8px 12px; border:1px solid #ddd; border-radius:4px; font-size:14px; }
.form-group input[type="checkbox"] { width:auto; }
.unread { font-weight:bold; }
.message-subject { color:#2c3e50; text-decoration:none; }
.message-subject:hover { color:#3498db; }
.sidebar { width:200px; float:left; }
.sidebar a { display:block; padding:10px 15px; color:#2c3e50; text-decoration:none; border-radius:4px; margin-bottom:2px; }
.sidebar a:hover, .sidebar a.active { background:#3498db; color:#fff; }
.content { margin-left:220px; }
.pagination { margin-top:16px; text-align:center; }
.pagination a, .pagination span { display:inline-block; padding:6px 12px; margin:0 2px; border:1px solid #ddd; border-radius:4px; text-decoration:none; color:#333; }
.pagination .current { background:#3498db; color:#fff; border-color:#3498db; }
.mail-meta { color:#7f8c8d; font-size:13px; margin-bottom:8px; }
.mail-body { line-height:1.6; margin-top:16px; padding-top:16px; border-top:1px solid #eee; }
.attachment-list { margin-top:16px; padding-top:16px; border-top:1px solid #eee; }
.attachment-item { display:inline-block; margin-right:12px; margin-bottom:8px; padding:6px 12px; background:#ecf0f1; border-radius:4px; font-size:13px; }
.attachment-item a { color:#2c3e50; text-decoration:none; }
.attachment-item a:hover { color:#3498db; }
.badge { display:inline-block; padding:2px 8px; border-radius:10px; font-size:11px; font-weight:bold; }
.badge-unread { background:#e74c3c; color:#fff; }
.stat-card { display:inline-block; width:200px; padding:20px; margin-right:20px; background:#fff; border-radius:8px; box-shadow:0 2px 4px rgba(0,0,0,0.1); text-align:center; }
.stat-card h3 { font-size:32px; color:#2c3e50; margin-bottom:4px; }
.stat-card p { color:#7f8c8d; font-size:14px; }
.dns-record { background:#f8f9fa; padding:12px 16px; border-radius:4px; margin-bottom:12px; font-family:monospace; font-size:13px; white-space:pre-wrap; }
.clearfix::after { content:""; display:table; clear:both; }
/* ===== MailGo 设计系统(参考 QQ 邮箱布局) ===== */
:root {
--primary: #1677ff;
--primary-hover: #0e5fd8;
--primary-soft: #e8f1ff;
--orange: #ff9500;
--orange-hover: #f08800;
--danger: #e34d59;
--danger-soft: #fde8e8;
--success: #34a853;
--success-soft: #e6f7ec;
--sidebar-bg: #f6f8fc;
--topbar-h: 56px;
--border: #e5e6eb;
--border-light: #f0f1f3;
--text: #1f2329;
--text-2: #646a73;
--text-3: #8f959e;
--radius: 8px;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
"Hiragino Sans GB", "Microsoft YaHei", sans-serif;
background: #f6f8fc; color: var(--text); font-size: 14px;
}
a { text-decoration: none; color: inherit; }
ul { list-style: none; }
input, textarea, select, button { font-family: inherit; }
input[type="checkbox"] { width: 15px; height: 15px; accent-color: var(--primary); cursor: pointer; flex-shrink: 0; }
svg { flex-shrink: 0; }
a, button { touch-action: manipulation; }
/* ---------- 顶部导航栏 ---------- */
.topbar {
position: fixed; top: 0; left: 0; right: 0; height: var(--topbar-h);
background: #fff; border-bottom: 1px solid var(--border);
display: flex; align-items: center; gap: 28px; padding: 0 20px; z-index: 100;
}
.logo { display: flex; align-items: center; gap: 9px; }
.logo-icon {
width: 34px; height: 34px; border-radius: 9px; color: #fff; font-size: 17px;
background: linear-gradient(135deg, #1677ff, #4aa3ff);
display: inline-flex; align-items: center; justify-content: center;
box-shadow: 0 2px 6px rgba(22, 119, 255, 0.35);
}
.logo-text { font-size: 19px; font-weight: 700; color: var(--primary); letter-spacing: 0.5px; }
.logo-text em { font-style: normal; font-weight: 400; font-size: 13px; color: var(--text-2); margin-left: 3px; }
.topbar-search {
flex: 0 1 340px; height: 34px; display: none; align-items: center; gap: 8px;
background: #f2f3f5; border: 1px solid transparent; border-radius: 17px;
padding: 0 14px; color: var(--text-3);
}
body.page-list .topbar-search { display: flex; }
.topbar-search:focus-within { background: #fff; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(22, 119, 255, 0.12); }
.topbar-search input { border: none; outline: none; background: transparent; flex: 1; font-size: 13px; color: var(--text); }
.topbar-search input::placeholder { color: var(--text-3); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 18px; }
.topbar-link { color: var(--text-2); font-size: 13.5px; }
.topbar-link:hover { color: var(--primary); }
.user-chip { display: flex; align-items: center; gap: 8px; }
.user-email { font-size: 13px; color: var(--text-2); max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logout-form { display: inline-flex; }
.icon-btn {
display: inline-flex; align-items: center; justify-content: center;
width: 30px; height: 30px; border: 1px solid var(--border); border-radius: 6px;
background: #fff; color: var(--text-2); cursor: pointer;
}
.icon-btn:hover { color: var(--danger); border-color: var(--danger); }
/* ---------- 头像 ---------- */
.avatar {
width: 36px; height: 36px; border-radius: 50%;
display: inline-flex; align-items: center; justify-content: center;
font-size: 15px; font-weight: 600; flex-shrink: 0; user-select: none;
}
.avatar-sm { width: 28px; height: 28px; font-size: 12.5px; }
/* ---------- 整体布局 ---------- */
.app-body { display: flex; min-height: 100vh; padding-top: var(--topbar-h); }
/* ---------- 左侧文件夹导航 ---------- */
.mail-sidebar {
width: 200px; flex-shrink: 0;
background: var(--sidebar-bg); border-right: 1px solid var(--border);
padding: 16px 12px; display: flex; flex-direction: column; gap: 14px;
position: sticky; top: var(--topbar-h); height: calc(100vh - var(--topbar-h));
}
.compose-btn {
display: flex; align-items: center; justify-content: center; gap: 7px;
height: 38px; border-radius: 19px; color: #fff; font-size: 14.5px; font-weight: 600;
background: linear-gradient(135deg, #ffa940, #ff9500);
box-shadow: 0 2px 8px rgba(255, 149, 0, 0.35);
transition: transform 0.1s, box-shadow 0.1s;
}
.compose-btn:hover { background: linear-gradient(135deg, #ff9d2e, #f08800); box-shadow: 0 3px 10px rgba(255, 149, 0, 0.45); transform: translateY(-1px); }
.folder-nav { display: flex; flex-direction: column; gap: 3px; }
.folder {
display: flex; align-items: center; gap: 10px;
height: 36px; padding: 0 10px; border-radius: 7px;
color: var(--text-2); font-size: 13.5px; position: relative;
}
.folder:hover { background: #eef1f6; color: var(--text); }
.folder.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.folder.active::before {
content: ""; position: absolute; left: -12px; top: 9px; bottom: 9px;
width: 3px; border-radius: 2px; background: var(--primary);
}
.folder .badge {
margin-left: auto; min-width: 20px; height: 20px; padding: 0 6px;
border-radius: 10px; background: #ff4d4f; color: #fff;
font-size: 11px; line-height: 20px; text-align: center; font-weight: 600;
}
.folder .count { margin-left: auto; color: var(--text-3); font-size: 12px; }
.sidebar-footer { margin-top: auto; border-top: 1px solid var(--border-light); padding-top: 12px; display: flex; flex-direction: column; gap: 3px; }
/* ---------- 主内容区 ---------- */
.mail-main { flex: 1; min-width: 0; background: #fff; display: flex; flex-direction: column; }
/* ---------- 列表工具条 ---------- */
.list-toolbar {
display: flex; align-items: center; gap: 10px;
padding: 10px 18px; border-bottom: 1px solid var(--border-light); background: #fff;
}
.tb-btn {
display: inline-flex; align-items: center; gap: 6px;
height: 30px; padding: 0 11px; border: 1px solid var(--border); border-radius: 6px;
background: #fff; color: var(--text-2); font-size: 13px; cursor: pointer;
}
.tb-btn:hover { color: var(--primary); border-color: var(--primary); }
.tb-btn.danger:hover { color: var(--danger); border-color: var(--danger); }
.check-all { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); cursor: pointer; user-select: none; }
.toolbar-spacer { flex: 1; }
.page-info { font-size: 13px; color: var(--text-3); }
/* ---------- 邮件列表 ---------- */
.mail-list { flex: 1; overflow-y: auto; }
.mail-row {
display: flex; align-items: center; gap: 10px;
padding: 0 18px; height: 54px; border-bottom: 1px solid var(--border-light);
cursor: pointer; transition: background 0.08s;
background: #fff;
}
.mail-row:not(.unread) { background: #fbfcfe; }
.mail-row:hover { background: #f2f6ff; }
.mail-row.selected { background: var(--primary-soft); }
.cell-check { display: flex; align-items: center; }
.cell-avatar { flex: 0 0 auto; }
.cell-from {
width: 150px; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
font-size: 13.5px; color: var(--text-2);
}
.mail-row.unread .cell-from { color: var(--text); font-weight: 600; }
.cell-subject-wrap { display: flex; align-items: center; gap: 7px; flex: 0 1 36%; min-width: 150px; }
.unread-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--primary); flex-shrink: 0; }
.cell-subject {
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
font-size: 13.5px; color: var(--text-2);
}
.mail-row.unread .cell-subject { color: var(--text); font-weight: 600; }
.cell-snippet {
flex: 1; min-width: 60px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
font-size: 12.5px; color: var(--text-3);
}
.cell-date {
width: 78px; flex-shrink: 0; text-align: right;
font-size: 12.5px; color: var(--text-3);
}
.mail-row.unread .cell-date { color: var(--text-2); }
.row-del { opacity: 0; transition: opacity 0.12s; }
.mail-row:hover .row-del { opacity: 1; }
.empty-tip { padding: 90px 0; text-align: center; color: var(--text-3); font-size: 14px; }
.empty-tip .empty-icon { font-size: 46px; display: block; margin-bottom: 14px; opacity: 0.5; }
/* ---------- 列表底部分页 ---------- */
.list-footer {
display: flex; align-items: center; gap: 12px;
padding: 10px 18px; border-top: 1px solid var(--border-light);
font-size: 13px; color: var(--text-3); background: #fff;
}
.pager { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.page-btn {
height: 28px; padding: 0 11px; border: 1px solid var(--border); border-radius: 6px;
background: #fff; color: var(--text-2); font-size: 13px;
display: inline-flex; align-items: center; gap: 4px;
}
.page-btn:hover:not(.disabled) { color: var(--primary); border-color: var(--primary); }
.page-btn.disabled { opacity: 0.45; cursor: not-allowed; }
.page-num { font-size: 13px; }
/* ---------- 邮件阅读页 ---------- */
.view-toolbar { display: flex; align-items: center; gap: 10px; padding: 10px 18px; border-bottom: 1px solid var(--border-light); }
.mail-head { padding: 22px 28px 0; }
.mail-title { font-size: 20px; font-weight: 600; line-height: 1.45; word-break: break-word; }
.mail-from-row { display: flex; align-items: center; gap: 12px; margin: 16px 0 18px; }
.mail-from-name { font-size: 14.5px; font-weight: 600; }
.mail-from-addr { color: var(--text-3); font-size: 12.5px; }
.mail-date { color: var(--text-3); font-size: 12.5px; margin-left: auto; }
.mail-body-wrap { padding: 22px 28px; flex: 1; overflow: auto; }
.mail-body { line-height: 1.7; font-size: 14.5px; }
.mail-body pre { white-space: pre-wrap; font-family: inherit; }
.mail-body-iframe {
width: 100%; min-height: 340px; border: 1px solid var(--border-light);
border-radius: 8px; background: #fff;
}
.attachment-list { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--border-light); }
.attachment-item {
display: inline-flex; align-items: center; gap: 7px;
margin: 0 12px 10px 0; padding: 7px 13px;
background: #f2f3f5; border-radius: 7px; font-size: 13px; color: var(--text);
}
.attachment-item a { color: var(--text); }
.attachment-item a:hover { color: var(--primary); }
.view-actions { display: flex; align-items: center; gap: 10px; padding: 18px 28px 24px; }
/* ---------- 写信页 ---------- */
.compose-toolbar { display: flex; align-items: center; gap: 10px; padding: 10px 18px; border-bottom: 1px solid var(--border-light); }
.compose-form { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.compose-field { display: flex; align-items: center; gap: 12px; padding: 9px 22px; border-bottom: 1px solid var(--border-light); }
.compose-field label { width: 54px; color: var(--text-2); font-size: 13.5px; flex-shrink: 0; }
.compose-field input { border: none; outline: none; flex: 1; font-size: 14px; color: var(--text); background: transparent; }
.editor-wrap { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.editor-wrap .ql-toolbar { border-left: none; border-right: none; border-top: none; }
.editor-wrap .ql-container { border: none; flex: 1; font-size: 14.5px; }
#editor { height: 100%; }
.attach-chips { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 22px; border-bottom: 1px solid var(--border-light); }
.attach-chip {
display: inline-flex; align-items: center; gap: 7px;
padding: 5px 11px; background: #f2f3f5; border-radius: 15px;
font-size: 12.5px; color: var(--text-2);
}
.attach-chip .chip-del { cursor: pointer; color: var(--text-3); border: none; background: none; font-size: 13px; line-height: 1; }
.attach-chip .chip-del:hover { color: var(--danger); }
.compose-footer {
display: flex; align-items: center; gap: 14px;
padding: 12px 22px; border-top: 1px solid var(--border-light);
font-size: 12.5px; color: var(--text-3); background: #fafbfc;
}
.quota-bar { width: 200px; height: 6px; background: #eceef1; border-radius: 3px; overflow: hidden; }
.quota-bar i { display: block; height: 100%; background: linear-gradient(90deg, #4aa3ff, var(--primary)); border-radius: 3px; }
.quota-bar.warn i { background: linear-gradient(90deg, #ffc53d, var(--orange)); }
.quota-bar.over i { background: var(--danger); }
/* ---------- 登录页 ---------- */
.login-page {
min-height: 100vh; display: flex; align-items: center; justify-content: center;
background: linear-gradient(165deg, #eaf2ff 0%, #f6f8fc 55%, #f0f6ff 100%);
}
.login-card {
width: 400px; max-width: calc(100vw - 32px);
background: #fff; border-radius: 14px;
box-shadow: 0 10px 40px rgba(22, 119, 255, 0.10);
padding: 42px 38px 36px;
}
.login-logo { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 8px; }
.login-title { text-align: center; font-size: 21px; font-weight: 700; color: var(--text); margin-bottom: 26px; }
.login-sub { text-align: center; color: var(--text-3); font-size: 13px; margin-bottom: 26px; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--text-3); font-size: 12.5px; margin: 18px 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border-light); }
/* ---------- 通用组件(兼容管理后台) ---------- */
.container { max-width: 1400px; margin: 20px auto; padding: var(--topbar-h) 20px 0; }
.card {
background: #fff; border: 1px solid var(--border-light); border-radius: var(--radius);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04); padding: 20px; margin-bottom: 16px;
}
.btn {
display: inline-block; padding: 8px 16px; border-radius: 6px; border: 1px solid transparent;
text-decoration: none; font-size: 14px; cursor: pointer; text-align: center;
background: #f2f3f5; color: var(--text); line-height: 1.4;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #d23b47; }
.btn-sm { padding: 4px 10px; font-size: 12px; border-radius: 5px; }
.alert { padding: 12px 16px; border-radius: 7px; margin-bottom: 16px; font-size: 13.5px; }
.alert-error { background: var(--danger-soft); color: #c0392b; }
.alert-success { background: var(--success-soft); color: #2e9e4f; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 7px; font-weight: 600; font-size: 13.5px; }
.form-group input, .form-group textarea, .form-group select {
width: 100%; padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px;
font-size: 14px; outline: none; background: #fff; color: var(--text);
}
.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
border-color: var(--primary); box-shadow: 0 0 0 3px rgba(22, 119, 255, 0.12);
}
.form-group input[type="checkbox"] { width: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border-light); font-size: 13.5px; }
th { background: #f8f9fb; font-weight: 600; color: var(--text-2); }
tbody tr:hover td { background: #fafbfc; }
.unread { font-weight: bold; }
.message-subject { color: var(--text); }
.message-subject:hover { color: var(--primary); }
.mail-meta { color: var(--text-2); font-size: 13px; margin-bottom: 10px; line-height: 1.9; }
.pagination { margin-top: 16px; text-align: center; }
.pagination a, .pagination span {
display: inline-block; padding: 6px 12px; margin: 0 2px;
border: 1px solid var(--border); border-radius: 6px; text-decoration: none;
color: var(--text-2); font-size: 13px;
}
.pagination .current { background: var(--primary); color: #fff; border-color: var(--primary); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: bold; }
.badge-unread { background: #ff4d4f; color: #fff; }
.stat-card {
display: inline-block; width: 200px; padding: 20px; margin: 0 20px 16px 0;
background: #fff; border: 1px solid var(--border-light); border-radius: var(--radius);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04); text-align: center;
}
.stat-card h3 { font-size: 30px; color: var(--primary); margin-bottom: 4px; }
.stat-card p { color: var(--text-2); font-size: 13.5px; }
.dns-record {
background: #f8f9fb; padding: 12px 16px; border-radius: 6px; margin-bottom: 12px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12.5px; white-space: pre-wrap;
}
.clearfix::after { content: ""; display: table; clear: both; }
/* 管理后台左侧导航兼容 */
.sidebar { width: 200px; float: left; background: #fff; border: 1px solid var(--border-light); border-radius: var(--radius); padding: 8px; }
.sidebar a { display: block; padding: 9px 14px; color: var(--text-2); text-decoration: none; border-radius: 6px; margin-bottom: 2px; font-size: 13.5px; }
.sidebar a:hover, .sidebar a.active { background: var(--primary-soft); color: var(--primary); }
.content { margin-left: 220px; }
.settings-main { padding: 24px; background: var(--sidebar-bg); }
/* ===== 移动端适配(≤767px ===== */
@media (max-width: 767px) {
:root { --topbar-h: 50px; }
/* 顶栏:紧凑单行 + 列表页搜索框下移为第二行 */
.topbar { height: auto; min-height: 50px; padding: 8px 12px; gap: 8px; flex-wrap: wrap; }
.logo-icon { width: 30px; height: 30px; font-size: 15px; border-radius: 8px; }
.logo-text { font-size: 17px; }
.logo-text em { display: none; }
.topbar-search { flex: 1 1 100%; order: 9; border-radius: 8px; }
body.page-list { --topbar-h: 96px; }
.topbar-right { gap: 12px; }
.user-email { display: none; }
/* 左侧文件夹栏 → 底部导航条 */
.mail-sidebar {
position: fixed; top: auto; bottom: 0; left: 0; right: 0;
width: 100%; height: auto; min-height: 58px;
padding: 4px 8px calc(4px + env(safe-area-inset-bottom));
flex-direction: row; align-items: center; gap: 2px;
background: #fff; border-top: 1px solid var(--border); border-right: none;
z-index: 90; box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.04);
}
.folder-nav { display: contents; }
.folder {
flex: 1; flex-direction: column; justify-content: center; gap: 2px;
height: 46px; padding: 0; border-radius: 8px; font-size: 10.5px;
position: relative;
}
.folder-nav .folder:nth-child(1) { order: 1; }
.folder-nav .folder:nth-child(2) { order: 2; }
.folder-nav .folder:nth-child(3) { order: 4; }
.folder svg { width: 19px; height: 19px; }
.folder.active::before { display: none; }
.folder .badge {
position: absolute; top: 1px; right: calc(50% - 20px);
min-width: 16px; height: 16px; padding: 0 4px;
font-size: 10px; line-height: 16px; border-radius: 8px;
}
.folder .count { display: none; }
.compose-btn {
width: 46px; height: 46px; padding: 0; border-radius: 50%;
margin: 0 4px; flex-shrink: 0; order: 3;
font-size: 0; box-shadow: 0 3px 10px rgba(255, 149, 0, 0.5);
transform: translateY(-10px);
}
.compose-btn:hover { transform: translateY(-10px); }
.compose-btn svg { width: 20px; height: 20px; }
.sidebar-footer {
margin: 0; border: none; padding: 0;
flex-direction: row; gap: 2px; order: 5;
}
.mail-main { padding-bottom: calc(58px + env(safe-area-inset-bottom)); }
/* 邮件列表 */
.list-toolbar { padding: 8px 10px; gap: 8px; }
.check-all { font-size: 12.5px; }
.tb-btn { height: 30px; padding: 0 9px; font-size: 12.5px; }
.page-info { font-size: 12px; }
.mail-row { padding: 0 10px; height: 60px; gap: 8px; }
.avatar { width: 32px; height: 32px; font-size: 13px; }
.cell-from { width: 88px; font-size: 13px; }
.cell-subject-wrap { flex: 1; min-width: 0; }
.cell-subject { font-size: 13px; }
.cell-snippet { display: none; }
.cell-date { width: 56px; font-size: 11.5px; }
.row-del { opacity: 1; }
.list-footer { padding: 8px 10px; }
.pager { margin-left: auto; gap: 4px; }
/* 阅读页 */
.view-toolbar { padding: 8px 10px; gap: 8px; }
.mail-head { padding: 16px 16px 0; }
.mail-title { font-size: 17px; }
.mail-from-row { gap: 10px; margin: 12px 0 14px; }
.mail-body-wrap { padding: 14px 16px; }
.mail-body-iframe { min-height: 260px; }
.view-actions { padding: 14px 16px 20px; flex-wrap: wrap; }
/* 写信页 */
.compose-toolbar { padding: 8px 10px; gap: 8px; }
.compose-field { padding: 8px 14px; gap: 8px; }
.compose-field label { width: 44px; font-size: 13px; }
.compose-footer { padding: 10px 14px; flex-wrap: wrap; }
.quota-bar { width: 130px; }
/* 设置页 */
.settings-main { padding: 14px; }
/* 管理后台 */
.container { padding: var(--topbar-h) 12px 0; margin: 12px auto; }
.sidebar { float: none; width: 100%; margin-bottom: 12px; display: flex; flex-wrap: wrap; gap: 2px; padding: 6px; }
.sidebar a { margin: 0; padding: 8px 12px; font-size: 13px; }
.content { margin-left: 0; overflow-x: auto; }
th, td { white-space: nowrap; }
.stat-card { width: calc(50% - 10px); margin: 0 0 10px; }
}
</style>
{{end}}
{{define "navbar"}}
{{if .currentUser}}
<nav class="navbar">
<a href="/inbox">MailGo</a>
{{if .currentUser.IsAdmin}}<a href="/admin">管理后台</a>{{end}}
<div class="right">
<span style="color:#ecf0f1;font-size:13px;">{{.currentUser.Username}}@{{.currentUser.Domain.Name}}</span>
<form method="POST" action="/logout" style="display:inline;">
<a href="#" onclick="this.parentElement.submit(); return false;">退出</a>
<header class="topbar">
<div class="topbar-left">
<a class="logo" href="/inbox">
<span class="logo-icon"></span>
<span class="logo-text">MailGo<em>邮箱</em></span>
</a>
</div>
<div class="topbar-search">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
<input id="mail-search" type="text" placeholder="搜索邮件(发件人 / 主题)" autocomplete="off">
</div>
<div class="topbar-right">
{{if .currentUser.IsAdmin}}<a class="topbar-link" href="/admin">管理后台</a>{{end}}
<a class="topbar-link" href="/settings">设置</a>
<span class="user-chip">
<span class="avatar avatar-sm" style="{{avatarStyle .currentUser.Username}}">{{initial .currentUser.Username}}</span>
<span class="user-email" title="{{.currentUser.Username}}@{{.currentUser.Domain.Name}}">{{.currentUser.Username}}@{{.currentUser.Domain.Name}}</span>
</span>
<form method="POST" action="/logout" class="logout-form">
<button type="submit" class="icon-btn" title="退出登录">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg>
</button>
</form>
</div>
</nav>
</header>
{{end}}
{{end}}
{{define "sidebar"}}
<aside class="mail-sidebar">
<a href="/compose" class="compose-btn">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
写信
</a>
<nav class="folder-nav">
<a class="folder {{if eq .activeFolder `inbox`}}active{{end}}" href="/inbox">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="22 12 16 12 14 15 10 15 8 12 2 12"/><path d="M5.45 5.11L2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"/></svg>
收件箱
{{if .inboxUnread}}<span class="badge">{{.inboxUnread}}</span>{{end}}
</a>
<a class="folder {{if eq .activeFolder `drafts`}}active{{end}}" href="/drafts">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/></svg>
草稿箱
{{if .draftsTotal}}<span class="count">{{.draftsTotal}}</span>{{end}}
</a>
<a class="folder {{if eq .activeFolder `sent`}}active{{end}}" href="/sent">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>
已发送
{{if .sentTotal}}<span class="count">{{.sentTotal}}</span>{{end}}
</a>
</nav>
<div class="sidebar-footer">
{{if .currentUser.IsAdmin}}
<a class="folder" href="/admin">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
管理后台
</a>
{{end}}
</div>
</aside>
{{end}}
{{define "listjs"}}
<script>
(function () {
var searchInput = document.getElementById('mail-search');
var rows = Array.prototype.slice.call(document.querySelectorAll('.mail-row'));
var selectAll = document.getElementById('select-all');
var btnDelete = document.getElementById('btn-delete');
// 行点击跳转(复选框 / 行内删除按钮除外)
rows.forEach(function (row) {
row.addEventListener('click', function (e) {
if (e.target.closest('.cell-check') || e.target.closest('.row-del')) return;
var link = row.querySelector('.cell-subject');
if (link) window.location.href = link.getAttribute('href');
});
});
// 全选
selectAll && selectAll.addEventListener('change', function () {
rows.forEach(function (row) {
var cb = row.querySelector('.row-check');
cb.checked = selectAll.checked;
row.classList.toggle('selected', cb.checked);
});
updateDeleteState();
});
rows.forEach(function (row) {
var cb = row.querySelector('.row-check');
cb.addEventListener('change', function () {
row.classList.toggle('selected', cb.checked);
if (!cb.checked && selectAll) selectAll.checked = false;
updateDeleteState();
});
});
function updateDeleteState() {
if (!btnDelete) return;
var n = rows.filter(function (r) { return r.isConnected && r.querySelector('.row-check').checked; }).length;
btnDelete.disabled = n === 0;
}
// 批量删除
btnDelete && btnDelete.addEventListener('click', function () {
var ids = rows.filter(function (r) { return r.isConnected && r.querySelector('.row-check').checked; })
.map(function (r) { return r.dataset.id; });
if (!ids.length) return;
if (!confirm('确定要删除选中的 ' + ids.length + ' 封邮件吗?')) return;
var done = 0;
ids.forEach(function (id) {
fetch('/mail/delete/' + id, { method: 'POST', body: new FormData() })
.then(function () { if (++done === ids.length) window.location.reload(); })
.catch(function () { if (++done === ids.length) window.location.reload(); });
});
});
// 刷新
var btnRefresh = document.getElementById('btn-refresh');
btnRefresh && btnRefresh.addEventListener('click', function () { window.location.reload(); });
// 搜索过滤(发件人 / 主题 / 摘要)
searchInput && searchInput.addEventListener('input', function () {
var q = searchInput.value.trim().toLowerCase();
rows.forEach(function (row) {
if (!row.isConnected) return;
var text = row.textContent.toLowerCase();
row.style.display = (!q || text.indexOf(q) !== -1) ? '' : 'none';
});
});
// 从 BFCache(往返缓存)恢复时静默同步最新列表状态:
// 阅读页已把邮件标记为已读/删除,恢复的旧 DOM 需要与服务端对齐,
// 避免“返回后仍是未读/已删邮件仍显示”。
// 真实浏览器在 BFCache 恢复时会触发 pageshow(persisted=true)
// visibilitychange 作为兜底(页面重新可见时也同步一次)。
var syncing = false;
function syncFromServer(forceReloadOnError) {
if (syncing) return;
syncing = true;
fetch(window.location.href, { headers: { 'Accept': 'text/html' }, credentials: 'same-origin' })
.then(function (r) { return r.text(); })
.then(function (html) {
var doc = new DOMParser().parseFromString(html, 'text/html');
var fresh = {};
doc.querySelectorAll('.mail-row').forEach(function (r) { fresh[r.dataset.id] = r.classList.contains('unread'); });
rows.forEach(function (row) {
var id = row.dataset.id;
if (!(id in fresh)) { row.remove(); return; } // 已被删除
row.classList.toggle('unread', fresh[id]);
var wrap = row.querySelector('.cell-subject-wrap');
var dot = row.querySelector('.unread-dot');
if (fresh[id] && !dot && wrap) {
var s = document.createElement('span');
s.className = 'unread-dot';
wrap.insertBefore(s, wrap.firstChild);
} else if (!fresh[id] && dot) {
dot.remove();
}
});
// 同步“共 N 封”与侧栏未读角标
var total = doc.querySelector('.page-info');
var curTotal = document.querySelector('.page-info');
if (total && curTotal) curTotal.textContent = total.textContent;
var badge = doc.querySelector('.folder.active .badge');
var curBadge = document.querySelector('.folder.active .badge');
if (badge && curBadge) curBadge.textContent = badge.textContent;
else if (!badge && curBadge) curBadge.remove();
})
.catch(function () {
// BFCache 恢复时 DOM 是旧的,同步失败只能整页刷新兜底;
// 标签页切回触发的同步失败则静默忽略(可能只是离线)。
if (forceReloadOnError) window.location.reload();
})
.then(function () { syncing = false; });
}
window.addEventListener('pageshow', function (e) {
if (e.persisted) syncFromServer(true);
});
document.addEventListener('visibilitychange', function () {
if (document.visibilityState === 'visible') syncFromServer(false);
});
})();
</script>
{{end}}
+96 -51
View File
@@ -3,64 +3,68 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>撰写邮件 - MailGo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>写信 - MailGo</title>
<link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">
{{template "styles" .}}
</head>
<body>
<body class="page-compose">
{{template "navbar" .}}
<div class="container">
<div class="clearfix">
<div class="sidebar">
<a href="/inbox" class="{{if eq .activeFolder `inbox`}}active{{end}}">收件箱</a>
<a href="/drafts" class="{{if eq .activeFolder `drafts`}}active{{end}}">草稿箱</a>
<a href="/sent" class="{{if eq .activeFolder `sent`}}active{{end}}">发件箱</a>
<a href="/compose" class="{{if eq .activeFolder `compose`}}active{{end}}">撰写邮件</a>
<a href="/settings" class="{{if eq .activeFolder `settings`}}active{{end}}">设置</a>
<div class="app-body">
{{template "sidebar" .}}
<main class="mail-main">
<div class="compose-toolbar">
<button type="submit" form="compose-form" class="btn btn-primary">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align:-2px;margin-right:4px;"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>
发送
</button>
<label class="tb-btn" style="cursor:pointer;">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/></svg>
附件
<input type="file" name="attachments" id="attach-input" multiple style="display:none;">
</label>
<a href="/inbox" class="tb-btn">取消</a>
<div class="toolbar-spacer"></div>
<span class="page-info">撰写新邮件</span>
</div>
<div class="content">
<div class="card">
<h2 style="margin-bottom:16px;">撰写邮件</h2>
{{if .error}}<div class="alert alert-error">{{.error}}</div>{{end}}
<form method="POST" action="/compose" enctype="multipart/form-data">
<div class="form-group">
<label>收件人</label>
<input type="email" name="to" required value="{{.to}}" placeholder="user@example.com">
</div>
<div class="form-group">
<label>抄送(可选)</label>
<input type="text" name="cc" value="{{.cc}}" placeholder="cc@example.com">
</div>
<div class="form-group">
<label>主题</label>
<input type="text" name="subject" value="{{.subject}}" placeholder="邮件主题">
</div>
<div class="form-group">
<label>正文</label>
<div id="editor" style="height:300px;"></div>
<input type="hidden" name="body" id="body-hidden">
<input type="hidden" name="html_body" id="html-body-hidden">
</div>
<div class="form-group">
<label>附件</label>
<input type="file" name="attachments" multiple>
</div>
<div class="form-group" style="color:#7f8c8d;font-size:12px;">
配额: {{formatBytes .usedBytes}} / {{formatBytes .quotaBytes}}
</div>
<button type="submit" class="btn btn-primary">发送邮件</button>
<a href="/inbox" class="btn" style="margin-left:8px;">取消</a>
</form>
{{if .error}}<div class="alert alert-error" style="margin:12px 18px 0;">{{.error}}</div>{{end}}
<form id="compose-form" class="compose-form" method="POST" action="/compose" enctype="multipart/form-data">
<div class="compose-field">
<label>收件人</label>
<input type="email" name="to" required value="{{.to}}" placeholder="输入收件人邮箱地址">
</div>
</div>
</div>
<div class="compose-field">
<label>抄送</label>
<input type="text" name="cc" value="{{.cc}}" placeholder="多个地址用逗号分隔(可选)">
</div>
<div class="compose-field">
<label>主题</label>
<input type="text" name="subject" value="{{.subject}}" placeholder="输入邮件主题">
</div>
<div id="attach-chips" class="attach-chips" style="{{if not .attachments}}display:none;{{end}}"></div>
<div class="editor-wrap">
<div id="editor" data-placeholder="请输入邮件内容..."></div>
<input type="hidden" name="body" id="body-hidden">
<input type="hidden" name="html_body" id="html-body-hidden">
</div>
<div class="compose-footer">
<span>附件配额</span>
<span class="quota-bar" id="quota-bar" data-used="{{.usedBytes}}" data-quota="{{.quotaBytes}}"><i></i></span>
<span id="quota-text">{{formatBytes .usedBytes}} / {{formatBytes .quotaBytes}}</span>
</div>
</form>
</main>
</div>
<script src="https://cdn.quilljs.com/1.3.7/quill.min.js"></script>
<script>
var quill = new Quill('#editor', {
theme: 'snow',
placeholder: '请输入邮件内容...',
placeholder: document.getElementById('editor').dataset.placeholder || '请输入邮件内容...',
modules: {
toolbar: [
[{ 'header': [1, 2, 3, false] }],
@@ -72,13 +76,54 @@
]
}
});
document.querySelector('form').addEventListener('submit', function() {
{{if .bodyContent}}
quill.root.innerHTML = {{.bodyContent | jsonify}};
{{end}}
document.getElementById('compose-form').addEventListener('submit', function () {
document.getElementById('body-hidden').value = quill.getText();
document.getElementById('html-body-hidden').value = quill.root.innerHTML;
});
{{if .bodyContent}}
quill.root.innerHTML = {{.bodyContent | safeJS}};
{{end}}
// 附件选择预览
var attachInput = document.getElementById('attach-input');
var chipsBox = document.getElementById('attach-chips');
var files = [];
function renderChips() {
chipsBox.innerHTML = '';
chipsBox.style.display = files.length ? 'flex' : 'none';
files.forEach(function (f, i) {
var chip = document.createElement('span');
chip.className = 'attach-chip';
chip.innerHTML = '📎 ' + f.name + ' (' + (f.size / 1024).toFixed(1) + ' KB)' +
'<button type="button" class="chip-del" data-i="' + i + '" title="移除">✕</button>';
chipsBox.appendChild(chip);
});
}
attachInput.addEventListener('change', function () {
files = Array.prototype.slice.call(attachInput.files);
renderChips();
});
chipsBox.addEventListener('click', function (e) {
var del = e.target.closest('.chip-del');
if (!del) return;
files.splice(parseInt(del.dataset.i, 10), 1);
var dt = new DataTransfer();
files.forEach(function (f) { dt.items.add(f); });
attachInput.files = dt.files;
renderChips();
});
// 配额进度条
var bar = document.getElementById('quota-bar');
if (bar) {
var used = parseInt(bar.dataset.used, 10) || 0;
var quota = parseInt(bar.dataset.quota, 10) || 1;
var pct = Math.min(100, Math.round(used / quota * 100));
bar.querySelector('i').style.width = pct + '%';
if (pct >= 90) bar.classList.add('warn');
if (pct >= 100) bar.classList.add('over');
}
</script>
</body>
</html>
+77 -58
View File
@@ -3,72 +3,91 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>草稿箱 - MailGo</title>
{{template "styles" .}}
</head>
<body>
<body class="page-list">
{{template "navbar" .}}
<div class="container">
<div class="clearfix">
<div class="sidebar">
<a href="/inbox" class="{{if eq .activeFolder `inbox`}}active{{end}}">收件箱</a>
<a href="/drafts" class="{{if eq .activeFolder `drafts`}}active{{end}}">草稿箱</a>
<a href="/sent" class="{{if eq .activeFolder `sent`}}active{{end}}">发件箱</a>
<a href="/compose" class="{{if eq .activeFolder `compose`}}active{{end}}">撰写邮件</a>
<a href="/settings" class="{{if eq .activeFolder `settings`}}active{{end}}">设置</a>
<div class="app-body">
{{template "sidebar" .}}
<main class="mail-main">
<div class="list-toolbar">
<label class="check-all" title="全选/取消全选">
<input type="checkbox" id="select-all">
全选
</label>
<button type="button" class="tb-btn" id="btn-refresh" title="刷新">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>
刷新
</button>
<button type="button" class="tb-btn danger" id="btn-delete" disabled>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
删除
</button>
<div class="toolbar-spacer"></div>
<span class="page-info">共 {{.total}} 封</span>
</div>
<div class="content">
<div class="card">
<h2 style="margin-bottom:16px;">草稿箱</h2>
{{if not .messages}}
<p style="color:#7f8c8d;text-align:center;padding:40px 0;">暂无草稿邮件</p>
{{else}}
<table>
<thead>
<tr>
<th style="width:25%;">发件人/收件人</th>
<th style="width:45%;">主题</th>
<th style="width:20%;">时间</th>
<th style="width:10%;">操作</th>
</tr>
</thead>
<tbody>
{{range .messages}}
<tr>
<td>{{.ToAddr}}</td>
<td>
<a href="/drafts/{{.ID}}" class="message-subject">
{{if .Subject}}{{.Subject}}{{else}}(无主题){{end}}
</a>
</td>
<td>{{.Date.Format "2006-01-02 15:04"}}</td>
<td>
<form method="POST" action="/mail/delete/{{.ID}}" style="display:inline;"
onsubmit="return confirm('确定要删除这封邮件吗?');">
<button type="submit" class="btn btn-danger btn-sm">删除</button>
</form>
</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
</div>
{{if .totalPages}}
<div class="pagination">
{{if gt .page 1}}
<a href="/drafts?page={{sub .page 1}}">上一页</a>
{{end}}
<span>第 {{.page}} / {{.totalPages}} 页</span>
{{if lt .page .totalPages}}
<a href="/drafts?page={{add .page 1}}">下一页</a>
{{end}}
</div>
{{if not .messages}}
<div class="mail-list">
<div class="empty-tip"><span class="empty-icon">📝</span>草稿箱暂无邮件</div>
</div>
{{else}}
<ul class="mail-list">
{{range .messages}}
<li class="mail-row" data-id="{{.ID}}">
<label class="cell-check" onclick="event.stopPropagation()">
<input type="checkbox" class="row-check" data-id="{{.ID}}">
</label>
<span class="cell-avatar">
<span class="avatar" style="{{avatarStyle .ToAddr}}">{{initial (mailName .ToAddr)}}</span>
</span>
<span class="cell-from" title="收件人:{{.ToAddr}}">致:{{mailName .ToAddr}}</span>
<span class="cell-subject-wrap">
<a class="cell-subject" href="/drafts/{{.ID}}">
{{if .Subject}}{{.Subject}}{{else}}(无主题){{end}}
</a>
</span>
<span class="cell-snippet">
{{if .TextBody}}{{truncate .TextBody 80}}{{else if .HtmlBody}}[HTML 邮件]{{end}}
</span>
<span class="cell-date">{{shortDate .Date}}</span>
<form method="POST" action="/mail/delete/{{.ID}}" class="row-del"
onsubmit="return confirm('确定要删除这封草稿吗?');">
<button type="submit" class="icon-btn" title="删除">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
</button>
</form>
</li>
{{end}}
</ul>
{{end}}
<div class="list-footer">
<span class="page-num">第 {{.page}} / {{if .totalPages}}{{.totalPages}}{{else}}1{{end}} 页</span>
<div class="pager">
{{if gt .page 1}}
<a class="page-btn" href="/drafts?page={{sub .page 1}}">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg>
上一页
</a>
{{else}}
<span class="page-btn disabled">上一页</span>
{{end}}
{{if lt .page .totalPages}}
<a class="page-btn" href="/drafts?page={{add .page 1}}">
下一页
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg>
</a>
{{else}}
<span class="page-btn disabled">下一页</span>
{{end}}
</div>
</div>
</div>
</main>
</div>
{{template "listjs" .}}
</body>
</html>
{{end}}
+72 -59
View File
@@ -3,73 +3,86 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>收件箱 - MailGo</title>
{{template "styles" .}}
</head>
<body>
<body class="page-list">
{{template "navbar" .}}
<div class="container">
<div class="clearfix">
<div class="sidebar">
<a href="/inbox" class="{{if eq .activeFolder `inbox`}}active{{end}}">收件箱</a>
<a href="/drafts" class="{{if eq .activeFolder `drafts`}}active{{end}}">草稿箱</a>
<a href="/sent" class="{{if eq .activeFolder `sent`}}active{{end}}">发件箱</a>
<a href="/compose" class="{{if eq .activeFolder `compose`}}active{{end}}">撰写邮件</a>
<a href="/settings" class="{{if eq .activeFolder `settings`}}active{{end}}">设置</a>
<div class="app-body">
{{template "sidebar" .}}
<main class="mail-main">
<div class="list-toolbar">
<label class="check-all" title="全选/取消全选">
<input type="checkbox" id="select-all">
全选
</label>
<button type="button" class="tb-btn" id="btn-refresh" title="刷新">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>
刷新
</button>
<button type="button" class="tb-btn danger" id="btn-delete" disabled>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
删除
</button>
<div class="toolbar-spacer"></div>
<span class="page-info">共 {{.total}} 封</span>
</div>
<div class="content">
<div class="card">
<h2 style="margin-bottom:16px;">收件箱</h2>
{{if not .messages}}
<p style="color:#7f8c8d;text-align:center;padding:40px 0;">暂无邮件</p>
{{else}}
<table>
<thead>
<tr>
<th style="width:25%;">发件人</th>
<th style="width:45%;">主题</th>
<th style="width:20%;">时间</th>
<th style="width:10%;">操作</th>
</tr>
</thead>
<tbody>
{{range .messages}}
<tr class="{{if not .IsRead}}unread{{end}}">
<td>{{decodeHeader .FromAddr}}</td>
<td>
<a href="/inbox/{{.ID}}" class="message-subject">
{{if .Subject}}{{.Subject}}{{else}}(无主题){{end}}
</a>
</td>
<td>{{.Date.Format "2006-01-02 15:04"}}</td>
<td>
{{if not .IsRead}}
<form method="POST" action="/mail/read/{{.ID}}" style="display:inline;">
<button type="submit" class="btn btn-primary btn-sm">已读</button>
</form>
{{end}}
</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
</div>
{{if .totalPages}}
<div class="pagination">
{{if gt .page 1}}
<a href="/inbox?page={{sub .page 1}}">上一页</a>
{{end}}
<span>第 {{.page}} / {{.totalPages}} 页</span>
{{if lt .page .totalPages}}
<a href="/inbox?page={{add .page 1}}">下一页</a>
{{end}}
</div>
{{if not .messages}}
<div class="mail-list">
<div class="empty-tip"><span class="empty-icon">📭</span>收件箱暂无邮件</div>
</div>
{{else}}
<ul class="mail-list">
{{range .messages}}
<li class="mail-row {{if not .IsRead}}unread{{end}}" data-id="{{.ID}}">
<label class="cell-check" onclick="event.stopPropagation()">
<input type="checkbox" class="row-check" data-id="{{.ID}}">
</label>
<span class="cell-avatar">
<span class="avatar" style="{{avatarStyle .FromAddr}}">{{initial (mailName (decodeHeader .FromAddr))}}</span>
</span>
<span class="cell-from" title="{{decodeHeader .FromAddr}}">{{mailName (decodeHeader .FromAddr)}}</span>
<span class="cell-subject-wrap">
{{if not .IsRead}}<span class="unread-dot"></span>{{end}}
<a class="cell-subject" href="/inbox/{{.ID}}">
{{if .Subject}}{{.Subject}}{{else}}(无主题){{end}}
</a>
</span>
<span class="cell-snippet">
{{if .TextBody}}{{truncate .TextBody 80}}{{else if .HtmlBody}}[HTML 邮件]{{end}}
</span>
<span class="cell-date">{{shortDate .Date}}</span>
</li>
{{end}}
</ul>
{{end}}
<div class="list-footer">
<span class="page-num">第 {{.page}} / {{if .totalPages}}{{.totalPages}}{{else}}1{{end}} 页</span>
<div class="pager">
{{if gt .page 1}}
<a class="page-btn" href="/inbox?page={{sub .page 1}}">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg>
上一页
</a>
{{else}}
<span class="page-btn disabled">上一页</span>
{{end}}
{{if lt .page .totalPages}}
<a class="page-btn" href="/inbox?page={{add .page 1}}">
下一页
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg>
</a>
{{else}}
<span class="page-btn disabled">下一页</span>
{{end}}
</div>
</div>
</div>
</main>
</div>
{{template "listjs" .}}
</body>
</html>
{{end}}
+46 -45
View File
@@ -3,54 +3,55 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录 - MailGo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>登录 - MailGo 邮箱</title>
{{template "styles" .}}
</head>
<body>
{{template "navbar" .}}
<div class="container">
<div style="max-width:400px;margin:80px auto;">
<div class="card">
<h2 style="text-align:center;margin-bottom:24px;">MailGo 登录</h2>
{{if .error}}<div class="alert alert-error">{{.error}}</div>{{end}}
<form method="POST" action="/login">
<div class="form-group">
<label>邮箱地址</label>
<input type="email" name="email" required autofocus placeholder="admin@example.com">
</div>
<div class="form-group">
<label>密码</label>
<input type="password" name="password" required placeholder="请输入密码">
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">登录</button>
</form>
{{if or .oauth2Enabled .ldapEnabled}}
<div style="text-align:center;margin:16px 0;color:#7f8c8d;">─── 或 ───</div>
{{end}}
{{if .ldapEnabled}}
<form method="POST" action="/login/ldap">
<div class="form-group">
<label>LDAP 用户名</label>
<input type="text" name="username" placeholder="LDAP 用户名">
</div>
<div class="form-group">
<label>LDAP 密码</label>
<input type="password" name="password" placeholder="LDAP 密码">
</div>
<button type="submit" class="btn" style="width:100%;background:#8e44ad;color:#fff;">LDAP 登录</button>
</form>
{{end}}
{{if .oauth2Enabled}}
<a href="/auth/oauth2" class="btn" style="width:100%;background:#3498db;color:#fff;text-align:center;display:block;margin-top:8px;">
{{if eq .oauth2Provider "google"}}Google{{else if eq .oauth2Provider "github"}}GitHub{{else}}OAuth2{{end}} 登录
</a>
{{end}}
</div>
<body class="login-page">
<div class="login-card">
<div class="login-logo">
<span class="logo-icon" style="width:44px;height:44px;font-size:22px;border-radius:12px;"></span>
<span style="font-size:26px;font-weight:700;color:var(--primary);">MailGo<em style="font-style:normal;font-weight:400;font-size:15px;color:var(--text-2);margin-left:4px;">邮箱</em></span>
</div>
<div class="login-sub">登录您的邮箱账户</div>
{{if .error}}<div class="alert alert-error">{{.error}}</div>{{end}}
<form method="POST" action="/login">
<div class="form-group">
<label>邮箱地址</label>
<input type="email" name="email" required autofocus placeholder="user@example.com" style="padding:11px 14px;border-radius:8px;">
</div>
<div class="form-group">
<label>密码</label>
<input type="password" name="password" required placeholder="请输入密码" style="padding:11px 14px;border-radius:8px;">
</div>
<button type="submit" class="btn btn-primary" style="width:100%;padding:11px 16px;font-size:15px;border-radius:8px;">登 录</button>
</form>
{{if or .oauth2Enabled .ldapEnabled}}
<div class="divider"></div>
{{end}}
{{if .ldapEnabled}}
<form method="POST" action="/login/ldap" style="margin-bottom:10px;">
<div class="form-group">
<label>LDAP 用户名</label>
<input type="text" name="username" placeholder="LDAP 用户名">
</div>
<div class="form-group">
<label>LDAP 密码</label>
<input type="password" name="password" placeholder="LDAP 密码">
</div>
<button type="submit" class="btn" style="width:100%;background:#7b5cd6;color:#fff;">LDAP 登录</button>
</form>
{{end}}
{{if .oauth2Enabled}}
<a href="/auth/oauth2" class="btn btn-primary" style="width:100%;text-align:center;display:block;margin-top:4px;">
{{if eq .oauth2Provider "google"}}Google{{else if eq .oauth2Provider "github"}}GitHub{{else}}OAuth2{{end}} 登录
</a>
{{end}}
</div>
</body>
</html>
+78 -59
View File
@@ -3,72 +3,91 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>发件箱 - MailGo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>已发送 - MailGo</title>
{{template "styles" .}}
</head>
<body>
<body class="page-list">
{{template "navbar" .}}
<div class="container">
<div class="clearfix">
<div class="sidebar">
<a href="/inbox" class="{{if eq .activeFolder `inbox`}}active{{end}}">收件箱</a>
<a href="/drafts" class="{{if eq .activeFolder `drafts`}}active{{end}}">草稿箱</a>
<a href="/sent" class="{{if eq .activeFolder `sent`}}active{{end}}">发件箱</a>
<a href="/compose" class="{{if eq .activeFolder `compose`}}active{{end}}">撰写邮件</a>
<a href="/settings" class="{{if eq .activeFolder `settings`}}active{{end}}">设置</a>
<div class="app-body">
{{template "sidebar" .}}
<main class="mail-main">
<div class="list-toolbar">
<label class="check-all" title="全选/取消全选">
<input type="checkbox" id="select-all">
全选
</label>
<button type="button" class="tb-btn" id="btn-refresh" title="刷新">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>
刷新
</button>
<button type="button" class="tb-btn danger" id="btn-delete" disabled>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
删除
</button>
<div class="toolbar-spacer"></div>
<span class="page-info">共 {{.total}} 封</span>
</div>
<div class="content">
<div class="card">
<h2 style="margin-bottom:16px;">发件箱</h2>
{{if not .messages}}
<p style="color:#7f8c8d;text-align:center;padding:40px 0;">暂无已发送邮件</p>
{{else}}
<table>
<thead>
<tr>
<th style="width:25%;">收件人</th>
<th style="width:45%;">主题</th>
<th style="width:20%;">时间</th>
<th style="width:10%;">操作</th>
</tr>
</thead>
<tbody>
{{range .messages}}
<tr>
<td>{{.ToAddr}}</td>
<td>
<a href="/sent/{{.ID}}" class="message-subject">
{{if .Subject}}{{.Subject}}{{else}}(无主题){{end}}
</a>
</td>
<td>{{.Date.Format "2006-01-02 15:04"}}</td>
<td>
<form method="POST" action="/mail/delete/{{.ID}}" style="display:inline;"
onsubmit="return confirm('确定要删除这封邮件吗?');">
<button type="submit" class="btn btn-danger btn-sm">删除</button>
</form>
</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
</div>
{{if .totalPages}}
<div class="pagination">
{{if gt .page 1}}
<a href="/sent?page={{sub .page 1}}">上一页</a>
{{end}}
<span>第 {{.page}} / {{.totalPages}} 页</span>
{{if lt .page .totalPages}}
<a href="/sent?page={{add .page 1}}">下一页</a>
{{end}}
</div>
{{if not .messages}}
<div class="mail-list">
<div class="empty-tip"><span class="empty-icon">📤</span>已发送暂无邮件</div>
</div>
{{else}}
<ul class="mail-list">
{{range .messages}}
<li class="mail-row" data-id="{{.ID}}">
<label class="cell-check" onclick="event.stopPropagation()">
<input type="checkbox" class="row-check" data-id="{{.ID}}">
</label>
<span class="cell-avatar">
<span class="avatar" style="{{avatarStyle .ToAddr}}">{{initial (mailName .ToAddr)}}</span>
</span>
<span class="cell-from" title="收件人:{{.ToAddr}}">{{mailName .ToAddr}}</span>
<span class="cell-subject-wrap">
<a class="cell-subject" href="/sent/{{.ID}}">
{{if .Subject}}{{.Subject}}{{else}}(无主题){{end}}
</a>
</span>
<span class="cell-snippet">
{{if .TextBody}}{{truncate .TextBody 80}}{{else if .HtmlBody}}[HTML 邮件]{{end}}
</span>
<span class="cell-date">{{shortDate .Date}}</span>
<form method="POST" action="/mail/delete/{{.ID}}" class="row-del"
onsubmit="return confirm('确定要删除这封邮件吗?');">
<button type="submit" class="icon-btn" title="删除">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
</button>
</form>
</li>
{{end}}
</ul>
{{end}}
<div class="list-footer">
<span class="page-num">第 {{.page}} / {{if .totalPages}}{{.totalPages}}{{else}}1{{end}} 页</span>
<div class="pager">
{{if gt .page 1}}
<a class="page-btn" href="/sent?page={{sub .page 1}}">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg>
上一页
</a>
{{else}}
<span class="page-btn disabled">上一页</span>
{{end}}
{{if lt .page .totalPages}}
<a class="page-btn" href="/sent?page={{add .page 1}}">
下一页
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg>
</a>
{{else}}
<span class="page-btn disabled">下一页</span>
{{end}}
</div>
</div>
</div>
</main>
</div>
{{template "listjs" .}}
</body>
</html>
{{end}}
+67 -32
View File
@@ -3,46 +3,81 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>设置 - MailGo</title>
{{template "styles" .}}
</head>
<body>
<body class="page-settings">
{{template "navbar" .}}
<div class="container">
<div class="clearfix">
<div class="sidebar">
<a href="/inbox" class="{{if eq .activeFolder `inbox`}}active{{end}}">收件箱</a>
<a href="/drafts" class="{{if eq .activeFolder `drafts`}}active{{end}}">草稿箱</a>
<a href="/sent" class="{{if eq .activeFolder `sent`}}active{{end}}">发件箱</a>
<a href="/compose" class="{{if eq .activeFolder `compose`}}active{{end}}">撰写邮件</a>
<a href="/settings" class="{{if eq .activeFolder `settings`}}active{{end}}">设置</a>
</div>
<div class="content">
<div class="card">
<h2 style="margin-bottom:16px;">设置</h2>
{{if .error}}<div class="alert alert-error">{{.error}}</div>{{end}}
{{if .success}}<div class="alert alert-success">{{.success}}</div>{{end}}
<h3 style="margin-bottom:12px;">修改密码</h3>
<form method="POST" action="/settings">
<div class="form-group">
<label>当前密码</label>
<input type="password" name="old_password" required placeholder="请输入当前密码">
<div class="app-body">
{{template "sidebar" .}}
<main class="mail-main settings-main">
{{if .error}}<div class="alert alert-error">{{.error}}</div>{{end}}
{{if .success}}<div class="alert alert-success">{{.success}}</div>{{end}}
{{if .mustChange}}<div class="alert" style="border:1px solid #ffa940;background:#fff7e6;color:#d46b08;border-radius:8px;padding:12px 16px;margin-bottom:16px;font-size:13.5px;">
⚠️ 首次登录/密码已被重置,请立即修改密码后再继续使用邮箱功能。
</div>{{end}}
<div class="card" style="max-width:720px;">
<h2 style="font-size:17px;margin-bottom:18px;display:flex;align-items:center;gap:10px;">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#1677ff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>
账号信息
</h2>
<div style="display:flex;align-items:center;gap:14px;margin-bottom:20px;">
<span class="avatar" style="width:52px;height:52px;font-size:22px;{{avatarStyle .currentUser.Username}}">{{initial .currentUser.Username}}</span>
<div>
<div style="font-size:16px;font-weight:600;">{{.currentUser.Username}}@{{.currentUser.Domain.Name}}</div>
<div style="color:var(--text-3);font-size:12.5px;margin-top:3px;">
已用 {{formatBytes .currentUser.UsedBytes}} / 配额 {{formatBytes .currentUser.QuotaBytes}}
{{if .currentUser.IsAdmin}} · 管理员{{end}}
</div>
<div class="form-group">
<label>新密码</label>
<input type="password" name="new_password" required placeholder="请输入新密码">
</div>
<div class="form-group">
<label>确认新密码</label>
<input type="password" name="confirm_password" required placeholder="请再次输入新密码">
</div>
<button type="submit" class="btn btn-primary">修改密码</button>
</form>
</div>
</div>
<div class="quota-bar" style="width:100%;" data-used="{{.currentUser.UsedBytes}}" data-quota="{{.currentUser.QuotaBytes}}"><i></i></div>
</div>
</div>
<div class="card" style="max-width:720px;">
<h2 style="font-size:17px;margin-bottom:18px;">修改密码</h2>
<form method="POST" action="/settings" style="max-width:420px;">
<div class="form-group">
<label>当前密码</label>
<input type="password" name="old_password" required placeholder="请输入当前密码" autocomplete="current-password">
</div>
<div class="form-group">
<label>新密码</label>
<input type="password" name="new_password" required placeholder="请输入新密码" autocomplete="new-password">
</div>
<div class="form-group">
<label>确认新密码</label>
<input type="password" name="confirm_password" required placeholder="请再次输入新密码" autocomplete="new-password">
</div>
<button type="submit" class="btn btn-primary">修改密码</button>
</form>
</div>
<div class="card" style="max-width:720px;">
<h2 style="font-size:17px;margin-bottom:10px;">帮助</h2>
<p style="color:var(--text-2);font-size:13.5px;line-height:1.9;">
客户端收发信(IMAP / SMTP)配置:<br>
IMAP 服务器:{{.currentUser.Domain.Name}} 端口 143 / SSL 993<br>
SMTP 服务器:{{.currentUser.Domain.Name}} 端口 587(提交)/ SSL 465
</p>
</div>
</main>
</div>
<script>
(function () {
var bar = document.querySelector('.quota-bar[data-quota]');
if (bar) {
var used = parseInt(bar.dataset.used, 10) || 0;
var quota = parseInt(bar.dataset.quota, 10) || 1;
var pct = Math.min(100, Math.round(used / quota * 100));
bar.querySelector('i').style.width = pct + '%';
if (pct >= 90) bar.classList.add('warn');
if (pct >= 100) bar.classList.add('over');
}
})();
</script>
</body>
</html>
{{end}}
+72 -56
View File
@@ -3,70 +3,86 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>查看邮件 - MailGo</title>
{{template "styles" .}}
<style>
.mail-body-iframe {
width: 100%;
min-height: 300px;
border: 1px solid #e0e0e0;
border-radius: 4px;
background: #fff;
}
</style>
</head>
<body>
<body class="page-view">
{{template "navbar" .}}
<div class="container">
<div class="clearfix">
<div class="sidebar">
<a href="/inbox" class="{{if eq .activeFolder `inbox`}}active{{end}}">收件箱</a>
<a href="/drafts" class="{{if eq .activeFolder `drafts`}}active{{end}}">草稿箱</a>
<a href="/sent" class="{{if eq .activeFolder `sent`}}active{{end}}">发件箱</a>
<a href="/compose" class="{{if eq .activeFolder `compose`}}active{{end}}">撰写邮件</a>
<a href="/settings" class="{{if eq .activeFolder `settings`}}active{{end}}">设置</a>
<div class="app-body">
{{template "sidebar" .}}
<main class="mail-main">
<div class="view-toolbar">
<a href="javascript:history.back()" class="tb-btn">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="19" y1="12" x2="5" y2="12"/><polyline points="12 19 5 12 12 5"/></svg>
返回
</a>
<a href="/compose?to={{mailEmail .message.FromAddr}}&subject={{if .message.Subject}}Re: {{.message.Subject}}{{end}}" class="tb-btn">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 17 4 12 9 7"/><path d="M20 18v-2a4 4 0 0 0-4-4H4"/></svg>
回复
</a>
<form method="POST" action="/mail/delete/{{.message.ID}}" style="display:inline;"
onsubmit="return confirm('确定要删除这封邮件吗?');">
<button type="submit" class="tb-btn danger">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
删除
</button>
</form>
</div>
<div class="content">
<div class="card">
<div style="margin-bottom:16px;">
<a href="javascript:history.back()" class="btn" style="background:#bdc3c7;color:#fff;">返回</a>
</div>
<h2>{{if .message.Subject}}{{.message.Subject}}{{else}}(无主题){{end}}</h2>
<div class="mail-meta" style="margin-top:12px;">
<p><strong>发件人:</strong> {{decodeHeader .message.FromAddr}}</p>
<p><strong>收件人:</strong> {{.message.ToAddr}}</p>
{{if .message.CcAddr}}<p><strong>抄送:</strong> {{.message.CcAddr}}</p>{{end}}
<p><strong>时间:</strong> {{.message.Date.Format "2006-01-02 15:04:05"}}</p>
</div>
<div class="mail-body">
{{if .message.HtmlBody}}
<iframe class="mail-body-iframe" srcdoc="{{.message.HtmlBody | safeJS}}" sandbox="allow-same-origin" onload="this.style.height=this.contentDocument.body.scrollHeight+20+'px'"></iframe>
{{else}}
<pre style="white-space:pre-wrap;font-family:inherit;">{{.message.TextBody}}</pre>
{{end}}
</div>
{{if .attachments}}
<div class="attachment-list">
<h4 style="margin-bottom:8px;">附件</h4>
{{range .attachments}}
<div class="attachment-item">
📎 <a href="/attachment/{{.ID}}">{{.FileName}}</a>
<span style="color:#7f8c8d;font-size:12px;">({{formatBytes .FileSize}})</span>
</div>
{{end}}
</div>
{{end}}
<div style="margin-top:20px;padding-top:16px;border-top:1px solid #eee;">
<form method="POST" action="/mail/delete/{{.message.ID}}" style="display:inline;"
onsubmit="return confirm('确定要删除这封邮件吗?');">
<button type="submit" class="btn btn-danger">删除邮件</button>
</form>
<a href="/compose?to={{decodeHeader .message.FromAddr}}&subject={{if .message.Subject}}Re: {{.message.Subject}}{{end}}" class="btn btn-primary" style="margin-left:8px;">回复</a>
<div class="mail-head">
<h1 class="mail-title">{{if .message.Subject}}{{.message.Subject}}{{else}}(无主题){{end}}</h1>
<div class="mail-from-row">
<span class="avatar" style="{{avatarStyle .message.FromAddr}}">{{initial (mailName (decodeHeader .message.FromAddr))}}</span>
<div>
<div class="mail-from-name">{{mailName (decodeHeader .message.FromAddr)}}</div>
<div class="mail-from-addr" title="{{decodeHeader .message.FromAddr}}">{{mailEmail .message.FromAddr}}</div>
</div>
<span class="mail-date">{{.message.Date.Format "2006-01-02 15:04:05"}}</span>
</div>
{{if .message.CcAddr}}
<div class="mail-from-addr" style="margin:-8px 0 16px 48px;">
抄送:{{.message.CcAddr}}
</div>
{{end}}
</div>
</div>
<div class="mail-body-wrap">
<div class="mail-body">
{{if .message.HtmlBody}}
<iframe class="mail-body-iframe" srcdoc="{{.message.HtmlBody}}" sandbox="allow-same-origin" onload="this.style.height=this.contentDocument.body.scrollHeight+20+'px'"></iframe>
{{else}}
<pre>{{.message.TextBody}}</pre>
{{end}}
</div>
{{if .attachments}}
<div class="attachment-list">
{{range .attachments}}
<span class="attachment-item">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/></svg>
<a href="/attachment/{{.ID}}">{{.FileName}}</a>
<span style="color:var(--text-3);font-size:12px;">({{formatBytes .FileSize}})</span>
</span>
{{end}}
</div>
{{end}}
</div>
<div class="view-actions">
<a href="/compose?to={{mailEmail .message.FromAddr}}&subject={{if .message.Subject}}Re: {{.message.Subject}}{{end}}" class="btn btn-primary">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align:-2px;margin-right:4px;"><polyline points="9 17 4 12 9 7"/><path d="M20 18v-2a4 4 0 0 0-4-4H4"/></svg>
回复
</a>
<form method="POST" action="/mail/delete/{{.message.ID}}" style="display:inline;"
onsubmit="return confirm('确定要删除这封邮件吗?');">
<button type="submit" class="btn btn-danger">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align:-2px;margin-right:4px;"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
删除邮件
</button>
</form>
</div>
</main>
</div>
</body>
</html>
+67
View File
@@ -0,0 +1,67 @@
package web
// P1 #3 回归测试:客户端 IP 不可通过 X-Forwarded-For 伪造。
// 外部直连时伪造头必须被忽略(防绕过登录封禁/恶意封禁他人),
// 本机回环(反向代理)转发时必须取 X-Forwarded-For 中的真实客户端 IP。
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// doLoginFailure 触发一次登录失败(使 BanStore 按 ClientIP 记录失败计数),
// 返回使用的请求。
func doLoginFailure(t *testing.T, ws *WebServer, remoteAddr, xff string) {
t.Helper()
form := strings.NewReader("email=nobody@example.com&password=wrong")
req := httptest.NewRequest(http.MethodPost, "/login", form)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.RemoteAddr = remoteAddr
if xff != "" {
req.Header.Set("X-Forwarded-For", xff)
}
w := httptest.NewRecorder()
ws.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK { // 登录失败重渲染登录页
t.Fatalf("login failure status = %d, want 200", w.Code)
}
}
func TestExternalClientIPCannotBeSpoofed(t *testing.T) {
ws, stores := newTestWebServer(t, "0123456789abcdef0123456789abcdef")
// 模拟外部攻击者直连 8080 端口,伪造 X-Forwarded-For
doLoginFailure(t, ws, "203.0.113.99:5555", "1.2.3.4")
// 失败计数必须记在真实来源 IP 上
if _, err := stores.Bans.GetByIP("1.2.3.4"); err == nil {
t.Fatal("spoofed X-Forwarded-For IP must not be recorded")
}
entry, err := stores.Bans.GetByIP("203.0.113.99")
if err != nil {
t.Fatalf("real client IP should be recorded: %v", err)
}
if entry.FailCount != 1 {
t.Fatalf("fail count = %d, want 1", entry.FailCount)
}
}
func TestLoopbackProxyXFFIsHonored(t *testing.T) {
ws, stores := newTestWebServer(t, "0123456789abcdef0123456789abcdef")
// 模拟本机 Caddy/Nginx 转发:RemoteAddr 是回环,XFF 是真实客户端
doLoginFailure(t, ws, "127.0.0.1:5555", "198.51.100.7")
entry, err := stores.Bans.GetByIP("198.51.100.7")
if err != nil {
t.Fatalf("proxied client IP should be recorded: %v", err)
}
if entry.FailCount != 1 {
t.Fatalf("fail count = %d, want 1", entry.FailCount)
}
if _, err := stores.Bans.GetByIP("127.0.0.1"); err == nil {
t.Fatal("proxy's own IP should not be recorded")
}
}
+154 -33
View File
@@ -12,9 +12,11 @@ import (
"net"
"os"
"path/filepath"
"sync"
"time"
"mail_go/config"
"mail_go/internal/connhub"
"mail_go/internal/db"
"mail_go/internal/imap_server"
"mail_go/internal/outbound"
@@ -22,6 +24,7 @@ import (
"mail_go/internal/smtp_server"
"mail_go/internal/storage"
"mail_go/internal/store"
"mail_go/internal/tlsutil"
"mail_go/internal/web"
"golang.org/x/crypto/bcrypt"
@@ -35,7 +38,7 @@ func applyDomainTLSConfig(stores *store.Stores, cfg *config.Config) {
applied := applyTLSCertPaths(cfg, domain.TlsCertPath, domain.TlsKeyPath)
if applied {
log.Printf("使用域名 %s 的 TLS 证书;更新证书后需重启服务生效", domain.Name)
log.Printf("使用域名 %s 的 TLS 证书;证书更新后自动热加载,无需重启服务", domain.Name)
}
}
@@ -59,6 +62,49 @@ func applyTLSCertPaths(cfg *config.Config, certPath, keyPath string) bool {
return applied
}
// tlsSource 返回证书路径来源:协议在 toml 中显式配置的证书优先;
// 否则取第一个启用 TLS 且有证书的域名(管理后台一键导入证书后自动
// 切换,无需重启)。结果缓存 10 秒,避免每次握手都查询数据库。
func tlsSource(explicitCert, explicitKey string, stores *store.Stores) tlsutil.Source {
var (
mu sync.Mutex
lastCheck time.Time
cachedCert string
cachedKey string
)
return func() (string, string) {
mu.Lock()
defer mu.Unlock()
if time.Since(lastCheck) < 10*time.Second {
return cachedCert, cachedKey
}
lastCheck = time.Now()
if explicitCert != "" && explicitKey != "" {
cachedCert, cachedKey = explicitCert, explicitKey
} else if d, err := stores.Domains.GetFirstTLSEnabledWithCert(); err == nil {
cachedCert, cachedKey = d.TlsCertPath, d.TlsKeyPath
} else {
cachedCert, cachedKey = "", ""
}
return cachedCert, cachedKey
}
}
// newTLSCertLoader 创建带热加载的 TLS 证书加载器(每次握手自动重载)。
// 初始路径取显式配置或启动时填充的路径;source 允许后续动态切换
// 证书来源。加载失败返回 nil,对应协议将不启用 TLS。
func newTLSCertLoader(explicitCert, explicitKey, initCert, initKey string, stores *store.Stores, proto string) *tlsutil.Loader {
if initCert == "" || initKey == "" {
initCert, initKey = explicitCert, explicitKey
}
loader, err := tlsutil.NewLoader(initCert, initKey, tlsSource(explicitCert, explicitKey, stores), log.Printf)
if err != nil {
log.Printf("%s TLS 证书初始化失败: %v(该协议将不启用 TLS)", proto, err)
return nil
}
return loader
}
func ensureSelfSignedTLSConfig(cfg *config.Config) {
if cfg.SMTP.TLSCert != "" && cfg.SMTP.TLSKey != "" && cfg.IMAP.TLSCert != "" && cfg.IMAP.TLSKey != "" && cfg.POP3.TLSCert != "" && cfg.POP3.TLSKey != "" {
return
@@ -168,9 +214,21 @@ func main() {
// 5. Initialize attachment storage
attStorage := storage.NewAttachmentStorage(cfg.Storage.AttachDir)
// 记录 toml 中显式配置的证书路径;此后 applyDomainTLSConfig 会用
// 域名证书填充空值,需要原始值来判断“显式配置优先”。
explicitSMTPCert, explicitSMTPKey := cfg.SMTP.TLSCert, cfg.SMTP.TLSKey
explicitIMAPCert, explicitIMAPKey := cfg.IMAP.TLSCert, cfg.IMAP.TLSKey
explicitPOP3Cert, explicitPOP3Key := cfg.POP3.TLSCert, cfg.POP3.TLSKey
applyDomainTLSConfig(stores, cfg)
ensureSelfSignedTLSConfig(cfg)
// 证书热加载器:每次 TLS 握手自动重载证书文件,证书更新后无需重启
smtpTLS := newTLSCertLoader(explicitSMTPCert, explicitSMTPKey, cfg.SMTP.TLSCert, cfg.SMTP.TLSKey, stores, "SMTP")
imapTLS := newTLSCertLoader(explicitIMAPCert, explicitIMAPKey, cfg.IMAP.TLSCert, cfg.IMAP.TLSKey, stores, "IMAP")
pop3TLS := newTLSCertLoader(explicitPOP3Cert, explicitPOP3Key, cfg.POP3.TLSCert, cfg.POP3.TLSKey, stores, "POP3")
// 6. Outbound delivery manager (external mail queue + worker)
outboundMgr := outbound.NewManager(cfg.Outbound, cfg.SMTP.Domain, stores)
if outboundMgr.Enabled() {
@@ -180,8 +238,27 @@ func main() {
fmt.Println("外发邮件投递未启用(outbound.max_per_day = 0")
}
// 7. Start SMTP server
smtpSrv := smtp_server.NewSMTPServer(cfg.SMTP, stores, attStorage, outboundMgr)
// 6. 连接注册中心(后台「当前连接」页 + IMAP 新邮件推送)
connHub := connhub.New()
// 7. Start IMAP server(先于 SMTP 创建,SMTP 投递成功时通知其推送)
imapSrv := imap_server.NewIMAPServer(cfg.IMAP, stores, imapTLS, cfg.Ban, connHub)
go func() {
if err := imapSrv.Start(); err != nil {
log.Printf("IMAP 服务启动失败: %v", err)
}
}()
// Start IMAPS if TLS is configured
if cfg.IMAP.TLSCert != "" && cfg.IMAP.TLSKey != "" {
go func() {
if err := imapSrv.StartTLS(); err != nil {
log.Printf("IMAPS 服务启动失败: %v", err)
}
}()
}
// 8. Start SMTP server(本地投递成功后触发 IMAP 新邮件推送)
smtpSrv := smtp_server.NewSMTPServer(cfg.SMTP, stores, attStorage, outboundMgr, smtpTLS, cfg.Ban, connHub, imapSrv)
go func() {
if err := smtpSrv.Start(); err != nil {
log.Printf("SMTP 服务启动失败: %v", err)
@@ -201,24 +278,8 @@ func main() {
}()
}
// 7. Start IMAP server
imapSrv := imap_server.NewIMAPServer(cfg.IMAP, stores)
go func() {
if err := imapSrv.Start(); err != nil {
log.Printf("IMAP 服务启动失败: %v", err)
}
}()
// Start IMAPS if TLS is configured
if cfg.IMAP.TLSCert != "" && cfg.IMAP.TLSKey != "" {
go func() {
if err := imapSrv.StartTLS(); err != nil {
log.Printf("IMAPS 服务启动失败: %v", err)
}
}()
}
// 8. Start POP3 server
pop3Srv := pop3_server.NewPOP3Server(cfg.POP3, stores)
// 9. Start POP3 server
pop3Srv := pop3_server.NewPOP3Server(cfg.POP3, stores, pop3TLS, cfg.Ban, connHub, imapSrv)
go func() {
if err := pop3Srv.Start(); err != nil {
log.Printf("POP3 服务启动失败: %v", err)
@@ -233,8 +294,11 @@ func main() {
}()
}
// 10. Start Web server
webServer := web.NewWebServer(cfg.Web, stores, attStorage, cfg.Storage, cfg.Auth, cfg.Ban, outboundMgr)
// 10. Start Web server(本地写信投递成功后同样触发 IMAP 新邮件推送)
webServer, err := web.NewWebServer(cfg.Web, stores, attStorage, cfg.Storage, cfg.Auth, cfg.Ban, cfg.Caddy, outboundMgr, connHub, imapSrv)
if err != nil {
log.Fatalf("Web 服务初始化失败: %v", err)
}
fmt.Printf("Web 服务启动在 %s\n", cfg.Web.Addr)
go func() {
if err := webServer.Start(); err != nil {
@@ -242,10 +306,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) {
@@ -274,8 +360,18 @@ func ensureAdminUser(stores *store.Stores, cfg *config.Config) {
fmt.Println("默认域名 example.com 创建成功")
}
// Hash the default admin password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
// 初始密码:优先取环境变量 MAILGO_ADMIN_PASSWORD
// 否则生成随机密码并打印一次(只能在本机启动日志中看到)。
// 无论哪种方式都会标记首次登录必须改密,杜绝默认口令。
adminPassword := os.Getenv("MAILGO_ADMIN_PASSWORD")
generated := false
if adminPassword == "" {
adminPassword = randomPassword()
generated = true
}
// Hash the admin password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(adminPassword), bcrypt.DefaultCost)
if err != nil {
log.Printf("密码哈希失败: %v", err)
return
@@ -283,13 +379,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,
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 {
@@ -297,5 +394,29 @@ func ensureAdminUser(stores *store.Stores, cfg *config.Config) {
return
}
fmt.Println("管理员账户 admin@example.com 创建成功(密码: admin")
if generated {
fmt.Printf("管理员账户 admin@example.com 创建成功,初始密码: %s\n", adminPassword)
} else {
fmt.Println("管理员账户 admin@example.com 创建成功(密码来自 MAILGO_ADMIN_PASSWORD")
}
fmt.Println("安全提示:该账户已被标记为“首次登录必须修改密码”,请登录后立即在 设置 页面修改。")
}
// randomPassword 生成 16 位随机密码(数字+大小写字母),用于初始管理员账户。
func randomPassword() string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
buf := make([]byte, 16)
if _, err := rand.Read(buf); err != nil {
// crypto/rand 失败极罕见;退化为时间种子以避免空密码
log.Printf("生成随机密码失败: %v,使用弱随机回退", err)
n := time.Now().UnixNano()
for i := range buf {
buf[i] = charset[(n>>(uint(i)*4))%int64(len(charset))]
}
return string(buf)
}
for i := range buf {
buf[i] = charset[int(buf[i])%len(charset)]
}
return string(buf)
}
+191
View File
@@ -0,0 +1,191 @@
# 安全漏洞修复 TODO
依据 2026-08-19 的安全审计结果(代码静态审计 + mail.lmve.net 线上验证)整理。
按优先级排列:P0 立即修复,P1 尽快修复,P2 排期修复,P3 加固项。
## P0 严重:可被完全接管
### 1. 会话签名密钥硬编码,可伪造任意管理员会话
- [x] 位置:`internal/web/server.go:176`
- 现状:`cookie.NewStore([]byte("mail-go-secret-key-change-in-production"))`,密钥写死在源码中且无配置项可更换。源码公开 = 任何人可签发 `userID=1, isAdmin=true` 的合法 cookie,直接以管理员身份进入线上后台。
- 修复方案:
- [x] `config.Config` 新增 `[web] secret_key` 字段。
- [x] `LoadConfig()` 首次启动时用 `crypto/rand` 生成 32 字节随机密钥(hex 编码)写入配置文件;配置文件权限收紧为 0600(兼顾原有的中继密码等敏感字段)。
- [x] 支持环境变量 `MAILGO_SECRET_KEY` 覆盖(覆盖值不落盘,便于容器部署)。
- [x] 启动时校验:密钥为空 / 等于旧硬编码默认值 / 短于 16 字节时拒绝启动(`config.ValidateSecretKey` + `NewWebServer` 返回 error)。
- 验证:
- [x] 重启后旧 cookie 全部失效(登录态被踢下线)。
- [x] 用旧硬编码密钥手工签发的 cookie 无法通过认证(`TestLegacyHardcodedKeyCannotForgeSession`:伪造 `userID=1, isAdmin=true` 的 cookie 被拒,302 回登录页)。
- [x] `config.LoadConfig()` 单测:首启生成、二次读取保持不变、旧配置补全、旧默认值替换、env 覆盖不落盘、文件权限 0600(`config/config_test.go`)。
- 已完成(2026-08-19)。注:部署新版后所有用户需重新登录;配置文件权限由 0644 收紧为 0600。
## P1 高危
### 2. OAuth2 state 固定值且回调不校验(登录 CSRF / 授权码注入)
- [x] 位置:`internal/web/handlers/auth.go`(原硬编码 `mailgo_oauth2_state``OAuth2Callback` 不校验 state
- 现状:当前部署未启用 OAuth2,属休眠漏洞,启用前必须修复。
- 修复方案:
- [x] `OAuth2Start``crypto/rand` 生成 16 字节随机 state,写入独立的短期 SameSite=Lax cookie`mail_go_oauth2_state`10 分钟过期,HttpOnly+Secure)。注意主会话 cookie 是 SameSite=Strict,跨站回调导航不会携带,故不能放主会话。
- [x] `OAuth2Callback` 读取 `c.Query("state")` 与 cookie 值做 `subtle.ConstantTimeCompare` 比对,缺失/不匹配返回 403。
- [x] 比对后立即清除 cookie`MaxAge=-1`),保证一次性使用。
- 验证:
- [x] 单测:state 缺失/不匹配/无 cookie 均 403start 设置的 cookie 与 URL state 一致且每次不同;有效 state 通过校验进入后续流程(`oauth2_state_test.go`)。
- [ ] 手工走完一次 OAuth2 流程(Google/GitHub)确认正常登录。
### 3. Gin 信任所有代理,`ClientIP()` 可伪造(封禁绕过 / 爆破)
- [x] 位置:`internal/web/server.go`(未调用 `SetTrustedProxies`
- 现状:gin 默认信任 0.0.0.0/0`X-Forwarded-For` 可任意伪造。线上 8080 端口当前被防火墙挡住,属纵深防御缺失;一旦 8080/socket 可达:伪造不同 IP 即可绕过登录失败封禁无限爆破,也可恶意封禁任意 IP 造成 DoS。
- 修复方案:
- [x] 统一 `engine.SetTrustedProxies([]string{"127.0.0.1", "::1"})`:外部直连时 XFF 完全不可信(防伪造/防封禁污染);本机 Caddy/Nginx 转发时 XFF 仍可信(保留真实客户端 IP)。注意 gin 对 Unix socket 监听无条件信任转发头,socket 必须保持仅本机可达。
- [ ] install.sh 文档注明:8080 端口必须保持仅本机可达(防火墙/绑定 127.0.0.1)。
- 验证:
- [x] 单测:外部直连 + 伪造 `X-Forwarded-For` 时封禁记录落在真实 IP 上;回环代理 + XFF 时记录 XFF 中的真实客户端 IP(`trustedproxy_test.go`)。
- [ ] 线上回归:Caddy 反代路径下管理后台封禁列表仍显示真实客户端 IP。
### 4. Web 写信 CRLF 邮件头注入
- [x] 位置:`internal/web/handlers/mail.go`(原 `to`/`cc`/`subject` 直接拼头、附件文件名拼进 `Content-Disposition`/`Content-Type`
- 现状:信封收件人经 `ParseAddress` 校验无法注入,但注入的头(如 `Reply-To`)会随邮件存储并外发,可被用于钓鱼。
- 修复方案:
- [x] 新增 `sanitizeHeaderField`strip `\r``\n`、NUL,应用于 From/To/Cc 头。
- [x] `subject``sanitizeHeaderField` + RFC 2047`mime.QEncoding`)编码非 ASCII 内容。
- [x] 附件名经 `mime.FormatMediaType` 生成 `Content-Disposition`/`Content-Type name` 参数(RFC 2231 编码,中和 CRLF 注入)。
- [x] 消息构建抽出为 `buildOutgoingMessage` 纯函数(可单测);`DownloadAttachment`/`AdminDownloadAttachment` 的响应头同步改用 `formatContentDisposition`
- 验证:
- [x] 单测:`to`/`cc`/`subject` 携带 CRLF 注入载荷时 RawData 无独立注入头;文件名含 CRLF/引号时头结构完好;非 ASCII 主题正确编码(`mail_injection_test.go`)。
- [ ] 含特殊字符附件名的邮件实测收发正常。
## P2 中危
### 5. 会话 Cookie 缺 Secure 标志
- [x] 位置:`internal/web/server.go`
- 修复方案:
- [x] `sessions.Options` 增加 `Secure: cfg.CookieSecure`;新增配置项 `[web].cookie_secure`(默认 true,仅本地 HTTP 调试时改 false;缺失字段按默认 true 处理,参照 relay_starttls 的原始文件检查)。
- [x] 修正 SameSite 注释(3 = Strict)。
- [x] 测试:会话 cookie 断言 HttpOnly+Secure+SameSite=Strict。
- 验证:
- [x] 测试断言 cookie 标志。
- [ ] 线上登录后检查 `Set-Cookie` 包含 `Secure; HttpOnly; SameSite=Strict`
### 6. SMTP/IMAP/POP3 认证无速率限制
- [x] 位置:`internal/smtp_server/server.go``internal/imap_server/``internal/pop3_server/server.go`
- 修复方案:
- [x] `store.RecordAuthFailure(ip, maxFail, minutes)`:认证失败计数复用 BanStore,达到 `ban.max_fail_attempts` 阈值即封禁 `ban.ban_duration_min` 分钟(与 Web 登录共用封禁记录)。
- [x] SMTP`NewSession` 记录 `c.Conn().RemoteAddr()` 提取 IP;Auth 回调失败计数 + 封禁 IP 拒绝认证。
- [x] IMAP`Login(connInfo,...)``connInfo.RemoteAddr` 取 IP;失败计数 + 封禁拒绝。
- [x] POP3`handleConn` 开头检查封禁直接拒绝;`handlePASS` 失败计数。
- [x] 三个服务器构造函数注入 `config.BanConfig`
- 验证:
- [x] store 层单测:达到阈值封禁、空 IP 无副作用、与 Web 共用封禁记录(`auth_guard_test.go`)。
- [ ] 线上用错误密码连续尝试触发封禁后,SMTP/IMAP/POP3 认证被拒。
### 7. 附件存储路径遍历防护无效
- [x] 位置:`internal/storage/attachment.go`
- 修复方案:
- [x] `FullPath` 改为白名单校验(UUID 文件名正则),非法路径返回错误;兜底校验最终路径仍在 baseDir 内。
- [x] `Save` 的扩展名白名单化(`safeExt`,丢弃 CR/LF、路径分隔符等)。
- 验证:
- [x] 单测:`../`、绝对路径、Windows 分隔符、空路径、注入文件名全部拒绝;合法文件名正常读写删(`attachment_test.go`)。
### 8. 默认管理员 admin@example.com/admin
- [x] 位置:`main.go``ensureAdminUser`
- 修复方案:
- [x] 初始密码改为:环境变量 `MAILGO_ADMIN_PASSWORD` 显式指定,否则生成 16 位随机密码打印一次。
- [x] User 模型新增 `MustChangePassword`:初始管理员、管理员重置密码的用户在登录后强制跳转设置页改密,改密后清除标记(`UpdatePassword` 顺带清除)。
- [x] AuthMiddleware 拦截(除 /settings、/logout),settings 页显示提示横幅。
- 验证:
- [x] 全新数据库启动后 admin/admin 无法登录(密码为随机值);登录后强制改密流程生效。
- [ ] 线上验证新装机流程。
### 9. Smarthost 中继 TLS 不验证证书(凭据可被 MITM 截获)
- [x] 位置:`internal/outbound/mailer.go`
- 修复方案:
- [x] 直投 MX 保持机会式 TLS`InsecureSkipVerify=true`,业界常规);relay 路径默认验证证书(`InsecureSkipVerify=false`),IP literal 时以 IP 作为 ServerName 校验 IP SAN。
- [x] 新增配置 `outbound.relay_tls_insecure`(默认 false),供自签证书内网中继显式放行。
- 验证:
- [x] 集成测试:自签证书 STARTTLS 中继默认握手失败(certificate 错误)、开启开关后完整 SMTP 流程成功(`mailer_test.go` 两个新测试)。
### 10. 缺安全响应头(点击劫持/降级风险)
- [x] 位置:`internal/web/middleware/security.go`(新中间件,全局注册)
- 修复方案:
- [x] `Strict-Transport-Security: max-age=31536000; includeSubDomains`
- [x] `X-Frame-Options: DENY` + CSP `frame-ancestors 'none'`(点击劫持)
- [x] `X-Content-Type-Options: nosniff`
- [x] `Referrer-Policy: strict-origin-when-cross-origin`
- [x] 基础 CSP`default-src 'self'` + 放宽项(内联脚本/样式必须 unsafe-inline`img-src https:` 允许邮件远程图片;`connect-src 'self'`/`form-action 'self'` 防数据外泄)。CSP 具体策略在 `security.go` 顶部注释说明。
- 验证:
- [x] 单测:5 个头均存在,关键值抽查(`security_test.go`)。
- [ ] 线上回归:登录/收件箱/管理页功能不受 CSP 影响;邮件远程图片正常加载。
### 11. LDAP/OAuth 错误信息泄露与用户枚举
- [x] 位置:`internal/web/handlers/auth.go`
- 修复方案:
- [x] 错误提示统一为通用文案("LDAP 认证失败…"、"LDAP 账号未接入本系统…"、"OAuth2 认证失败…"),原始 err 只写日志,不回显页面;不再在提示中回显用户邮箱。
- 验证:
- [x] 现有 OAuth2 测试仍通过(错误页文案不含内部细节)。
- [ ] 线上(启用 LDAP/OAuth 后)验证失败页面不含内部地址/DN/原始错误串。
## P3 低危 / 加固
### 12. Referer 开放重定向
- [x] 位置:`internal/web/handlers/mail.go`Delete/MarkRead
- [x] 修复:新增 `safeRedirectPath`——仅接受以 `/` 开头且非 `//` 的同站相对路径,外部 URL/协议跳转一律回退 `/inbox`
- 验证:
- [x] 单测:`https://evil.com/``//evil.com``javascript:` 等拒绝,相对路径放行(`TestSafeRedirectPath`)。
### 13. Web 发信配额检查 TOCTOU
- [x] 位置:`internal/web/handlers/mail.go``internal/store/user_store.go`
- [x] 修复:新增 `UserStore.TryReserveQuota`(单条原子 SQL `UPDATE ... WHERE used_bytes + ? <= quota_bytes`),DoSend 先原子预扣全部附件大小,超配额即拒发;后续读取/保存/落库失败的附件按大小补偿回退。
- 验证:
- [x] 单测:预扣到配额上限、超额拒绝且不部分扣费、释放后可再扣、非正 delta 拒绝(`TestTryReserveQuota*`)。
### 14. compose 页 safeJS 在 JS 上下文绕过转义(自 XSS)
- [x] 位置:`internal/web/templates/compose.html``internal/web/server.go`
- [x] 修复:新增 `jsonify` 模板函数(`json.Marshal`,默认转义 `< > &``\u003c` 等,无法逃出 `</script>`);`quill.root.innerHTML` 改用 `jsonify`。**移除** `templateFuncs` 中危险的 `safeHTML`/`safeJS`view/admin 模板的 `srcdoc` 改回默认属性转义(行为一致,前已实测)。
- 验证:
- [x] 单测:`</script>` 载荷不产生裸逃逸、控制字符转义、输出为合法字符串字面量(`TestJsonifyEscapesScriptBreakout`);全模板渲染测试通过。
### 15. Content-Disposition 文件名未编码
- [x] 位置:`internal/web/handlers/mail.go``internal/web/handlers/admin.go`
- [x] 修复:随 P1 #4 一并完成——`formatContentDisposition` 使用 `mime.FormatMediaType`(RFC 2231),两处下载端点均已应用,并有 `TestFormatContentDisposition` 覆盖。
### 16. 会话治理
- [x] 位置:`internal/web/handlers/auth.go``internal/web/middleware/auth.go`
- [x] 修复:登录成功(Web/LDAP/OAuth2 三处)先 `session.Clear()` 清旧状态再写入;会话记录 `loginAt`AuthMiddleware 实施**绝对过期 7 天**(超时强制登出)+ **滑动续期**(活跃会话每 12 小时写回刷新)。
- 验证:
- [x] 单测:8 天前的会话被重定向登录页;1 小时前的会话正常访问(用配置密钥签名构造会话,`TestSessionAbsoluteExpiryForcesRelogin`/`TestSessionWithinExpiryWorks`)。
## 已确认安全、无需改动
- bcrypt 密码哈希;GORM 全参数化查询(无 SQL 注入)。
- SMTP 非开放中继、认证用户强制 From=登录身份。
- LDAP 过滤器已 `EscapeFilter`
- 邮件 HTML 经 sandbox iframe(无 `allow-scripts`)渲染,`srcdoc` 属性转义经实测有效,无存储型 XSS。
- Web 登录错误提示不区分用户是否存在(无枚举)。
## 修复顺序建议
1. ~~#1P0~~ 已完成 2026-08-19
2. ~~#2、#3、#4P1~~ 已完成 2026-08-19
3. ~~#5-#11P2~~ 已完成 2026-08-19
4. ~~#12-#16P3~~ 已完成 2026-08-19
**全部安全审计项已修复完成。** 剩余建议(非代码项):
- 部署侧:Caddy 加固(可选,应用层已加安全头)、8080 端口保持仅本机可达、GitHub 仓库中 3 个 50MB+ 的 exe 文件建议改用 LFS 或删除
- 线上验证:部署新版后检查登录/收件箱/管理页、协议认证封禁、邮件远程图片加载(CSP 影响)