From 10d69263c97e6d9fc86b8a25e26b6ff6fe8036e6 Mon Sep 17 00:00:00 2001 From: Sakurasan <1173092237@qq.com> Date: Fri, 31 Mar 2023 19:05:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- opencat.go | 1 + router/router.go | 17 +++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/opencat.go b/opencat.go index f742f5f..52ff078 100644 --- a/opencat.go +++ b/opencat.go @@ -43,6 +43,7 @@ func main() { r.POST("/1/users/init", router.Handleinit) r.POST("/v1/chat/completions", router.HandleProy) r.GET("/v1/models", router.HandleProy) + r.GET("/v1/dashboard/billing/credit_grants", router.HandleProy) r.Run(":80") } diff --git a/router/router.go b/router/router.go index 3f793bd..429429a 100644 --- a/router/router.go +++ b/router/router.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "log" "net" "net/http" "opencatd-open/store" @@ -232,10 +233,6 @@ func HandleProy(c *gin.Context) { var localuser bool auth := c.Request.Header.Get("Authorization") if len(auth) > 7 && auth[:7] == "Bearer " { - if len(auth[7:]) < 1 { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) - return - } if store.IsExistAuthCache(auth[7:]) { localuser = true } @@ -259,20 +256,22 @@ func HandleProy(c *gin.Context) { // 创建 API 请求 req, err := http.NewRequest(c.Request.Method, baseUrl+c.Request.URL.Path, c.Request.Body) if err != nil { + log.Println(err) c.JSON(http.StatusOK, gin.H{"error": err.Error()}) return } req.Header = c.Request.Header - if store.KeysCache.ItemCount() == 0 { - c.JSON(http.StatusOK, gin.H{"error": "No Api-Key Available"}) - return - } if localuser { + if store.KeysCache.ItemCount() == 0 { + c.JSON(http.StatusOK, gin.H{"error": "No Api-Key Available"}) + return + } req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem())) } resp, err := client.Do(req) if err != nil { + log.Println(err) c.JSON(http.StatusOK, gin.H{"error": err.Error()}) return } @@ -300,6 +299,7 @@ func HandleProy(c *gin.Context) { bodyRes, err := io.ReadAll(resp.Body) if err != nil { + log.Println(err) c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) return } @@ -310,6 +310,7 @@ func HandleProy(c *gin.Context) { // 返回 API 响应主体 c.Writer.WriteHeader(resp.StatusCode) if _, err := io.Copy(c.Writer, resbody); err != nil { + log.Println(err) c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) return }