From 1a94d9294311b6416220051cd81cdc6ff742b45d Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Sun, 14 May 2023 12:47:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20poe=E6=B7=BB=E5=8A=A0proxy=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/chat.js | 3 ++- utils/poe/credential.js | 16 +++++++++--- utils/poe/index.js | 57 +++++++++++++++++++++-------------------- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/apps/chat.js b/apps/chat.js index 387f63c..b1b4ae0 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -1760,7 +1760,8 @@ export class chatgpt extends plugin { throw new Error('未绑定Poe Cookie,请使用#chatgpt设置Poe token命令绑定cookie') } let client = new PoeClient({ - quora_cookie: cookie + quora_cookie: cookie, + proxy: Config.proxy }) await client.setCredentials() await client.getChatId() diff --git a/utils/poe/credential.js b/utils/poe/credential.js index 2cf5fcf..572b0a2 100644 --- a/utils/poe/credential.js +++ b/utils/poe/credential.js @@ -1,10 +1,14 @@ import fetch from 'node-fetch' import { readFileSync, writeFile } from 'fs' -const scrape = async (pbCookie) => { +const scrape = async (pbCookie, proxy) => { + let option = { headers: { cookie: `${pbCookie}` } } + if (proxy) { + option.proxy = proxy + } const _setting = await fetch( 'https://poe.com/api/settings', - { headers: { cookie: `${pbCookie}` } } + option ) if (_setting.status !== 200) throw new Error('Failed to fetch token') const appSettings = await _setting.json() @@ -17,10 +21,14 @@ const scrape = async (pbCookie) => { } } -const getUpdatedSettings = async (channelName, pbCookie) => { +const getUpdatedSettings = async (channelName, pbCookie, proxy) => { + let option = { headers: { cookie: `${pbCookie}` } } + if (proxy) { + option.proxy = proxy + } const _setting = await fetch( `https://poe.com/api/settings?channel=${channelName}`, - { headers: { cookie: `${pbCookie}` } } + option ) if (_setting.status !== 200) throw new Error('Failed to fetch token') const appSettings = await _setting.json() diff --git a/utils/poe/index.js b/utils/poe/index.js index a01189a..668b126 100644 --- a/utils/poe/index.js +++ b/utils/poe/index.js @@ -2,6 +2,16 @@ import { readFileSync } from 'fs' import { scrape } from './credential.js' import fetch from 'node-fetch' import crypto from 'crypto' +import { Config } from '../config.js' + +let proxy +if (Config.proxy) { + try { + proxy = (await import('https-proxy-agent')).default + } catch (e) { + console.warn('未安装https-proxy-agent,请在插件目录下执行pnpm add https-proxy-agent') + } +} // used when test as a single file // const _path = process.cwd() const _path = process.cwd() + '/plugins/chatgpt-plugin/utils/poe' @@ -41,7 +51,7 @@ export class PoeClient { reConnectWs = false async setCredentials () { - let result = await scrape(this.config.quora_cookie) + let result = await scrape(this.config.quora_cookie, this.config.proxy ? proxy(Config.proxy) : null) console.log(result) this.config.quora_formkey = result.appSettings.formkey this.config.channel_name = result.channelName @@ -79,14 +89,18 @@ export class PoeClient { let payload = JSON.stringify(request) let baseString = payload + this.headers['poe-formkey'] + 'WpuLMiXEKKE98j56k' const md5 = crypto.createHash('md5').update(baseString).digest('hex') - const response = await fetch('https://poe.com/api/gql_POST', { + let option = { method: 'POST', headers: Object.assign(this.headers, { 'poe-tag-id': md5, 'content-type': 'application/json' }), body: payload - }) + } + if (this.config.proxy) { + option.agent = proxy(Config.proxy) + } + const response = await fetch('https://poe.com/api/gql_POST', option) let text = await response.text() try { let result = JSON.parse(text) @@ -103,9 +117,13 @@ export class PoeClient { let retry = 10 while (retry >= 0) { let url = `https://poe.com/_next/data/${this.nextData.buildId}/${displayName}.json` - let r = await fetch(url, { + let option = { headers: this.headers - }) + } + if (this.config.proxy) { + option.agent = proxy(Config.proxy) + } + let r = await fetch(url, option) let res = await r.text() try { let chatData = (JSON.parse(res)).pageProps.payload.chatOfBotDisplayName @@ -119,9 +137,13 @@ export class PoeClient { } async getChatId () { - let r = await fetch('https://poe.com', { + let option = { headers: this.headers - }) + } + if (this.config.proxy) { + option.agent = proxy(Config.proxy) + } + let r = await fetch('https://poe.com', option) let text = await r.text() const jsonRegex = /