Skip to content

Commit

Permalink
fix HAR exported from Safari (#7)
Browse files Browse the repository at this point in the history
* fix HAR exported from Safari

* use original HAR filename

* need PUID
  • Loading branch information
xqdoo00o authored Aug 3, 2023
1 parent a0369fd commit f4d081d
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f4d081d

Please sign in to comment.