用CloudFlare Workers搭建baidu镜像站

不多说开始搭建吧

从网上搜到了利用 cloudflare workers 来搭建一个网站的镜像。

链接:简单几步用Cloudflare Workers搭建WorkersProxy加速任意网站

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// List of domains bind to your WorkersProxy.
const domain_list = ['https://你的cloudflareWorker应用地址/']

// Website you intended to retrieve for users.
const upstream = 'https://需要加速的网站的地址/'

// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'https://需要加速的网站移动端的地址/'

// Countries and regions where you wish to suspend your service.
const blocked_region = []

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

我在gist.github.com 在备份一份,免得丢失。

主要就是修改第31行和第32行,改成自己相应的网址就好。

1
2
3
4
5
6
7
8
// List of domains bind to your WorkersProxy.
const domain_list = ['https://你的cloudflareWorker应用地址/']

// Website you intended to retrieve for users.
const upstream = 'https://需要加速的网站的地址/'

// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'https://需要加速的网站移动端的地址/'

改成下面的:

1
2
3
4
5
6
7
8
// List of domains bind to your WorkersProxy.
const domain_list = ['https://你的cloudflareWorker应用地址/']

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

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

ok,访问试一试,完全正常。

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
,