feat: 分级拦截日志 + 两类日志文件;成绩录入防呆/解析/宽度修复
## 日志(用户要求:分级 + 每个拦截都要有指出代码行的调试日志 + 两类文件)
- 新增 internal/logging:拦截点捕获(file:line + 函数名,相对路径)、两类文件轮转、
按级别/类别分流。httpapi 与 middleware 各自薄封装,无循环依赖。
- 两类目录(数据目录下):
operations/ 放行类(INFO「操作已放行」+ 常规运行日志,按 log.level)
interceptions/ 拦截类(WARN 中文原因 + 拦截点 + method/path/actor/role/requestId、
DEBUG 上下文、ERROR 服务端异常)—— **固定 debug 采集**,
否则默认 info 下"每次拦截的调试日志"根本看不见
- 命名 operations-YYYYMMDD[.N].log,按天+按大小轮转,每类保留 keepFiles 份;写盘失败
降级只写控制台且只 Warn 一次。
- 所有业务错误统一走 newAPIError 捕获拦截点(11 处内联 &apiError{} 已收进构造函数,
防止绕过);中间件层拦截(401/403/CSRF/未改初始密码/429)也留痕,响应体与状态码一字未变。
- config.yaml 的 log 段新增 dir / maxSizeMB / keepFiles(留空即默认)。
- 服务端包数 12 → 13(新增 internal/logging)。
## 成绩录入(用户实机踩坑)
- 真因一:成绩格 placeholder 是光秃秃的 `1:06.33`,而全仓库没有任何 ::placeholder 样式,
示例文本与真实输入长得一样 → 用户把灰字当成已填内容,那 13 格其实是空的。
- 真因二:2 条报错是秒位 ≥ 60(1:70)或全角冒号(1:06);1:06 与 1:06.00 在解析器里
本来就等价(都 = 66),格式不是问题。
- 真因三:成绩格没有宽度定义(旁边名次 72px、状态 110px 都有),被挤到第 4 位数字看不见。
修复:parseSecondsDetail 区分 empty/seconds-overflow/unrecognized;新增 entryGuards.ts
纯函数(录入页与审核页共用),报错**打出那一格的实际内容**并说明原因;解析器新认全角
冒号/全角数字/1分06秒 等写法;成绩格 min-width 112px(按 fontTools 实测文本宽度定);
示例文本改 `例 1:06.33` 等并给录入表格内的 ::placeholder 加淡色斜体;新增
onBeforeRouteLeave 离开守卫(原来切菜单会静默丢未保存成绩)。
测试:服务端 13 包全绿(372 个测试函数);前端 vue-tsc 通过、205 例全过;Console 18 文件。
docs/ 与 devdocs/ 同步(FAQ 到 Q76,部署指南新增第 10 节「服务端日志与拦截复盘」)。
This commit is contained in:
1 parent
8e707e1e9b
commit
e5e0944d26
32 files changed
+2017
-138
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
@@ -88,7 +89,29 @@ func (a *Auditor) flush(ctx context.Context, r *http.Request, entries []AuditEnt
|
||||
if err != nil {
|
||||
// 审计失败不能吞掉业务结果,但必须留下痕迹。
|
||||
a.log.Error("写入审计失败", "err", err, "action", e.Action, "requestId", requestID)
|
||||
continue
|
||||
}
|
||||
// 落库成功即「这次操作被放行」:默认 info 级别下这是唯一的放行痕迹,
|
||||
// 与拦截的 WARN 成对,现场才能对上「谁在什么时候做了什么」。
|
||||
a.log.Info("操作已放行",
|
||||
"action", e.Action,
|
||||
"entity", e.EntityType,
|
||||
"entityId", e.EntityID,
|
||||
"actor", auditActorLabel(u, e.ActorID),
|
||||
"requestId", requestID)
|
||||
}
|
||||
}
|
||||
|
||||
// auditActorLabel 是放行日志里的操作人:登录请求的身份来自条目自带的 ActorID,
|
||||
// 其余来自会话;两边都拿不到就是 guest。记「用户名#id」比裸 id 好认。
|
||||
func auditActorLabel(u *auth.User, actorID int64) string {
|
||||
switch {
|
||||
case actorID != 0:
|
||||
return fmt.Sprintf("user#%d", actorID)
|
||||
case u != nil && u.ID != 0:
|
||||
return fmt.Sprintf("%s#%d", u.Username, u.ID)
|
||||
default:
|
||||
return "guest"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.yeij.top/AskaEth/Scoreboard/server/internal/auth"
|
||||
"git.yeij.top/AskaEth/Scoreboard/server/internal/logging"
|
||||
)
|
||||
|
||||
// HeaderRequestID 是客户端可复用的幂等/追踪请求头。
|
||||
@@ -123,11 +125,12 @@ func Authenticate(svc *auth.Service, logger *slog.Logger) func(http.Handler) htt
|
||||
|
||||
// RequireRole 要求至少 need 权限;未登录或权限不足一律 403。
|
||||
// 用于「guest 也不该看」的只读接口(系统配置),以便与未登录写操作区分。
|
||||
func RequireRole(need auth.Role) func(http.Handler) http.Handler {
|
||||
func RequireRole(logger *slog.Logger, need auth.Role) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !User(r.Context()).Role.Allows(need) {
|
||||
writeError(w, http.StatusForbidden, "FORBIDDEN", "无权访问该资源", map[string]any{"required": need.String()})
|
||||
intercept(logger, w, r, http.StatusForbidden, "FORBIDDEN", "无权访问该资源",
|
||||
map[string]any{"required": need.String()})
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
@@ -136,16 +139,17 @@ func RequireRole(need auth.Role) func(http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
// RequireWrite 要求至少 need 权限的写操作;guest 返回 401(需登录),角色不足返回 403。
|
||||
func RequireWrite(need auth.Role) func(http.Handler) http.Handler {
|
||||
func RequireWrite(logger *slog.Logger, need auth.Role) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
u := User(r.Context())
|
||||
if u.Role == auth.RoleGuest {
|
||||
writeError(w, http.StatusUnauthorized, "AUTH_REQUIRED", "请先登录", nil)
|
||||
intercept(logger, w, r, http.StatusUnauthorized, "AUTH_REQUIRED", "请先登录", nil)
|
||||
return
|
||||
}
|
||||
if !u.Role.Allows(need) {
|
||||
writeError(w, http.StatusForbidden, "FORBIDDEN", "无权执行该操作", map[string]any{"required": need.String()})
|
||||
intercept(logger, w, r, http.StatusForbidden, "FORBIDDEN", "无权执行该操作",
|
||||
map[string]any{"required": need.String()})
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
@@ -155,7 +159,7 @@ func RequireWrite(need auth.Role) func(http.Handler) http.Handler {
|
||||
|
||||
// MustChangePassword 阻止「必须修改密码」的账号执行写操作。
|
||||
// 只放行改密与登出:否则用户会被永久锁在无法改密的死循环里(见 devdocs/08 第 1 节)。
|
||||
func MustChangePassword(next http.Handler) http.Handler {
|
||||
func MustChangePassword(logger *slog.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
u := User(r.Context())
|
||||
if u.Role == auth.RoleGuest || !u.MustChangePassword {
|
||||
@@ -172,12 +176,12 @@ func MustChangePassword(next http.Handler) http.Handler {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusForbidden, "AUTH_REQUIRED", "请先修改初始密码", nil)
|
||||
intercept(logger, w, r, http.StatusForbidden, "AUTH_REQUIRED", "请先修改初始密码", nil)
|
||||
})
|
||||
}
|
||||
|
||||
// CSRF 校验双提交 Cookie:已登录的写请求必须带 X-CSRF-Token 且与 csrf_token Cookie 一致。
|
||||
func CSRF(next http.Handler) http.Handler {
|
||||
func CSRF(logger *slog.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet, http.MethodHead, http.MethodOptions:
|
||||
@@ -187,7 +191,7 @@ func CSRF(next http.Handler) http.Handler {
|
||||
// 登录是 guest 请求,拿不到 CSRF Token;跨站表单仍能把受害者登进攻击者的账号,
|
||||
// 因此对带 Origin/Referer 的登录请求单独做同源校验。
|
||||
if r.Method == http.MethodPost && r.URL.Path == "/api/auth/login" && !sameOrigin(r) {
|
||||
writeError(w, http.StatusForbidden, "FORBIDDEN", "登录请求来源校验失败", nil)
|
||||
intercept(logger, w, r, http.StatusForbidden, "FORBIDDEN", "登录请求来源校验失败,请刷新页面重试", nil)
|
||||
return
|
||||
}
|
||||
if User(r.Context()).Role == auth.RoleGuest {
|
||||
@@ -200,7 +204,7 @@ func CSRF(next http.Handler) http.Handler {
|
||||
cookie = c.Value
|
||||
}
|
||||
if !auth.ValidCSRF(r.Header.Get("X-CSRF-Token"), cookie) {
|
||||
writeError(w, http.StatusForbidden, "FORBIDDEN", "CSRF 校验失败", nil)
|
||||
intercept(logger, w, r, http.StatusForbidden, "FORBIDDEN", "CSRF 校验失败,请刷新页面重试", nil)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
@@ -225,7 +229,7 @@ func sameOrigin(r *http.Request) bool {
|
||||
}
|
||||
|
||||
// RateLimitWrite 对已登录用户的写操作限流(默认 30 次/分钟/用户)。
|
||||
func RateLimitWrite(svc *auth.Service) func(http.Handler) http.Handler {
|
||||
func RateLimitWrite(svc *auth.Service, logger *slog.Logger) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
@@ -235,7 +239,7 @@ func RateLimitWrite(svc *auth.Service) func(http.Handler) http.Handler {
|
||||
}
|
||||
u := User(r.Context())
|
||||
if u.Role != auth.RoleGuest && !svc.AllowWrite(u.ID) {
|
||||
writeError(w, http.StatusTooManyRequests, "RATE_LIMITED", "操作过于频繁,请稍后再试", nil)
|
||||
intercept(logger, w, r, http.StatusTooManyRequests, "RATE_LIMITED", "操作过于频繁,请稍后再试", nil)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
@@ -283,6 +287,43 @@ func (w *statusWriter) Flush() {
|
||||
}
|
||||
|
||||
// writeError 输出统一错误结构;中间件不依赖 httpapi,避免包循环。
|
||||
// intercept 记录一次中间件层的拦截,再写出统一响应。
|
||||
// 这几个中间件都在 handler 之前就返回响应,不经 httpapi 的 writeAPIError——
|
||||
// 不在这里记的话,这些拦截完全没有痕迹。收成一个函数是为了「只要写了响应就一定有日志」。
|
||||
// 与 httpapi 的拦截日志同一形状:WARN 给原因与拦截点,DEBUG 补上下文,两类都进 interceptions/。
|
||||
func intercept(logger *slog.Logger, w http.ResponseWriter, r *http.Request, status int, code, message string, detail any) {
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
u := User(r.Context())
|
||||
actor := actorLabel(u)
|
||||
role := u.Role.String()
|
||||
reqID := GetRequestID(r.Context())
|
||||
// skip=2:0 是本包 Origin 自己的帧,1 是 intercept,2 才是做校验的那个中间件函数。
|
||||
origin := logging.Origin(2)
|
||||
|
||||
logger.Warn("请求被拦截",
|
||||
"reason", message, "origin", origin, "code", code,
|
||||
"method", r.Method, "path", r.URL.Path,
|
||||
"actor", actor, "role", role, "requestId", reqID, "ip", ClientIP(r),
|
||||
logging.ChannelKey, logging.ChannelInterception)
|
||||
logger.Debug("拦截上下文",
|
||||
"code", code, "detail", detail, "origin", origin,
|
||||
"method", r.Method, "path", r.URL.Path, "ip", ClientIP(r),
|
||||
"actor", actor, "role", role, "requestId", reqID,
|
||||
logging.ChannelKey, logging.ChannelInterception)
|
||||
|
||||
writeError(w, status, code, message, detail)
|
||||
}
|
||||
|
||||
// actorLabel 是日志里的操作人:未登录记 guest,登录后记「用户名#id」,比裸 id 好认。
|
||||
func actorLabel(u *auth.User) string {
|
||||
if u == nil || u.ID == 0 {
|
||||
return "guest"
|
||||
}
|
||||
return fmt.Sprintf("%s#%d", u.Username, u.ID)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, code, message string, detail any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
|
||||
@@ -51,7 +51,7 @@ func TestRequestIDKeepsNormalHeader(t *testing.T) {
|
||||
// 登录是 guest 请求,CSRF Token 校验被跳过,靠同源校验挡住 login CSRF(P2-6)。
|
||||
func TestCSRFBlocksCrossOriginLogin(t *testing.T) {
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })
|
||||
h := CSRF(next)
|
||||
h := CSRF(nil, next)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/auth/login", nil)
|
||||
req.Host = "scoreboard.local"
|
||||
|
||||
Reference in new issue
Block a user