Skip to content

Commit

Permalink
fix: 修复public-api没有ja3的配置
Browse files Browse the repository at this point in the history
  • Loading branch information
liuhuapiaoyuan committed Apr 16, 2024
1 parent abce98f commit 1aeb052
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
11 changes: 4 additions & 7 deletions backend-api/backend-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ func ProxyRequestGet(path string, r *ghttp.Request) (resStr string, err error) {
if Authorization != "" {
userToken = r.Header.Get("Authorization")[7:]
}
userId, chatgptId, accessToken, err := ChatgptSessionService.GetAccessToken(ctx, userToken)
_, _, accessToken, err := ChatgptSessionService.GetAccessToken(ctx, userToken)

g.Log().Debug(ctx, "userToken", userToken)
g.Log().Debug(ctx, "userId", userId)
g.Log().Debug(ctx, "chatgptId", chatgptId)
UpStream := config.CHATPROXY(ctx)
if err != nil {
// 处理错误
Expand Down Expand Up @@ -96,6 +93,7 @@ func ProxyRequestGet(path string, r *ghttp.Request) (resStr string, err error) {
req.Header.Add(key, value)
}
}
req.Header.Set("authkey", config.AUTHKEY(ctx))
req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("Host", "chat.openai.com")
req.Header.Set("Origin", "https://chat.openai.com/chat")
Expand All @@ -104,6 +102,7 @@ func ProxyRequestGet(path string, r *ghttp.Request) (resStr string, err error) {
resp, err := client.Do(req)
if err != nil {
// 处理错误
g.Log().Error(ctx, err)
panic(err)
}
defer resp.Body.Close()
Expand All @@ -124,8 +123,6 @@ func ProxyAll(r *ghttp.Request) {
if Authorization != "" {
userToken = r.Header.Get("Authorization")[7:]
}
g.Log().Debug(ctx, "userToken", userToken)

isStream := strings.Contains(r.Header.Get("accept"), "text/event-stream")

// 获得当前的请求域名
Expand Down Expand Up @@ -191,7 +188,6 @@ func ProxyAll(r *ghttp.Request) {
// log content-type
g.Log().Debug(ctx, "content-type", response.Header.Get("Content-Type"))
}

// 判断response的Cotnent-Type是否是json
if strings.Contains(response.Header.Get("Content-Type"), "json") {
isStream = false
Expand Down Expand Up @@ -292,6 +288,7 @@ func AttachGPT4Mobile(ctx g.Ctx, response *http.Response) error {
func CreateConversation(r *ghttp.Request, userId int, chatgptId int, userToken string, userAgent string, conversationPath string) {
id := strings.Split(conversationPath, "/")[4]
r.Request.Header.Set("Authorization", "Bearer "+userToken)
g.Log().Info(r.GetCtx(), "准备开始创建话题CreateConversation", id)
resStr, err := ProxyRequestGet("/backend-api/conversation/"+id, r)
if err != nil {
return
Expand Down
12 changes: 11 additions & 1 deletion public-api/proxypublic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package publicapi

import (
"chatgpt-mirror-server/config"
"chatgpt-mirror-server/utility"
"crypto/tls"
"net/http"
"net/http/httputil"
Expand All @@ -25,12 +26,21 @@ func ProxyPublic(r *ghttp.Request) {
},
ForceAttemptHTTP2: true,
}
if config.Ja3Proxy != nil {
proxy.Transport = &http.Transport{
Proxy: http.ProxyURL(config.Ja3Proxy),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
ForceAttemptHTTP2: true,
}
}
newreq := r.Request.Clone(ctx)
newreq.URL.Host = u.Host
newreq.URL.Scheme = u.Scheme
newreq.Host = u.Host
newreq.Header.Set("authkey", config.AUTHKEY(ctx))

utility.HeaderModify(&newreq.Header)
// newreq.Header.Set("Cookie", "__Secure-next-auth.session-token="+carinfo.RefreshCookie)
// // 去除header 中的 压缩
// newreq.Header.Del("Accept-Encoding")
Expand Down

0 comments on commit 1aeb052

Please sign in to comment.