Skip to content

Commit 6272303

Browse files
committed
🐛 Fix: some case will cause proxy not work
1 parent e19bb6e commit 6272303

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

src/lib/Request.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,36 @@ function requestInterceptor (options: IOldReqOptions | AxiosRequestConfig): Axio
3333
url: (options.url as string) || '',
3434
headers: options.headers || {}
3535
}
36-
36+
// user request config proxy
3737
if (options.proxy) {
38-
if (typeof options.proxy === 'string') {
38+
let proxyOptions = options.proxy
39+
if (typeof proxyOptions === 'string') {
3940
try {
40-
const proxyOptions = new URL(options.proxy)
41-
if (options.url?.startsWith('https://')) {
42-
opt.proxy = false
43-
opt.httpsAgent = tunnel.httpsOverHttp({
44-
proxy: {
45-
host: proxyOptions.hostname,
46-
port: parseInt(proxyOptions.port, 10)
47-
}
48-
})
49-
} else {
50-
opt.proxy = {
51-
host: proxyOptions.hostname,
52-
port: parseInt(proxyOptions.port, 10),
53-
protocol: 'http'
54-
}
55-
}
41+
proxyOptions = new URL(options.proxy)
5642
} catch (e) {
43+
proxyOptions = false
44+
opt.proxy = false
45+
console.error(e)
5746
}
5847
__isOldOptions = true
5948
}
49+
if (proxyOptions) {
50+
if (options.url?.startsWith('https://')) {
51+
opt.proxy = false
52+
opt.httpsAgent = tunnel.httpsOverHttp({
53+
proxy: {
54+
host: proxyOptions?.hostname,
55+
port: parseInt(proxyOptions?.port, 10)
56+
}
57+
})
58+
} else {
59+
opt.proxy = {
60+
host: proxyOptions.hostname,
61+
port: parseInt(proxyOptions.port, 10),
62+
protocol: 'http'
63+
}
64+
}
65+
}
6066
}
6167
if ('formData' in options) {
6268
const form = new FormData()
@@ -172,7 +178,19 @@ export class Request implements IRequest {
172178
this.options.headers = options.headers || {}
173179
this.options.maxBodyLength = Infinity
174180
this.options.maxContentLength = Infinity
175-
this.options.httpsAgent = httpsAgent
181+
if (this.options.proxy && options.url?.startsWith('https://')) {
182+
this.options.httpsAgent = tunnel.httpsOverHttp({
183+
proxy: {
184+
host: this.options.proxy.host,
185+
port: this.options.proxy.port
186+
}
187+
})
188+
this.options.proxy = false
189+
} else {
190+
this.options.httpsAgent = httpsAgent
191+
}
192+
// !NOTICE this.options !== options
193+
// this.options is the default options
176194
const instance = axios.create(this.options)
177195
instance.interceptors.response.use(responseInterceptor, responseErrorHandler)
178196

src/plugins/commander/i18n.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const i18n: IPlugin = {
66
cmd.program
77
.command('i18n')
88
.arguments('[lang]')
9+
.description('change picgo language')
910
.action(async (lang: string = '') => {
1011
const list = ctx.i18n.getLanguageList()
1112
if (!lang) {

src/plugins/transformer/path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
1111
await Promise.all(ctx.input.map(async (item: string, index: number) => {
1212
let info: IPathTransformedImgInfo
1313
if (isUrl(item)) {
14-
info = await getURLFile(item)
14+
info = await getURLFile(item, ctx)
1515
} else {
1616
info = await getFSFile(item)
1717
}

src/plugins/uploader/smms.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ const postOptions = (fileName: string, image: Buffer, apiToken: string, backupDo
2626

2727
const handle = async (ctx: IPicGo): Promise<IPicGo> => {
2828
const smmsConfig = ctx.getConfig<ISmmsConfig>('picBed.smms')
29+
if (!smmsConfig) {
30+
throw new Error('Can not find smms config!')
31+
}
2932
const imgList = ctx.output
3033
for (const img of imgList) {
3134
if (img.fileName && img.buffer) {
3235
let image = img.buffer
3336
if (!image && img.base64Image) {
3437
image = Buffer.from(img.base64Image, 'base64')
3538
}
36-
const postConfig = postOptions(img.fileName, image, smmsConfig?.token, smmsConfig.backupDomain)
39+
const postConfig = postOptions(img.fileName, image, smmsConfig?.token, smmsConfig?.backupDomain)
3740
try {
3841
const res: string = await ctx.request(postConfig)
3942
const body = JSON.parse(res)
@@ -50,7 +53,7 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
5053
title: ctx.i18n.translate<ILocalesKey>('UPLOAD_FAILED'),
5154
body: body.message
5255
})
53-
throw new Error(body.message)
56+
throw new Error(body)
5457
}
5558
} catch (e: any) {
5659
ctx.log.error(e)

src/utils/common.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import axios from 'axios'
21
import fs from 'fs-extra'
32
import path from 'path'
43
import { imageSize } from 'image-size'
54
import {
65
IImgSize,
76
IPathTransformedImgInfo,
87
IPluginNameType,
9-
ILogger
8+
ILogger,
9+
IPicGo
1010
} from '../types'
1111
import { URL } from 'url'
1212

@@ -62,15 +62,18 @@ export const getFSFile = async (filePath: string): Promise<IPathTransformedImgIn
6262
}
6363
}
6464

65-
export const getURLFile = async (url: string): Promise<IPathTransformedImgInfo> => {
65+
export const getURLFile = async (url: string, ctx: IPicGo): Promise<IPathTransformedImgInfo> => {
6666
url = handleUrlEncode(url)
6767
let isImage = false
6868
let extname = ''
6969
let timeoutId: NodeJS.Timeout
7070
const requestFn = new Promise<IPathTransformedImgInfo>((resolve, reject) => {
7171
(async () => {
7272
try {
73-
const res = await axios.get(url, {
73+
const res = await ctx.request({
74+
method: 'get',
75+
url,
76+
resolveWithFullResponse: true,
7477
responseType: 'arraybuffer'
7578
})
7679
.then((resp) => {
@@ -79,7 +82,7 @@ export const getURLFile = async (url: string): Promise<IPathTransformedImgInfo>
7982
isImage = true
8083
extname = `.${contentType.split('image/')[1]}`
8184
}
82-
return resp.data
85+
return resp.data as Buffer
8386
})
8487
clearTimeout(timeoutId)
8588
if (isImage) {

0 commit comments

Comments
 (0)