Skip to content

Commit

Permalink
feat: 增加ws的兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
liuhuapiaoyuan committed Feb 19, 2024
1 parent 9e2fa94 commit 45e87aa
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
10 changes: 3 additions & 7 deletions backend-api/backend-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) // 禁用支付
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {

Expand Down
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 45e87aa

Please sign in to comment.