GitHub 下载 / clone 加速

从获取 API Token 到 git 全局改写、jsDelivr 短链与排障,一篇看完。

目录

概览

规则只有一条:把原始 GitHub 地址原样拼在本站前缀后面, 并在请求里带上你的 API Token。

  • 前缀:https://cdnjs.mhimg.cn/gitjs/
  • 路径里的 https://(含那两个斜杠)要保留,本站会原样转发;
  • 强制鉴权:未携带 Token 的请求返回 401;
  • 不做本地缓存:源站零落盘,纯转发。

注册并获取 API Token

本站不是匿名开放的。注册与取 Token 一共三步:

  1. 打开 用户中心,点「QQ 快捷登录」—— 首次登录即自动注册,不用填表、不用密码。
  2. 在「我的 API Token」一栏复制 Token(形如 f9aa55…)。
  3. 按下面的示例,把 Token 带在请求里。

Token 有三种携带方式:

  • ① Authorization: Bearer <Token> —— curl / git / 一切能设请求头的场景,推荐,不会出现在 URL 与访问日志里;
  • ② ?access_token=<Token> —— 浏览器无法设请求头时使用(Token 会出现在 URL 里);
  • ③ 登录 Cookie —— 在浏览器登录本站后自动携带。
自检:带上 Token 请求一次
export TOKEN="你的API_TOKEN"
curl -sSI -H "Authorization: Bearer $TOKEN" \
  "https://cdnjs.mhimg.cn/gitjs/https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore" | head -n 1

下载文件 / release 附件

把原始地址原样拼在前缀后面即可:

命令行
# 先备好 Token(见上一步),下面用变量代替
TOKEN="你的API_TOKEN"

# 下载文件:Token 放在 Authorization 头里(推荐,不会出现在 URL 与访问日志中)
curl -L -H "Authorization: Bearer $TOKEN" -O \
  https://cdnjs.mhimg.cn/gitjs/https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-amd64

wget --header="Authorization: Bearer $TOKEN" \
  https://cdnjs.mhimg.cn/gitjs/https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# git 仓库:先给本站配上 Token 头(只对本站生效),再照常 clone
git config --global http.https://cdnjs.mhimg.cn/.extraHeader "Authorization: Bearer $TOKEN"
git clone https://cdnjs.mhimg.cn/gitjs/https://github.com/git/git.git
git clone https://cdnjs.mhimg.cn/gitjs/https://github.com/vuejs/core.git --depth=1
场景原始地址加速地址
release 附件 https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-amd64 https://cdnjs.mhimg.cn/gitjs/https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-amd64
raw 文件 https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore https://cdnjs.mhimg.cn/gitjs/https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
源码包 https://github.com/vuejs/core/archive/refs/tags/v3.4.0.tar.gz https://cdnjs.mhimg.cn/gitjs/https://github.com/vuejs/core/archive/refs/tags/v3.4.0.tar.gz
git clone https://github.com/git/git.git https://cdnjs.mhimg.cn/gitjs/https://github.com/git/git.git

让 git 自动加速

两条命令:改写地址 + 附带 Token。

~/.gitconfig
# 1) 只把 github.com 的 https 请求改写到本站(其它仓库不受影响)
git config --global url."https://cdnjs.mhimg.cn/gitjs/https://github.com/".insteadOf "https://github.com/"

# 2) 给本站请求带上你的 Token(Authorization 头,只对本域名生效)
git config --global http.https://cdnjs.mhimg.cn/.extraHeader "Authorization: Bearer 你的API_TOKEN"

# 之后照常敲命令即可:自动走加速,也自动带上 Token
git clone https://github.com/vuejs/core.git

# 还原(卸载改写与 Token 头)
git config --global --unset url."https://cdnjs.mhimg.cn/gitjs/https://github.com/".insteadOf
git config --global --unset http.https://cdnjs.mhimg.cn/.extraHeader

规则只匹配 https://github.com/,所以 gitlab / gitee / 自建 Git 都不受影响; git@github.com:(SSH)同样不受影响 —— SSH 走 22 端口,本站不代理。

jsDelivr 风格短链

引用仓库里的 .js / .css 时,这种写法更短,支持 @版本号 / @commit / @分支:

仓库文件直链
# jsDelivr 风格的仓库文件直链(支持 @版本 / @commit / @分支)
# 浏览器无法自定义请求头,两种方式二选一:
#   A. 已在本站登录 → 浏览器会自动带登录 Cookie,直接访问链接即可
#   B. 未登录 / 在别的网站引用 → 用 ?access_token=(注意:Token 会出现在 URL 里)
https://cdnjs.mhimg.cn/gitjs/gh/jquery/jquery@3.7.1/dist/jquery.min.js?access_token=你的API_TOKEN
https://cdnjs.mhimg.cn/gitjs/gh/github/gitignore@main/Node.gitignore?access_token=你的API_TOKEN

# 放进 <script> / <link> 直接用
<script src="https://cdnjs.mhimg.cn/gitjs/gh/jquery/jquery@3.7.1/dist/jquery.min.js?access_token=你的API_TOKEN"></script>
<link rel="stylesheet" href="https://cdnjs.mhimg.cn/gitjs/gh/twbs/bootstrap@5.3.3/dist/css/bootstrap.min.css?access_token=你的API_TOKEN">

这一族只服务仓库里的文件,不代理 release 附件与源码包。 网页引用时浏览器无法设请求头,需用 ?access_token=,或先在浏览器登录本站。

批量加速小脚本

gh.sh
#!/usr/bin/env bash
# 用法: export MH_TOKEN=你的API_TOKEN  &&  ./gh.sh https://github.com/user/repo/releases/download/v1/a.zip
set -e
TOKEN="${MH_TOKEN:?请先 export MH_TOKEN=你的API_TOKEN}"
PREFIX="https://cdnjs.mhimg.cn/gitjs/"
for u in "$@"; do
    echo "→ ${PREFIX}${u}"
    curl -fL --retry 2 -H "Authorization: Bearer ${TOKEN}" -O "${PREFIX}${u}"
done

存成 gh.sh,导出 Token 后执行:

使用
export MH_TOKEN=<你的Token>
chmod +x gh.sh && ./gh.sh <GitHub 地址> [更多地址…]

验证与排障

常用自检命令
# 1) 不带 Token:应返回 401(说明强制鉴权已生效)
curl -sS -o /dev/null -w "%{http_code}\n" "https://cdnjs.mhimg.cn/gitjs/https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore"

# 2) 带上你的 Token:应返回 200(会带 Content-Length 与 X-MH-Upstream 头)
curl -sS -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer 你的API_TOKEN" \
  "https://cdnjs.mhimg.cn/gitjs/https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore"

常见问题

为什么必须注册 / 带 Token?

公益带宽与上游配额有限。强制鉴权后每个请求绑定真实账号,才能防匿名刷量、对违规仓库定向拦截, 并让你在用户中心看到自己的调用次数与流量。

Token 会不会泄露给上游?

不会。转发前会清掉 Authorization 与 Cookie,Token 只用于本站鉴权。 怀疑泄漏时到用户中心「重置 Token」即可让旧的立即失效。

为什么有的地址必须保留两个斜杠?

加速地址的形状是「本站域名 + 原始完整地址」,中间的 https:// 是原始地址的一部分。 少写一个斜杠,上游收到就是不认识的路径,会 404。

支持私有仓库吗?

不支持。转发时会清掉认证头,私有仓库请直连 GitHub;本站只加速公开资源。

下载失败 / 中途断了怎么办?

调度器每隔几分钟体检线路(先校验内容正确性,再测大文件速度),连续失败的节点会被自动摘除。 遇到 5xx 重试一次通常会换到别的线路。

免责声明:上游是第三方公开加速节点,我们无法保证其持续可用,也不对拉取内容完整性负责;生产环境请保留官方源回退,并对二进制做 sha256 / GPG 校验。