From c7f3c236628c7ec5049d4fce1d3289509e4524ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Tue, 9 Apr 2024 13:23:06 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=20githubSpeedUp.js=20?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=EF=BC=8C=E5=A2=9E=E5=BC=BA=20proxy.?= =?UTF-8?q?js=EF=BC=8C=E6=94=AF=E6=8C=81path=E5=8C=B9=E9=85=8D=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E6=8B=BC=E6=8E=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/config/index.js | 5 ++- .../lib/interceptor/impl/req/githubSpeedUp.js | 37 ------------------- .../src/lib/interceptor/impl/req/proxy.js | 26 ++++++++----- .../src/lib/interceptor/impl/req/sni.js | 2 +- .../mitmproxy/src/lib/interceptor/index.js | 3 +- packages/mitmproxy/src/options.js | 18 ++++++--- 6 files changed, 34 insertions(+), 57 deletions(-) delete mode 100644 packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index bfbac4ca..6db255fb 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -79,8 +79,9 @@ module.exports = { cacheDays: 7, desc: 'PR详情页:标题右边那个Code按钮的HTML代理请求地址,感觉上应该可以缓存。暂时先设置为缓存7天' }, - '^(/[^/]+){2,}\\.(jpg|jpeg|png|gif)(\\?.*)?$': { - githubSpeedUp: true, + '^((/[^/]+){2,})/raw((/[^/]+)+\\.(jpg|jpeg|png|gif))(\\?.*)?$': { + // eslint-disable-next-line no-template-curly-in-string + proxy: 'https://raw.githubusercontent.com${m[1]}${m[3]}', cacheDays: 7, desc: '仓库内图片,重定向改为代理,并缓存7天。' } diff --git a/packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js b/packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js deleted file mode 100644 index c4de2fbc..00000000 --- a/packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js +++ /dev/null @@ -1,37 +0,0 @@ -const proxyApi = require('./proxy') - -module.exports = { - name: 'githubSpeedUp', - priority: 121, - requestIntercept (context, interceptOpt, req, res, ssl, next) { - const { rOptions, log } = context - - // 目前,只拦截github.com,后续可以继续拦截其他域名,做一些特殊处理 - if (rOptions.hostname !== 'github.com') { - return - } - - const url = `${rOptions.method} ➜ ${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${req.url}` - - // 判断是否为仓库内的图片文件 - const matched = req.url.match('^(/[^/]+){2}/raw(/[^/]+)+\\.(jpg|jpeg|png|gif)(\\?.*)?$') - if (matched) { - // 拼接代理地址 - const proxyConf = 'https://raw.githubusercontent.com' + req.url.replace('/raw/', '/') - - // 执行代理 - const proxyTarget = proxyApi.doProxy(proxyConf, rOptions, req) - - res.setHeader('DS-Interceptor', `githubSpeedUp: proxy -> ${proxyTarget}`) - - log.info(`githubSpeedUp intercept: ${url} -> ${proxyConf}`) - - return true - } - - return true // true代表请求结束 - }, - is (interceptOpt) { - return !!interceptOpt.githubSpeedUp - } -} diff --git a/packages/mitmproxy/src/lib/interceptor/impl/req/proxy.js b/packages/mitmproxy/src/lib/interceptor/impl/req/proxy.js index 285704b1..2b1c420e 100644 --- a/packages/mitmproxy/src/lib/interceptor/impl/req/proxy.js +++ b/packages/mitmproxy/src/lib/interceptor/impl/req/proxy.js @@ -1,7 +1,7 @@ const url = require('url') const lodash = require('lodash') -function doProxy (proxyConf, rOptions, req, interceptOpt) { +function doProxy (proxyConf, rOptions, req, interceptOpt, matched) { // 获取代理目标地址 let proxyTarget if (interceptOpt && interceptOpt.replace) { @@ -19,10 +19,19 @@ function doProxy (proxyConf, rOptions, req, interceptOpt) { proxyTarget = proxyConf + uri } - // eslint-disable-next-line - // no-template-curly-in-string - // eslint-disable-next-line no-template-curly-in-string - proxyTarget = proxyTarget.replace('${host}', rOptions.hostname) + // 替换内容 + if (proxyTarget.indexOf('${') >= 0) { + // eslint-disable-next-line + // no-template-curly-in-string + // eslint-disable-next-line no-template-curly-in-string + proxyTarget = proxyTarget.replace('${host}', rOptions.hostname) + + if (matched) { + for (let i = 0; i < matched.length; i++) { + proxyTarget = proxyTarget.replace('${m[' + i + ']}', matched[i]) + } + } + } const proxy = proxyTarget.indexOf('http:') === 0 || proxyTarget.indexOf('https:') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget // eslint-disable-next-line node/no-deprecated-api @@ -44,9 +53,8 @@ function doProxy (proxyConf, rOptions, req, interceptOpt) { module.exports = { name: 'proxy', - priority: 122, - doProxy, - requestIntercept (context, interceptOpt, req, res, ssl, next) { + priority: 121, + requestIntercept (context, interceptOpt, req, res, ssl, next, matched) { const { rOptions, log, RequestCounter } = context const originHostname = rOptions.hostname @@ -77,7 +85,7 @@ module.exports = { } // 替换 rOptions 中的地址,并返回代理目标地址 - const proxyTarget = doProxy(proxyConf, rOptions, req, interceptOpt) + const proxyTarget = doProxy(proxyConf, rOptions, req, interceptOpt, matched) if (context.requestCount) { log.info('proxy choice:', JSON.stringify(context.requestCount)) diff --git a/packages/mitmproxy/src/lib/interceptor/impl/req/sni.js b/packages/mitmproxy/src/lib/interceptor/impl/req/sni.js index 91fca35d..a60c04f2 100644 --- a/packages/mitmproxy/src/lib/interceptor/impl/req/sni.js +++ b/packages/mitmproxy/src/lib/interceptor/impl/req/sni.js @@ -1,6 +1,6 @@ module.exports = { name: 'sni', - priority: 123, + priority: 122, requestIntercept (context, interceptOpt, req, res, ssl, next) { const { rOptions, log } = context diff --git a/packages/mitmproxy/src/lib/interceptor/index.js b/packages/mitmproxy/src/lib/interceptor/index.js index 3c7fbd39..8f4a7226 100644 --- a/packages/mitmproxy/src/lib/interceptor/index.js +++ b/packages/mitmproxy/src/lib/interceptor/index.js @@ -7,7 +7,6 @@ const abort = require('./impl/req/abort') const cacheReq = require('./impl/req/cacheReq') -const githubSpeedUp = require('./impl/req/githubSpeedUp') const proxy = require('./impl/req/proxy') const sni = require('./impl/req/sni') @@ -20,7 +19,7 @@ module.exports = [ OPTIONS, success, redirect, abort, cacheReq, - githubSpeedUp, proxy, sni, + proxy, sni, // response interceptor impls cacheRes, script diff --git a/packages/mitmproxy/src/options.js b/packages/mitmproxy/src/options.js index 1a925a3b..0e0e66f4 100644 --- a/packages/mitmproxy/src/options.js +++ b/packages/mitmproxy/src/options.js @@ -69,14 +69,20 @@ module.exports = (config) => { const matchIntercepts = [] const matchInterceptsOpts = {} for (const regexp in interceptOpts) { // 遍历拦截配置 - const interceptOpt = interceptOpts[regexp] - // interceptOpt.key = regexp + // 判断是否匹配拦截器 + let matched if (regexp !== true && regexp !== 'true') { - if (!matchUtil.isMatched(rOptions.path, regexp)) { + matched = matchUtil.isMatched(rOptions.path, regexp) + if (matched == null) { // 拦截器匹配失败 continue } } - log.info(`interceptor matched, regexp: '${regexp}' =>`, JSON.stringify(interceptOpt), ', path:', rOptions.path) + + // 获取拦截器 + const interceptOpt = interceptOpts[regexp] + // interceptOpt.key = regexp + + // log.info(`interceptor matched, regexp: '${regexp}' =>`, JSON.stringify(interceptOpt), ', url:', url) for (const impl of interceptorImpls) { // 根据拦截配置挑选合适的拦截器来处理 if (impl.is && impl.is(interceptOpt)) { @@ -96,12 +102,12 @@ module.exports = (config) => { if (impl.requestIntercept) { // req拦截器 interceptor.requestIntercept = (context, req, res, ssl, next) => { - return impl.requestIntercept(context, interceptOpt, req, res, ssl, next) + return impl.requestIntercept(context, interceptOpt, req, res, ssl, next, matched) } } else if (impl.responseIntercept) { // res拦截器 interceptor.responseIntercept = (context, req, res, proxyReq, proxyRes, ssl, next) => { - return impl.responseIntercept(context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) + return impl.responseIntercept(context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next, matched) } }