Cloudflare Workers™ 官网介绍:
在边缘运行代码,提供强大的 Web 可扩展性
在边缘应用自定义安全规则和过滤逻辑来检测恶意 Bots 病毒并防止它们消耗资源,从而提高安全性。
将更多个性化和交互性纳入静态 HTML 页面,并在边缘运行动态请求,从而改善用户体验。
将更多操作流程和请求处理转移到边缘,以提高缓存命中率并降低带宽成本,从而降低运营成本。
简单点说就是在 Cloudflare Workers 部署一个 Worker 时,它会在 30 秒之内部署到 Cloudflare 的整个边缘网络,全世界共 100 多个节点。每个请求都会由离用户最近地点的 Worker 来处理,可保证最快、最便捷的代码运行。
优点
- 用 Cloudflare Workers 加速任意网站, 无需购买或配置服务器
- 可以用来做静态资源 CDN..不用把域名接入 Cloudflare
- 可以根据 user-agent 屏蔽部分地区或 ip
- 可以绑定自定义域名
缺点
- Cloudflare Workers 每天只要免费的 10 万次请求 ,不过对于个人查资料等来说完全足够了。
开始创建 Worker
1、Cloudflare 官网: https://www.cloudflare.com/ ,注册并登录。
2、点击进去之后选择 Free 免费
3、点击创建 Worker
4、在 Worker 编辑页面粘贴代码,如下:
// 你要镜像的网站.
const upstream = 'www.google.com'
// 镜像网站的目录,比如你想镜像某个网站的二级目录则填写二级目录的目录名,镜像 google 用不到,默认即可.
const upstream_path = '/'
// 镜像站是否有手机访问专用网址,没有则填一样的.
const upstream_mobile = 'www.google.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',
'//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', 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;
}
如图:
到这里就全部完成了,至此你可以使用你的专属 Google 镜像访问地址了。
何先生搭建好的 Google 镜像站地址是:https://gentle-band-363a.hexsen.workers.dev
THE END
- 最新
- 最热
只看作者