-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcf-proxy.js
More file actions
341 lines (292 loc) · 9.33 KB
/
cf-proxy.js
File metadata and controls
341 lines (292 loc) · 9.33 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const url = new URL(request.url);
// 如果访问根目录,返回HTML
if (url.pathname === "/") {
return new Response(getRootHtml(), {
headers: {
"Content-Type": "text/html; charset=utf-8",
},
});
}
// 目标根地址,自行替换
const targetBaseUrl = "http://xxx.com";
// 从请求路径中提取目标 URL
let actualUrlStr = decodeURIComponent(url.pathname.replace("/", ""));
// 判断用户输入的 URL 是否带有协议
actualUrlStr = ensureProtocol(targetBaseUrl + actualUrlStr, url.protocol);
// 保留查询参数
actualUrlStr += url.search;
// 创建新 Headers 对象,排除以 'cf-' 开头的请求头
const newHeaders = filterHeaders(
request.headers,
(name) => !name.startsWith("cf-")
);
// 创建一个新的请求以访问目标 URL
const modifiedRequest = new Request(actualUrlStr, {
headers: newHeaders,
method: request.method,
body: request.body,
redirect: "manual",
});
// 发起对目标 URL 的请求
const response = await fetch(modifiedRequest);
let body = response.body;
// 处理重定向
if ([301, 302, 303, 307, 308].includes(response.status)) {
body = response.body;
// 创建新的 Response 对象以修改 Location 头部
return handleRedirect(response, body);
} else if (response.headers.get("Content-Type")?.includes("text/html")) {
body = await handleHtmlContent(
response,
url.protocol,
url.host,
actualUrlStr
);
}
// 创建修改后的响应对象
const modifiedResponse = new Response(body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
// 添加禁用缓存的头部
setNoCacheHeaders(modifiedResponse.headers);
// 添加 CORS 头部,允许跨域访问
setCorsHeaders(modifiedResponse.headers);
return modifiedResponse;
} catch (error) {
// 如果请求目标地址时出现错误,返回带有错误消息的响应和状态码 500(服务器错误)
return jsonResponse(
{
error: error.message,
},
500
);
}
}
// 确保 URL 带有协议
function ensureProtocol(url, defaultProtocol) {
return url.startsWith("http://") || url.startsWith("https://")
? url
: defaultProtocol + "//" + url;
}
// 处理重定向
function handleRedirect(response, body) {
const location = new URL(response.headers.get("location"));
const modifiedLocation = `/${encodeURIComponent(location.toString())}`;
return new Response(body, {
status: response.status,
statusText: response.statusText,
headers: {
...response.headers,
Location: modifiedLocation,
},
});
}
// 处理 HTML 内容中的相对路径
async function handleHtmlContent(response, protocol, host, actualUrlStr) {
const originalText = await response.text();
const regex = new RegExp("((href|src|action)=[\"'])/(?!/)", "g");
let modifiedText = replaceRelativePaths(
originalText,
protocol,
host,
new URL(actualUrlStr).origin
);
return modifiedText;
}
// 替换 HTML 内容中的相对路径
function replaceRelativePaths(text, protocol, host, origin) {
const regex = new RegExp("((href|src|action)=[\"'])/(?!/)", "g");
return text.replace(regex, `$1${protocol}//${host}/${origin}/`);
}
// 返回 JSON 格式的响应
function jsonResponse(data, status) {
return new Response(JSON.stringify(data), {
status: status,
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
}
// 过滤请求头
function filterHeaders(headers, filterFunc) {
return new Headers([...headers].filter(([name]) => filterFunc(name)));
}
// 设置禁用缓存的头部
function setNoCacheHeaders(headers) {
headers.set("Cache-Control", "no-store");
}
// 设置 CORS 头部
function setCorsHeaders(headers) {
headers.set("Access-Control-Allow-Origin", "*");
headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
headers.set("Access-Control-Allow-Headers", "*");
}
// 返回根目录的 HTML
function getRootHtml() {
return `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>页面未找到 - 404错误</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
color: #333;
line-height: 1.6;
}
.error-container {
background: rgba(255, 255, 255, 0.95);
padding: 3rem;
border-radius: 20px;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 500px;
width: 90%;
animation: fadeIn 0.8s ease-out;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(30px); }
to { opacity: 1; transform: translateY(0); }
}
.error-code {
font-size: 8rem;
font-weight: bold;
color: #667eea;
line-height: 1;
margin-bottom: 1rem;
text-shadow: 3px 3px 0 rgba(0, 0, 0, 0.1);
}
.error-title {
font-size: 2rem;
margin-bottom: 1rem;
color: #2d3748;
}
.error-message {
color: #4a5568;
margin-bottom: 2rem;
font-size: 1.1rem;
}
.action-buttons {
display: flex;
gap: 1rem;
justify-content: center;
flex-wrap: wrap;
margin-top: 2rem;
}
.btn {
padding: 0.75rem 1.5rem;
border: none;
border-radius: 50px;
font-size: 1rem;
cursor: pointer;
transition: all 0.3s ease;
text-decoration: none;
display: inline-block;
font-weight: 600;
}
.btn-primary {
background: #667eea;
color: white;
}
.btn-primary:hover {
background: #5a6fd8;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
.btn-secondary {
background: #e2e8f0;
color: #4a5568;
}
.btn-secondary:hover {
background: #cbd5e0;
transform: translateY(-2px);
}
.countdown {
margin-top: 1.5rem;
padding: 1rem;
background: #f7fafc;
border-radius: 10px;
font-size: 0.9rem;
color: #718096;
}
.countdown-number {
font-weight: bold;
color: #667eea;
}
@media (max-width: 480px) {
.error-container {
padding: 2rem 1.5rem;
}
.error-code {
font-size: 6rem;
}
.error-title {
font-size: 1.5rem;
}
.action-buttons {
flex-direction: column;
align-items: center;
}
.btn {
width: 100%;
max-width: 250px;
}
}
</style>
</head>
<body>
<div class="error-container">
<div class="error-code">404</div>
<h1 class="error-title">页面迷路了</h1>
<p class="error-message">
抱歉,您要访问的页面不存在。可能已被移动、删除,或者您输入了错误的网址。
</p>
<div class="action-buttons">
<a href="https://store.steampowered.com/" class="btn btn-primary">返回首页</a>
<a href="javascript:history.back()" class="btn btn-secondary">返回上一页</a>
</div>
<div class="countdown">
<span id="countdown-text"><span class="countdown-number">10</span> 秒后自动跳转到首页</span>
</div>
</div>
<script>
// 倒计时功能
let seconds = 10;
const countdownElement = document.getElementById('countdown-text');
const countdownNumber = countdownElement.querySelector('.countdown-number');
const countdown = setInterval(() => {
seconds--;
countdownNumber.textContent = seconds;
if (seconds <= 0) {
clearInterval(countdown);
window.location.href = 'https://store.steampowered.com/';
}
}, 1000);
// 如果用户与页面交互,停止自动跳转
document.addEventListener('click', () => {
clearInterval(countdown);
countdownElement.innerHTML = '自动跳转已取消';
});
</script>
</body>
</html>`;
}