From 98b83cc0d37e09175035d69978cf85b2d23c6b98 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 10:54:54 +0800 Subject: [PATCH] feature: githubSpeedUp.js --- .../lib/interceptor/impl/req/githubSpeedUp.js | 35 +++++++++++++++++++ .../mitmproxy/src/lib/interceptor/index.js | 3 +- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js diff --git a/packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js b/packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js new file mode 100644 index 0000000..7c8e1b8 --- /dev/null +++ b/packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js @@ -0,0 +1,35 @@ +module.exports = { + name: 'githubSpeedUp', + priority: 104, + 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 redirect = 'https://raw.githubusercontent.com' + matched[0].replace('/raw/', '/') + + res.writeHead(302, { + Location: redirect, + 'DS-Interceptor': 'githubSpeedUp' + }) + res.end() + + log.info('githubSpeedUp intercept:', url) + + return true + } + + return true // true代表请求结束 + }, + is (interceptOpt) { + return true + } +} diff --git a/packages/mitmproxy/src/lib/interceptor/index.js b/packages/mitmproxy/src/lib/interceptor/index.js index 8f4a722..799e4b5 100644 --- a/packages/mitmproxy/src/lib/interceptor/index.js +++ b/packages/mitmproxy/src/lib/interceptor/index.js @@ -4,6 +4,7 @@ const OPTIONS = require('./impl/req/OPTIONS.js') const success = require('./impl/req/success') const redirect = require('./impl/req/redirect') const abort = require('./impl/req/abort') +const githubSpeedUp = require('./impl/req/githubSpeedUp') const cacheReq = require('./impl/req/cacheReq') @@ -17,7 +18,7 @@ const script = require('./impl/res/script') module.exports = [ // request interceptor impls OPTIONS, - success, redirect, abort, + success, redirect, abort, githubSpeedUp, cacheReq, proxy, sni,