增加对自定义路由不为根路径的支持

pull/3/head
hunshnet 2020-04-19 01:22:02 +08:00
parent d3768b71d9
commit 0dd4fb3a25
2 changed files with 8 additions and 4 deletions

View File

@ -37,6 +37,10 @@ github release、archive以及项目文件的加速项目支持clone有Clo
`index.js`默认配置下clone走github.com.cnpmjs.org项目文件会走jsDeliver如需走worker修改Config变量即可
`ASSET_URL`是静态资源的url实际上就是现在显示出来的那个输入框单页面
`PREFIX`是前缀,默认(根路径情况为"/"如果自定义路由为example.com/gh/*请将PREFIX改为 '/gh/',注意,少一个杠都会错!
## Python版本部署
### Docker部署

View File

@ -4,6 +4,8 @@
* static files (404.html, sw.js, conf.js)
*/
const ASSET_URL = 'https://hunshcn.github.io/gh-proxy'
// 前缀如果自定义路由为example.com/gh/*将PREFIX改为 '/gh/',注意,少一个杠都会错!
const PREFIX = '/'
// git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关0为关闭默认开启
const Config = {
jsdelivr: 1,
@ -59,10 +61,10 @@ async function fetchHandler(e) {
const urlObj = new URL(urlStr)
let path = urlObj.searchParams.get('q')
if (path) {
return Response.redirect('https://' + urlObj.host + '/' + path, 301)
return Response.redirect('https://' + urlObj.host + PREFIX + path, 301)
}
// cfworker 会把路径中的 `//` 合并成 `/`
path = urlObj.href.substr(urlObj.origin.length + 1).replace(/^https?:\/+/, 'https://')
path = urlObj.href.substr(urlObj.origin.length + PREFIX.length).replace(/^https?:\/+/, 'https://')
const exp1 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/
const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/
const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-upload-pack).*$/
@ -102,8 +104,6 @@ function httpHandler(req, pathname) {
const reqHdrNew = new Headers(reqHdrRaw)
const refer = reqHdrNew.get('referer')
let urlStr = pathname
if (urlStr.startsWith('github')) {
urlStr = 'https://' + urlStr