From 91f954936e75e5c2810bd6fa0be525379ea9d299 Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 22 Apr 2026 10:41:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor(server,middleware):=20=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E5=B8=B8=E9=87=8F=E6=8F=90=E9=AB=98=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ratelimit: 使用 accessUnknown 常量替代硬编码字符串 - testutil: 提取 testListenAddr 常量 Co-Authored-By: Claude Opus 4.7 --- internal/middleware/security/ratelimit.go | 2 +- internal/server/testutil.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/middleware/security/ratelimit.go b/internal/middleware/security/ratelimit.go index c6058f1..730106d 100644 --- a/internal/middleware/security/ratelimit.go +++ b/internal/middleware/security/ratelimit.go @@ -339,7 +339,7 @@ func (rl *RateLimiter) getRetryAfter(key string) int64 { func keyByIP(ctx *fasthttp.RequestCtx) string { ip := netutil.ExtractClientIPNet(ctx) if ip == nil { - return "unknown" + return accessUnknown } return ip.String() } diff --git a/internal/server/testutil.go b/internal/server/testutil.go index 3311337..6ac7324 100644 --- a/internal/server/testutil.go +++ b/internal/server/testutil.go @@ -12,6 +12,9 @@ import ( "rua.plus/lolly/internal/ssl" ) +// testListenAddr 是测试用的随机端口监听地址 +const testListenAddr = "127.0.0.1:0" + // MockFastServer 是 fasthttp.Server 的 Mock 包装 // 定义在此文件以便 TestServerOptions 可以引用 type MockFastServer struct { @@ -108,7 +111,7 @@ func MustStartTestServer(cfg *config.Config) *Server { } if listenAddr == "" || listenAddr == ":80" { if len(cfg.Servers) > 0 { - cfg.Servers[0].Listen = "127.0.0.1:0" + cfg.Servers[0].Listen = testListenAddr } }