From 45e87aa550962cd002db7592ac5ee151018c7387 Mon Sep 17 00:00:00 2001 From: liuhuapiaoyuan <278780765@qq.com> Date: Mon, 19 Feb 2024 11:41:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0ws=E7=9A=84=E5=85=BC?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend-api/backend-api.go | 10 +++------- config.yaml | 2 ++ config/config.go | 3 +++ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/backend-api/backend-api.go b/backend-api/backend-api.go index 28e7106..0f7ebb9 100644 --- a/backend-api/backend-api.go +++ b/backend-api/backend-api.go @@ -30,7 +30,7 @@ func init() { s := g.Server() s.BindHandler("/backend-api/*any", ProxyAll) // s.BindHandler("/public-api/*any", ProxyAll) - // s.BindHandler("/_next/data/*any", NextDataGptsFixed) + // s.BindHandler("/_next/data/*any", NextDataGptsFixed) backendGroup := s.Group("/backend-api") backendGroup.POST("/accounts/data_export", NotFound) // 禁用导出 backendGroup.POST("/payments/checkout", NotFound) // 禁用支付 @@ -73,6 +73,7 @@ func ProxyAll(r *ghttp.Request) { return } UpStream := config.CHATPROXY(ctx) + WsUpStream := config.WS_SERVICE(ctx) u, _ := url.Parse(UpStream) proxy := httputil.NewSingleHostReverseProxy(u) proxy.Transport = &http.Transport{ @@ -138,18 +139,13 @@ func ProxyAll(r *ghttp.Request) { modifiedBody := strings.Replace(bodyStr, "https://files.oaiusercontent.com", cdnhost, -1) if strings.Contains(bodyStr, "wss://") { - // 使用正则 wss://中间的域名/client/hubs/conversations 把域名替换成本地域名 - - // modifiedBody = strings.Replace(string(modifiedBody), "wss://chatgpt-async-webps-prod-southcentralus-22.webpubsub.azure.com", - // "wss://host.docker.internal:7999", -1) - re := regexp.MustCompile(`wss://([^/]+)/client`) // 在URL中搜索匹配的部分 matches := re.FindStringSubmatch(bodyStr) if len(matches) > 1 { - modifiedBody = strings.Replace(modifiedBody, "wss://"+matches[1]+"/client/hubs/conversations?", "ws://127.0.0.1:7999/client/hubs/conversations?host="+matches[1]+"&", -1) + modifiedBody = strings.Replace(modifiedBody, "wss://"+matches[1]+"/client/hubs/conversations?", WsUpStream+"/client/hubs/conversations?host="+matches[1]+"&", -1) g.Log().Debug(ctx, "wss替换成ws", matches[1]) } else { diff --git a/config.yaml b/config.yaml index 6633fe8..15a494c 100644 --- a/config.yaml +++ b/config.yaml @@ -62,4 +62,6 @@ AUTHKEY: "maidou" ONLYTOKEN: false CDNHOST: "https://file.chatgpt.ggss.club" ASSET_PREFIX: "https://cdn.chatgpt.ggss.club" +# websocket服务地址 +WS_SERVICE: "ws://host.docker.internal:7999" # 5a07e29c-31d8-4976-ba66-f183598587d9 diff --git a/config/config.go b/config/config.go index 8172b26..f44a45a 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,9 @@ import ( func CHATPROXY(ctx g.Ctx) string { return g.Cfg().MustGetWithEnv(ctx, "CHATPROXY").String() } +func WS_SERVICE(ctx g.Ctx) string { + return g.Cfg().MustGetWithEnv(ctx, "WS_SERVICE").String() +} func AUTHKEY(ctx g.Ctx) string { // g.Log().Debug(ctx, "config.AUTHKEY", g.Cfg().MustGetWithEnv(ctx, "AUTHKEY").String())