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

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

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

CloudFlare是一个非常优秀的CDN服务商,不仅提供了免费的CDN加速服务,同时可以免费开启SSL证书、网站防火墙、AMP加速、支持DNSSEC解析、Google Analytics等相应的服务,相当于给自己的网站做了全方位的加速与保护。

近几年cloudflare除了cdn外,还提供了一些列的云服务,比如这个worker,这个worker可以理解为一个serverless的平台,通过编写worker的代码及数量来提供分布式的服务。

今天我们通过这个免费的worker来实现一个github镜像站,这样访问github会更快。

一、注册cloudflare

官网地址https://dash.cloudflare.com/

只需要一个邮箱就能注册了,非常方便。

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

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

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

左侧的菜单朗中选择workers

然后点击创建服务

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

选择第一个http处理程序

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


点击右下角创建服务后

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

点击快速编辑

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

将左侧代码更换为

// 你要镜像的网站.
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 = upstream_mobile;
    }

    url.host = upstream_domain;
    if (url.pathname == '/') {
        url.pathname = upstream_path;
    } else {
        url.pathname = upstream_path + url.pathname;
    }

    if (blocked_region.includes(region)) {
        response = new Response('Access denied: WorkersProxy is not available in your region yet.', {
            status: 403
        });
    } else if (blocked_ip_address.includes(ip_address)) {
        response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', {
            status: 403
        });
    } else {
        let method = request.method;
        let request_headers = request.headers;
        let new_request_headers = new Headers(request_headers);

        new_request_headers.set('Host', url.hostname);
        new_request_headers.set('Referer', url.hostname);

        let original_response = await fetch(url.href, {
            method: method,
            headers: new_request_headers
        })

        let original_response_clone = original_response.clone();
        let original_text = null;
        let response_headers = original_response.headers;
        let new_response_headers = new Headers(response_headers);
        let status = original_response.status;

        new_response_headers.set('access-control-allow-origin', '*');
        new_response_headers.set('access-control-allow-credentials', true);
        new_response_headers.delete('content-security-policy');
        new_response_headers.delete('content-security-policy-report-only');
        new_response_headers.delete('clear-site-data');

        const content_type = new_response_headers.get('content-type');
        if (content_type.includes('text/html') && content_type.includes('UTF-8')) {
            original_text = await replace_response_text(original_response_clone, upstream_domain, url_hostname);
        } else {
            original_text = original_response_clone.body
        }

        response = new Response(original_text, {
            status,
            headers: new_response_headers
        })
    }
    return response;
}

async function replace_response_text(response, upstream_domain, host_name) {
    let text = await response.text()

    var i, j;
    for (i in replace_dict) {
        j = replace_dict[i]
        if (i == '$upstream') {
            i = upstream_domain
        } else if (i == '$custom_domain') {
            i = host_name
        }

        if (j == '$upstream') {
            j = upstream_domain
        } else if (j == '$custom_domain') {
            j = host_name
        }

        let re = new RegExp(i, 'g')
        text = text.replace(re, j);
    }
    return text;
}


async function device_status(user_agent_info) {
    var agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < agents.length; v++) {
        if (user_agent_info.indexOf(agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

点击保存并部署

打开cloudflare给你的url地址就可以看到github可以打开了。

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

总结

cloudflare通过自己编写代码的worker服务来开启一个镜像站点服务,关键还是免费的,同理我们可以将域名更换为其他的站点进行镜像服务。

{{collectdata}}

网友评论0