移除 githubSpeedUp.js 拦截器,增强 proxy.js,支持path匹配结果拼接。

pull/291/head
王良 2024-04-09 13:23:06 +08:00
parent 724f09fac6
commit c7f3c23662
6 changed files with 34 additions and 57 deletions

View File

@ -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天。'
}

View File

@ -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
}
}

View File

@ -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))

View File

@ -1,6 +1,6 @@
module.exports = {
name: 'sni',
priority: 123,
priority: 122,
requestIntercept (context, interceptOpt, req, res, ssl, next) {
const { rOptions, log } = context

View File

@ -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

View File

@ -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)
}
}