使用CloudFlare搭建反向代理
使用CloudFlare搭建反向代理
+使用CloudFlare搭建反向代理
使用CloudFlare搭建反向代理
CloudFlare,互联网大善人!
FREE套餐CloudFlare每日提供 10w 请求的免费额度,个人用完全足够,但并不能保证很多人同时使用,强烈建议仅自用
@@ -205,14 +205,20 @@准备
一个CloudFlare账号,如果没有就去CloudFlare注册 这不废话一个可以添加到CloudFlare的域名(可选),主要是CloudFlare的一些域名被墙,导致无法直接访问 -创建并配置Workers
在 Cloudflare 的面板左侧点击 Workers 和 Pages 进入后点击创建按钮
+创建并配置Workers
创建 Workers
在 Cloudflare 的面板左侧点击 Workers 和 Pages 进入后点击创建按钮
点击创建Workers按钮
名字随便填,我这里就直接填proxy了,然后点击部署
点击编辑代码进入代码编辑页面
-将以下代码覆盖掉左侧代码框的内容
+在这里你有两个选择
++
+- 通用反代(优点:可直接反代任意网站,缺点:容易寄,如果泄露被滥用的几率大)
+- 指定反代(优点:稳定,不容易寄,缺点:需要一条条的配置需要反代的地址)
+个人推荐使用指定反代,按需选择
+指定反代
将以下代码覆盖掉左侧代码框的内容
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 addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
// 域名映射表
const domainMappings = {
"/steam-store": "https://store.steampowered.com",
"/steam-api": "https://api.steampowered.com"
};
const specialCases = {
"*": {
"Origin": "DELETE",
"Referer": "DELETE"
}
};
function handleSpecialCases(request) {
const rules = specialCases["*"];
for (const [key, value] of Object.entries(rules)) {
switch (value) {
case "KEEP":
break;
case "DELETE":
request.headers.delete(key);
break;
default:
request.headers.set(key, value);
break;
}
}
}
async function handleRequest(request) {
const url = new URL(request.url);
// 如果是访问根目录就返回一个HTML
if (url.pathname === "/") {
return new Response(`
<html>
<head>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f9f9f9;
}
h1 {
font-family: Arial, sans-serif;
color: #333;
text-align: center;
}
</style>
</head>
<body>
<h1>Why don't you play Genshin Impact?</h1>
</body>
</html>
`, {
headers: { "Content-Type": "text/html" }
});
}
const basePath = Object.keys(domainMappings).find(path => url.pathname.startsWith(path));
if (!basePath) {
return new Response("Path not found in domain mappings", { status: 404 });
}
const targetBase = domainMappings[basePath];
const targetPath = url.pathname.slice(basePath.length) + url.search + url.hash;
const targetUrl = new URL(targetPath, targetBase);
const modifiedRequest = new Request(targetUrl, {
headers: request.headers,
method: request.method,
body: request.body,
redirect: 'follow'
});
handleSpecialCases(modifiedRequest);
const response = await fetch(modifiedRequest);
const modifiedResponse = new Response(response.body, response);
modifiedResponse.headers.set('Access-Control-Allow-Origin', '*');
return modifiedResponse;
}可修改 域名映射表(domainMappings) 中的内容为自己想要的网站
@@ -223,7 +229,12 @@https://store.steampowered.com
访问 https://你的域名/steam-api 反代的就是 https://api.steampowered.com其他同理,你可以添加也可以减少
-点击部署
+通用反代
将以下代码覆盖掉左侧代码框的内容
++ +
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 addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
const specialCases = {
"*": {
"Origin": "DELETE",
"Referer": "DELETE"
}
};
function handleSpecialCases(request) {
const rules = specialCases["*"];
for (const [key, value] of Object.entries(rules)) {
switch (value) {
case "KEEP":
break;
case "DELETE":
request.headers.delete(key);
break;
default:
request.headers.set(key, value);
break;
}
}
}
async function handleRequest(request) {
const url = new URL(request.url);
// 如果是访问根目录就返回一个HTML
if (url.pathname === "/") {
return new Response(`
<html>
<head>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f9f9f9;
}
h1 {
font-family: Arial, sans-serif;
color: #333;
text-align: center;
}
</style>
</head>
<body>
<h1>Why don't you play Genshin Impact?</h1>
</body>
</html>
`, {
headers: { "Content-Type": "text/html" }
});
}
if (url.pathname.startsWith("/https://")) {
const targetUrl = url.pathname.slice(1);
// 动态URL
const modifiedRequest = new Request(targetUrl, {
headers: request.headers,
method: request.method,
body: request.body,
redirect: 'follow'
});
handleSpecialCases(modifiedRequest);
// 发送请求到目标URL
const response = await fetch(modifiedRequest);
const modifiedResponse = new Response(response.body, response);
// 允许跨域访问
modifiedResponse.headers.set('Access-Control-Allow-Origin', '*');
return modifiedResponse;
}
return new Response("Path not found", { status: 404 });
}直接在 https://域名 后面加反代的地址就行,也就是 https://域名/需要反代的网站 比如
+访问 https://你的域名/https://store.steampowered.com 反代的就是 https://store.steampowered.com
+
访问 https://你的域名/https://api.steampowered.com 反代的就是 https://api.steampowered.com部署
点击部署
在下方出现以下提示就可以啦
访问 https://你的域名 出现下面界面就成功啦
当然,有概率你是会出现网页无法访问,这就是CloudFlare的部分域名被墙了……
@@ -242,7 +253,7 @@
点击自定义域
输入任意的域名前缀+你的域名.例如proxy.example.com , 将example.com替换为你的域名
点击添加域即可然后将 Cloudflare 的域名替换为自己的即可
-本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 MapleLeaf!评论评论