xfy 351f477822 feat(middleware): 增强 rewrite 安全性,添加 ReDoS 保护
- 新增 validateRegexSafety 检测危险正则模式
- 防止嵌套量词导致的灾难性回溯攻击
- 限制正则模式长度 (max 1000 chars)
- 补充 compression 和 accesslog 文档注释

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-03 16:57:40 +08:00

55 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package accesslog 提供访问日志中间件,记录每个请求的详细信息。
//
// 该文件包含访问日志相关的核心逻辑,包括:
// - 请求方法和路径记录
// - 响应状态码和大小记录
// - 请求处理耗时记录
//
// 使用示例:
//
// accessLog := accesslog.New(cfg.Logging)
// chain := middleware.NewChain(accessLog)
//
// 作者xfy
package accesslog
import (
"time"
"github.com/valyala/fasthttp"
"rua.plus/lolly/internal/config"
"rua.plus/lolly/internal/logging"
)
// AccessLog 访问日志中间件,记录请求方法、路径、状态码、响应大小和处理时间。
type AccessLog struct {
logger *logging.Logger
}
// New 创建访问日志中间件。
func New(cfg *config.LoggingConfig) *AccessLog {
return &AccessLog{
logger: logging.New(cfg),
}
}
// Name 返回中间件名称。
func (a *AccessLog) Name() string {
return "accesslog"
}
// Process 包装 handler在请求处理后记录访问日志。
func (a *AccessLog) Process(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
start := time.Now()
next(ctx)
duration := time.Since(start)
a.logger.LogAccess(ctx, ctx.Response.StatusCode(), int64(len(ctx.Response.Body())), duration)
}
}
// Close 关闭日志文件。
func (a *AccessLog) Close() error {
return a.logger.Close()
}