fix: poe添加proxy支持

This commit is contained in:
ikechan8370 2023-05-14 12:47:20 +08:00
parent 63f8629037
commit 1a94d92943
3 changed files with 43 additions and 33 deletions

View file

@ -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()

View file

@ -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()

View file

@ -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 = /<script id="__NEXT_DATA__" type="application\/json">(.+?)<\/script>/
const jsonText = text.match(jsonRegex)[1]
@ -255,24 +277,3 @@ export class PoeClient {
}
}
}
async function testPoe () {
// const key = 'deb04db9f2332a3287b7d2545061af62'
// const channel = 'poe-chan55-8888-ujygckefewomybvkqfrp'
const cookie = 'p-b=WSvmyvjHVJoMtQVkirtn-A%3D%3D'
let client = new PoeClient({
// quora_formkey: key,
// channel_name: channel,
quora_cookie: cookie
})
await client.setCredentials()
await client.getChatId()
let ai = 'a2'
await client.sendMsg(ai, '你说话不是很通顺啊')
const response = await client.getResponse(ai)
return response
}
// testPoe().then(res => {
// console.log(res)
// })