利用 Cloudflare Workers 免费搭建 Google 镜像反代网站

Workers-Proxy是基于Cloudflare Workers的轻量级Javascript 反向代理。 用户可以在Cloudflare的全球网络上部署反向代理,而无需设置虚拟专用服务器和配置Nginx或Apache。

通过配置地区和 IP 过滤器,你可以根据法律规定在部分国家和地区停用反向代理服务。借助移动端跳转工具,你可以根据用户的设备来分发不同的网站。

特点

  • 建立镜像网站
  • 利用Cloudflare的全球网络提高加载速度
  • 提高安全性(隐藏网站的IP地址)阻止特定区域或IP地址
  • 将移动用户重定向到其他网页

搭建方法

1. 首先登录 Cloudflare 后 点击 Workers

2. 点进去后新建一个 Workers 子域名(已创建的可省略该步骤):

3. 点击创建一个 Worker

4. 将 Workers 代码粘贴进去,然后可以在左上角双击修改域名,再点击保存并部署即可(可根据需求修改红色框内的代码):

Workers 代码

 注意:如果部署完成后提示所在区域无法使用,请删除下图第十行中的 ‘CN’ 即可。 

 可将 www.google.com 修改为你想要的网址即可:

// Website you intended to retrieve for users.
const upstream = 'www.google.com'

// Custom pathname for the upstream website.
const upstream_path = '/'

// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'www.google.com'

// Countries and regions where you wish to suspend your service.
const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU']

// IP addresses which you wish to block from using your service.
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']

// Whether to use HTTPS protocol for upstream address.
const https = true

// Whether to disable cache.
const disable_cache = false

// Replace texts.
const replace_dict = {
    '$upstream': '$custom_domain',
    '//google.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', upstream_domain);
        new_request_headers.set('Referer', url.protocol + '//' + url_hostname);

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

        connection_upgrade = new_request_headers.get("Upgrade");
        if (connection_upgrade && connection_upgrade.toLowerCase() == "websocket") {
            return original_response;
        }

        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;
		
		if (disable_cache) {
			new_response_headers.set('Cache-Control', 'no-store');
	    }

        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');
		
		if (new_response_headers.get("x-pjax-url")) {
            new_response_headers.set("x-pjax-url", response_headers.get("x-pjax-url").replace("//" + upstream_domain, "//" + url_hostname));
        }
		
        const content_type = new_response_headers.get('content-type');
        if (content_type != null && 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;
}

END~

技术教程

Krypt iON 虚机无限免费添加 Cloudflare Pro Plesk 订阅

2020-5-6 7:43:08

技术教程

安卓最新修改微信号教程

2020-5-30 7:27:44

10 条回复 A文章作者 M管理员
  1. 搜索失败

    About this page

    Our systems have detected unusual traffic from your computer network. This page checks to see if it’s really you sending the requests, and not a robot. Why did this happen?

    • 配置不当

  2. 我们的系统检测到您的计算机网络中存在异常流量。此网页用于确认这些请求是由您而不是自动程序发出的。为什么会这样?

    • 不太清楚 自己改改应该就好了

    • 就是IP被屏蔽的意思 ✗吐舌头✗

    • 一个IP用的人太多了,如何解决不知道~

    • 应该IP会定期变更得把!一般不会这样!

    • 分配的是 CF 的IP 应该是会有变动的

  3. 你可以翻墙,通过谷歌验证就行了,不过可能还会出现。。。

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索