|
|
|
@ -1,8 +1,51 @@
|
|
|
|
|
const url = require('url')
|
|
|
|
|
const lodash = require('lodash')
|
|
|
|
|
|
|
|
|
|
function doProxy (proxyConf, rOptions, req, interceptOpt) {
|
|
|
|
|
// 获取代理目标地址
|
|
|
|
|
let proxyTarget
|
|
|
|
|
if (interceptOpt && interceptOpt.replace) {
|
|
|
|
|
const regexp = new RegExp(interceptOpt.replace)
|
|
|
|
|
proxyTarget = req.url.replace(regexp, proxyConf)
|
|
|
|
|
} else if (proxyConf.indexOf('http:') === 0 || proxyConf.indexOf('https:') === 0) {
|
|
|
|
|
proxyTarget = proxyConf
|
|
|
|
|
} else {
|
|
|
|
|
let uri = req.url
|
|
|
|
|
if (uri.indexOf('http') === 0) {
|
|
|
|
|
// eslint-disable-next-line node/no-deprecated-api
|
|
|
|
|
const URL = url.parse(uri)
|
|
|
|
|
uri = URL.path
|
|
|
|
|
}
|
|
|
|
|
proxyTarget = proxyConf + uri
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
// no-template-curly-in-string
|
|
|
|
|
// eslint-disable-next-line no-template-curly-in-string
|
|
|
|
|
proxyTarget = proxyTarget.replace('${host}', rOptions.hostname)
|
|
|
|
|
|
|
|
|
|
const proxy = proxyTarget.indexOf('http:') === 0 || proxyTarget.indexOf('https:') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget
|
|
|
|
|
// eslint-disable-next-line node/no-deprecated-api
|
|
|
|
|
const URL = url.parse(proxy)
|
|
|
|
|
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数
|
|
|
|
|
delete rOptions.origional.agent
|
|
|
|
|
delete rOptions.origional.headers
|
|
|
|
|
rOptions.protocol = URL.protocol
|
|
|
|
|
rOptions.hostname = URL.host
|
|
|
|
|
rOptions.host = URL.host
|
|
|
|
|
rOptions.headers.host = URL.host
|
|
|
|
|
rOptions.path = URL.path
|
|
|
|
|
if (URL.port == null) {
|
|
|
|
|
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return proxyTarget
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
name: 'proxy',
|
|
|
|
|
priority: 121,
|
|
|
|
|
doProxy,
|
|
|
|
|
requestIntercept (context, interceptOpt, req, res, ssl, next) {
|
|
|
|
|
const { rOptions, log, RequestCounter } = context
|
|
|
|
|
|
|
|
|
@ -33,42 +76,8 @@ module.exports = {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取代理目标地址
|
|
|
|
|
let proxyTarget
|
|
|
|
|
if (interceptOpt.replace) {
|
|
|
|
|
const regexp = new RegExp(interceptOpt.replace)
|
|
|
|
|
proxyTarget = req.url.replace(regexp, proxyConf)
|
|
|
|
|
} else if (proxyConf.indexOf('http:') === 0 || proxyConf.indexOf('https:') === 0) {
|
|
|
|
|
proxyTarget = proxyConf
|
|
|
|
|
} else {
|
|
|
|
|
let uri = req.url
|
|
|
|
|
if (uri.indexOf('http') === 0) {
|
|
|
|
|
// eslint-disable-next-line node/no-deprecated-api
|
|
|
|
|
const URL = url.parse(uri)
|
|
|
|
|
uri = URL.path
|
|
|
|
|
}
|
|
|
|
|
proxyTarget = proxyConf + uri
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
// no-template-curly-in-string
|
|
|
|
|
// eslint-disable-next-line no-template-curly-in-string
|
|
|
|
|
proxyTarget = proxyTarget.replace('${host}', rOptions.hostname)
|
|
|
|
|
|
|
|
|
|
const proxy = proxyTarget.indexOf('http') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget
|
|
|
|
|
// eslint-disable-next-line node/no-deprecated-api
|
|
|
|
|
const URL = url.parse(proxy)
|
|
|
|
|
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数
|
|
|
|
|
delete rOptions.origional.agent
|
|
|
|
|
delete rOptions.origional.headers
|
|
|
|
|
rOptions.protocol = URL.protocol
|
|
|
|
|
rOptions.hostname = URL.host
|
|
|
|
|
rOptions.host = URL.host
|
|
|
|
|
rOptions.headers.host = URL.host
|
|
|
|
|
rOptions.path = URL.path
|
|
|
|
|
if (URL.port == null) {
|
|
|
|
|
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80
|
|
|
|
|
}
|
|
|
|
|
// 替换 rOptions 中的地址,并返回代理目标地址
|
|
|
|
|
const proxyTarget = doProxy(proxyConf, rOptions, req, interceptOpt)
|
|
|
|
|
|
|
|
|
|
if (context.requestCount) {
|
|
|
|
|
log.info('proxy choice:', JSON.stringify(context.requestCount))
|
|
|
|
|