还在为 GitHub 下载速度慢而烦恼吗?还在对着进度条望眼欲穿吗?今天,就为大家带来一款神器——gh-proxy,让你彻底告别 GitHub 下载龟速时代,体验飞一般的速度!
项目地址:https://github.com/hunshcn/gh-proxy

简介
github release、archive以及项目文件的加速项目,支持clone,有Cloudflare Workers无服务器版本以及Python版本
如何部署 gh-proxy?
Cloudflare Workers部署(推荐 )
首页:https://workers.cloudflare.com
1.注册,登陆,Start building。2.复制 index.js 到左侧代码框,Save and deploy。如果正常,右侧应显示首页。
'use strict'
/**
* static files (404.html, sw.js, conf.js)
*/
constASSET_URL='https://hunshcn.github.io/gh-proxy/'
// 前缀,如果自定义路由为example.com/gh/*,将PREFIX改为 '/gh/',注意,少一个杠都会错!
constPREFIX='/'
// 分支文件使用jsDelivr镜像的开关,0为关闭,默认关闭
constConfig={
jsdelivr:0,
}
const whiteList =[]// 白名单,路径里面有包含字符的才会通过,e.g. ['/username/']
/** @type{ResponseInit} */
constPREFLIGHT_INIT={
status:204,
headers:newHeaders({
'access-control-allow-origin':'*',
'access-control-allow-methods':
'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
'access-control-max-age':'1728000',
}),
}
const exp1 =
/^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/i
const exp2 =/^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob|raw)\/.*$/i
const exp3 =/^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$/i
const exp4 =
/^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+?\/.+$/i
const exp5 =
/^(?:https?:\/\/)?gist\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+$/i
const exp6 =/^(?:https?:\/\/)?github\.com\/.+?\/.+?\/tags.*$/i
/**
* @param{any}body
* @param{number}status
* @param{Object<string, string>}headers
*/
functionmakeRes(body, status =200, headers ={}){
headers['access-control-allow-origin']='*'
returnnewResponse(body,{ status, headers })
}
/**
* @param{string}urlStr
*/
functionnewUrl(urlStr){
try{
returnnewURL(urlStr)
}catch(err){
returnnull
}
}
addEventListener('fetch',(e)=>{
const ret =fetchHandler(e).catch((err)=>
makeRes('cfworker error:\n'+ err.stack,502)
)
e.respondWith(ret)
})
functioncheckUrl(u){
for(let i of[exp1, exp2, exp3, exp4, exp5, exp6]){
if(u.search(i)===0){
returntrue
}
}
returnfalse
}
/**
* @param{FetchEvent}e
*/
asyncfunctionfetchHandler(e){
const req = e.request
const urlStr = req.url
const urlObj =newURL(urlStr)
let path = urlObj.searchParams.get('q')
if(path){
returnResponse.redirect('https://'+ urlObj.host+PREFIX+ path,301)
}
// cfworker 会把路径中的 `//` 合并成 `/`
path = urlObj.href
.substr(urlObj.origin.length+PREFIX.length)
.replace(/^https?:\/+/,'https://')
if(
path.search(exp1)===0||
path.search(exp5)===0||
path.search(exp6)===0||
path.search(exp3)===0||
path.search(exp4)===0
){
returnhttpHandler(req, path)
}elseif(path.search(exp2)===0){
if(Config.jsdelivr){
const newUrl = path
.replace('/blob/','@')
.replace(
/^(?:https?:\/\/)?github\.com/,
'https://cdn.jsdelivr.net/gh'
)
returnResponse.redirect(newUrl,302)
}else{
path = path.replace('/blob/','/raw/')
returnhttpHandler(req, path)
}
}elseif(path.search(exp4)===0){
const newUrl = path
.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/,'@$1')
.replace(
/^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com/,
'https://cdn.jsdelivr.net/gh'
)
returnResponse.redirect(newUrl,302)
}else{
returnfetch(ASSET_URL+ path)
}
}
/**
* @param{Request}req
* @param{string}pathname
*/
functionhttpHandler(req, pathname){
const reqHdrRaw = req.headers
// preflight
if(
req.method==='OPTIONS'&&
reqHdrRaw.has('access-control-request-headers')
){
returnnewResponse(null,PREFLIGHT_INIT)
}
const reqHdrNew =newHeaders(reqHdrRaw)
let urlStr = pathname
let flag =!Boolean(whiteList.length)
for(let i of whiteList){
if(urlStr.includes(i)){
flag =true
break
}
}
if(!flag){
returnnewResponse('blocked',{status:403})
}
if(urlStr.search(/^https?:\/\//)!==0){
urlStr ='https://'+ urlStr
}
const urlObj =newUrl(urlStr)
/** @type{RequestInit} */
const reqInit ={
method: req.method,
headers: reqHdrNew,
redirect:'manual',
body: req.body,
}
returnproxy(urlObj, reqInit)
}
/**
*
* @param{URL}urlObj
* @param{RequestInit}reqInit
*/
asyncfunctionproxy(urlObj, reqInit){
const res =awaitfetch(urlObj.href, reqInit)
const resHdrOld = res.headers
const resHdrNew =newHeaders(resHdrOld)
const status = res.status
if(resHdrNew.has('location')){
let _location = resHdrNew.get('location')
if(checkUrl(_location)) resHdrNew.set('location',PREFIX+ _location)
else{
reqInit.redirect='follow'
returnproxy(newUrl(_location), reqInit)
}
}
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')
returnnewResponse(res.body,{
status,
headers: resHdrNew,
})
}
先部署默认的Workers,然后再点击编辑代码
复制index.js到左侧代码框并部署
右侧显示主页
ASSET_URL是静态资源的url(实际上就是现在显示出来的那个输入框单页面)
PREFIX是前缀,默认(根路径情况为"/"),如果自定义路由为example.com/gh/*,请将PREFIX改为 '/gh/',注意,少一个杠都会错!
3.为 Workers 项目使用优选域名
给你将要使用的域名,添加一个CNAME类型的解析记录,名称为您所需的自定义域示例ghproxy,目标为优选域名示例visa.com即可
重要提示:不要打开小黄云(Cloudflare 的代理功能)!!!
左侧选择 Workers 路由 > 添加路由
路由填入 Worker 项目最终使用的自定义域ghproxy.laolang.dpdns.org/*,Worker 选中对应的Worker项目名后点击保存即可
注意自定义域末位必须加上/*,也就是ghproxy.laolang.dpdns.org/*!!!
4.绑定自定义域
进入我们的Workers
选择设置,域和路由点击添加
自定义一个绑定的子域名ghproxy.laolang.dpdns.org,点击添加域。 4.浏览器访问我们绑定的自定义域名
Docker部署
1.购买一台能访问github的服务器,登录机器,安装Docker,然后执行下面的命令
安装Docker请看我之前的文章一步到位!Docker全家桶一键安装,省时省力,马上上手
docker run -d--name="gh-proxy-py"\
-p0.0.0.0:8080:80 \
--restart=always \
hunsh/gh-proxy-py:latest
2.进行ngixn反代,通过域名访问
参考我的文章Nginx Proxy Manager:告别繁琐配置,轻松管理你的反向代理!
如何使用 gh-proxy?
这是最简单也是最常用的方法。只需要在 GitHub 仓库的 URL 前面加上 https://ghproxy.laolang.dpdns.org/ 即可。
例如,原始的 GitHub 仓库地址是:
https://github.com/hunshcn/gh-proxy/archive/refs/heads/master.zip
使用 gh-proxy 加速后的地址就是:
https://ghproxy.laolang.dpdns.org/https://github.com/hunshcn/gh-proxy/archive/refs/heads/master.zip
将这个修改后的链接复制到你的下载工具中,就可以享受加速下载了。下图是在极空间NAS中进行下载
当然也可以在浏览器页面输入
具体用法可以参考 gh-proxy 的官方文档。
注意事项
大量使用请自行部署,以上域名仅为演示使用。
请勿滥用 gh-proxy,避免对 GitHub 服务器造成不必要的压力。
如果在使用过程中遇到问题,可以参考 gh-proxy 的官方文档或在 GitHub 上提交 issue。
总结
gh-proxy 是一款非常实用的 GitHub 镜像加速工具,可以有效解决国内用户访问 GitHub 速度慢的问题。如果你经常需要从 GitHub 下载资源,不妨尝试一下 gh-proxy,相信它会给你带来惊喜!
赶紧分享给你的小伙伴们,一起告别 GitHub 下载龟速时代吧!
本文链接:https://kinber.cn/post/5232.html 转载需授权!
推荐本站淘宝优惠价购买喜欢的宝贝: