Browse Source

重构 proxy.js 和 redirect.js,使它们的逻辑代码复用。

pull/291/head
王良 8 months ago
parent
commit
3c45b06bd5
  1. 68
      packages/mitmproxy/src/lib/interceptor/impl/req/proxy.js
  2. 28
      packages/mitmproxy/src/lib/interceptor/impl/req/redirect.js

68
packages/mitmproxy/src/lib/interceptor/impl/req/proxy.js

@ -1,41 +1,62 @@
const url = require('url')
const lodash = require('lodash')
function doProxy (proxyConf, rOptions, req, interceptOpt, matched) {
// 获取代理目标地址
let proxyTarget
// 替换占位符
function replacePlaceholder (url, rOptions, matched) {
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) {
const regexp = new RegExp(interceptOpt.replace)
proxyTarget = req.url.replace(regexp, proxyConf)
} else if (proxyConf.indexOf('http:') === 0 || proxyConf.indexOf('https:') === 0) {
proxyTarget = proxyConf
targetUrl = rOptions.path.replace(regexp, urlConf)
} else if (urlConf.indexOf('http:') === 0 || urlConf.indexOf('https:') === 0) {
targetUrl = urlConf
} else {
let uri = req.url
let uri = rOptions.path
if (uri.indexOf('http') === 0) {
// eslint-disable-next-line node/no-deprecated-api
const URL = url.parse(uri)
uri = URL.path
}
proxyTarget = proxyConf + uri
targetUrl = urlConf + uri
}
// 替换内容
if (proxyTarget.indexOf('${') >= 0) {
// eslint-disable-next-line
// no-template-curly-in-string
// eslint-disable-next-line no-template-curly-in-string
proxyTarget = proxyTarget.replace('${host}', rOptions.hostname)
// 替换占位符
targetUrl = replacePlaceholder(targetUrl, rOptions, matched)
if (matched) {
for (let i = 0; i < matched.length; i++) {
proxyTarget = proxyTarget.replace('${m[' + i + ']}', matched[i])
}
}
}
// 拼接协议
targetUrl = targetUrl.indexOf('http:') === 0 || targetUrl.indexOf('https:') === 0 ? targetUrl : rOptions.protocol + '//' + targetUrl
return targetUrl
}
function doProxy (proxyConf, rOptions, req, interceptOpt, matched) {
// 获取代理目标地址
const proxyTarget = buildTargetUrl(rOptions, proxyConf, interceptOpt, matched)
const proxy = proxyTarget.indexOf('http:') === 0 || proxyTarget.indexOf('https:') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget
// 替换rOptions的属性
// eslint-disable-next-line node/no-deprecated-api
const URL = url.parse(proxy)
const URL = url.parse(proxyTarget)
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数
delete rOptions.origional.agent
delete rOptions.origional.headers
@ -54,6 +75,9 @@ function doProxy (proxyConf, rOptions, req, interceptOpt, matched) {
module.exports = {
name: 'proxy',
priority: 121,
replacePlaceholder,
buildTargetUrl,
doProxy,
requestIntercept (context, interceptOpt, req, res, ssl, next, matched) {
const { rOptions, log, RequestCounter } = context

28
packages/mitmproxy/src/lib/interceptor/impl/req/redirect.js

@ -1,33 +1,13 @@
const proxyApi = require('./proxy')
module.exports = {
name: 'redirect',
priority: 102,
requestIntercept (context, interceptOpt, req, res, ssl, next, matched) {
const { rOptions, log } = context
let redirect
if (typeof interceptOpt.redirect === 'string') {
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])
}
}
}
// 获取重定向目标地址
const redirect = proxyApi.buildTargetUrl(rOptions, interceptOpt.redirect, interceptOpt, matched)
res.writeHead(302, {
Location: redirect,

Loading…
Cancel
Save