使用cloudflare免费做一个github镜像站

CloudFlare是一个非常优秀的CDN服务商,不仅提供了免费的CDN加速服务,同时可以免费开启SSL证书、网站防火墙、AMP加速、支持DNSSEC解析、Google Analytics等相应的服务,相当于给自己的网站做了全方位的加速与保护。
近几年cloudflare除了cdn外,还提供了一些列的云服务,比如这个worker,这个worker可以理解为一个serverless的平台,通过编写worker的代码及数量来提供分布式的服务。
今天我们通过这个免费的worker来实现一个github镜像站,这样访问github会更快。
一、注册cloudflare
官网地址https://dash.cloudflare.com/只需要一个邮箱就能注册了,非常方便。

从官网注册完成之后,会跳转到转到控制中心页面。

左侧的菜单朗中选择workers
然后点击创建服务

选择第一个http处理程序

点击右下角创建服务后

点击快速编辑

将左侧代码更换为
// 你要镜像的网站.
const upstream = 'www.github.com'
// 镜像网站的目录,比如你想镜像某个网站的二级目录则填写二级目录的目录名,镜像 google 用不到,默认即可.
const upstream_path = '/'
// 镜像站是否有手机访问专用网址,没有则填一样的.
const upstream_mobile = 'www.github.com'
// 屏蔽国家和地区.
const blocked_region = ['KP', 'SY', 'PK', 'CU']
// 屏蔽 IP 地址.
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']
// 镜像站是否开启 HTTPS.
const https = true
// 文本替换.
const replace_dict = {
'$upstream': '$custom_domain',
'//github.com': ''
}
// 以下保持默认,不要动
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request));
})
async function fetchAndApply(request) {
const region = request.headers.get('cf-ipcountry').toUpperCase();
const ip_address = request.headers.get('cf-connecting-ip');
const user_agent = request.headers.get('user-agent');
let response = null;
let url = new URL(request.url);
let url_hostname = url.hostname;
if (https == true) {
url.protocol = 'https:';
} else {
url.protocol = 'http:';
}
if (await device_status(user_agent)) {
var upstream_domain = upstream;
} else {
var upstream_domain...点击查看剩余70%
网友评论0