Skip to content

Commit b582ac6

Browse files
bugfix: 解决success、abort、redirect拦截器跨域问题。
1 parent 785e7be commit b582ac6

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

packages/mitmproxy/src/lib/interceptor/impl/req/abort.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ module.exports = {
55
const { rOptions, log } = context
66

77
if (interceptOpt.abort === true || interceptOpt.abort === 'true') {
8-
res.writeHead(403, {
8+
const headers = {
99
'Content-Type': 'text/plain; charset=utf-8',
1010
'DS-Interceptor': 'abort',
11-
})
11+
}
12+
13+
// headers.Access-Control-Allow-*:避免跨域问题
14+
if (rOptions.headers.origin) {
15+
headers['Access-Control-Allow-Credentials'] = 'true'
16+
headers['Access-Control-Allow-Origin'] = rOptions.headers.origin
17+
}
18+
19+
res.writeHead(403, headers)
1220
res.write(
1321
'DevSidecar 403: Request abort.\n\n'
1422
+ ' This request is matched by abort intercept.\n\n'

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ module.exports = {
99
// 获取重定向目标地址
1010
const redirect = proxyApi.buildTargetUrl(rOptions, interceptOpt.redirect, interceptOpt, matched)
1111

12-
res.writeHead(302, {
12+
const headers = {
1313
'Location': redirect,
1414
'DS-Interceptor': 'redirect',
15-
})
15+
}
16+
17+
// headers.Access-Control-Allow-*:避免跨域问题
18+
if (rOptions.headers.origin) {
19+
headers['Access-Control-Allow-Credentials'] = 'true'
20+
headers['Access-Control-Allow-Origin'] = rOptions.headers.origin
21+
}
22+
23+
res.writeHead(302, headers)
1624
res.end()
1725

1826
const url = `${rOptions.method}${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${req.url}`

packages/mitmproxy/src/lib/interceptor/impl/req/success.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ module.exports = {
55
const { rOptions, log } = context
66

77
if (interceptOpt.success === true || interceptOpt.success === 'true') {
8-
res.writeHead(200, {
8+
const headers = {
99
'Content-Type': 'text/plain; charset=utf-8',
1010
'DS-Interceptor': 'success',
11-
})
11+
}
12+
13+
// headers.Access-Control-Allow-*:避免跨域问题
14+
if (rOptions.headers.origin) {
15+
headers['Access-Control-Allow-Credentials'] = 'true'
16+
headers['Access-Control-Allow-Origin'] = rOptions.headers.origin
17+
}
18+
19+
res.writeHead(200, headers)
1220
res.write(
1321
'DevSidecar 200: Request success.\n\n'
1422
+ ' This request is matched by success intercept.\n\n'

0 commit comments

Comments
 (0)