feature: 新增 githubSpeedUp.js,定制github的加速策略,目前加速了仓库内图片的访问速度。

pull/290/head
王良 2024-04-09 11:43:29 +08:00
parent 98b83cc0d3
commit 7c9c90e649
4 changed files with 65 additions and 47 deletions

View File

@ -78,6 +78,11 @@ module.exports = {
'^(/[^/]+){2}/pull/\\d+/open_with_menu.*$': { '^(/[^/]+){2}/pull/\\d+/open_with_menu.*$': {
cacheDays: 7, cacheDays: 7,
desc: 'PR详情页标题右边那个Code按钮的HTML代理请求地址感觉上应该可以缓存。暂时先设置为缓存7天' desc: 'PR详情页标题右边那个Code按钮的HTML代理请求地址感觉上应该可以缓存。暂时先设置为缓存7天'
},
'^(/[^/]+){2,}\\.(jpg|jpeg|png|gif)(\\?.*)?$': {
githubSpeedUp: true,
cacheDays: 7,
desc: '仓库内图片重定向改为代理并缓存7天。'
} }
}, },
'github-releases.githubusercontent.com': { 'github-releases.githubusercontent.com': {

View File

@ -1,6 +1,8 @@
const proxyApi = require('./proxy')
module.exports = { module.exports = {
name: 'githubSpeedUp', name: 'githubSpeedUp',
priority: 104, priority: 131,
requestIntercept (context, interceptOpt, req, res, ssl, next) { requestIntercept (context, interceptOpt, req, res, ssl, next) {
const { rOptions, log } = context const { rOptions, log } = context
@ -14,15 +16,15 @@ module.exports = {
// 判断是否为仓库内的图片文件 // 判断是否为仓库内的图片文件
const matched = req.url.match('^(/[^/]+){2}/raw(/[^/]+)+\\.(jpg|jpeg|png|gif)(\\?.*)?$') const matched = req.url.match('^(/[^/]+){2}/raw(/[^/]+)+\\.(jpg|jpeg|png|gif)(\\?.*)?$')
if (matched) { if (matched) {
const redirect = 'https://raw.githubusercontent.com' + matched[0].replace('/raw/', '/') // 拼接代理地址
const proxyConf = 'https://raw.githubusercontent.com' + req.url.replace('/raw/', '/')
res.writeHead(302, { // 执行代理
Location: redirect, const proxyTarget = proxyApi.doProxy(proxyConf, rOptions, req)
'DS-Interceptor': 'githubSpeedUp'
})
res.end()
log.info('githubSpeedUp intercept:', url) res.setHeader('DS-Interceptor', `githubSpeedUp: proxy -> ${proxyTarget}`)
log.info(`githubSpeedUp intercept: ${url} -> ${proxyConf}`)
return true return true
} }
@ -30,6 +32,6 @@ module.exports = {
return true // true代表请求结束 return true // true代表请求结束
}, },
is (interceptOpt) { is (interceptOpt) {
return true return !!interceptOpt.githubSpeedUp
} }
} }

View File

@ -1,8 +1,51 @@
const url = require('url') const url = require('url')
const lodash = require('lodash') const lodash = require('lodash')
function doProxy (proxyConf, rOptions, req, interceptOpt) {
// 获取代理目标地址
let proxyTarget
if (interceptOpt && interceptOpt.replace) {
const regexp = new RegExp(interceptOpt.replace)
proxyTarget = req.url.replace(regexp, proxyConf)
} else if (proxyConf.indexOf('http:') === 0 || proxyConf.indexOf('https:') === 0) {
proxyTarget = proxyConf
} else {
let uri = req.url
if (uri.indexOf('http') === 0) {
// eslint-disable-next-line node/no-deprecated-api
const URL = url.parse(uri)
uri = URL.path
}
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)
const proxy = proxyTarget.indexOf('http:') === 0 || proxyTarget.indexOf('https:') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget
// eslint-disable-next-line node/no-deprecated-api
const URL = url.parse(proxy)
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数
delete rOptions.origional.agent
delete rOptions.origional.headers
rOptions.protocol = URL.protocol
rOptions.hostname = URL.host
rOptions.host = URL.host
rOptions.headers.host = URL.host
rOptions.path = URL.path
if (URL.port == null) {
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80
}
return proxyTarget
}
module.exports = { module.exports = {
name: 'proxy', name: 'proxy',
priority: 121, priority: 121,
doProxy,
requestIntercept (context, interceptOpt, req, res, ssl, next) { requestIntercept (context, interceptOpt, req, res, ssl, next) {
const { rOptions, log, RequestCounter } = context const { rOptions, log, RequestCounter } = context
@ -33,42 +76,8 @@ module.exports = {
} }
} }
// 获取代理目标地址 // 替换 rOptions 中的地址,并返回代理目标地址
let proxyTarget const proxyTarget = doProxy(proxyConf, rOptions, req, interceptOpt)
if (interceptOpt.replace) {
const regexp = new RegExp(interceptOpt.replace)
proxyTarget = req.url.replace(regexp, proxyConf)
} else if (proxyConf.indexOf('http:') === 0 || proxyConf.indexOf('https:') === 0) {
proxyTarget = proxyConf
} else {
let uri = req.url
if (uri.indexOf('http') === 0) {
// eslint-disable-next-line node/no-deprecated-api
const URL = url.parse(uri)
uri = URL.path
}
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)
const proxy = proxyTarget.indexOf('http') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget
// eslint-disable-next-line node/no-deprecated-api
const URL = url.parse(proxy)
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数
delete rOptions.origional.agent
delete rOptions.origional.headers
rOptions.protocol = URL.protocol
rOptions.hostname = URL.host
rOptions.host = URL.host
rOptions.headers.host = URL.host
rOptions.path = URL.path
if (URL.port == null) {
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80
}
if (context.requestCount) { if (context.requestCount) {
log.info('proxy choice:', JSON.stringify(context.requestCount)) log.info('proxy choice:', JSON.stringify(context.requestCount))

View File

@ -4,13 +4,14 @@ const OPTIONS = require('./impl/req/OPTIONS.js')
const success = require('./impl/req/success') const success = require('./impl/req/success')
const redirect = require('./impl/req/redirect') const redirect = require('./impl/req/redirect')
const abort = require('./impl/req/abort') const abort = require('./impl/req/abort')
const githubSpeedUp = require('./impl/req/githubSpeedUp')
const cacheReq = require('./impl/req/cacheReq') const cacheReq = require('./impl/req/cacheReq')
const proxy = require('./impl/req/proxy') const proxy = require('./impl/req/proxy')
const sni = require('./impl/req/sni') const sni = require('./impl/req/sni')
const githubSpeedUp = require('./impl/req/githubSpeedUp')
// response interceptor impls // response interceptor impls
const cacheRes = require('./impl/res/cacheRes') const cacheRes = require('./impl/res/cacheRes')
const script = require('./impl/res/script') const script = require('./impl/res/script')
@ -18,9 +19,10 @@ const script = require('./impl/res/script')
module.exports = [ module.exports = [
// request interceptor impls // request interceptor impls
OPTIONS, OPTIONS,
success, redirect, abort, githubSpeedUp, success, redirect, abort,
cacheReq, cacheReq,
proxy, sni, proxy, sni,
githubSpeedUp,
// response interceptor impls // response interceptor impls
cacheRes, script cacheRes, script