Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 篡改猴插件地址,可以自定义配置了。 #301

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/core/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ module.exports = {
sni: 'baidu.com'
},
'^(/[\\w-.]+){2,}/?(\\?.*)?$': {
// 自定义篡改猴插件地址配置
tampermonkeyScript: 'https://mirror.ghproxy.com/https://raw.githubusercontent.com/docmirror/dev-sidecar/scripts/tampermonkey.js',
// 脚本地址配置
script: [
// Github油猴脚本
'https://mirror.ghproxy.com/https://raw.githubusercontent.com/docmirror/dev-sidecar/scripts/github/monkey.js'
],
scriptUrlDesc: '上面所使用的脚本地址,使用了高速镜像地址。',
remark: '注:上面所使用的脚本地址,为高速镜像地址。',
desc: '油猴脚本:高速下载 Git Clone/SSH、Release、Raw、Code(ZIP) 等文件 (公益加速)、项目列表单文件快捷下载、添加 git clone 命令'
},
// 以下三项暂时先注释掉,因为已经有油猴脚本提供高速下载地址了。
Expand Down
40 changes: 29 additions & 11 deletions packages/mitmproxy/src/lib/interceptor/impl/res/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -89,23 +93,37 @@ module.exports = {
const hostnameConfig = intercepts[hostnamePattern]

const scriptProxy = {}
const handleScriptUrl = (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]
handleScriptUrl(scriptUrl, 'script', (scriptKey) => {
pathConfig.script[i] = scriptKey
} else if (script.indexOf('/') === 0) {
// 相对地址
scriptProxy[script] = script
}
})
}
}

// 处理 tampermonkeyScript 配置
if (typeof pathConfig.tampermonkeyScript === 'string') {
handleScriptUrl(pathConfig.tampermonkeyScript, 'tampermonkey', (scriptKey) => {
pathConfig.tampermonkeyScript = scriptKey
})
}
}

// 自动创建脚本
Expand Down