From b0b4c00ccc999314bcbb07b313fa783b3d56b033 Mon Sep 17 00:00:00 2001 From: NewName Date: Sat, 17 May 2025 16:37:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99Github=E4=B8=8B=E8=BD=BD=E4=B9=9F?= =?UTF-8?q?=E5=BA=94=E7=94=A8IP=E9=99=90=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ghproxy/main.go | 5 +++-- ghproxy/ratelimiter.go | 23 ++--------------------- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/ghproxy/main.go b/ghproxy/main.go index e928c4f..955275b 100644 --- a/ghproxy/main.go +++ b/ghproxy/main.go @@ -96,8 +96,9 @@ func main() { c.File("./public/bj.svg") }) - // 最后才注册NoRoute处理器,处理不能直接匹配的路径 - router.NoRoute(handler) + // 注册NoRoute处理器,应用限流中间件 + // 为GitHub文件下载应用限流 + ApplyRateLimit(router, "*", "GET", handler) err := router.Run(fmt.Sprintf("%s:%d", host, port)) if err != nil { diff --git a/ghproxy/ratelimiter.go b/ghproxy/ratelimiter.go index f1aaa4c..287f336 100644 --- a/ghproxy/ratelimiter.go +++ b/ghproxy/ratelimiter.go @@ -265,7 +265,7 @@ func RateLimitMiddleware(limiter *IPRateLimiter) gin.HandlerFunc { // 检查是否允许本次请求 if !ipLimiter.Allow() { c.JSON(429, gin.H{ - "error": "请求频率过高,每两小时最多下载10个镜像包", + "error": "请求频率过高,触发IP限流", }) c.Abort() return @@ -294,23 +294,4 @@ func ApplyRateLimit(router *gin.Engine, path string, method string, handler gin. default: router.Any(path, RateLimitMiddleware(limiter), handler) } -} - -// 示例:使用此限流器 -/* -func initSkopeoRoutes(router *gin.Engine) { - - os.MkdirAll("./temp", 0755) - - router.GET("/ws/:taskId", handleWebSocket) - - // 对下载API应用限流 - ApplyRateLimit(router, "/api/download", "POST", handleDownload) - - // 常规路由 - router.GET("/api/task/:taskId", getTaskStatus) - router.GET("/api/files/:filename", serveFile) - - go cleanupTempFiles() -} -*/ \ No newline at end of file +} \ No newline at end of file