fix: 兼容5和7的proxy依赖

This commit is contained in:
ikechan8370 2023-09-12 11:08:14 +08:00
parent 5e7d73908f
commit f620e10abb
8 changed files with 727 additions and 334 deletions

View file

@ -1,17 +1,11 @@
import fetch, { FormData } from 'node-fetch'
import { makeForwardMsg } from './common.js'
import { Config } from './config.js'
import { getProxy } from './proxy.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')
}
}
let proxy = getProxy()
export default class BingDrawClient {
constructor(opts) {
constructor (opts) {
this.opts = opts
if (Config.proxy && !Config.sydneyForceUseReverse) {
// 如果设置代理,走代理
@ -19,7 +13,7 @@ export default class BingDrawClient {
}
}
async getImages(prompt, e) {
async getImages (prompt, e) {
let urlEncodedPrompt = encodeURIComponent(prompt)
let url = `${this.opts.baseUrl}/images/create?q=${urlEncodedPrompt}&rt=4&FORM=GENCRE`
// let d = Math.ceil(Math.random() * 255)
@ -82,7 +76,7 @@ export default class BingDrawClient {
}
}
if (!success) {
//最后尝试使用https://cn.bing.com进行一次绘图
// 最后尝试使用https://cn.bing.com进行一次绘图
logger.info('尝试使用https://cn.bing.com进行绘图')
url = `https://cn.bing.com/images/create?q=${urlEncodedPrompt}&rt=3&FORM=GENCRE`
fetchOptions.referrer = 'https://cn.bing.com/images/create/'

View file

@ -6,11 +6,11 @@ import fetch, {
} from 'node-fetch'
import crypto from 'crypto'
import WebSocket from 'ws'
import HttpsProxyAgent from 'https-proxy-agent'
import { Config, pureSydneyInstruction } from './config.js'
import { formatDate, getMasterQQ, isCN, getUserData } from './common.js'
import delay from 'delay'
import moment from 'moment'
import { getProxy } from './proxy.js'
if (!globalThis.fetch) {
globalThis.fetch = fetch
@ -19,12 +19,7 @@ if (!globalThis.fetch) {
globalThis.Response = Response
}
// workaround for ver 7.x and ver 5.x
let proxy = HttpsProxyAgent
if (typeof proxy !== 'function') {
proxy = (p) => {
return new HttpsProxyAgent.HttpsProxyAgent(p)
}
}
let proxy = getProxy()
async function getKeyv () {
let Keyv

View file

@ -2,21 +2,8 @@ import { Configuration, OpenAIApi } from 'openai'
import { Config, defaultOpenAIAPI, defaultOpenAIReverseProxy } from './config.js'
import fs from 'fs'
import { isCN, mkdirs } from './common.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')
}
}
function getProxy () {
if (!Config.proxy || proxy) {
return proxy
} else {
throw new Error('未安装https-proxy-agent请在插件目录下执行pnpm add https-proxy-agent')
}
}
import { getProxy } from './proxy.js'
let proxy = getProxy()
export async function createImage (prompt, n = 1, size = '512x512') {
let basePath = Config.openAiBaseUrl
if (Config.openAiBaseUrl && Config.proxy && !Config.openAiForceUseReverse) {
@ -34,7 +21,7 @@ export async function createImage (prompt, n = 1, size = '512x512') {
if (Config.debug) {
logger.info({ prompt, n, size })
}
let proxyFn = getProxy()
let proxyFn = proxy
const response = await openai.createImage({
prompt,
n,

17
utils/proxy.js Normal file
View file

@ -0,0 +1,17 @@
// workaround for ver 7.x and ver 5.x
import HttpsProxyAgent from 'https-proxy-agent'
let proxy = HttpsProxyAgent
if (typeof proxy !== 'function') {
proxy = (p) => {
return new HttpsProxyAgent.HttpsProxyAgent(p)
}
}
/**
* return a proxy function
* @returns {*|createHttpsProxyAgent|((opts: (string | createHttpsProxyAgent.HttpsProxyAgentOptions)) => HttpsProxyAgent)}
*/
export function getProxy () {
return proxy
}

View file

@ -1,14 +1,8 @@
import { Config } from './config.js'
import { ChatGPTAPI } from 'chatgpt'
import fetch from 'node-fetch'
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')
}
}
import { getProxy } from './proxy.js'
let proxy = getProxy()
const newFetch = (url, options = {}) => {
const defaultOptions = Config.proxy
? {