Skip to content

Commit

Permalink
增强 redirect.js,支持path匹配结果拼接重定向地址。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Apr 9, 2024
1 parent c7f3c23 commit 0069e1c
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/mitmproxy/src/lib/interceptor/impl/req/redirect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
name: 'redirect',
priority: 102,
requestIntercept (context, interceptOpt, req, res, ssl, next) {
requestIntercept (context, interceptOpt, req, res, ssl, next, matched) {
const { rOptions, log } = context

let redirect
Expand All @@ -15,10 +15,19 @@ module.exports = {
redirect = interceptOpt.redirect(req.url)
}

// 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 (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, {
Location: redirect,
Expand Down

0 comments on commit 0069e1c

Please sign in to comment.