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 1/6] =?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 { From b2f5d733486023e4ba1489349e2b0c8b0a489d37 Mon Sep 17 00:00:00 2001 From: Sakurasan <1173092237@qq.com> Date: Fri, 31 Mar 2023 18:21:57 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fix=20=E8=87=AA=E5=AE=9A=E4=B9=89=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E9=AA=8C=E8=AF=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- opencat.go | 4 ++-- router/router.go | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/opencat.go b/opencat.go index f5dfdb1..f742f5f 100644 --- a/opencat.go +++ b/opencat.go @@ -41,8 +41,8 @@ func main() { // 初始化用户 r.POST("/1/users/init", router.Handleinit) - r.Any("/v1/chat/completions", router.HandleProy) - r.Any("/", router.HandleProy) + r.POST("/v1/chat/completions", router.HandleProy) + r.GET("/v1/models", router.HandleProy) r.Run(":80") } diff --git a/router/router.go b/router/router.go index de5d75d..3f793bd 100644 --- a/router/router.go +++ b/router/router.go @@ -231,15 +231,14 @@ 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 && auth[:7] == "Bearer " { if len(auth[7:]) < 1 { c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) return } - if !store.IsExistAuthCache(auth[7:]) { - localuser = false + if store.IsExistAuthCache(auth[7:]) { + localuser = true } - localuser = true } client := http.DefaultClient tr := &http.Transport{ 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 3/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86?= =?UTF-8?q?=E9=80=BB=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 } From cf023aadc1919a514b3ffb042b9b5e07cfb3a894 Mon Sep 17 00:00:00 2001 From: Sakurasan <1173092237@qq.com> Date: Fri, 31 Mar 2023 19:55:20 +0800 Subject: [PATCH 4/6] update --- opencat.go | 6 +++--- router/router.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/opencat.go b/opencat.go index 52ff078..b5b47eb 100644 --- a/opencat.go +++ b/opencat.go @@ -41,9 +41,9 @@ 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.POST("/v1/chat/completions", router.HandleReverseProxy) + r.GET("/v1/models", router.HandleReverseProxy) + r.GET("/v1/dashboard/billing/credit_grants", router.HandleReverseProxy) r.Run(":80") } diff --git a/router/router.go b/router/router.go index 429429a..6b3989a 100644 --- a/router/router.go +++ b/router/router.go @@ -9,6 +9,7 @@ import ( "log" "net" "net/http" + "net/http/httputil" "opencatd-open/store" "time" @@ -315,3 +316,38 @@ func HandleProy(c *gin.Context) { return } } + +func HandleReverseProxy(c *gin.Context) { + proxy := &httputil.ReverseProxy{ + Director: func(req *http.Request) { + req.URL.Scheme = "https" + req.URL.Host = "api.openai.com" + // req.Header.Set("Authorization", "Bearer YOUR_API_KEY_HERE") + }, + } + + var localuser bool + auth := c.Request.Header.Get("Authorization") + if len(auth) > 7 && auth[:7] == "Bearer " { + if store.IsExistAuthCache(auth[7:]) { + localuser = true + } + } + + // req, err := http.NewRequest(c.Request.Method, baseUrl+c.Request.URL.Path, c.Request.Body) + // if err != nil { + // c.JSON(http.StatusOK, gin.H{"error": err.Error()}) + // return + // } + // req.Header = c.Request.Header + if localuser { + if store.KeysCache.ItemCount() == 0 { + c.JSON(http.StatusOK, gin.H{"error": "No Api-Key Available"}) + return + } + c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem())) + } + + proxy.ServeHTTP(c.Writer, c.Request) + +} From 89f6e0df6a18879503dac6cbf7adaa258482e8fe Mon Sep 17 00:00:00 2001 From: Sakurasan <1173092237@qq.com> Date: Fri, 31 Mar 2023 21:08:07 +0800 Subject: [PATCH 5/6] up --- router/router.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/router/router.go b/router/router.go index 6b3989a..e26b732 100644 --- a/router/router.go +++ b/router/router.go @@ -324,14 +324,26 @@ func HandleReverseProxy(c *gin.Context) { req.URL.Host = "api.openai.com" // req.Header.Set("Authorization", "Bearer YOUR_API_KEY_HERE") }, + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext, + ForceAttemptHTTP2: true, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + }, } var localuser bool auth := c.Request.Header.Get("Authorization") if len(auth) > 7 && auth[:7] == "Bearer " { - if store.IsExistAuthCache(auth[7:]) { - localuser = true - } + log.Println(store.IsExistAuthCache(auth[7:])) + localuser = store.IsExistAuthCache(auth[7:]) } // req, err := http.NewRequest(c.Request.Method, baseUrl+c.Request.URL.Path, c.Request.Body) From a452469b51bf8cde96552ee5862ac7ebcd67bcb7 Mon Sep 17 00:00:00 2001 From: Sakurasan <1173092237@qq.com> Date: Fri, 31 Mar 2023 21:55:24 +0800 Subject: [PATCH 6/6] up --- opencat.go | 7 ++++--- router/router.go | 21 ++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/opencat.go b/opencat.go index b5b47eb..7d5806c 100644 --- a/opencat.go +++ b/opencat.go @@ -41,9 +41,10 @@ func main() { // 初始化用户 r.POST("/1/users/init", router.Handleinit) - r.POST("/v1/chat/completions", router.HandleReverseProxy) - r.GET("/v1/models", router.HandleReverseProxy) - r.GET("/v1/dashboard/billing/credit_grants", router.HandleReverseProxy) + + 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 e26b732..c4bc2df 100644 --- a/router/router.go +++ b/router/router.go @@ -234,9 +234,7 @@ func HandleProy(c *gin.Context) { var localuser bool auth := c.Request.Header.Get("Authorization") if len(auth) > 7 && auth[:7] == "Bearer " { - if store.IsExistAuthCache(auth[7:]) { - localuser = true - } + localuser = store.IsExistAuthCache(auth[7:]) } client := http.DefaultClient tr := &http.Transport{ @@ -346,20 +344,21 @@ func HandleReverseProxy(c *gin.Context) { localuser = store.IsExistAuthCache(auth[7:]) } - // req, err := http.NewRequest(c.Request.Method, baseUrl+c.Request.URL.Path, c.Request.Body) - // if err != nil { - // c.JSON(http.StatusOK, gin.H{"error": err.Error()}) - // return - // } - // req.Header = c.Request.Header + req, err := http.NewRequest(c.Request.Method, c.Request.URL.Path, c.Request.Body) + if err != nil { + c.JSON(http.StatusOK, gin.H{"error": err.Error()}) + return + } + req.Header = c.Request.Header if localuser { if store.KeysCache.ItemCount() == 0 { c.JSON(http.StatusOK, gin.H{"error": "No Api-Key Available"}) return } - c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem())) + // c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem())) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem())) } - proxy.ServeHTTP(c.Writer, c.Request) + proxy.ServeHTTP(c.Writer, req) }