| package chatgpt |
|
|
| import ( |
| "io" |
| "net/http" |
| "sync" |
| "time" |
|
|
| "aurora/httpclient" |
| "aurora/internal/accounts" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| type CookieBootstrap struct { |
| mu sync.Mutex |
| bootstrapped bool |
| lastBootAt time.Time |
| } |
|
|
| |
| |
| var cookieBootstrap CookieBootstrap |
|
|
| |
| |
| |
| |
| |
| func bootstrapCookieJar(client httpclient.AuroraHttpClient, account *accounts.Account) bool { |
| cookieBootstrap.mu.Lock() |
| defer cookieBootstrap.mu.Unlock() |
|
|
| |
| if cookies := client.GetCookies("https://chatgpt.com"); hasUsableCookies(cookies) { |
| if time.Since(cookieBootstrap.lastBootAt) < 10*time.Minute { |
| cookieBootstrap.bootstrapped = true |
| return true |
| } |
| } |
|
|
| |
| header := createBaseHeader() |
| urls := []string{ |
| "https://chatgpt.com/", |
| "https://chatgpt.com/api/auth/session", |
| "https://chatgpt.com/cdn-cgi/trace", |
| } |
| for _, u := range urls { |
| resp, err := client.Request(http.MethodGet, u, header, nil, nil) |
| if err != nil || resp == nil { |
| continue |
| } |
| |
| |
| if resp.Body != nil { |
| io.Copy(io.Discard, resp.Body) |
| resp.Body.Close() |
| } |
| _ = header |
| } |
|
|
| cookieBootstrap.lastBootAt = time.Now() |
| cookieBootstrap.bootstrapped = true |
|
|
| |
| cookies := client.GetCookies("https://chatgpt.com") |
| if !hasUsableCookies(cookies) { |
| return false |
| } |
| return true |
| } |
|
|
| |
| |
| func hasUsableCookies(cookies []*http.Cookie) bool { |
| if len(cookies) == 0 { |
| return false |
| } |
| names := make(map[string]bool, len(cookies)) |
| for _, c := range cookies { |
| names[c.Name] = true |
| } |
| return names["cf_clearance"] || names["__Secure-next-auth.session-token.0"] |
| } |
|
|
| |
| |
| func ensureBootstrapped(client httpclient.AuroraHttpClient, account *accounts.Account) { |
| cookies := client.GetCookies("https://chatgpt.com") |
| if hasUsableCookies(cookies) { |
| return |
| } |
| bootstrapCookieJar(client, account) |
| } |
|
|