From 048a625181b95da83115033325c24dd97645244b Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Wed, 25 Jun 2025 00:19:38 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(auth):=20support=20new=20mo?= =?UTF-8?q?del=20API=20paths=20in=20authentication=20and=20routing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Updated TokenAuth middleware to handle requests for both `/v1beta/models/` and `/v1/models/`. - Adjusted distributor middleware to recognize the new model path. - Enhanced relay mode determination to include the new model path. - Added route for handling POST requests to `/models/*path`. These changes ensure compatibility with the new model API structure, improving the overall routing and authentication flow. --- middleware/auth.go | 2 +- middleware/distributor.go | 2 +- relay/constant/relay_mode.go | 2 +- router/relay-router.go | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/middleware/auth.go b/middleware/auth.go index f387029f..ecf4844b 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -184,7 +184,7 @@ func TokenAuth() func(c *gin.Context) { } } // gemini api 从query中获取key - if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models/") { + if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models/") || strings.HasPrefix(c.Request.URL.Path, "/v1/models/") { skKey := c.Query("key") if skKey != "" { c.Request.Header.Set("Authorization", "Bearer "+skKey) diff --git a/middleware/distributor.go b/middleware/distributor.go index e2f63602..d1159f8c 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -179,7 +179,7 @@ func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) { } c.Set("platform", string(constant.TaskPlatformKling)) c.Set("relay_mode", relayMode) - } else if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models/") { + } else if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models/") || strings.HasPrefix(c.Request.URL.Path, "/v1/models/") { // Gemini API 路径处理: /v1beta/models/gemini-2.0-flash:generateContent relayMode := relayconstant.RelayModeGemini modelName := extractModelNameFromGeminiPath(c.Request.URL.Path) diff --git a/relay/constant/relay_mode.go b/relay/constant/relay_mode.go index 02a286e2..1b8a1b2d 100644 --- a/relay/constant/relay_mode.go +++ b/relay/constant/relay_mode.go @@ -80,7 +80,7 @@ func Path2RelayMode(path string) int { relayMode = RelayModeRerank } else if strings.HasPrefix(path, "/v1/realtime") { relayMode = RelayModeRealtime - } else if strings.HasPrefix(path, "/v1beta/models") { + } else if strings.HasPrefix(path, "/v1beta/models") || strings.HasPrefix(path, "/v1/models") { relayMode = RelayModeGemini } return relayMode diff --git a/router/relay-router.go b/router/relay-router.go index aa7f27a8..325ef135 100644 --- a/router/relay-router.go +++ b/router/relay-router.go @@ -63,6 +63,7 @@ func SetRelayRouter(router *gin.Engine) { httpRouter.DELETE("/models/:model", controller.RelayNotImplemented) httpRouter.POST("/moderations", controller.Relay) httpRouter.POST("/rerank", controller.Relay) + httpRouter.POST("/models/*path", controller.Relay) } relayMjRouter := router.Group("/mj")