|
|
|
@ -1,41 +1,10 @@
|
|
|
|
|
const url = require('url')
|
|
|
|
|
const lodash = require('lodash')
|
|
|
|
|
module.exports = {
|
|
|
|
|
name: 'proxy',
|
|
|
|
|
priority: 121,
|
|
|
|
|
requestIntercept (context, interceptOpt, req, res, ssl, next) {
|
|
|
|
|
const { rOptions, log, RequestCounter } = context
|
|
|
|
|
|
|
|
|
|
const originHostname = rOptions.hostname
|
|
|
|
|
|
|
|
|
|
let proxyConf = interceptOpt.proxy
|
|
|
|
|
if (RequestCounter && interceptOpt.backup && interceptOpt.backup.length > 0) {
|
|
|
|
|
// 优选逻辑
|
|
|
|
|
const backupList = [proxyConf]
|
|
|
|
|
for (const bk of interceptOpt.backup) {
|
|
|
|
|
backupList.push(bk)
|
|
|
|
|
}
|
|
|
|
|
const key = rOptions.hostname + '/' + interceptOpt.key
|
|
|
|
|
const count = RequestCounter.getOrCreate(key, backupList)
|
|
|
|
|
if (count.value == null) {
|
|
|
|
|
count.doRank()
|
|
|
|
|
}
|
|
|
|
|
if (count.value == null) {
|
|
|
|
|
log.error('`count.value` is null, the count:', count)
|
|
|
|
|
} else {
|
|
|
|
|
count.doCount(count.value)
|
|
|
|
|
proxyConf = count.value
|
|
|
|
|
context.requestCount = {
|
|
|
|
|
key,
|
|
|
|
|
value: count.value,
|
|
|
|
|
count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function doProxy (proxyConf, rOptions, req, interceptOpt) {
|
|
|
|
|
// 获取代理目标地址
|
|
|
|
|
let proxyTarget
|
|
|
|
|
if (interceptOpt.replace) {
|
|
|
|
|
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) {
|
|
|
|
@ -55,7 +24,7 @@ module.exports = {
|
|
|
|
|
// 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
|
|
|
|
|
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) // 备份原始请求参数
|
|
|
|
@ -70,6 +39,46 @@ module.exports = {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
const originHostname = rOptions.hostname
|
|
|
|
|
|
|
|
|
|
let proxyConf = interceptOpt.proxy
|
|
|
|
|
if (RequestCounter && interceptOpt.backup && interceptOpt.backup.length > 0) {
|
|
|
|
|
// 优选逻辑
|
|
|
|
|
const backupList = [proxyConf]
|
|
|
|
|
for (const bk of interceptOpt.backup) {
|
|
|
|
|
backupList.push(bk)
|
|
|
|
|
}
|
|
|
|
|
const key = rOptions.hostname + '/' + interceptOpt.key
|
|
|
|
|
const count = RequestCounter.getOrCreate(key, backupList)
|
|
|
|
|
if (count.value == null) {
|
|
|
|
|
count.doRank()
|
|
|
|
|
}
|
|
|
|
|
if (count.value == null) {
|
|
|
|
|
log.error('`count.value` is null, the count:', count)
|
|
|
|
|
} else {
|
|
|
|
|
count.doCount(count.value)
|
|
|
|
|
proxyConf = count.value
|
|
|
|
|
context.requestCount = {
|
|
|
|
|
key,
|
|
|
|
|
value: count.value,
|
|
|
|
|
count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 替换 rOptions 中的地址,并返回代理目标地址
|
|
|
|
|
const proxyTarget = doProxy(proxyConf, rOptions, req, interceptOpt)
|
|
|
|
|
|
|
|
|
|
if (context.requestCount) {
|
|
|
|
|
log.info('proxy choice:', JSON.stringify(context.requestCount))
|
|
|
|
|
}
|
|
|
|
|