简介
现在都流行无服务器函数,作为大牌CDN提供商cloud flare自然不甘落后推出了worker服务。免费用户有10万请求/每天 的额度
https://workers.cloudflare.com/
作用
- 反向代理
- 镜像网站
- 在线代理
- 加速任意网站
- 建立免费网址导航
反向代理
addEventListener('fetch', event => {
const request = event.request;
const url = new URL(request.url);
const response = fetch('https://magnetdog.net/' + url.pathname + url.search, {
method: request.method,
headers: request.headers,
body: request.body,
});
event.respondWith(response);
});
建立免费网址导航
系统安装
- 在 Cloudflare Worker 管理页面创建一个新的 Worker 。
- 在Worker编辑页面左边粘贴
index.js
中的代码。 - 根据自身需要修改
config
的配置内容
Github地址:https://github.com/sleepwood/CF-Worker-Dir/
演示地址:http://gethe.best
免费部署 JSProxy 服务
SProxy 一个基于浏览器端 JS 实现的在线代理,这里不多介绍!
本文主要介绍一下利用CloudFlare Worker
来搭建一个JSProxy服务。
CloudFlare Worker
是 CloudFlare 的边缘计算服务。开发者可通过 JavaScript 对 CDN 进行编程,从而能灵活处理 HTTP 请求。这使得很多任务可在 CDN 上完成,无需自己的服务器参与。
CFW免费服务,支持每天10 万次免费请求!基本也够用了!
项目地址:https://github.com/EtherDream/jsproxy
Cloudflare workers + Github 实现的动态博客系统
项目地址 https://github.com/kasuganosoras/cloudflare-worker-blog
Cloudflare workers + Github 实现的动态博客系统,使用边缘计算,无需服务器
用户可以使用 JavaScript 编写自己的程序,然后可以直接通过域名访问运行。
获取到自己的**.workers.dev
域名 Cname 到(**.workers.dev)
然后去 Workers 点击 ADD route
输入自定义域名,例如 gd.mjj.com/* 后面要加上 /*,下面的 workers 选择刚才的项目
利用 Cloudflare Worker 搭建镜像网站!
// 反代目标网站.
const upstream = 'google.com'
// 反代目标网站的移动版.
const upstream_mobile = 'google.com'
// 访问区域黑名单(按需设置).
const blocked_region = ['TK']
// IP地址黑名单(按需设置).
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']
// 路径替换.
const replace_dict = {
'$upstream': '$custom_domain',
'//archiveofourown.org': ''
}
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_host = url.host;
if (url.protocol == 'http:') {
url.protocol = 'https:'
response = Response.redirect(url.href);
return response;
}
if (await device_status(user_agent)) {
var upstream_domain = upstream;
} else {
var upstream_domain = upstream_mobile;
}
url.host = 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 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.href);
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('cache-control' ,'public, max-age=14400')
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_host);
} 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;
}
const upstream
= '反代目标网站'const upstream_mobile
= '反代目标网站的移动版'const blocked_region
= ['访问区域黑名单(按需设置)']const blocked_ip_address
= ['IP地址黑名单(按需设置)', '127.0.0.1']const replace_dict
= 路径替换
在 CF Workers 搭建自己的 t.cn 短网址入口
addEventListener('fetch', (event) => {
return event.respondWith(handleRequest(event.request));
})
const handleRequest = async (request) => {
const render = (body) => {
return new Response(`
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>T.CN 短网址</title>
</head>
<body>${body}</body>
</html>`.trim(), {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8'
}
});
}
request = new URL(request.url);
if (request.pathname !== '/') return new Response(null, { status: 404 });
if (request.searchParams.has('url')) {
const url = request.searchParams.get('url');
const response = await fetch(`http://service.weibo.com/share/share.php?url=${encodeURIComponent(url)}&title=1`);
const html = await response.text();
const short = html.match(/http:\/\/t.cn\/\w+/i);
const refer = html.match(/\$refer\s+: "(.+?)"/i);
if (short && refer) {
return render(`
缩短结果:<a href="${short[0]}">${short[0]}</a><br />
原始网址:<a href="${refer[1]}">${refer[1]}</a><br />
<a href="/">返回</a>
`);
}
return render(`请求失败`);
}
return render(`
<form method="GET">
<input name="url" placeholder="URL" />
<button type="submit">压缩</button>
</form>
`);
}
改进版
addEventListener('fetch', (event) => {
return event.respondWith(handleRequest(event.request));
})
const handleRequest = async (request) => {
const render = (body) => {
return new Response(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>T.CN 短网址</title>
<style media="screen">
body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
#message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px; border-radius: 3px; }
#message h2 { color: rgba(0,0,0,0.6); font-weight: bold; font-size: 14px; margin: 0 0 8px; }
#message h1 { color: #ffa100; font-size: 30px; font-weight: 300; margin: 0 0 16px;}
#message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
#message a { display: block; text-align: center; background: #039be5; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
#message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
#load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
@media (max-width: 600px) {
body, #message { margin-top: 0; background: white; box-shadow: none; }
body { border-top: 16px solid #ffa100; }
}
</style>
</head>
<body>
${body}
</body>
</html>`.trim(), {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8'
}
});
}
request = new URL(request.url);
if (request.pathname !== '/') return new Response(null, { status: 404 });
if (request.searchParams.has('url')) {
const url = request.searchParams.get('url');
const response = await fetch(`http://service.weibo.com/share/share.php?url=${encodeURIComponent(url)}&title=1`);
const html = await response.text();
const short = html.match(/http:\/\/t.cn\/\w+/i);
const refer = html.match(/\$refer\s+: "(.+?)"/i);
if (short && refer) {
return render(`
<div id="message" align="center">
<center><h1>缩短结果:</h1><a href="${short[0]}">${short[0]}</a></center>
<p></p>
<center><h1>原始网址:</h1><a href="${refer[1]}">${refer[1]}</a></center>
<p></p>
<a href="/">返回</a>
</div>
`);
}
return render(`
<div id="message" align="center">
<center><h1>请求失败</h1></center>
</div>
`);
}
return render(`
<div id="message" align="center">
<center><h1>T.CN 短网址</h1></center>
<p></p>
<form method="GET">
<input name="url" placeholder="URL" />
<button type="submit">压缩</button>
</form>
</div>
`);
}
评论 (0)