重构 proxy.js 和 redirect.js,使它们的逻辑代码复用。
parent
0069e1c26b
commit
3c45b06bd5
|
@ -1,41 +1,62 @@
|
||||||
const url = require('url')
|
const url = require('url')
|
||||||
const lodash = require('lodash')
|
const lodash = require('lodash')
|
||||||
|
|
||||||
function doProxy (proxyConf, rOptions, req, interceptOpt, matched) {
|
// 替换占位符
|
||||||
// 获取代理目标地址
|
function replacePlaceholder (url, rOptions, matched) {
|
||||||
let proxyTarget
|
if (url.indexOf('${') >= 0) {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
// no-template-curly-in-string
|
||||||
|
// eslint-disable-next-line no-template-curly-in-string
|
||||||
|
url = url.replace('${host}', rOptions.hostname)
|
||||||
|
|
||||||
|
if (matched && url.indexOf('${') >= 0) {
|
||||||
|
for (let i = 0; i < matched.length; i++) {
|
||||||
|
url = url.replace('${m[' + i + ']}', matched[i] == null ? '' : matched[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移除多余的占位符
|
||||||
|
if (url.indexOf('${') >= 0) {
|
||||||
|
url = url.replace(/\$\{[^}]+}/g, '')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildTargetUrl (rOptions, urlConf, interceptOpt, matched) {
|
||||||
|
let targetUrl
|
||||||
if (interceptOpt && interceptOpt.replace) {
|
if (interceptOpt && interceptOpt.replace) {
|
||||||
const regexp = new RegExp(interceptOpt.replace)
|
const regexp = new RegExp(interceptOpt.replace)
|
||||||
proxyTarget = req.url.replace(regexp, proxyConf)
|
targetUrl = rOptions.path.replace(regexp, urlConf)
|
||||||
} else if (proxyConf.indexOf('http:') === 0 || proxyConf.indexOf('https:') === 0) {
|
} else if (urlConf.indexOf('http:') === 0 || urlConf.indexOf('https:') === 0) {
|
||||||
proxyTarget = proxyConf
|
targetUrl = urlConf
|
||||||
} else {
|
} else {
|
||||||
let uri = req.url
|
let uri = rOptions.path
|
||||||
if (uri.indexOf('http') === 0) {
|
if (uri.indexOf('http') === 0) {
|
||||||
// eslint-disable-next-line node/no-deprecated-api
|
// eslint-disable-next-line node/no-deprecated-api
|
||||||
const URL = url.parse(uri)
|
const URL = url.parse(uri)
|
||||||
uri = URL.path
|
uri = URL.path
|
||||||
}
|
}
|
||||||
proxyTarget = proxyConf + uri
|
targetUrl = urlConf + uri
|
||||||
}
|
}
|
||||||
|
|
||||||
// 替换内容
|
// 替换占位符
|
||||||
if (proxyTarget.indexOf('${') >= 0) {
|
targetUrl = replacePlaceholder(targetUrl, rOptions, matched)
|
||||||
// eslint-disable-next-line
|
|
||||||
// no-template-curly-in-string
|
|
||||||
// eslint-disable-next-line no-template-curly-in-string
|
|
||||||
proxyTarget = proxyTarget.replace('${host}', rOptions.hostname)
|
|
||||||
|
|
||||||
if (matched) {
|
// 拼接协议
|
||||||
for (let i = 0; i < matched.length; i++) {
|
targetUrl = targetUrl.indexOf('http:') === 0 || targetUrl.indexOf('https:') === 0 ? targetUrl : rOptions.protocol + '//' + targetUrl
|
||||||
proxyTarget = proxyTarget.replace('${m[' + i + ']}', matched[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const proxy = proxyTarget.indexOf('http:') === 0 || proxyTarget.indexOf('https:') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget
|
return targetUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
function doProxy (proxyConf, rOptions, req, interceptOpt, matched) {
|
||||||
|
// 获取代理目标地址
|
||||||
|
const proxyTarget = buildTargetUrl(rOptions, proxyConf, interceptOpt, matched)
|
||||||
|
|
||||||
|
// 替换rOptions的属性
|
||||||
// eslint-disable-next-line node/no-deprecated-api
|
// eslint-disable-next-line node/no-deprecated-api
|
||||||
const URL = url.parse(proxy)
|
const URL = url.parse(proxyTarget)
|
||||||
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数
|
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数
|
||||||
delete rOptions.origional.agent
|
delete rOptions.origional.agent
|
||||||
delete rOptions.origional.headers
|
delete rOptions.origional.headers
|
||||||
|
@ -54,6 +75,9 @@ function doProxy (proxyConf, rOptions, req, interceptOpt, matched) {
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'proxy',
|
name: 'proxy',
|
||||||
priority: 121,
|
priority: 121,
|
||||||
|
replacePlaceholder,
|
||||||
|
buildTargetUrl,
|
||||||
|
doProxy,
|
||||||
requestIntercept (context, interceptOpt, req, res, ssl, next, matched) {
|
requestIntercept (context, interceptOpt, req, res, ssl, next, matched) {
|
||||||
const { rOptions, log, RequestCounter } = context
|
const { rOptions, log, RequestCounter } = context
|
||||||
|
|
||||||
|
|
|
@ -1,33 +1,13 @@
|
||||||
|
const proxyApi = require('./proxy')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'redirect',
|
name: 'redirect',
|
||||||
priority: 102,
|
priority: 102,
|
||||||
requestIntercept (context, interceptOpt, req, res, ssl, next, matched) {
|
requestIntercept (context, interceptOpt, req, res, ssl, next, matched) {
|
||||||
const { rOptions, log } = context
|
const { rOptions, log } = context
|
||||||
|
|
||||||
let redirect
|
// 获取重定向目标地址
|
||||||
if (typeof interceptOpt.redirect === 'string') {
|
const redirect = proxyApi.buildTargetUrl(rOptions, interceptOpt.redirect, interceptOpt, matched)
|
||||||
if (interceptOpt.redirect.indexOf('http:') === 0 || interceptOpt.redirect.indexOf('https:') === 0) {
|
|
||||||
redirect = interceptOpt.redirect
|
|
||||||
} else {
|
|
||||||
redirect = rOptions.protocol + '//' + interceptOpt.redirect + req.url
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
redirect = interceptOpt.redirect(req.url)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 替换内容
|
|
||||||
if (redirect.indexOf('${') >= 0) {
|
|
||||||
// eslint-disable-next-line
|
|
||||||
// no-template-curly-in-string
|
|
||||||
// eslint-disable-next-line no-template-curly-in-string
|
|
||||||
redirect = redirect.replace('${host}', rOptions.hostname)
|
|
||||||
|
|
||||||
if (matched) {
|
|
||||||
for (let i = 0; i < matched.length; i++) {
|
|
||||||
redirect = redirect.replace('${m[' + i + ']}', matched[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res.writeHead(302, {
|
res.writeHead(302, {
|
||||||
Location: redirect,
|
Location: redirect,
|
||||||
|
|
Loading…
Reference in New Issue