-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: 新增 githubSpeedUp.js,定制github的加速策略,目前加速了仓库内图片的访问速度。 (#290)
- Loading branch information
1 parent
55906b8
commit e67f051
Showing
4 changed files
with
90 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const proxyApi = require('./proxy') | ||
|
||
module.exports = { | ||
name: 'githubSpeedUp', | ||
priority: 131, | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters