Commit 4e9ce998 authored by Travis Fischer's avatar Travis Fischer

fix: minor fixes

parent d57db8e7
...@@ -159,7 +159,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { ...@@ -159,7 +159,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
this._page.on('response', this._onResponse.bind(this)) this._page.on('response', this._onResponse.bind(this))
// bypass cloudflare and login // bypass cloudflare and login
var authInfo = await getOpenAIAuth({ const authInfo = await getOpenAIAuth({
email: this._email, email: this._email,
password: this._password, password: this._password,
browser: this._browser, browser: this._browser,
...@@ -167,8 +167,10 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { ...@@ -167,8 +167,10 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
isGoogleLogin: this._isGoogleLogin, isGoogleLogin: this._isGoogleLogin,
isMicrosoftLogin: this._isMicrosoftLogin isMicrosoftLogin: this._isMicrosoftLogin
}) })
console.log('Cloudflare Cookie: ', authInfo.clearanceToken)
console.log('Useragent: ', authInfo.userAgent) if (this._debug) {
console.log('chatgpt', this._email, 'auth', authInfo)
}
} catch (err) { } catch (err) {
if (this._browser) { if (this._browser) {
await this._browser.close() await this._browser.close()
...@@ -282,8 +284,10 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { ...@@ -282,8 +284,10 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
} }
if (url.endsWith('/conversation')) { if (url.endsWith('/conversation')) {
if (status === 403 && !this._isRefreshing) { if (status === 403) {
await this.refreshSession() console.log(`ChatGPT "${this._email}" error 403...`)
// this will be handled in the sendMessage error handler
// await this.refreshSession()
} }
} else if (url.endsWith('api/auth/session')) { } else if (url.endsWith('api/auth/session')) {
if (status === 401) { if (status === 401) {
...@@ -308,18 +312,16 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { ...@@ -308,18 +312,16 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
* Attempts to handle 401 errors by re-authenticating. * Attempts to handle 401 errors by re-authenticating.
*/ */
async resetSession() { async resetSession() {
console.log( console.log(`ChatGPT "${this._email}" resetSession...`)
`ChatGPT "${this._email}" session expired; re-authenticating...`
)
try { try {
console.log('>>> closing session', this._email) console.log('>>> closing session', this._email)
await this.closeSession() await this.closeSession()
console.log('<<< closing session', this._email) console.log('<<< closing session', this._email)
await this.initSession() await this.initSession()
console.log(`ChatGPT "${this._email}" re-authenticated successfully`) console.log(`ChatGPT "${this._email}" refreshSession success`)
} catch (err) { } catch (err) {
console.error( console.error(
`ChatGPT "${this._email}" error re-authenticating`, `ChatGPT "${this._email}" resetSession error`,
err.toString() err.toString()
) )
} }
...@@ -329,49 +331,53 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { ...@@ -329,49 +331,53 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
* Attempts to handle 403 errors by refreshing the page. * Attempts to handle 403 errors by refreshing the page.
*/ */
async refreshSession() { async refreshSession() {
if (this._isRefreshing) {
return
}
this._isRefreshing = true this._isRefreshing = true
console.log(`ChatGPT "${this._email}" session expired (403); refreshing...`) console.log(`ChatGPT "${this._email}" refreshSession...`)
try { try {
if (!this._minimize) { if (!this._minimize) {
await maximizePage(this._page) await maximizePage(this._page)
} }
await this._page.reload() await this._page.reload()
let response let response
const timeout = 120000 // 2 minutes in milliseconds const timeout = 120000 // 2 minutes in milliseconds
// Wait for a response that includes the 'cf_clearance' cookie
try { try {
// Wait for a response that includes the 'cf_clearance' cookie
response = await this._page.waitForResponse( response = await this._page.waitForResponse(
(response) => { (response) => {
// Check if the `set-cookie` header exists in the response headers const cookie = response.headers()['set-cookie']
const setCookie = response.headers()['set-cookie'] if (cookie?.includes('cf_clearance=')) {
if (setCookie) { const cfClearance = cookie
// Check if the `set-cookie` value contains the `cf_clearance=` string .split('cf_clearance=')?.[1]
let check = setCookie.includes('cf_clearance=') ?.split(';')?.[0]
if (check) { // console.log('Cloudflare Cookie:', cfClearance)
console.log('Found cf_clearance in set-cookie header') return true
// split setCookie at cf-clearance= and get the second part, then remove the semicolon at the end
let cf_clearance = setCookie
.split('cf_clearance=')[1]
.split(';')[0]
console.log('Cloudflare Cookie:', cf_clearance)
}
return check
} }
return false return false
}, },
{ timeout } { timeout }
) )
} catch (err) { } catch (err) {
// useful for when cloudflare cookie is still valid, to catch TimeoutError // Useful for when cloudflare cookie is still valid, to catch TimeoutError
response = !!(await this._getInputBox()) response = !!(await this._getInputBox())
} }
if (!response) { if (!response) {
throw new types.ChatGPTError('Could not fetch cf_clearance cookie') throw new types.ChatGPTError('Could not fetch cf_clearance cookie')
} }
if (this._minimize && this.isChatPage) { if (this._minimize && this.isChatPage) {
await minimizePage(this._page) await minimizePage(this._page)
} }
console.log(`ChatGPT "${this._email}" refreshed session successfully`)
console.log(`ChatGPT "${this._email}" refreshSession success`)
} catch (err) { } catch (err) {
console.error( console.error(
`ChatGPT "${this._email}" error refreshing session`, `ChatGPT "${this._email}" error refreshing session`,
......
...@@ -52,7 +52,7 @@ export async function getOpenAIAuth({ ...@@ -52,7 +52,7 @@ export async function getOpenAIAuth({
password, password,
browser, browser,
page, page,
timeoutMs = 2 * 60 * 1000, timeoutMs = 3 * 60 * 1000,
isGoogleLogin = false, isGoogleLogin = false,
isMicrosoftLogin = false, isMicrosoftLogin = false,
captchaToken = process.env.CAPTCHA_TOKEN, captchaToken = process.env.CAPTCHA_TOKEN,
...@@ -619,6 +619,12 @@ async function waitForRecaptcha( ...@@ -619,6 +619,12 @@ async function waitForRecaptcha(
console.log('waiting to solve recaptcha...') console.log('waiting to solve recaptcha...')
do { do {
const captcha = await page.$('textarea#g-recaptcha-response')
if (!captcha) {
// the user may have gone past the page manually
break
}
const value = (await captcha.evaluate((el) => el.value))?.trim() const value = (await captcha.evaluate((el) => el.value))?.trim()
if (value?.length) { if (value?.length) {
// recaptcha has been solved! // recaptcha has been solved!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment