From 0b4f3d1397f02a3e75120590d619b5f63bd1f8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Sun, 14 Apr 2024 00:00:11 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20script.js=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E5=9C=B0=E5=9D=80=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/lib/interceptor/impl/res/script.js | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/mitmproxy/src/lib/interceptor/impl/res/script.js b/packages/mitmproxy/src/lib/interceptor/impl/res/script.js index 180882a..9726971 100644 --- a/packages/mitmproxy/src/lib/interceptor/impl/res/script.js +++ b/packages/mitmproxy/src/lib/interceptor/impl/res/script.js @@ -5,14 +5,10 @@ function getScript (key, script) { const scriptUrl = contextPath + key const hash = CryptoJs.SHA256(script).toString(CryptoJs.enc.Base64) - return ` - -` + return `` } -function getScriptByAbsoluteUrl (scriptUrl) { - return ` - -` +function getScriptByUrlOrPath (scriptUrlOrPath) { + return `` } module.exports = { @@ -20,32 +16,38 @@ module.exports = { priority: 202, responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) { const { rOptions, log, setting } = context + + // github特殊处理 + if (rOptions.hostname === 'github.com' && rOptions.headers['turbo-frame'] === 'repo-content-turbo-frame') { + return + } + let keys = interceptOpt.script if (typeof keys === 'string') { keys = [keys] } try { const scripts = monkey.get(setting.script.dirAbsolutePath) - let tags = getScript('global', scripts.global.script) + let tags = '\r\n\t' + getScript('global', scripts.global.script) for (const key of keys) { let scriptTag - if (key.indexOf('http:') === 0 || key.indexOf('https:') === 0) { - scriptTag = getScriptByAbsoluteUrl(key) + if (key.indexOf('/') >= 0) { + scriptTag = getScriptByUrlOrPath(key) // 1.绝对地址或相对地址(注意:当目标站点限制跨域脚本资源的情况下,可以使用相对地址,再结合proxy.js进行代理,可规避掉跨域限制问题。) } else { const script = scripts[key] if (script == null) { continue } - scriptTag = getScript(key, script.script) + scriptTag = getScript(key, script.script) // 2.DS内置脚本 } - tags += '\r\n' + scriptTag + tags += '\r\n\t' + scriptTag } res.setHeader('DS-Script-Interceptor', 'true') log.info('script response intercept: insert script', rOptions.hostname, rOptions.path, ', head:', tags) return { - head: tags + head: tags + '\r\n' } } catch (err) { res.setHeader('DS-Script-Interceptor', 'error')