开启代理后报错
xingyu42 opened this issue · 11 comments
xingyu42 commented
ExCrino commented
我也遇到了这个问题,我自己改了改
以下是修改的地方:
**1.yenai-plugin\lib\request\request.js
getAgent 方法重写为**
async getAgent () {
let HttpsProxyAgent = ''
let { proxyAddress, switchProxy } = Config.proxy
if (!proxyAddress) return null
if (proxyAddress === 'http://0.0.0.0:0') return null
if (HttpsProxyAgent === '') {
HttpsProxyAgent = await import('https-proxy-agent').catch((err) => {
logger.error(err)
})
HttpsProxyAgent = HttpsProxyAgent ? HttpsProxyAgent.HttpsProxyAgent : undefined
}
if (HttpsProxyAgent) {
return new HttpsProxyAgent(proxyAddress)
}
return null
}
get方法中
if (options.params) {
url = url + '?' + qs(options.params)
}
if (!options.agent) options.agent = this.getAgent()
try {
let res = await fetch(url, options)
改为
if (options.data) {
url = url + '?' + qs(options.data)
}
if (!options.agent) options.agent = this.getAgent()
try {
let res = await fetch(url, options.agent)
post方法修改方式同get
ExCrino commented
xingyu42 commented
xingyu42 commented
ExCrino commented
好吧,看来我上次改的还有点问题,治标不治本了属于是
首先是request.js 直接复制代码把原来换了吧,懒得说哪里改了
POST 方法
async post (url, options = {}) {
options.method = 'POST'
options.Agent = null
options.headers = {
'User-Agent': CHROME_UA,
'Content-Type': 'application/json',
...options.headers
}
logger.debug(`[Yenai-Plugin] POST请求:${decodeURI(url)}`)
if (options.data) {
if (/json/.test(options.headers['Content-Type'])) {
options.body = JSON.stringify(options.data)
} else if (
/x-www-form-urlencoded/.test(options.headers['Content-Type'])
) {
options.body = qs(options.data)
} else {
options.body = options.data
}
delete options.data
}
if (!options.Agent) options.Agent = this.getAgent()
try {
let res = await fetch(url, options)
if (!options.closeCheckStatus) {
res = checkStatus(res)
}
if (options.statusCode) {
return res[options.statusCode]()
}
return res
} catch (err) {
logger.error(err)
throw Error(
`Request Post Error,reason:${err.message.match(/reason:(.*)/)[1]}`
)
}
#}
Get方法
async get (url, options = {}) {
// 处理参数
if (options.params) {
url = url + '?' + qs(options.params)
}
logger.debug(`[Yenai-Plugin] GET请求:${decodeURI(url)}`)
options.headers = {
'User-Agent': CHROME_UA,
...options.headers
}
if (!options.agent) options.agent = this.getAgent()
try {
let res = await fetch(url, options.agent)
if (!options.closeCheckStatus) {
res = checkStatus(res)
}
if (options.statusCode) {
return res[options.statusCode]()
}
return res
} catch (err) {
logger.error(err)
throw Error(
`Request Get Error,${err.message.match(/reason:(.*)/i) || err.message}`
)
}
}
接下来是model\setu.js中
async setuApi (r18, num = 1, tag = []) {
let api = 'https://api.lolicon.app/setu/v2'
const { imgSize, excludeAI } = Config.setu
const size = imgSize[_.max(Object.keys(imgSize).filter(item => num > item))] || 'original'
let params = {
r18,
num,
tag,
size,
excludeAI
}
let result = await request.post(api, { data: params }).then(res => res.json())
if (_.isEmpty(result.data)) throw Error('没有找到相关的tag')
// 消息
return await Promise.all(result.data.map(async item => {
let { pid, title, tags, author, r18, urls, aiType } = item
return [
`${this.sendMsgs}\n`,
`标题:${title}\n`,
`画师:${author}\n`,
`Pid:${pid}\n`,
`R18:${r18}\n`,
`AI:${aiType ? aiType == 1 ? '是' : '否' : '未知'}\n`,
`tag:${_.truncate(tags.join(','))}\n`,
await Pixiv._requestPixivImg(urls?.original || urls?.regular || urls?.small)
]
}))
}
ExCrino commented
@ExCrino 改了后图可以搜了,就pixiv登录信息还是走不了,算了这个也不重要
最后还是从根本上解决了问题
request.js可以改回原来的样子
httpsProxyAgentMod.js里改一下就好
export class HttpsProxyAgent extends HttpsProxyAgentOrig.HttpsProxyAgent {
constructor (opts) {
super(opts)
this.tlsConnectionOptions = opts.tls
const callback = (req, opts) => callback(req, Object.assign(opts, this.tlsConnectionOptions)).bind(this)
}
}
xingyu42 commented
@ExCrino 改了后图可以搜了,就pixiv登录信息还是走不了,算了这个也不重要
最后还是从根本上解决了问题
request.js可以改回原来的样子
httpsProxyAgentMod.js里改一下就好
export class HttpsProxyAgent extends HttpsProxyAgentOrig.HttpsProxyAgent { constructor (opts) { super(opts) this.tlsConnectionOptions = opts.tls const callback = (req, opts) => callback(req, Object.assign(opts, this.tlsConnectionOptions)).bind(this) } }
搞了一大圈最后在源头上改一下就好了,某种意义上的戏剧性,谢谢没想到是这样解决的
ExCrino commented
@ExCrino 改了后图可以搜了,就pixiv登录信息还是走不了,算了这个也不重要
最后还是从根本上解决了问题
request.js可以改回原来的样子
httpsProxyAgentMod.js里改一下就好export class HttpsProxyAgent extends HttpsProxyAgentOrig.HttpsProxyAgent { constructor (opts) { super(opts) this.tlsConnectionOptions = opts.tls const callback = (req, opts) => callback(req, Object.assign(opts, this.tlsConnectionOptions)).bind(this) } }搞了一大圈最后在源头上改一下就好了,某种意义上的戏剧性,谢谢没想到是这样解决的
因为我改了半天也显示不了P站的图片,最后发现是梯子的规则里没有P站,不是代码的问题
misaka20002 commented
改了httpsProxyAgentMod.js还是不行呀
xingyu42 commented
改了httpsProxyAgentMod.js还是不行呀
没日志怎么看,现在好了吗