feature: githubSpeedUp.js

pull/290/head
王良 2024-04-09 10:54:54 +08:00
parent 55906b828e
commit 98b83cc0d3
2 changed files with 37 additions and 1 deletions

View File

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

View File

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