自建GitHub文件加速下载服务
不需要服务器
- 项目地址:https://github.com/hunshcn/gh-proxy
- 原理: 利用cloudflare workers自建一个API,来实现GitHub文件加速。
搭建
准备工作:
注册CF账号,一个域名(一二级无所谓)
域名绑定CF中,添加解析,接入DNS (没有接入使用不了,这个坑注意一下还要绑定好你的域名)
内容无所谓(8.8.8.8)

进入Worlers 创建workers

粘贴JS代码保存部署-添加路由-等待生效
Cloudflare Workers免费套餐的每天10万请求数 可按需升级
* static files (404.html, sw.js, conf.js)
const ASSET_URL = ‘https://hunshcn.github.io/gh-proxy’
// 前缀,如果自定义路由为example.com/gh/*,将PREFIX改为 ‘/gh/’,注意,少一个杠都会错!
// git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关,0为关闭,默认开启
/** @type {RequestInit} */
‘access-control-allow-origin’: ‘*’,
‘access-control-allow-methods’: ‘GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS’,
‘access-control-max-age’: ‘1728000’,
* @param {Object<string, string>} headers
function makeRes(body, status = 200, headers = {}) {
headers[‘access-control-allow-origin’] = ‘*’
return new Response(body, {status, headers})
function newUrl(urlStr) {
addEventListener(‘fetch’, e => {
const ret = fetchHandler(e)
.catch(err => makeRes(‘cfworker error:\n’ + err.stack, 502))
async function fetchHandler(e) {
const urlObj = new URL(urlStr)
let path = urlObj.searchParams.get(‘q’)
return Response.redirect(‘https://’ + urlObj.host + PREFIX + path, 301)
// cfworker 会把路径中的 `//` 合并成 `/`
path = urlObj.href.substr(urlObj.origin.length + PREFIX.length).replace(/^https?:\/+/, ‘https://’)
const exp1 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/i
const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/i
const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$/i
const exp4 = /^(?:https?:\/\/)?raw\.githubusercontent\.com\/.+?\/.+?\/.+?\/.+$/i
if (path.search(exp1) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4) === 0)) {
return httpHandler(req, path)
} else if (path.search(exp2) === 0) {
const newUrl = path.replace(‘/blob/’, ‘@’).replace(/^(?:https?:\/\/)?github\.com/, ‘https://cdn.jsdelivr.net/gh’)
return Response.redirect(newUrl, 302)
path = path.replace(‘/blob/’, ‘/raw/’)
return httpHandler(req, path)
} else if (path.search(exp3) === 0) {
const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, ‘https://github.com.cnpmjs.org’)
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)
return fetch(ASSET_URL + path)
* @param {string} pathname
function httpHandler(req, pathname) {
const reqHdrRaw = req.headers
if (req.method === ‘OPTIONS’ &&
reqHdrRaw.has(‘access-control-request-headers’)
return new Response(null, PREFLIGHT_INIT)
const reqHdrNew = new Headers(reqHdrRaw)
if (urlStr.startsWith(‘github’)) {
urlStr = ‘https://’ + urlStr
const urlObj = newUrl(urlStr)
/** @type {RequestInit} */
return proxy(urlObj, reqInit, rawLen, 0)
* @param {RequestInit} reqInit
async function proxy(urlObj, reqInit, rawLen) {
const res = await fetch(urlObj.href, reqInit)
const resHdrOld = res.headers
const resHdrNew = new Headers(resHdrOld)
const newLen = resHdrOld.get(‘content-length’) || ”
const badLen = (rawLen !== newLen)
return makeRes(res.body, 400, {
‘–error’: `bad len: ${newLen}, except: ${rawLen}`,
‘access-control-expose-headers’: ‘–error’,
const status = res.status
resHdrNew.set(‘access-control-expose-headers’, ‘*’)
resHdrNew.set(‘access-control-allow-origin’, ‘*’)
resHdrNew.delete(‘content-security-policy’)
resHdrNew.delete(‘content-security-policy-report-only’)
resHdrNew.delete(‘clear-site-data’)
return new Response(res.body, {

本文链接:https://www.gnews.xyz/143.html (GNEWS.XYZ)