mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 05:47:11 +00:00
fix: poe添加proxy支持
This commit is contained in:
parent
63f8629037
commit
1a94d92943
3 changed files with 43 additions and 33 deletions
|
|
@ -1760,7 +1760,8 @@ export class chatgpt extends plugin {
|
||||||
throw new Error('未绑定Poe Cookie,请使用#chatgpt设置Poe token命令绑定cookie')
|
throw new Error('未绑定Poe Cookie,请使用#chatgpt设置Poe token命令绑定cookie')
|
||||||
}
|
}
|
||||||
let client = new PoeClient({
|
let client = new PoeClient({
|
||||||
quora_cookie: cookie
|
quora_cookie: cookie,
|
||||||
|
proxy: Config.proxy
|
||||||
})
|
})
|
||||||
await client.setCredentials()
|
await client.setCredentials()
|
||||||
await client.getChatId()
|
await client.getChatId()
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,14 @@
|
||||||
import fetch from 'node-fetch'
|
import fetch from 'node-fetch'
|
||||||
import { readFileSync, writeFile } from 'fs'
|
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(
|
const _setting = await fetch(
|
||||||
'https://poe.com/api/settings',
|
'https://poe.com/api/settings',
|
||||||
{ headers: { cookie: `${pbCookie}` } }
|
option
|
||||||
)
|
)
|
||||||
if (_setting.status !== 200) throw new Error('Failed to fetch token')
|
if (_setting.status !== 200) throw new Error('Failed to fetch token')
|
||||||
const appSettings = await _setting.json()
|
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(
|
const _setting = await fetch(
|
||||||
`https://poe.com/api/settings?channel=${channelName}`,
|
`https://poe.com/api/settings?channel=${channelName}`,
|
||||||
{ headers: { cookie: `${pbCookie}` } }
|
option
|
||||||
)
|
)
|
||||||
if (_setting.status !== 200) throw new Error('Failed to fetch token')
|
if (_setting.status !== 200) throw new Error('Failed to fetch token')
|
||||||
const appSettings = await _setting.json()
|
const appSettings = await _setting.json()
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,16 @@ import { readFileSync } from 'fs'
|
||||||
import { scrape } from './credential.js'
|
import { scrape } from './credential.js'
|
||||||
import fetch from 'node-fetch'
|
import fetch from 'node-fetch'
|
||||||
import crypto from 'crypto'
|
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
|
// used when test as a single file
|
||||||
// const _path = process.cwd()
|
// const _path = process.cwd()
|
||||||
const _path = process.cwd() + '/plugins/chatgpt-plugin/utils/poe'
|
const _path = process.cwd() + '/plugins/chatgpt-plugin/utils/poe'
|
||||||
|
|
@ -41,7 +51,7 @@ export class PoeClient {
|
||||||
reConnectWs = false
|
reConnectWs = false
|
||||||
|
|
||||||
async setCredentials () {
|
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)
|
console.log(result)
|
||||||
this.config.quora_formkey = result.appSettings.formkey
|
this.config.quora_formkey = result.appSettings.formkey
|
||||||
this.config.channel_name = result.channelName
|
this.config.channel_name = result.channelName
|
||||||
|
|
@ -79,14 +89,18 @@ export class PoeClient {
|
||||||
let payload = JSON.stringify(request)
|
let payload = JSON.stringify(request)
|
||||||
let baseString = payload + this.headers['poe-formkey'] + 'WpuLMiXEKKE98j56k'
|
let baseString = payload + this.headers['poe-formkey'] + 'WpuLMiXEKKE98j56k'
|
||||||
const md5 = crypto.createHash('md5').update(baseString).digest('hex')
|
const md5 = crypto.createHash('md5').update(baseString).digest('hex')
|
||||||
const response = await fetch('https://poe.com/api/gql_POST', {
|
let option = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: Object.assign(this.headers, {
|
headers: Object.assign(this.headers, {
|
||||||
'poe-tag-id': md5,
|
'poe-tag-id': md5,
|
||||||
'content-type': 'application/json'
|
'content-type': 'application/json'
|
||||||
}),
|
}),
|
||||||
body: payload
|
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()
|
let text = await response.text()
|
||||||
try {
|
try {
|
||||||
let result = JSON.parse(text)
|
let result = JSON.parse(text)
|
||||||
|
|
@ -103,9 +117,13 @@ export class PoeClient {
|
||||||
let retry = 10
|
let retry = 10
|
||||||
while (retry >= 0) {
|
while (retry >= 0) {
|
||||||
let url = `https://poe.com/_next/data/${this.nextData.buildId}/${displayName}.json`
|
let url = `https://poe.com/_next/data/${this.nextData.buildId}/${displayName}.json`
|
||||||
let r = await fetch(url, {
|
let option = {
|
||||||
headers: this.headers
|
headers: this.headers
|
||||||
})
|
}
|
||||||
|
if (this.config.proxy) {
|
||||||
|
option.agent = proxy(Config.proxy)
|
||||||
|
}
|
||||||
|
let r = await fetch(url, option)
|
||||||
let res = await r.text()
|
let res = await r.text()
|
||||||
try {
|
try {
|
||||||
let chatData = (JSON.parse(res)).pageProps.payload.chatOfBotDisplayName
|
let chatData = (JSON.parse(res)).pageProps.payload.chatOfBotDisplayName
|
||||||
|
|
@ -119,9 +137,13 @@ export class PoeClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getChatId () {
|
async getChatId () {
|
||||||
let r = await fetch('https://poe.com', {
|
let option = {
|
||||||
headers: this.headers
|
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()
|
let text = await r.text()
|
||||||
const jsonRegex = /<script id="__NEXT_DATA__" type="application\/json">(.+?)<\/script>/
|
const jsonRegex = /<script id="__NEXT_DATA__" type="application\/json">(.+?)<\/script>/
|
||||||
const jsonText = text.match(jsonRegex)[1]
|
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)
|
|
||||||
// })
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue