## P0:双通道项目重复发布跳分(45→85→收敛) results.rank 一列同时充当「个人名次输出」「手工名次输入」「榜单数据源」, 团体手工通道把个人名次当团体名次又按团体表发一遍分。 - 新增 channel_ranks(每个计分通道各自持久化名次,发布时全量重建) 与 channel_manual_ranks(按单位录入的团体手工名次,故意不挂通道外键, 避免规则包重导时级联删掉录入数据) - results.rank 语义收窄为「只表示个人名次」,只由 scope=athlete 通道写回; 团体手工名次的来源改为 TeamRanks 优先、团体报名项兜底, 个人报名项的名次绝不进团体通道 - writeLedgerTx 内重建 channel_ranks;buildRanked / export.loadRanked / realtime.loadRanked 三份同源 SQL 一起改读新表 - 回归:发布两次流水与名次逐字节一致(domain + httpapi 各一条,含变异验证) ## 奖牌结算开关与奖牌通道(P1(5),新功能) - events.award_medals + 通道 count_to_medals / count_to_team_medals (迁移默认 1,升级不改变既有榜单) - 单位榜入选 = 小项结算奖牌 && 通道发奖牌 && 前三名 &&(团体项目 || 个人奖牌并入单位) - 个人榜只取个人通道;金牌榜与积分榜同源同数据、按通道过滤独立计算 - 前端:小项「该项启用奖牌结算」、通道「发奖牌/个人奖牌并入单位金牌榜」、 录入页新增「团体名次」区块(含新接口 GET/PUT .../team-ranks) ## 身份口径改回证件号(P1(15),推翻第三十六轮) - 匹配顺序恢复 证件号 → 号码布 → 姓名+单位;新增 athleteByIDCard - 同表重复证件号 = 同一人报多个组别(notices 提示,不报错); 只有「同证件号 + 同小项同组别」才是真重复(conflicts[].duplicate) - 补齐后端缺失但前端/文档已在依赖的三处契约 ## 其余 P1 - 安全:会话撤销断开 ws、实时层按角色裁剪、整库快照限流(含 hello 路径, 被限流的请求挂起并由心跳兜底补发,不丢刷新) - 错误分级:手填名次非正数 / gender / 组别年龄 / 导出不存在的小项与单位 / 积分模板 awardRanks → 400 或 404,不再 500 - 算分一致性:破纪录加分看得失状态;批量退赛·恢复·移出、作废成绩、 报名导入改状态一律无条件重算推榜(不受延迟结算约束,与实时模式逐字节等价) - 配置改动触发重算:通道 / 小项参数 / 积分模板 - 升级自愈:channel_ranks 空而已有已发布成绩时,启动时一次性全量重算 (服务对外监听之前完成,避免升级后奖牌榜全 0) - 示例数据模式下破坏性操作被阻断,不再假成功 - Console:修复「只剩一块屏」后屏幕监听器被永久摘掉 - 大屏模板引擎:嵌套深度与体积安全阀,任何输入都不抛异常 ## 文档 - docs/:使用手册(奖牌开关、奖牌通道、团体名次、枚举列留空口径)、 常见问题(奖牌排查、升级后奖牌榜为 0、历史重放已知限制)、 部署指南、上线检查清单 - 版本 dev_0.1.7 → dev_0.1.8(console/package.json 同步) 自检:服务端 13 包 ×2 全绿;前端 vue-tsc + 449 例 ×2 + pnpm build; Console 35 例 + 语法检查。
146 lines
3.9 KiB
Go
146 lines
3.9 KiB
Go
// Package middleware 提供请求日志、错误恢复、认证/授权与审计等 HTTP 中间件。
|
||
package middleware
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"net"
|
||
"net/http"
|
||
"strings"
|
||
"sync/atomic"
|
||
|
||
"git.yeij.top/AskaEth/Scoreboard/server/internal/auth"
|
||
)
|
||
|
||
type ctxKey int
|
||
|
||
const (
|
||
ctxKeyUser ctxKey = iota
|
||
ctxKeyRequestID
|
||
ctxKeyAudit
|
||
ctxKeySession
|
||
)
|
||
|
||
// User 返回当前请求的身份;未经 Authenticate 中间件时是 guest。
|
||
func User(ctx context.Context) *auth.User {
|
||
if u, ok := ctx.Value(ctxKeyUser).(*auth.User); ok {
|
||
return u
|
||
}
|
||
return &auth.User{Role: auth.RoleGuest}
|
||
}
|
||
|
||
// WithUser 把身份写入 context,供测试或非标准链路使用。
|
||
func WithUser(ctx context.Context, u *auth.User) context.Context {
|
||
return context.WithValue(ctx, ctxKeyUser, u)
|
||
}
|
||
|
||
// Session 返回当前请求背后的会话;未登录或未经 Authenticate 中间件时为 nil。
|
||
//
|
||
// /ws 这类长连接要拿它做会话复查:握手时身份固化,会话被撤销后必须能对应到具体这一条连接。
|
||
func Session(ctx context.Context) *auth.Session {
|
||
if s, ok := ctx.Value(ctxKeySession).(*auth.Session); ok {
|
||
return s
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// WithSession 把会话写入 context,供测试或非标准链路使用;nil 表示没有会话。
|
||
func WithSession(ctx context.Context, s *auth.Session) context.Context {
|
||
if s == nil {
|
||
return ctx
|
||
}
|
||
return context.WithValue(ctx, ctxKeySession, s)
|
||
}
|
||
|
||
// GetRequestID 返回本次请求的 request_id。
|
||
func GetRequestID(ctx context.Context) string {
|
||
if v, ok := ctx.Value(ctxKeyRequestID).(string); ok {
|
||
return v
|
||
}
|
||
return ""
|
||
}
|
||
|
||
// trustedProxies 是可信代理网段白名单;默认空,表示任何代理头都不可信。
|
||
var trustedProxies atomic.Pointer[[]*net.IPNet]
|
||
|
||
// SetTrustedProxies 配置可信代理(IP 或 CIDR 列表);传空表示不信任任何代理头。
|
||
func SetTrustedProxies(entries []string) error {
|
||
nets := make([]*net.IPNet, 0, len(entries))
|
||
for _, raw := range entries {
|
||
entry := strings.TrimSpace(raw)
|
||
if entry == "" {
|
||
continue
|
||
}
|
||
if strings.Contains(entry, "/") {
|
||
_, n, err := net.ParseCIDR(entry)
|
||
if err != nil {
|
||
return fmt.Errorf("middleware: 非法可信代理 %q: %w", entry, err)
|
||
}
|
||
nets = append(nets, n)
|
||
continue
|
||
}
|
||
ip := net.ParseIP(entry)
|
||
if ip == nil {
|
||
return fmt.Errorf("middleware: 非法可信代理 %q", entry)
|
||
}
|
||
bits := 128
|
||
if ip.To4() != nil {
|
||
bits = 32
|
||
}
|
||
nets = append(nets, &net.IPNet{IP: ip, Mask: net.CIDRMask(bits, bits)})
|
||
}
|
||
trustedProxies.Store(&nets)
|
||
return nil
|
||
}
|
||
|
||
func isTrustedProxy(ip net.IP) bool {
|
||
nets := trustedProxies.Load()
|
||
if nets == nil || ip == nil {
|
||
return false
|
||
}
|
||
for _, n := range *nets {
|
||
if n.Contains(ip) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func remoteHost(addr string) string {
|
||
host, _, err := net.SplitHostPort(addr)
|
||
if err != nil {
|
||
return addr
|
||
}
|
||
return host
|
||
}
|
||
|
||
// ClientIP 解析客户端 IP。
|
||
// 只有当直连对端(RemoteAddr)属于可信代理白名单时才采信 X-Forwarded-For / X-Real-IP,
|
||
// 否则一律用 RemoteAddr——代理头是客户端可伪造的,据此限流等于没有限流。
|
||
func ClientIP(r *http.Request) string {
|
||
remote := net.ParseIP(remoteHost(r.RemoteAddr))
|
||
if remote == nil || !isTrustedProxy(remote) {
|
||
return remoteHost(r.RemoteAddr)
|
||
}
|
||
|
||
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
||
parts := strings.Split(xff, ",")
|
||
// 从右往左取第一个非代理地址:这是可信代理链上离服务端最近的真实客户端。
|
||
for i := len(parts) - 1; i >= 0; i-- {
|
||
ip := net.ParseIP(strings.TrimSpace(parts[i]))
|
||
if ip == nil || isTrustedProxy(ip) {
|
||
continue
|
||
}
|
||
return ip.String()
|
||
}
|
||
// 整条链路都是可信代理时,最左侧即原始客户端。
|
||
if ip := net.ParseIP(strings.TrimSpace(parts[0])); ip != nil {
|
||
return ip.String()
|
||
}
|
||
}
|
||
if ip := net.ParseIP(strings.TrimSpace(r.Header.Get("X-Real-IP"))); ip != nil {
|
||
return ip.String()
|
||
}
|
||
return remoteHost(r.RemoteAddr)
|
||
}
|