随手记:用 Cloudflare Workers 手搓一个极简 GitHub 文件加速与镜像代理
【摘要】 随手记:用 Cloudflare Workers 手搓一个极简 GitHub 文件加速与镜像代理在日常开发或部署自动化脚本时,从 GitHub 拉取 Release 二进制包、Clone 仓库,或是下载 raw.githubusercontent.com 上的配置文件,经常会遇到网络波动或龟速下载的尴尬。与其依赖第三方不稳定的公共镜像站,不如利用 Cloudflare Workers 免费额...
随手记:用 Cloudflare Workers 手搓一个极简 GitHub 文件加速与镜像代理
在日常开发或部署自动化脚本时,从 GitHub 拉取 Release 二进制包、Clone 仓库,或是下载
raw.githubusercontent.com 上的配置文件,经常会遇到网络波动或龟速下载的尴尬。与其依赖第三方不稳定的公共镜像站,不如利用 Cloudflare Workers 免费额度(每天 10 万次请求),花 5 分钟自己部署一个完全可控的 GitHub 边缘加速代理。
这篇文章就来拆解如何用不到 80 行原生 JavaScript,实现一个支持
raw 文件、Release 附件以及 git clone 协议的轻量级 Workers 代理。一、 核心原理:边缘节点的请求重写与透传
Cloudflare Workers 运行在距离用户最近的全球边缘节点上,而它回源请求 GitHub 服务器走的是 Cloudflare 内部的高速骨干网。
我们要做的事情非常纯粹:
-
接收用户的请求路径(例如
[https://gh.your-domain.com/https://github.com/user/repo/releases/download/](https://gh.your-domain.com/https://github.com/user/repo/releases/download/)...)。 -
校验目标域名白名单,防止代理被恶意滥用成开放式跳板(Open Proxy)。
-
剥离前缀,保留原始的
Headers(包括Range断点续传头、Git 协议的Authorization认证头等)和请求体,直接透传给 GitHub。 -
处理 GitHub 302 重定向(Release 文件通常会跳转到
objects.githubusercontent.com),确保下载流量依然走代理通道。
二、 完整 Workers 代码实现
在 Cloudflare Dashboard 创建一个新的 Worker,将以下代码粘贴进去即可运行:
JavaScript
// 允许代理的 GitHub 相关域名白名单,防止被恶意扫描滥用
const ALLOWED_HOSTS = new Set([
'github.com',
'raw.githubusercontent.com',
'gist.githubusercontent.com',
'objects.githubusercontent.com',
'codeload.github.com',
'api.github.com',
]);
export default {
async fetch(request) {
const url = new URL(request.url);
// 提取路径中的目标 URL,例如 /https://github.com/... 或 /github.com/...
let targetPath = url.pathname.slice(1) + url.search;
if (!targetPath) {
return new Response('GitHub Edge Proxy is running.', { status: 200 });
}
// 自动补全协议头
if (!targetPath.startsWith('http://') && !targetPath.startsWith('https://')) {
targetPath = 'https://' + targetPath;
}
let targetUrl;
try {
targetUrl = new URL(targetPath);
} catch {
return new Response('Invalid target URL.', { status: 400 });
}
// 严格校验域名白名单
if (!ALLOWED_HOSTS.has(targetUrl.hostname)) {
return new Response(`Host ${targetUrl.hostname} is not allowed.`, { status: 403 });
}
// 构造回源请求,完整保留 Method、Headers 与 Body(支持 git push/clone 与断点续传)
const proxyHeaders = new Headers(request.headers);
proxyHeaders.set('Host', targetUrl.hostname);
proxyHeaders.delete('cf-connecting-ip');
proxyHeaders.delete('cf-ipcountry');
proxyHeaders.delete('cf-ray');
proxyHeaders.delete('cf-visitor');
const response = await fetch(targetUrl.toString(), {
method: request.method,
headers: proxyHeaders,
body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : null,
redirect: 'manual', // 手动拦截重定向,防止下载 Release 时跳回直连域名
});
// 处理 301/302 跳转,重写 Location 头部继续走代理
const newHeaders = new Headers(response.headers);
if ([301, 302, 307, 308].includes(response.status)) {
const location = newHeaders.get('location');
if (location) {
newHeaders.set('location', `${url.origin}/${location}`);
}
}
// 允许跨域调用(方便前端页面或脚本直接拉取 raw JSON/YAML 配置)
newHeaders.set('Access-Control-Allow-Origin', '*');
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders,
});
},
};
三、 绑定自定义域名与日常使用姿势
由于默认的
*.workers.dev 域名在某些网络环境下可能受限,强烈建议在 Worker 的 Settings -> Domains & Routes 中绑定一个自己的自定义二级域名(例如 gh.example.com)。部署完成后,日常使用几乎是无感的:
-
加速下载 Release 资产或脚本:
Bashwget https://gh.example.com/https://github.com/derailed/k9s/releases/download/v0.32.5/k9s_Linux_amd64.tar.gz -
无缝加速
git clone(无需修改每次输入的命令): 通过 Git 全局配置规则,自动将所有[https://github.com/](https://github.com/)的请求重写到代理域名:
Bashgit config --global url."https://gh.example.com/https://github.com/".insteadOf "https://github.com/"(注:如果只想针对特定目录生效,去掉--global参数即可;需要恢复时执行git config --global --unset url."[https://gh.example.com/https://github.com/](https://gh.example.com/https://github.com/)".insteadOf)
四、 进阶安全与细节微调
-
防盗刷保护:如果你的域名公开在博客或脚本里,建议在 Cloudflare 侧给该子域名加上一层简单的 Rate Limiting(速率限制)规则,或者在代码里增加一段简单的路径 Token 校验(例如必须以
/secret-token/[https://github.com/](https://github.com/)...开头)。 -
大文件传输:Workers 对响应体没有严格的内存缓冲限制(采用流式传输
response.body),因此即便是几个 GB 的 Release 压缩包或模型权重文件,也能保持低内存占用稳定跑满带宽。
一个 Serverless 边缘函数,零服务器维护成本,却能把日常拉代码、下依赖的体验提升一个档次——这大概就是边缘计算最接地气的用法。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)