Skip to content

Commit

Permalink
移除 githubSpeedUp.js 拦截器,增强 proxy.js,支持path匹配结果拼接。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Apr 9, 2024
1 parent 724f09f commit c7f3c23
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 57 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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天。'
}
Expand Down
37 changes: 0 additions & 37 deletions packages/mitmproxy/src/lib/interceptor/impl/req/githubSpeedUp.js

This file was deleted.

26 changes: 17 additions & 9 deletions packages/mitmproxy/src/lib/interceptor/impl/req/proxy.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/lib/interceptor/impl/req/sni.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
name: 'sni',
priority: 123,
priority: 122,
requestIntercept (context, interceptOpt, req, res, ssl, next) {
const { rOptions, log } = context

Expand Down
3 changes: 1 addition & 2 deletions packages/mitmproxy/src/lib/interceptor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -20,7 +19,7 @@ module.exports = [
OPTIONS,
success, redirect, abort,
cacheReq,
githubSpeedUp, proxy, sni,
proxy, sni,

// response interceptor impls
cacheRes, script
Expand Down
18 changes: 12 additions & 6 deletions packages/mitmproxy/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)
}
}

Expand Down

0 comments on commit c7f3c23

Please sign in to comment.