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