From 00dfa59ca11bcb2050f46cf239fb84d8b22171b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Mon, 22 Apr 2024 01:38:24 +0800 Subject: [PATCH] =?UTF-8?q?responseReplace.js=E5=A4=84=E7=90=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E8=BD=BD=E8=AF=B7=E6=B1=82=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D=E5=B0=BD?= =?UTF-8?q?=E9=87=8F=E4=BF=9D=E6=8C=81=E5=8E=9F=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=B8=94=E9=BB=98=E8=AE=A4=E4=B8=8D=E5=85=81?= =?UTF-8?q?=E8=AE=B8=E4=BD=BF=E7=94=A8=E7=BC=93=E5=AD=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/impl/req/requestReplace.js | 2 +- .../interceptor/impl/res/responseReplace.js | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/mitmproxy/src/lib/interceptor/impl/req/requestReplace.js b/packages/mitmproxy/src/lib/interceptor/impl/req/requestReplace.js index 7b5a1e4..27ad856 100644 --- a/packages/mitmproxy/src/lib/interceptor/impl/req/requestReplace.js +++ b/packages/mitmproxy/src/lib/interceptor/impl/req/requestReplace.js @@ -32,7 +32,7 @@ module.exports = { // 替换请求头 if (requestReplaceConfig.headers) { replaceRequestHeaders(rOptions, requestReplaceConfig.headers, log) - actions += 'headers' + actions += (actions ? ',' : '') + 'headers' } // 替换下载文件请求的请求地址(此功能主要是为了方便拦截配置) diff --git a/packages/mitmproxy/src/lib/interceptor/impl/res/responseReplace.js b/packages/mitmproxy/src/lib/interceptor/impl/res/responseReplace.js index c75bb57..f848e2e 100644 --- a/packages/mitmproxy/src/lib/interceptor/impl/res/responseReplace.js +++ b/packages/mitmproxy/src/lib/interceptor/impl/res/responseReplace.js @@ -1,4 +1,5 @@ const lodash = require('lodash') +const cacheReq = require('../req/cacheReq') const REMOVE = '[remove]' // 替换响应头 @@ -71,16 +72,31 @@ module.exports = { let actions = '' + const replaceHeaders = responseReplaceConfig.headers || {} + // 处理文件下载请求 if (responseReplaceConfig.doDownload || rOptions.doDownload) { const filename = (rOptions.path.match('^.*/([^/?]+)/?(\\?.*)?$') || [])[1] || 'UNKNOWN_FILENAME' - res.setHeader('Content-Disposition', 'attachment; filename=' + filename) - actions += 'download:' + filename + // 设置文件下载响应头 + replaceHeaders['content-disposition'] = `attachment; filename="${encodeURIComponent(filename)}"` + // 设置文件类型 + if (replaceHeaders['content-type'] == null) { + replaceHeaders['content-type'] = 'application/octet-stream' + } + // 如果未手动配置需要缓存,则不允许使用缓存 + const maxAge = cacheReq.getMaxAge(interceptOpt) + if (maxAge == null || maxAge <= 0) { + replaceHeaders['cache-control'] = '[remove]' + replaceHeaders['last-modified'] = '[remove]' + replaceHeaders.expires = '[remove]' + } + + actions += (actions ? ',' : '') + 'download:' + filename } // 替换响应头 - if (replaceResponseHeaders(responseReplaceConfig.headers, res, proxyRes)) { - actions += 'headers' + if (replaceResponseHeaders(replaceHeaders, res, proxyRes)) { + actions += (actions ? ',' : '') + 'headers' } if (actions) {