0%

利用cloudflare_workers反代理镜像Google

实现本文可以最低成本使用世界上优秀的搜索引擎,而非竞价排名的毒瘤.

多说两句

​ 反代理技术很多,如Nginx,caddy,或是使用php脚本等,都需要一个合格的外国服务器或是虚拟主机,而cloudflare workers边缘计算则可以无需任何服务器或是虚拟主机,只需要一个邮箱就可以.

​ 嫌麻烦可以直接看福利部分,若嫌弃不稳定则可以使用多吉搜索(国产版duckduckgo)

步骤

1.注册cloudflare 账号,并验证邮箱

2.登录cloudflare 后进入,点击右上角的人像图标,点击 Account Home

Account Home

3.点击Workers

Workers

4.注册一个你喜欢二级域名,如我的是fuckqq,点Set up,再次确认再点Confirm

fuckqq

5.选择Free的套餐,点击Continue with Free

free

6.点击Create a Worker

Create a Worker

7.在编辑页面的左上角修改下项目名称,如g,粘贴代码(代码在最下面)

步骤

9.点击Save and Deploy即可部署完毕,要求再次确认点击Save and Deploy即可.

福利

​ 通过类似关键词(google workers 反代理)可以找到别的博主已经部署好的网站,如下

麻烦

老是出现流量异常?不能访问?打开漫长?不想折腾?可以使用Chrome的两款插件

  1. 谷歌访问助手-破解版
  2. 集装箱-内含Google助手

提示

  • 若为公开,建议开启搜索安全模式
  • 出现验证码,则可以修改 upstream的域名,如www.google.com.hk或是在google全球各国域名与各国使用语言中随便找一个.
  • 为了不必要的麻烦建议开启对地区或IP进行限制,如默认代码中就对中国,朝鲜,叙利亚等地区进行禁止访问限制
  • 如果默认的域名被污染可以尝试绑定域名进行解决
  • 每日的流量有限制,需求大的可以多创建几个账号做好负载均衡

代码

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Website you intended to retrieve for users.
const upstream = 'www.google.com'

// Custom pathname for the upstream website.
const upstream_path = '/'

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

// Whether to use HTTPS protocol for upstream address.
const https = true

// Replace texts.
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;
}

类似

参考