-
Notifications
You must be signed in to change notification settings - Fork 23
/
_worker.js
623 lines (557 loc) · 17.9 KB
/
_worker.js
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch((error) => {
return createJsonResponse(500, { success: false, error: error.message });
})
);
});
function setCorsHeaders(response) {
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS"
);
response.headers.set(
"Access-Control-Allow-Headers",
"Content-Type, X-Admin-Password"
);
}
function verifyAdminPassword(request) {
const adminPassword = request.headers.get("X-Admin-Password");
if (adminPassword !== ADMIN_PASSWORD) {
return createJsonResponse(401, { success: false, message: "Unauthorized" });
}
return null;
}
function isValidId(id) {
return /^[a-zA-Z0-9]+$/.test(id);
}
// 定义路由表
const routes = {
[`POST:/${PATH_SECRET}/send-cookies`]: handleSendCookies,
[`GET:/${PATH_SECRET}/admin`]: handleAdminPage,
[`GET:/${PATH_SECRET}/admin/list-cookies`]: handleListCookies,
[`GET:/${PATH_SECRET}/admin/list-cookies-by-host`]: handleListCookiesByHost,
[`DELETE:/${PATH_SECRET}/admin/delete`]: handleDelete,
[`PUT:/${PATH_SECRET}/admin/update`]: handleUpdate,
[`OPTIONS:/${PATH_SECRET}/`]: handleCorsPreflightRequest,
};
async function handleAdminPage(request) {
const htmlContent = `
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cookie 管理器</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: url('https://picsum.photos/1920/1080?blur=5') no-repeat center center fixed;
background-size: cover;
margin: 10px;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
background: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(10px);
border-radius: 12px;
padding: 2rem;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
width: 90%;
max-width: 800px;
}
h1, h2 {
color: #0078D4;
}
input, textarea, button {
width: 100%;
padding: 0.5rem;
margin-bottom: 1rem;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #0078D4;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #005a9e;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 0.5rem;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<div class="container">
<h1>Cookie 管理器</h1>
<!-- 添加管理员密码输入和保存按钮 -->
<div>
<h2>管理员密码</h2>
<input type="password" id="adminPassword" placeholder="输入管理员密码">
<button id="savePassword">保存密码</button>
</div>
<!-- 创建 Cookie -->
<div id="cookieManagement" style="display: none;">
<h2>创建 Cookie</h2>
<form id="createForm">
<input type="text" id="createId" placeholder="ID" required>
<input type="url" id="createUrl" placeholder="URL" required>
<textarea id="createCookies" placeholder="Cookies (JSON 格式)" rows="3" required></textarea>
<button type="submit">创建</button>
</form>
</div>
<!-- 列出 Cookies -->
<div id="cookieList" style="display: none;">
<h2>已存储的 Cookies</h2>
<button id="refreshList">刷新列表</button>
<table>
<thead>
<tr>
<th>ID</th>
<th>URL</th>
<th>操作</th>
</tr>
</thead>
<tbody id="cookieList">
<!-- 动态内容 -->
</tbody>
</table>
</div>
<!-- 更新 Cookie -->
<div id="updateCookie" style="display: none;">
<h2>更新 Cookie</h2>
<form id="updateForm">
<input type="text" id="updateId" placeholder="ID" required>
<input type="text" id="updateUrl" placeholder="URL">
<textarea id="updateCookies" placeholder="Cookies (JSON 格式)" rows="3" required></textarea>
<button type="submit">更新</button>
</form>
</div>
<!-- 删除 Cookie -->
<div id="deleteCookie" style="display: none;">
<h2>删除 Cookie</h2>
<form id="deleteForm">
<input type="text" id="deleteId" placeholder="ID" required>
<button type="submit">删除</button>
</form>
</div>
</div>
<script>
const API_BASE = '/${PATH_SECRET}';
let adminPassword = '';
document.addEventListener('DOMContentLoaded', () => {
// 从本地存储中获取保存的密码
adminPassword = localStorage.getItem('adminPassword') || '';
if (adminPassword) {
document.getElementById('adminPassword').value = adminPassword;
showCookieManagement();
}
document.getElementById('savePassword').addEventListener('click', saveAdminPassword);
document.getElementById('createForm').addEventListener('submit', createCookie);
document.getElementById('updateForm').addEventListener('submit', updateCookie);
document.getElementById('deleteForm').addEventListener('submit', deleteCookie);
document.getElementById('refreshList').addEventListener('click', loadCookies);
});
function saveAdminPassword() {
const password = document.getElementById('adminPassword').value;
if (password) {
localStorage.setItem('adminPassword', password);
adminPassword = password;
showCookieManagement();
loadCookies();
alert('管理员密码已保存');
} else {
alert('请输入有效的管理员密码');
}
}
function showCookieManagement() {
document.getElementById('cookieManagement').style.display = 'block';
document.getElementById('cookieList').style.display = 'block';
document.getElementById('updateCookie').style.display = 'block';
document.getElementById('deleteCookie').style.display = 'block';
loadCookies();
}
async function loadCookies() {
const response = await fetch(API_BASE + '/admin/list-cookies', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Admin-Password': adminPassword
}
});
if (response.ok) {
const data = await response.json();
const list = data.cookies;
const tbody = document.getElementById('cookieList');
tbody.innerHTML = '';
list.forEach(cookie => {
const tr = document.createElement('tr');
tr.innerHTML = \`
<td>\${cookie.id}</td>
<td>\${new URL(cookie.url).hostname}</td>
<td>
<button onclick="deleteCookieById('\${cookie.id}')">删除</button>
</td>
\`;
tbody.appendChild(tr);
});
} else {
alert('无法加载 Cookies 列表,请检查管理员密码是否正确');
}
}
async function createCookie(event) {
event.preventDefault();
const id = document.getElementById('createId').value;
const url = document.getElementById('createUrl').value;
let cookies;
try {
cookies = JSON.parse(document.getElementById('createCookies').value);
} catch {
alert('Cookies 必须是有效的 JSON');
return;
}
const response = await fetch(API_BASE + '/send-cookies', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Admin-Password': adminPassword
},
body: JSON.stringify({ id, url, cookies })
});
const result = await response.json();
alert(result.message);
if (response.ok) {
loadCookies();
document.getElementById('createForm').reset();
}
}
async function updateCookie(event) {
event.preventDefault();
const key = document.getElementById('updateId').value;
const url = document.getElementById('updateUrl').value;
let value;
try {
value = JSON.parse(document.getElementById('updateCookies').value);
} catch {
alert('Cookies 必须是有效的 JSON');
return;
}
const response = await fetch(API_BASE + '/admin/update', {
method: 'PUT',
headers: { 'Content-Type': 'application/json', 'X-Admin-Password': adminPassword },
body: JSON.stringify({ key, value, url })
});
const result = await response.json();
alert(result.message);
if (response.ok) {
loadCookies();
document.getElementById('updateForm').reset();
}
}
async function deleteCookie(event) {
event.preventDefault();
const key = document.getElementById('deleteId').value;
const response = await fetch(API_BASE + '/admin/delete?key='+encodeURIComponent(key), {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'X-Admin-Password': adminPassword
}
});
const result = await response.json();
alert(result.message);
if (response.ok) {
loadCookies();
document.getElementById('deleteForm').reset();
}
}
async function deleteCookieById(id) {
if (!confirm('确定要删除 ID 为'+id+' 的 Cookie 吗?')) return;
const response = await fetch(API_BASE + '/admin/delete?key='+encodeURIComponent(id), {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'X-Admin-Password': adminPassword
}
});
const result = await response.json();
alert(result.message);
if (response.ok) {
loadCookies();
}
}
</script>
</body>
</html>
`;
const response = new Response(htmlContent, {
status: 200,
headers: { "Content-Type": "text/html" },
});
setCorsHeaders(response);
return response;
}
async function handleRequest(request) {
const url = new URL(request.url);
const path = url.pathname;
const method = request.method;
// 检查路径是否包含正确的 PATH_SECRET
if (!path.startsWith(`/${PATH_SECRET}/`)) {
return createJsonResponse(404, { success: false, message: "Not Found" });
}
// 处理 OPTIONS 请求
if (method === "OPTIONS") {
return handleCorsPreflightRequest();
}
// 对所有 /admin 开头的路径进行密码校验
if (path.includes("/admin/")) {
const authResponse = verifyAdminPassword(request);
if (authResponse) return authResponse;
}
// 处理动态路由
if (path.includes("/receive-cookies/")) {
return handleReceiveCookies(request, path);
}
if (path.includes("/admin/list-cookies-by-host/")) {
return handleListCookiesByHost(request, path);
}
const routeKey = `${method}:${path}`;
const handler = routes[routeKey];
if (handler) {
return handler(request);
}
return createJsonResponse(404, { success: false, message: "Not Found" });
}
async function handleListCookiesByHost(request, path) {
const host = decodeURIComponent(path.split("/").pop());
const list = await COOKIE_STORE.list();
const cookies = await Promise.all(
list.keys.map(async (key) => {
const data = await COOKIE_STORE.get(key.name);
if (data) {
try {
const { id, url } = JSON.parse(data);
if (new URL(url).hostname === host) {
return { id, url };
}
} catch (e) {
console.error(`Error parsing data for key ${key.name}:`, e);
}
}
return null;
})
);
const filteredCookies = cookies.filter((cookie) => cookie !== null);
return createJsonResponse(200, { success: true, cookies: filteredCookies });
}
function handleCorsPreflightRequest() {
const response = new Response(null, { status: 204 });
setCorsHeaders(response);
return response;
}
async function handleSendCookies(request) {
const { id, url, cookies } = await request.json();
if (!isValidId(id)) {
return createJsonResponse(400, {
success: false,
message: "Invalid ID. Only letters and numbers are allowed.",
});
}
// 验证 cookies 格式
if (!Array.isArray(cookies) || !cookies.every(cookie =>
cookie.name &&
cookie.value &&
cookie.domain &&
typeof cookie.httpOnly === 'boolean' &&
typeof cookie.secure === 'boolean' &&
cookie.sameSite
)) {
return createJsonResponse(400, {
success: false,
message: "Invalid cookie format",
});
}
// 处理 URL 格式:如果不是完整的 URL,则添加 https:// 前缀
const processedUrl = url.includes('://') ? url : `https://${url}`;
// 存储数据,确保域名不带点前缀
await COOKIE_STORE.put(id, JSON.stringify({
id,
url: processedUrl,
cookies: cookies.map(cookie => {
// 如果域名以点开头,去掉点
const domain = cookie.domain.startsWith('.') ? cookie.domain.slice(1) : cookie.domain;
return {
domain: domain,
expirationDate: cookie.expirationDate,
hostOnly: true,
httpOnly: cookie.httpOnly,
name: cookie.name,
path: cookie.path || "/",
sameSite: cookie.sameSite.toLowerCase(),
secure: cookie.secure,
session: cookie.session || false,
storeId: null,
value: cookie.value
};
})
}));
return createJsonResponse(200, {
success: true,
message: "Cookies saved successfully",
});
}
async function handleReceiveCookies(request, path) {
const cookieId = path.split("/").pop();
if (!isValidId(cookieId)) {
return createJsonResponse(400, {
success: false,
message: "Invalid cookie ID",
});
}
const data = await COOKIE_STORE.get(cookieId);
if (!data) {
return createJsonResponse(404, {
success: false,
message: "Cookies not found",
});
}
const { cookies } = JSON.parse(data);
return createJsonResponse(200, {
success: true,
cookies: cookies.map(cookie => ({
domain: cookie.domain,
expirationDate: cookie.expirationDate,
hostOnly: cookie.hostOnly,
httpOnly: cookie.httpOnly,
name: cookie.name,
path: cookie.path || "/",
sameSite: cookie.sameSite,
secure: cookie.secure,
session: false,
storeId: null,
value: cookie.value
}))
});
}
async function handleDelete(request) {
const url = new URL(request.url);
const key = url.searchParams.get("key");
if (!isValidId(key)) {
return createJsonResponse(400, {
success: false,
message: "Invalid key. Only letters and numbers are allowed.",
});
}
await COOKIE_STORE.delete(key);
return createJsonResponse(200, {
success: true,
message: "Data deleted successfully",
});
}
async function handleListCookies(request) {
const list = await COOKIE_STORE.list();
const cookies = await Promise.all(
list.keys.map(async (key) => {
const data = await COOKIE_STORE.get(key.name);
if (data) {
try {
const { id, url } = JSON.parse(data);
return { id, url };
} catch (e) {
console.error(`Error parsing data for key ${key.name}:`, e);
return null;
}
}
return null;
})
);
const filteredCookies = cookies.filter((cookie) => cookie !== null);
return createJsonResponse(200, { success: true, cookies: filteredCookies });
}
async function handleUpdate(request) {
const { key, value, url } = await request.json();
if (!isValidId(key)) {
return createJsonResponse(400, {
success: false,
message: "Invalid key. Only letters and numbers are allowed.",
});
}
// 验证 cookies 格式
if (!Array.isArray(value) || !value.every(cookie =>
cookie.name &&
cookie.value &&
cookie.domain &&
typeof cookie.httpOnly === 'boolean' &&
typeof cookie.secure === 'boolean' &&
cookie.sameSite
)) {
return createJsonResponse(400, {
success: false,
message: "Invalid cookie format",
});
}
// 检查记录是否存在
const existingData = await COOKIE_STORE.get(key);
if (!existingData) {
return createJsonResponse(404, {
success: false,
message: "Cookie not found",
});
}
// 获取现有数据
const data = JSON.parse(existingData);
// 如果提供了新的 URL,则更新
if (url) {
// 处理 URL 格式:如果不是完整的 URL,则添加 https:// 前缀
data.url = url.includes('://') ? url : `https://${url}`;
}
// 更新 cookies
data.cookies = value.map(cookie => {
const domain = cookie.domain.startsWith('.') ? cookie.domain.slice(1) : cookie.domain;
return {
domain: domain,
expirationDate: cookie.expirationDate,
hostOnly: true,
httpOnly: cookie.httpOnly,
name: cookie.name,
path: cookie.path || "/",
sameSite: cookie.sameSite.toLowerCase(),
secure: cookie.secure,
session: cookie.session || false,
storeId: null,
value: cookie.value
};
});
// 保存更新后的数据
await COOKIE_STORE.put(key, JSON.stringify(data));
return createJsonResponse(200, {
success: true,
message: "Cookies and URL updated successfully",
});
}
function createJsonResponse(status, body) {
const response = new Response(JSON.stringify(body), {
status: status,
headers: { "Content-Type": "application/json" },
});
setCorsHeaders(response);
return response;
}