From 6b2db1c1e770b565ee235006c368e05e1869d580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 19 Apr 2024 16:46:05 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E7=AF=A1=E6=94=B9=E7=8C=B4=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?url=E4=BA=86=EF=BC=8C=E4=B8=8D=E5=86=8D=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E4=BD=BF=E7=94=A8DS=E5=86=85=E7=BD=AE=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E4=BA=86=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 | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/mitmproxy/src/lib/interceptor/impl/res/script.js b/packages/mitmproxy/src/lib/interceptor/impl/res/script.js index cde8f4a..32ca9b4 100644 --- a/packages/mitmproxy/src/lib/interceptor/impl/res/script.js +++ b/packages/mitmproxy/src/lib/interceptor/impl/res/script.js @@ -63,7 +63,11 @@ module.exports = { } // 插入油猴脚本浏览器扩展 - tags = '\r\n\t' + getScript('tampermonkey', scripts.tampermonkey.script) + tags + if (typeof interceptOpt.tampermonkeyScript === 'string') { + tags = '\r\n\t' + getScriptByUrlOrPath(interceptOpt.tampermonkeyScript) + tags + } else { + tags = '\r\n\t' + getScript('tampermonkey', scripts.tampermonkey.script) + tags + } res.setHeader('DS-Script-Interceptor', 'true') log.info('script response intercept: insert script', rOptions.hostname, rOptions.path, ', head:', tags) @@ -89,23 +93,37 @@ module.exports = { const hostnameConfig = intercepts[hostnamePattern] const scriptProxy = {} + const handleScript = (scriptUrl, name, replaceScriptUrlFun) => { + if (scriptUrl.indexOf('https:') === 0 || scriptUrl.indexOf('http:') === 0) { + // 绝对地址 + const scriptKey = SCRIPT_PROXY_URL_PRE + scriptUrl.replace('.js', '').replace(/[\W_]+/g, '_') + '.js' // 伪脚本地址:移除 script 中可能存在的特殊字符,并转为相对地址 + scriptProxy[scriptKey] = scriptUrl + log.info(`替换${name}配置值:'${scriptUrl}' -> '${scriptKey}'`) + if (typeof replaceScriptUrlFun === 'function') replaceScriptUrlFun(scriptKey) + } else if (scriptUrl.indexOf('/') === 0) { + // 相对地址 + scriptProxy[scriptUrl] = scriptUrl + } + } + for (const pathPattern in hostnameConfig) { const pathConfig = hostnameConfig[pathPattern] + // 处理 script 配置 if (typeof pathConfig.script === 'object' && pathConfig.script.length > 0) { for (let i = 0; i < pathConfig.script.length; i++) { - const script = pathConfig.script[i] - if (script.indexOf('https:') === 0 || script.indexOf('http:') === 0) { - // 绝对地址 - const scriptKey = SCRIPT_PROXY_URL_PRE + script.replace('.js', '').replace(/[\W_]+/g, '_') + '.js' // 伪脚本地址:移除 script 中可能存在的特殊字符,并转为相对地址 - scriptProxy[scriptKey] = script - log.info(`替换script配置值:'${pathConfig.script[i]}' -> '${scriptKey}'`) + const scriptUrl = pathConfig.script[i] + handleScript(scriptUrl, 'script', (scriptKey) => { pathConfig.script[i] = scriptKey - } else if (script.indexOf('/') === 0) { - // 相对地址 - scriptProxy[script] = script - } + }) } } + + // 处理 tampermonkeyScript 配置 + if (typeof pathConfig.tampermonkeyScript === 'string') { + handleScript(pathConfig.tampermonkeyScript, 'tampermonkey', (scriptKey) => { + pathConfig.tampermonkeyScript = scriptKey + }) + } } // 自动创建脚本