From 8c75f9575b3a3125aafd73a427fd74590663638f Mon Sep 17 00:00:00 2001 From: Sakurasan <1173092237@qq.com> Date: Fri, 31 Mar 2023 16:59:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=BC=80=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- opencat.go | 3 ++- router/router.go | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/opencat.go b/opencat.go index 57670ee..f5dfdb1 100644 --- a/opencat.go +++ b/opencat.go @@ -41,7 +41,8 @@ func main() { // 初始化用户 r.POST("/1/users/init", router.Handleinit) - r.POST("/v1/chat/completions", router.HandleProy) + r.Any("/v1/chat/completions", router.HandleProy) + r.Any("/", router.HandleProy) r.Run(":80") } diff --git a/router/router.go b/router/router.go index d9f0199..de5d75d 100644 --- a/router/router.go +++ b/router/router.go @@ -229,6 +229,7 @@ func GenerateToken() string { } func HandleProy(c *gin.Context) { + var localuser bool auth := c.Request.Header.Get("Authorization") if auth[:7] == "Bearer " { if len(auth[7:]) < 1 { @@ -236,12 +237,9 @@ func HandleProy(c *gin.Context) { return } if !store.IsExistAuthCache(auth[7:]) { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) - return + localuser = false } - } else { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) - return + localuser = true } client := http.DefaultClient tr := &http.Transport{ @@ -270,7 +268,9 @@ func HandleProy(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"error": "No Api-Key Available"}) return } - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem())) + if localuser { + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem())) + } resp, err := client.Do(req) if err != nil {