Skip to content

Commit

Permalink
feat: add 2captcha support
Browse files Browse the repository at this point in the history
  • Loading branch information
MrlolDev committed Dec 12, 2022
1 parent 4d7e518 commit 658be86
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 4 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@
"test:prettier": "prettier '**/*.{js,jsx,ts,tsx}' --check"
},
"dependencies": {
"delay": "^5.0.0",
"eventsource-parser": "^0.0.5",
"expiry-map": "^2.0.0",
"p-timeout": "^6.0.0",
"puppeteer-extra": "^3.3.4",
"puppeteer-extra-plugin-recaptcha": "^3.6.6",
"puppeteer-extra-plugin-stealth": "^2.11.1",
"remark": "^14.0.2",
"strip-markdown": "^5.0.0",
"delay": "^5.0.0",
"uuid": "^9.0.0",
"puppeteer-extra": "^3.3.4",
"puppeteer-extra-plugin-stealth": "^2.11.1"
"uuid": "^9.0.0"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
Expand Down
39 changes: 39 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './chatgpt-conversation'
export * from './types'
export * from './utils'
export * from './openai-auth'
export * from './openai-auth-2captcha'

129 changes: 129 additions & 0 deletions src/openai-auth-2captcha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import delay from 'delay'
import {
type Browser,
type Page,
type Protocol,
type PuppeteerLaunchOptions
} from 'puppeteer'
import puppeteer from 'puppeteer-extra'
import RecaptchaPlugin from 'puppeteer-extra-plugin-recaptcha'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'

import { OpenAIAuth, getBrowser } from './openai-auth'

puppeteer.use(StealthPlugin())

/**
* Bypasses OpenAI's use of Cloudflare to get the cookies required to use
* ChatGPT. Uses Puppeteer with a stealth plugin under the hood.
*
* If you pass `email` and `password`, then it will log into the account and
* include a `sessionToken` in the response.
*
* If you don't pass `email` and `password`, then it will just return a valid
* `clearanceToken`.
*
* This can be useful because `clearanceToken` expires after ~2 hours, whereas
* `sessionToken` generally lasts much longer. We recommend renewing your
* `clearanceToken` every hour or so and creating a new instance of `ChatGPTAPI`
* with your updated credentials.
*/
export async function getOpenAIAuth2Captcha({
email,
password,
timeoutMs = 2 * 60 * 1000,
browser,
captchaToken
}: {
email?: string
password?: string
timeoutMs?: number
browser?: Browser
captchaToken?: string
}): Promise<OpenAIAuth> {
let page: Page
let origBrowser = browser
puppeteer.use(
RecaptchaPlugin({
provider: {
id: '2captcha',
token: captchaToken
},
visualFeedback: true // colorize reCAPTCHAs (violet = detected, green = solved)
})
)

try {
if (!browser) {
browser = await getBrowser()
}

const userAgent = await browser.userAgent()
page = (await browser.pages())[0] || (await browser.newPage())
page.setDefaultTimeout(timeoutMs)

await page.goto('https://chat.openai.com/auth/login')

// NOTE: this is where you may encounter a CAPTCHA
await page.solveRecaptchas()

await page.waitForSelector('#__next .btn-primary', { timeout: timeoutMs })

// once we get to this point, the Cloudflare cookies are available
await delay(1000)

// login as well (optional)
if (email && password) {
await Promise.all([
page.click('#__next .btn-primary'),
page.waitForNavigation({
waitUntil: 'networkidle0'
})
])
await page.type('#username', email, { delay: 10 })
await page.solveRecaptchas()
await page.click('button[type="submit"]')
await page.waitForSelector('#password')
await page.type('#password', password, { delay: 10 })
await Promise.all([
page.click('button[type="submit"]'),
page.waitForNavigation({
waitUntil: 'networkidle0'
})
])
/*var capacityLimit = await page.$('')
if (capacityLimit) {
throw `ChatGPT is at capacity right now`
}*/
}

const pageCookies = await page.cookies()
const cookies = pageCookies.reduce(
(map, cookie) => ({ ...map, [cookie.name]: cookie }),
{}
)

const authInfo: OpenAIAuth = {
userAgent,
clearanceToken: cookies['cf_clearance']?.value,
sessionToken: cookies['__Secure-next-auth.session-token']?.value,
cookies
}

return authInfo
} catch (err) {
console.error(err)
throw err
} finally {
if (origBrowser) {
if (page) {
await page.close()
}
} else if (browser) {
await browser.close()
}

page = null
browser = null
}
}

0 comments on commit 658be86

Please sign in to comment.