From f4d081d60ac780f9ff558c29f42f452e057b84db Mon Sep 17 00:00:00 2001 From: David xu Date: Thu, 3 Aug 2023 21:34:45 +0800 Subject: [PATCH] fix HAR exported from Safari (#7) * fix HAR exported from Safari * use original HAR filename * need PUID --- api.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/api.go b/api.go index 6f2999a..f569717 100644 --- a/api.go +++ b/api.go @@ -17,8 +17,9 @@ import ( tls_client "github.com/bogdanfinn/tls-client" ) -var initVer, initHex, arkURL, arkBx, arkBody string +var initVer, initHex, arkURL, arkBx string var arkHeader http.Header +var arkBody url.Values var ( jar = tls_client.NewCookieJar() options = []tls_client.HttpClientOption{ @@ -56,7 +57,7 @@ type HARData struct { } func readHAR() { - file, err := os.ReadFile("chatgpt.har") + file, err := os.ReadFile("chat.openai.com.har") if err != nil { fmt.Println(err) return @@ -94,7 +95,7 @@ func readHAR() { } } } - arkBody = "" + arkBody = make(url.Values) for _, p := range arkReq.Request.PostData.Params { // arkBody except bda & rnd if p.Name == "bda" { @@ -104,7 +105,11 @@ func readHAR() { } arkBx = Decrypt(cipher, bv+bw) } else if p.Name != "rnd" { - arkBody += "&" + p.Name + "=" + p.Value + query, err := url.QueryUnescape(p.Value) + if err != nil { + panic(err) + } + arkBody.Set(p.Name, query) } } } @@ -148,24 +153,27 @@ func GetOpenAITokenWithBx(bx string, puid string, proxy string) (string, string, //goland:noinspection SpellCheckingInspection,GoUnhandledErrorResult func sendRequest(bda string, puid string, proxy string) (string, error) { - if arkBx == "" || arkBody == "" || len(arkHeader) == 0 { + if arkBx == "" || len(arkBody) == 0 || len(arkHeader) == 0 { return "", errors.New("a valid HAR file required") } + if puid == "" { + return "", errors.New("a valid PUID required") + } if proxy != "" { (*client).SetProxy(proxy) } if bda == "" { bda = getBDA() } - form := "bda=" + url.QueryEscape(base64.StdEncoding.EncodeToString([]byte(bda))) + arkBody + "&rnd=" + strconv.FormatFloat(rand.Float64(), 'f', -1, 64) - req, _ := http.NewRequest(http.MethodPost, arkURL, strings.NewReader(form)) + arkBody.Set("bda", base64.StdEncoding.EncodeToString([]byte(bda))) + arkBody.Set("rnd", strconv.FormatFloat(rand.Float64(), 'f', -1, 64)) + req, _ := http.NewRequest(http.MethodPost, arkURL, strings.NewReader(arkBody.Encode())) req.Header = arkHeader.Clone() req.Header.Set("cookie", "_puid="+puid+";") resp, err := (*client).Do(req) if err != nil { return "", err } - defer resp.Body.Close() if resp.StatusCode != 200 { return "", errors.New("status code " + resp.Status)