From 318a03be66587f35d897ade3d8fad48e7115743f Mon Sep 17 00:00:00 2001 From: timordnung Date: Wed, 21 Dec 2022 10:40:35 +0100 Subject: [PATCH 1/4] Added Proxy Auth --- src/chatgpt-api-browser.ts | 16 +++++ src/openai-auth.ts | 134 ++++++++++++++++++++++++++++++++++++- 2 files changed, 149 insertions(+), 1 deletion(-) diff --git a/src/chatgpt-api-browser.ts b/src/chatgpt-api-browser.ts index e9135f8..3405b1b 100644 --- a/src/chatgpt-api-browser.ts +++ b/src/chatgpt-api-browser.ts @@ -118,6 +118,22 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { this._page = (await this._browser.pages())[0] || (await this._browser.newPage()) + if (this._proxyServer && this._proxyServer.includes('@')) { + const proxyUsername = this._proxyServer.split('@')[0].split(':')[0] + const proxyPassword = this._proxyServer.split('@')[0].split(':')[1] + try { + await this._page.authenticate({ + username: proxyUsername, + password: proxyPassword + }) + } catch (err) { + console.error( + `Proxy "${this._proxyServer}" throws an error at authenticating`, + err.toString() + ) + } + } + // bypass annoying popup modals this._page.evaluateOnNewDocument(() => { window.localStorage.setItem('oai/apps/hasSeenOnboarding/chat', 'true') diff --git a/src/openai-auth.ts b/src/openai-auth.ts index 16f36ec..9ad2d51 100644 --- a/src/openai-auth.ts +++ b/src/openai-auth.ts @@ -240,6 +240,135 @@ export async function getOpenAIAuth({ } } +export async function signUp({ + email, + password, + browser, + page, + timeoutMs = 2 * 60 * 1000, + isGoogleLogin = false, + captchaToken = process.env.CAPTCHA_TOKEN +}: { + email?: string + password?: string + browser?: Browser + page?: Page + timeoutMs?: number + isGoogleLogin?: boolean + captchaToken?: string +}): Promise { + const origBrowser = browser + const origPage = page + + try { + if (!browser) { + browser = await getBrowser({ captchaToken }) + } + + const userAgent = await browser.userAgent() + if (!page) { + page = (await browser.pages())[0] || (await browser.newPage()) + page.setDefaultTimeout(timeoutMs) + } + + await page.goto('https://chat.openai.com/auth/login', { + waitUntil: 'networkidle2' + }) + + // NOTE: this is where you may encounter a CAPTCHA + if (hasRecaptchaPlugin) { + await page.solveRecaptchas() + } + + await checkForChatGPTAtCapacity(page) + + // once we get to this point, the Cloudflare cookies should be available + + // login as well (optional) + if (email && password) { + await waitForConditionOrAtCapacity(page, () => + page.waitForSelector( + '#__next > div > div > div.flex.flex-row.gap-3 > button:nth-child(2)', + { timeout: timeoutMs } + ) + ) + await delay(500) + + // click login button and wait for navigation to finish + await Promise.all([ + page.waitForNavigation({ + waitUntil: 'networkidle2', + timeout: timeoutMs + }), + + page.click( + '#__next > div > div > div.flex.flex-row.gap-3 > button:nth-child(2)' + ) + ]) + + await checkForChatGPTAtCapacity(page) + + let submitP: () => Promise + + if (isGoogleLogin) { + await page.click('button[data-provider="google"]') + await page.waitForSelector('input[type="email"]') + await page.type('input[type="email"]', email, { delay: 10 }) + await Promise.all([ + page.waitForNavigation(), + await page.keyboard.press('Enter') + ]) + await page.waitForSelector('input[type="password"]', { visible: true }) + await page.type('input[type="password"]', password, { delay: 10 }) + submitP = () => page.keyboard.press('Enter') + } else { + await page.waitForSelector('#email') + await page.type('#email', email, { delay: 20 }) + await delay(100) + + if (hasRecaptchaPlugin) { + console.log('solveRecaptchas()') + const res = await page.solveRecaptchas() + // console.log('solveRecaptchas result', res) + } + + await page.click('button[type="submit"]') + await page.waitForSelector('#password', { timeout: timeoutMs }) + await page.type('#password', password, { delay: 10 }) + submitP = () => page.click('button[type="submit"]') + } + + await Promise.all([ + waitForConditionOrAtCapacity(page, () => + page.waitForNavigation({ + waitUntil: 'networkidle2', + timeout: timeoutMs + }) + ), + submitP() + ]) + } else { + await delay(2000) + await checkForChatGPTAtCapacity(page) + } + + return true + } catch (err) { + throw err + } finally { + if (origBrowser) { + if (page && page !== origPage) { + await page.close() + } + } else if (browser) { + await browser.close() + } + + page = null + browser = null + } +} + /** * Launches a non-puppeteer instance of Chrome. Note that in my testing, I wasn't * able to use the built-in `puppeteer` version of Chromium because Cloudflare @@ -313,7 +442,10 @@ export async function getBrowser( } if (proxyServer) { - puppeteerArgs.push(`--proxy-server=${proxyServer}`) + const ipPort = proxyServer.includes('@') + ? proxyServer.split('@')[1] + : proxyServer + puppeteerArgs.push(`--proxy-server=${ipPort}`) } const browser = await puppeteer.launch({ From fa29f3da2fc883aebff35db788fd4de0efdd8224 Mon Sep 17 00:00:00 2001 From: timordnung Date: Wed, 21 Dec 2022 10:41:39 +0100 Subject: [PATCH 2/4] Added Proxy Auth --- .env.example | 4 +- src/openai-auth.ts | 129 --------------------------------------------- 2 files changed, 2 insertions(+), 131 deletions(-) diff --git a/.env.example b/.env.example index 876b8ab..436630b 100644 --- a/.env.example +++ b/.env.example @@ -9,5 +9,5 @@ # ChatGPT # ----------------------------------------------------------------------------- -OPENAI_EMAIL= -OPENAI_PASSWORD= +OPENAI_EMAIL="addresse5@cubemail.club" +OPENAI_PASSWORD="AddressePassword" diff --git a/src/openai-auth.ts b/src/openai-auth.ts index 9ad2d51..d5bf875 100644 --- a/src/openai-auth.ts +++ b/src/openai-auth.ts @@ -240,135 +240,6 @@ export async function getOpenAIAuth({ } } -export async function signUp({ - email, - password, - browser, - page, - timeoutMs = 2 * 60 * 1000, - isGoogleLogin = false, - captchaToken = process.env.CAPTCHA_TOKEN -}: { - email?: string - password?: string - browser?: Browser - page?: Page - timeoutMs?: number - isGoogleLogin?: boolean - captchaToken?: string -}): Promise { - const origBrowser = browser - const origPage = page - - try { - if (!browser) { - browser = await getBrowser({ captchaToken }) - } - - const userAgent = await browser.userAgent() - if (!page) { - page = (await browser.pages())[0] || (await browser.newPage()) - page.setDefaultTimeout(timeoutMs) - } - - await page.goto('https://chat.openai.com/auth/login', { - waitUntil: 'networkidle2' - }) - - // NOTE: this is where you may encounter a CAPTCHA - if (hasRecaptchaPlugin) { - await page.solveRecaptchas() - } - - await checkForChatGPTAtCapacity(page) - - // once we get to this point, the Cloudflare cookies should be available - - // login as well (optional) - if (email && password) { - await waitForConditionOrAtCapacity(page, () => - page.waitForSelector( - '#__next > div > div > div.flex.flex-row.gap-3 > button:nth-child(2)', - { timeout: timeoutMs } - ) - ) - await delay(500) - - // click login button and wait for navigation to finish - await Promise.all([ - page.waitForNavigation({ - waitUntil: 'networkidle2', - timeout: timeoutMs - }), - - page.click( - '#__next > div > div > div.flex.flex-row.gap-3 > button:nth-child(2)' - ) - ]) - - await checkForChatGPTAtCapacity(page) - - let submitP: () => Promise - - if (isGoogleLogin) { - await page.click('button[data-provider="google"]') - await page.waitForSelector('input[type="email"]') - await page.type('input[type="email"]', email, { delay: 10 }) - await Promise.all([ - page.waitForNavigation(), - await page.keyboard.press('Enter') - ]) - await page.waitForSelector('input[type="password"]', { visible: true }) - await page.type('input[type="password"]', password, { delay: 10 }) - submitP = () => page.keyboard.press('Enter') - } else { - await page.waitForSelector('#email') - await page.type('#email', email, { delay: 20 }) - await delay(100) - - if (hasRecaptchaPlugin) { - console.log('solveRecaptchas()') - const res = await page.solveRecaptchas() - // console.log('solveRecaptchas result', res) - } - - await page.click('button[type="submit"]') - await page.waitForSelector('#password', { timeout: timeoutMs }) - await page.type('#password', password, { delay: 10 }) - submitP = () => page.click('button[type="submit"]') - } - - await Promise.all([ - waitForConditionOrAtCapacity(page, () => - page.waitForNavigation({ - waitUntil: 'networkidle2', - timeout: timeoutMs - }) - ), - submitP() - ]) - } else { - await delay(2000) - await checkForChatGPTAtCapacity(page) - } - - return true - } catch (err) { - throw err - } finally { - if (origBrowser) { - if (page && page !== origPage) { - await page.close() - } - } else if (browser) { - await browser.close() - } - - page = null - browser = null - } -} - /** * Launches a non-puppeteer instance of Chrome. Note that in my testing, I wasn't * able to use the built-in `puppeteer` version of Chromium because Cloudflare From 36a33d0f96b219b8a05cf3f3bd47d6ca6cc725f0 Mon Sep 17 00:00:00 2001 From: timordnung Date: Wed, 21 Dec 2022 10:42:53 +0100 Subject: [PATCH 3/4] Added Proxy Auth --- .env.example | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 436630b..105e67b 100644 --- a/.env.example +++ b/.env.example @@ -9,5 +9,5 @@ # ChatGPT # ----------------------------------------------------------------------------- -OPENAI_EMAIL="addresse5@cubemail.club" -OPENAI_PASSWORD="AddressePassword" +OPENAI_EMAIL="" +OPENAI_PASSWORD="" From ba377298716482267e5b90c7acad5ce0c426f13a Mon Sep 17 00:00:00 2001 From: timordnung Date: Wed, 21 Dec 2022 10:43:20 +0100 Subject: [PATCH 4/4] Added Proxy Auth --- .env.example | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 105e67b..876b8ab 100644 --- a/.env.example +++ b/.env.example @@ -9,5 +9,5 @@ # ChatGPT # ----------------------------------------------------------------------------- -OPENAI_EMAIL="" -OPENAI_PASSWORD="" +OPENAI_EMAIL= +OPENAI_PASSWORD=