CloudFlare Workers 反代任意网站和挂载单页代码

CloudFlare Workers是一个支持jsproxy的无服务器函数服务,提供全球CDN支持,免费用户有每天10万请求额度;
CloudFlare官网:https://dash.cloudflare.com

Workers 单页挂载代码

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

// HTML代码
let html = `
<!DOCTYPE html>
<html>
  <head><title>Test</title></head>
  <body><div>Hello world!</div></body>
</html>
`;

/**
 * Respond to the request
 * @param {Request} request
 */
async function handleRequest(request) {
  return new Response(html, {
    headers: {
      'Content-Type': 'text/html; charset=UTF-8'
    },
    status: 200
  })
}

Workers 反代任意网站

两个版本代码,第一个功能多一点,第二个功能少一点,任选其一,第一个速度会快一些而且稳定。

https://github.com/Siujoeng-Lau/Workers-Proxy

// List of domains bind to your WorkersProxy. 所有绑定到这个worker的域名
const domain_list = ['https://cdn.reverse-proxy.live/', '这里需要填写你的项目域名']

// Website you intended to retrieve for users.要加速的网站.
const upstream = 'http://google.com'

// Website you intended to retrieve for users using mobile devices.要给手机用户加速的网站. (可以填成和上面一样的)
const upstream_mobile = 'http://google.com'

// Countries and regions where you wish to suspend your service.要屏蔽的地区
const blocked_region = [ 'KP', 'SY', 'PK', 'CU']

// IP addresses which you wish to block from using your service.要屏蔽的ip
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 (await device_status(user_agent)){
        upstream_domain = upstream
    } else {
        upstream_domain = upstream_mobile
    }

    for(let domain of domain_list) {
        url = url.replace(domain, upstream_domain)
    };

    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;
}

async 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;
}

https://github.com/Dandelionss/CloudFlare-Workers

addEventListener("fetch", event => {
  let url = new URL(event.request.url);
  url.hostname = "www.google.com"; //改成你想反代的网站

  let request = new Request(url, event.request);
  event.respondWith(
    fetch(request, {
      headers: {
        'Referer': 'https://www.cloudflare.com/', //referer头,不必修改
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' //
      }
    })
  );
});
© 版权声明
THE END
点赞0