Skip to content

Commit

Permalink
🐛 Fix: some case will cause proxy not work
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Nov 13, 2022
1 parent e19bb6e commit 6272303
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
56 changes: 37 additions & 19 deletions src/lib/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions src/plugins/commander/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/transformer/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
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)
}
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/uploader/smms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ const postOptions = (fileName: string, image: Buffer, apiToken: string, backupDo

const handle = async (ctx: IPicGo): Promise<IPicGo> => {
const smmsConfig = ctx.getConfig<ISmmsConfig>('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) {
let image = img.buffer
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)
Expand All @@ -50,7 +53,7 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
title: ctx.i18n.translate<ILocalesKey>('UPLOAD_FAILED'),
body: body.message
})
throw new Error(body.message)
throw new Error(body)
}
} catch (e: any) {
ctx.log.error(e)
Expand Down
13 changes: 8 additions & 5 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import axios from 'axios'
import fs from 'fs-extra'
import path from 'path'
import { imageSize } from 'image-size'
import {
IImgSize,
IPathTransformedImgInfo,
IPluginNameType,
ILogger
ILogger,
IPicGo
} from '../types'
import { URL } from 'url'

Expand Down Expand Up @@ -62,15 +62,18 @@ export const getFSFile = async (filePath: string): Promise<IPathTransformedImgIn
}
}

export const getURLFile = async (url: string): Promise<IPathTransformedImgInfo> => {
export const getURLFile = async (url: string, ctx: IPicGo): Promise<IPathTransformedImgInfo> => {
url = handleUrlEncode(url)
let isImage = false
let extname = ''
let timeoutId: NodeJS.Timeout
const requestFn = new Promise<IPathTransformedImgInfo>((resolve, reject) => {
(async () => {
try {
const res = await axios.get(url, {
const res = await ctx.request({
method: 'get',
url,
resolveWithFullResponse: true,
responseType: 'arraybuffer'
})
.then((resp) => {
Expand All @@ -79,7 +82,7 @@ export const getURLFile = async (url: string): Promise<IPathTransformedImgInfo>
isImage = true
extname = `.${contentType.split('image/')[1]}`
}
return resp.data
return resp.data as Buffer
})
clearTimeout(timeoutId)
if (isImage) {
Expand Down

0 comments on commit 6272303

Please sign in to comment.