Skip to content

Commit 09523c5

Browse files
feat: abort拦截器,允许自定义响应内容
1 parent 8038bb4 commit 09523c5

File tree

1 file changed

+68
-13
lines changed
  • packages/mitmproxy/src/lib/interceptor/impl/req

1 file changed

+68
-13
lines changed

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

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,75 @@ module.exports = {
44
requestIntercept (context, interceptOpt, req, res, ssl, next) {
55
const { rOptions, log } = context
66

7-
res.writeHead(403, {
8-
'Content-Type': 'text/plain; charset=utf-8',
9-
'DS-Interceptor': 'abort',
10-
})
11-
res.write(
12-
'DevSidecar 403: Request abort.\n\n'
13-
+ ' This request is matched by abort intercept.\n'
14-
+ ' 因配置abort拦截器,本请求直接返回403禁止访问。',
15-
)
16-
res.end()
7+
if (interceptOpt.abort === true || interceptOpt.abort === 'true') {
8+
res.writeHead(403, {
9+
'Content-Type': 'text/plain; charset=utf-8',
10+
'DS-Interceptor': 'abort',
11+
})
12+
res.write(
13+
'DevSidecar 403: Request abort.\n\n'
14+
+ ' This request is matched by abort intercept.\n\n'
15+
+ ' 因配置abort拦截器,本请求直接返回403禁止访问。',
16+
)
17+
res.end()
1718

18-
const url = `${rOptions.method}${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${req.url}`
19-
log.info('abort intercept:', url)
20-
return true // true代表请求结束
19+
const url = `${rOptions.method}${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${req.url}`
20+
log.info('abort intercept:', url)
21+
return true // true代表请求结束
22+
} else {
23+
const response = interceptOpt.abort
24+
25+
// status
26+
const status = response.status || 403
27+
response.status = status
28+
29+
// body
30+
const body = response.html || response.json || response.script || response.css || response.text || response.body
31+
|| `DevSidecar ${status}: Request abort.\n\n`
32+
+ ' This request is matched by abort intercept.\n\n'
33+
+ ` 因配置abort拦截器,本请求直接返回${status}禁止访问。`
34+
35+
// headers
36+
const headers = response.headers || {}
37+
response.headers = headers
38+
headers['DS-Interceptor'] = 'abort'
39+
// headers.Content-Type
40+
if (status !== 204) {
41+
// (1)如果没有Content-Type,根据response的内容自动设置
42+
if (!headers['Content-Type']) {
43+
if (response.html != null) {
44+
headers['Content-Type'] = 'text/html'
45+
} else if (response.json != null) {
46+
headers['Content-Type'] = 'application/json'
47+
} else if (response.script != null) {
48+
headers['Content-Type'] = 'application/javascript'
49+
} else if (response.css != null) {
50+
headers['Content-Type'] = 'text/css'
51+
} else {
52+
headers['Content-Type'] = 'text/plain'
53+
}
54+
}
55+
// (2)如果Content-Type没有charset,自动设置为utf-8
56+
if (headers['Content-Type'] != null && !headers['Content-Type'].includes('charset')) {
57+
headers['Content-Type'] += '; charset=utf-8'
58+
}
59+
}
60+
// headers.Access-Control-Allow-*:避免跨域问题
61+
if (rOptions.headers.origin && !headers['Access-Control-Allow-Origin']) {
62+
headers['Access-Control-Allow-Credentials'] = 'true'
63+
headers['Access-Control-Allow-Origin'] = rOptions.headers.origin
64+
}
65+
66+
res.writeHead(status, headers)
67+
if (status !== 204) {
68+
res.write(body)
69+
}
70+
res.end()
71+
72+
const url = `${rOptions.method}${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${req.url}`
73+
log.info('abort intercept:', url, ', response:', JSON.stringify(response))
74+
return true // true代表请求结束
75+
}
2176
},
2277
is (interceptOpt) {
2378
return !!interceptOpt.abort

0 commit comments

Comments
 (0)