mirror of https://github.com/hunshcn/gh-proxy
增加raw.githubusercontent.com支持
parent
327d22a30d
commit
4a3fe663f4
|
@ -68,6 +68,7 @@ python版本的机器如果无法正常访问github.io会启动报错,请自
|
||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
|
||||||
|
* 2020.04.10 增加对`raw.githubusercontent.com`文件的支持
|
||||||
* 2020.04.09 增加Python版本(使用Flask)
|
* 2020.04.09 增加Python版本(使用Flask)
|
||||||
* 2020.03.23 新增了clone的支持
|
* 2020.03.23 新增了clone的支持
|
||||||
* 2020.03.22 初始版本
|
* 2020.03.22 初始版本
|
||||||
|
|
11
index.js
11
index.js
|
@ -58,16 +58,16 @@ async function fetchHandler(e) {
|
||||||
const urlStr = req.url
|
const urlStr = req.url
|
||||||
const urlObj = new URL(urlStr)
|
const urlObj = new URL(urlStr)
|
||||||
let path = urlObj.searchParams.get('q')
|
let path = urlObj.searchParams.get('q')
|
||||||
if(path)
|
if (path) {
|
||||||
{
|
|
||||||
return Response.redirect('https://' + urlObj.host + '/' + path, 301)
|
return Response.redirect('https://' + urlObj.host + '/' + path, 301)
|
||||||
}
|
}
|
||||||
// cfworker 会把路径中的 `//` 合并成 `/`
|
// cfworker 会把路径中的 `//` 合并成 `/`
|
||||||
path = urlObj.href.substr(urlObj.origin.length + 1).replace(/^https?:\/+/, 'https://')
|
path = urlObj.href.substr(urlObj.origin.length + 1).replace(/^https?:\/+/, 'https://')
|
||||||
const exp = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/
|
const exp1 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/
|
||||||
const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/
|
const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/
|
||||||
const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-upload-pack).*$/
|
const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-upload-pack).*$/
|
||||||
if (path.search(exp)===0 || !Config.jsdelivr && path.search(exp2)===0 || !Config.cnpmjs && path.search(exp3)===0) {
|
const exp4 = /^(?:https?:\/\/)?raw\.githubusercontent\.com\/.+?\/.+?\/.+?\/.+$/
|
||||||
|
if (path.search(exp1) === 0 || !Config.jsdelivr && path.search(exp2) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4))) {
|
||||||
return httpHandler(req, path)
|
return httpHandler(req, path)
|
||||||
} else if (path.search(exp2) === 0) {
|
} else if (path.search(exp2) === 0) {
|
||||||
const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh')
|
const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh')
|
||||||
|
@ -75,6 +75,9 @@ async function fetchHandler(e) {
|
||||||
} else if (path.search(exp3) === 0) {
|
} else if (path.search(exp3) === 0) {
|
||||||
const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org')
|
const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org')
|
||||||
return Response.redirect(newUrl, 302)
|
return Response.redirect(newUrl, 302)
|
||||||
|
} else if (path.search(exp4) === 0) {
|
||||||
|
const newUrl = path.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1').replace(/^(?:https?:\/\/)?raw\.githubusercontent\.com/, 'https://cdn.jsdelivr.net/gh')
|
||||||
|
return Response.redirect(newUrl, 302)
|
||||||
} else {
|
} else {
|
||||||
return fetch(ASSET_URL + path)
|
return fetch(ASSET_URL + path)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue