diff --git a/middleware/auth.go b/middleware/auth.go index 85c46e282..a5d283d26 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -13,6 +13,7 @@ import ( "github.com/QuantumNous/new-api/model" "github.com/QuantumNous/new-api/service" "github.com/QuantumNous/new-api/setting/ratio_setting" + "github.com/QuantumNous/new-api/types" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" @@ -195,7 +196,7 @@ func TokenAuth() func(c *gin.Context) { } c.Request.Header.Set("Authorization", "Bearer "+key) } - // 检查path包含/v1/messages 或 /v1/models + // 检查path包含/v1/messages 或 /v1/models if strings.Contains(c.Request.URL.Path, "/v1/messages") || strings.Contains(c.Request.URL.Path, "/v1/models") { anthropicKey := c.Request.Header.Get("x-api-key") if anthropicKey != "" { @@ -256,7 +257,7 @@ func TokenAuth() func(c *gin.Context) { return } if common.IsIpInCIDRList(ip, allowIps) == false { - abortWithOpenAiMessage(c, http.StatusForbidden, "您的 IP 不在令牌允许访问的列表中") + abortWithOpenAiMessage(c, http.StatusForbidden, "您的 IP 不在令牌允许访问的列表中", types.ErrorCodeAccessDenied) return } logger.LogDebug(c, "Client IP %s passed the token IP restrictions check", clientIp) diff --git a/middleware/distributor.go b/middleware/distributor.go index a33404726..95fa64a30 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -114,11 +114,11 @@ func Distribute() func(c *gin.Context) { // common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id)) // message = "数据库一致性已被破坏,请联系管理员" //} - abortWithOpenAiMessage(c, http.StatusServiceUnavailable, message, string(types.ErrorCodeModelNotFound)) + abortWithOpenAiMessage(c, http.StatusServiceUnavailable, message, types.ErrorCodeModelNotFound) return } if channel == nil { - abortWithOpenAiMessage(c, http.StatusServiceUnavailable, fmt.Sprintf("分组 %s 下模型 %s 无可用渠道(distributor)", usingGroup, modelRequest.Model), string(types.ErrorCodeModelNotFound)) + abortWithOpenAiMessage(c, http.StatusServiceUnavailable, fmt.Sprintf("分组 %s 下模型 %s 无可用渠道(distributor)", usingGroup, modelRequest.Model), types.ErrorCodeModelNotFound) return } } diff --git a/middleware/utils.go b/middleware/utils.go index 24caa83c7..f198af81f 100644 --- a/middleware/utils.go +++ b/middleware/utils.go @@ -5,13 +5,14 @@ import ( "github.com/QuantumNous/new-api/common" "github.com/QuantumNous/new-api/logger" + "github.com/QuantumNous/new-api/types" "github.com/gin-gonic/gin" ) -func abortWithOpenAiMessage(c *gin.Context, statusCode int, message string, code ...string) { +func abortWithOpenAiMessage(c *gin.Context, statusCode int, message string, code ...types.ErrorCode) { codeStr := "" if len(code) > 0 { - codeStr = code[0] + codeStr = string(code[0]) } userId := c.GetInt("id") c.JSON(statusCode, gin.H{