Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix HAR exported from Safari #7

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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