From 62723038b45eaac6db65875aeb9b002e58f9990d Mon Sep 17 00:00:00 2001 From: PiEgg Date: Sun, 13 Nov 2022 13:11:59 +0800 Subject: [PATCH] :bug: Fix: some case will cause proxy not work --- src/lib/Request.ts | 56 ++++++++++++++++++++++----------- src/plugins/commander/i18n.ts | 1 + src/plugins/transformer/path.ts | 2 +- src/plugins/uploader/smms.ts | 7 +++-- src/utils/common.ts | 13 +++++--- 5 files changed, 52 insertions(+), 27 deletions(-) diff --git a/src/lib/Request.ts b/src/lib/Request.ts index 93b1944..4c7494b 100644 --- a/src/lib/Request.ts +++ b/src/lib/Request.ts @@ -33,30 +33,36 @@ function requestInterceptor (options: IOldReqOptions | AxiosRequestConfig): Axio url: (options.url as string) || '', headers: options.headers || {} } - + // user request config proxy if (options.proxy) { - if (typeof options.proxy === 'string') { + let proxyOptions = options.proxy + if (typeof proxyOptions === 'string') { try { - const proxyOptions = new URL(options.proxy) - if (options.url?.startsWith('https://')) { - opt.proxy = false - opt.httpsAgent = tunnel.httpsOverHttp({ - proxy: { - host: proxyOptions.hostname, - port: parseInt(proxyOptions.port, 10) - } - }) - } else { - opt.proxy = { - host: proxyOptions.hostname, - port: parseInt(proxyOptions.port, 10), - protocol: 'http' - } - } + proxyOptions = new URL(options.proxy) } catch (e) { + proxyOptions = false + opt.proxy = false + console.error(e) } __isOldOptions = true } + if (proxyOptions) { + if (options.url?.startsWith('https://')) { + opt.proxy = false + opt.httpsAgent = tunnel.httpsOverHttp({ + proxy: { + host: proxyOptions?.hostname, + port: parseInt(proxyOptions?.port, 10) + } + }) + } else { + opt.proxy = { + host: proxyOptions.hostname, + port: parseInt(proxyOptions.port, 10), + protocol: 'http' + } + } + } } if ('formData' in options) { const form = new FormData() @@ -172,7 +178,19 @@ export class Request implements IRequest { this.options.headers = options.headers || {} this.options.maxBodyLength = Infinity this.options.maxContentLength = Infinity - this.options.httpsAgent = httpsAgent + if (this.options.proxy && options.url?.startsWith('https://')) { + this.options.httpsAgent = tunnel.httpsOverHttp({ + proxy: { + host: this.options.proxy.host, + port: this.options.proxy.port + } + }) + this.options.proxy = false + } else { + this.options.httpsAgent = httpsAgent + } + // !NOTICE this.options !== options + // this.options is the default options const instance = axios.create(this.options) instance.interceptors.response.use(responseInterceptor, responseErrorHandler) diff --git a/src/plugins/commander/i18n.ts b/src/plugins/commander/i18n.ts index 73bccc0..5f59c62 100644 --- a/src/plugins/commander/i18n.ts +++ b/src/plugins/commander/i18n.ts @@ -6,6 +6,7 @@ const i18n: IPlugin = { cmd.program .command('i18n') .arguments('[lang]') + .description('change picgo language') .action(async (lang: string = '') => { const list = ctx.i18n.getLanguageList() if (!lang) { diff --git a/src/plugins/transformer/path.ts b/src/plugins/transformer/path.ts index 470ebac..8a3e2e5 100644 --- a/src/plugins/transformer/path.ts +++ b/src/plugins/transformer/path.ts @@ -11,7 +11,7 @@ const handle = async (ctx: IPicGo): Promise => { await Promise.all(ctx.input.map(async (item: string, index: number) => { let info: IPathTransformedImgInfo if (isUrl(item)) { - info = await getURLFile(item) + info = await getURLFile(item, ctx) } else { info = await getFSFile(item) } diff --git a/src/plugins/uploader/smms.ts b/src/plugins/uploader/smms.ts index 0a77c7d..97631c6 100644 --- a/src/plugins/uploader/smms.ts +++ b/src/plugins/uploader/smms.ts @@ -26,6 +26,9 @@ const postOptions = (fileName: string, image: Buffer, apiToken: string, backupDo const handle = async (ctx: IPicGo): Promise => { const smmsConfig = ctx.getConfig('picBed.smms') + if (!smmsConfig) { + throw new Error('Can not find smms config!') + } const imgList = ctx.output for (const img of imgList) { if (img.fileName && img.buffer) { @@ -33,7 +36,7 @@ const handle = async (ctx: IPicGo): Promise => { if (!image && img.base64Image) { image = Buffer.from(img.base64Image, 'base64') } - const postConfig = postOptions(img.fileName, image, smmsConfig?.token, smmsConfig.backupDomain) + const postConfig = postOptions(img.fileName, image, smmsConfig?.token, smmsConfig?.backupDomain) try { const res: string = await ctx.request(postConfig) const body = JSON.parse(res) @@ -50,7 +53,7 @@ const handle = async (ctx: IPicGo): Promise => { title: ctx.i18n.translate('UPLOAD_FAILED'), body: body.message }) - throw new Error(body.message) + throw new Error(body) } } catch (e: any) { ctx.log.error(e) diff --git a/src/utils/common.ts b/src/utils/common.ts index 41c49a3..c0ed119 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -1,4 +1,3 @@ -import axios from 'axios' import fs from 'fs-extra' import path from 'path' import { imageSize } from 'image-size' @@ -6,7 +5,8 @@ import { IImgSize, IPathTransformedImgInfo, IPluginNameType, - ILogger + ILogger, + IPicGo } from '../types' import { URL } from 'url' @@ -62,7 +62,7 @@ export const getFSFile = async (filePath: string): Promise => { +export const getURLFile = async (url: string, ctx: IPicGo): Promise => { url = handleUrlEncode(url) let isImage = false let extname = '' @@ -70,7 +70,10 @@ export const getURLFile = async (url: string): Promise const requestFn = new Promise((resolve, reject) => { (async () => { try { - const res = await axios.get(url, { + const res = await ctx.request({ + method: 'get', + url, + resolveWithFullResponse: true, responseType: 'arraybuffer' }) .then((resp) => { @@ -79,7 +82,7 @@ export const getURLFile = async (url: string): Promise isImage = true extname = `.${contentType.split('image/')[1]}` } - return resp.data + return resp.data as Buffer }) clearTimeout(timeoutId) if (isImage) {