chatgpt-api/src/openai-auth.ts

424 wiersze
11 KiB
TypeScript
Czysty Zwykły widok Historia

import * as fs from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import * as url from 'node:url'
2022-12-14 02:05:29 +00:00
import delay from 'delay'
import { TimeoutError } from 'p-timeout'
2022-12-15 22:59:28 +00:00
import type { Browser, Page, Protocol, PuppeteerLaunchOptions } from 'puppeteer'
import puppeteer from 'puppeteer-extra'
import RecaptchaPlugin from 'puppeteer-extra-plugin-recaptcha'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
2022-12-14 02:05:29 +00:00
import * as types from './types'
puppeteer.use(StealthPlugin())
let hasRecaptchaPlugin = false
let hasNopechaExtension = false
const __filename = url.fileURLToPath(import.meta.url)
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
/**
* Represents everything that's required to pass into `ChatGPTAPI` in order
* to authenticate with the unofficial ChatGPT API.
*/
export type OpenAIAuth = {
userAgent: string
clearanceToken: string
sessionToken: string
cookies?: Record<string, Protocol.Network.Cookie>
}
/**
* 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 getOpenAIAuth({
email,
password,
browser,
page,
2022-12-14 02:05:29 +00:00
timeoutMs = 2 * 60 * 1000,
isGoogleLogin = false,
2022-12-16 23:12:28 +00:00
captchaToken = process.env.CAPTCHA_TOKEN,
executablePath
}: {
email?: string
password?: string
browser?: Browser
page?: Page
2022-12-14 02:05:29 +00:00
timeoutMs?: number
isGoogleLogin?: boolean
captchaToken?: string
2022-12-16 23:12:28 +00:00
executablePath?: string
}): Promise<OpenAIAuth> {
const origBrowser = browser
const origPage = page
try {
if (!browser) {
2022-12-16 23:12:28 +00:00
browser = await getBrowser({ captchaToken, executablePath })
}
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', {
2022-12-16 01:00:09 +00:00
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) {
2022-12-15 22:59:28 +00:00
await waitForConditionOrAtCapacity(page, () =>
page.waitForSelector('#__next .btn-primary', { timeout: timeoutMs })
)
await delay(500)
2022-12-15 22:59:28 +00:00
// click login button and wait for navigation to finish
await Promise.all([
page.waitForNavigation({
2022-12-16 01:00:09 +00:00
waitUntil: 'networkidle2',
timeout: timeoutMs
2022-12-15 22:59:28 +00:00
}),
page.click('#__next .btn-primary')
])
2022-12-14 02:05:29 +00:00
await checkForChatGPTAtCapacity(page)
let submitP: () => Promise<void>
2022-12-14 02:05:29 +00:00
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 {
2022-12-14 02:05:29 +00:00
await page.waitForSelector('#username')
await page.type('#username', email, { delay: 20 })
await delay(100)
if (hasNopechaExtension) {
await waitForRecaptcha(page, { timeoutMs })
} else if (hasRecaptchaPlugin) {
const res = await page.solveRecaptchas()
console.log('solveRecaptchas result', res)
}
await delay(1200)
const frame = page.mainFrame()
const submit = await page.waitForSelector('button[type="submit"]', {
timeout: timeoutMs
})
frame.focus('button[type="submit"]')
await submit.focus()
await submit.click()
await page.waitForSelector('#password', { timeout: timeoutMs })
await page.type('#password', password, { delay: 10 })
submitP = () => page.click('button[type="submit"]')
}
2022-12-14 02:05:29 +00:00
await Promise.all([
2022-12-15 22:59:28 +00:00
waitForConditionOrAtCapacity(page, () =>
page.waitForNavigation({
2022-12-16 01:00:09 +00:00
waitUntil: 'networkidle2',
timeout: timeoutMs
2022-12-15 22:59:28 +00:00
})
),
submitP()
2022-12-14 02:05:29 +00:00
])
2022-12-15 22:59:28 +00:00
} else {
await delay(2000)
await checkForChatGPTAtCapacity(page)
}
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) {
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
* recognizes it and blocks access.
*/
export async function getBrowser(
opts: PuppeteerLaunchOptions & {
captchaToken?: string
nopechaKey?: string
} = {}
) {
2022-12-16 14:02:33 +00:00
const {
captchaToken = process.env.CAPTCHA_TOKEN,
nopechaKey = process.env.NOPECHA_KEY,
2022-12-16 14:02:33 +00:00
executablePath = defaultChromeExecutablePath(),
...launchOptions
} = opts
if (captchaToken && !hasRecaptchaPlugin) {
hasRecaptchaPlugin = true
2022-12-15 22:59:28 +00:00
// console.log('use captcha', captchaToken)
puppeteer.use(
RecaptchaPlugin({
provider: {
id: '2captcha',
token: captchaToken
},
visualFeedback: true // colorize reCAPTCHAs (violet = detected, green = solved)
})
)
}
const puppeteerArgs = [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-infobars',
'--disable-dev-shm-usage',
'--disable-blink-features=AutomationControlled',
'--no-first-run',
'--no-service-autorun',
'--password-store=basic',
'--system-developer-mode'
]
if (nopechaKey) {
const nopechaPath = path.join(
__dirname,
'..',
'third-party',
'nopecha-chrome-extension'
)
puppeteerArgs.push(`--disable-extensions-except=${nopechaPath}`)
puppeteerArgs.push(`--load-extension=${nopechaPath}`)
hasNopechaExtension = true
}
const browser = await puppeteer.launch({
headless: false,
2022-12-17 06:09:32 +00:00
// https://peter.sh/experiments/chromium-command-line-switches/
args: puppeteerArgs,
ignoreDefaultArgs: [
'--disable-extensions',
'--enable-automation',
'--disable-component-extensions-with-background-pages'
2022-12-17 06:09:32 +00:00
],
ignoreHTTPSErrors: true,
2022-12-16 14:02:33 +00:00
executablePath,
...launchOptions
})
// TOdO: this is a really hackity hack way of setting the API key...
if (hasNopechaExtension) {
const page = (await browser.pages())[0] || (await browser.newPage())
await page.goto(`https://nopecha.com/setup#${nopechaKey}`)
await delay(1000)
const page3 = await browser.newPage()
await page.close()
const extensionId = 'npgnhlnhpphdlkfdnggbdpbhoopefaai'
const extensionUrl = `chrome-extension://${extensionId}/popup.html`
await page3.goto(extensionUrl, { waitUntil: 'networkidle2' })
await delay(500)
const editKey = await page3.waitForSelector('#edit_key .clickable')
await editKey.click()
const settingsInput = await page3.$('input.settings_text')
await settingsInput.type(nopechaKey)
await settingsInput.evaluate((el, value) => {
el.value = value
}, nopechaKey)
await settingsInput.press('Enter')
await delay(500)
await editKey.click()
await delay(2000)
}
return browser
}
/**
2022-12-14 02:05:29 +00:00
* Gets the default path to chrome's executable for the current platform.
*/
2022-12-14 02:05:29 +00:00
export const defaultChromeExecutablePath = (): string => {
2022-12-13 19:56:34 +00:00
switch (os.platform()) {
case 'win32':
return 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
2022-12-14 02:05:29 +00:00
2022-12-13 19:56:34 +00:00
case 'darwin':
return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
2022-12-14 02:05:29 +00:00
default: {
2022-12-13 19:56:34 +00:00
/**
* Since two (2) separate chrome releases exist on linux, we first do a
* check to ensure we're executing the right one.
2022-12-13 19:56:34 +00:00
*/
const chromeExists = fs.existsSync('/usr/bin/google-chrome')
return chromeExists
? '/usr/bin/google-chrome'
: '/usr/bin/google-chrome-stable'
}
}
}
2022-12-14 02:05:29 +00:00
async function checkForChatGPTAtCapacity(page: Page) {
2022-12-15 22:59:28 +00:00
// console.log('checkForChatGPTAtCapacity', page.url())
let res: any[]
2022-12-14 02:05:29 +00:00
try {
2022-12-15 03:24:19 +00:00
res = await page.$x("//div[contains(., 'ChatGPT is at capacity')]")
2022-12-14 02:05:29 +00:00
} catch (err) {
// ignore errors likely due to navigation
}
2022-12-15 03:24:19 +00:00
if (res?.length) {
2022-12-14 22:14:35 +00:00
const error = new types.ChatGPTError('ChatGPT is at capacity')
2022-12-14 02:05:29 +00:00
error.statusCode = 503
throw error
}
}
2022-12-15 22:59:28 +00:00
async function waitForConditionOrAtCapacity(
page: Page,
condition: () => Promise<any>,
opts: {
pollingIntervalMs?: number
} = {}
) {
const { pollingIntervalMs = 500 } = opts
return new Promise<void>((resolve, reject) => {
let resolved = false
async function waitForCapacityText() {
if (resolved) {
return
}
try {
await checkForChatGPTAtCapacity(page)
if (!resolved) {
setTimeout(waitForCapacityText, pollingIntervalMs)
}
} catch (err) {
if (!resolved) {
resolved = true
return reject(err)
}
}
}
condition()
.then(() => {
if (!resolved) {
resolved = true
resolve()
}
})
.catch((err) => {
if (!resolved) {
resolved = true
reject(err)
}
})
setTimeout(waitForCapacityText, pollingIntervalMs)
})
}
async function waitForRecaptcha(
page: Page,
opts: {
pollingIntervalMs?: number
timeoutMs?: number
} = {}
) {
if (!hasNopechaExtension) {
return
}
const { pollingIntervalMs = 100, timeoutMs } = opts
const captcha = await page.$('textarea#g-recaptcha-response')
const startTime = Date.now()
if (captcha) {
console.log('waiting to solve recaptcha...')
do {
const value = (await captcha.evaluate((el) => el.value))?.trim()
if (value?.length) {
// recaptcha has been solved!
break
}
if (timeoutMs) {
const now = Date.now()
if (now - startTime >= timeoutMs) {
throw new TimeoutError('Timed out waiting to solve Recaptcha')
}
}
await delay(pollingIntervalMs)
} while (true)
}
}