主机测评 - 翘课网 - 国外VPS,国外服务器,主机,测评及优惠码

使用Cloudflare Workers加速任意网站

QioKe 124 0

特别声明:如未注明出处均为翘课网原创、如有侵权请联系我们删除。任何商家都有跑路的可能!请备份好自己的数据!翘课网不会为任何商家做担保!

源码:https://github.com/Siujoeng-Lau/WorkersProxy

使用方法: 修改index.js开头的常量, 然后将它部署到cloudflare workers上即可

1、优点:

用Cloudflare Workers加速任意网站, 无需购买或配置服务器

可以用来做静态资源CDN..不用把域名接入cf

可以根据user-agent屏蔽部分地区或ip

可以绑定自定义域名

2、缺点:
Cloudflare Workers一天免费10万次请求

3、具体源码如下:

// Custom domain of your WorkersProxy.
const main_domain = 'https://cdn.reverse-proxy.live/'

// Subdomain of your Cloudflare Worker.
const workers_domain = 'https://google.xasiimov.workers.dev'

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

// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'https://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', '10.0.0.0']

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 = request.url;

    if (device_status(user_agent)){
        url = url.replace(main_domain, upstream);
        url = url.replace(workers_domain, upstream);
    } else {
        url = url.replace(main_domain, upstream_mobile);
        url = url.replace(workers_domain, upstream_mobile);
    }

    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 headers = request.headers;
        response = fetch(url, {
            method: method,
            headers: headers
        })
    }
    return response;
}

function device_status (userAgentInfo) {
    var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}
4、index.js的具体配置

网址带上https ://
// 自定义域名 (没有就空着)
const main_domain =

// Workers 域名.
const workers_domain =

// 要加速的网站.
const upstream =

// 要给手机用户加速的网站. (如果没有就跟上面一样)
const upstream_mobile =

// 要屏蔽的地区
const blocked_region = [‘CN’, ‘KP’, ‘SY’, ‘PK’, ‘CU’]

// 要屏蔽的ip
const blocked_ip_address = [‘0.0.0.0’, ‘10.0.0.0’]

评论列表 (已有0条评论)

消灭零回复

发表评论 (已有0条评论)