forked from xxnuo/serverless-qrcode-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
734 lines (658 loc) · 20.5 KB
/
index.js
File metadata and controls
734 lines (658 loc) · 20.5 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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
let KV_BINDING;
let DB;
const banPath = [
'login', 'admin', '__total_count',
// static files
'admin.html', 'login.html',
'daisyui@5.css', 'tailwindcss@4.js',
'qr-code-styling.js', 'zxing.js',
'robots.txt', 'wechat.svg',
'favicon.svg',
];
// 数据库初始化
async function initDatabase() {
// 创建表
await DB.prepare(`
CREATE TABLE IF NOT EXISTS mappings (
path TEXT PRIMARY KEY,
target TEXT NOT NULL,
name TEXT,
expiry TEXT,
enabled INTEGER DEFAULT 1,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
)
`).run();
// 检查是否需要添加新列
const tableInfo = await DB.prepare("PRAGMA table_info(mappings)").all();
const columns = tableInfo.results.map(col => col.name);
// 添加 isWechat 列(如果不存在)
if (!columns.includes('isWechat')) {
await DB.prepare(`
ALTER TABLE mappings
ADD COLUMN isWechat INTEGER DEFAULT 0
`).run();
}
// 添加 qrCodeData 列(如果不存在)
if (!columns.includes('qrCodeData')) {
await DB.prepare(`
ALTER TABLE mappings
ADD COLUMN qrCodeData TEXT
`).run();
}
// 添加索引
await DB.prepare(`
CREATE INDEX IF NOT EXISTS idx_expiry ON mappings(expiry)
`).run();
await DB.prepare(`
CREATE INDEX IF NOT EXISTS idx_created_at ON mappings(created_at)
`).run();
// 组合索引:用于启用状态和过期时间的组合查询
await DB.prepare(`
CREATE INDEX IF NOT EXISTS idx_enabled_expiry ON mappings(enabled, expiry)
`).run();
}
// Cookie 相关函数
function verifyAuthCookie(request, env) {
const cookie = request.headers.get('Cookie') || '';
const authToken = cookie.split(';').find(c => c.trim().startsWith('token='));
if (!authToken) return false;
return authToken.split('=')[1].trim() === env.PASSWORD;
}
function setAuthCookie(password) {
return {
'Set-Cookie': `token=${password}; Path=/; HttpOnly; SameSite=Strict; Max-Age=86400`,
'Content-Type': 'application/json'
};
}
function clearAuthCookie() {
return {
'Set-Cookie': 'token=; Path=/; HttpOnly; SameSite=Strict; Max-Age=0',
'Content-Type': 'application/json'
};
}
// 数据库操作相关函数
async function listMappings(page = 1, pageSize = 10) {
const offset = (page - 1) * pageSize;
// 使用单个查询获取分页数据和总数
const results = await DB.prepare(`
WITH filtered_mappings AS (
SELECT * FROM mappings
WHERE path NOT IN (${banPath.map(() => '?').join(',')})
)
SELECT
filtered.*,
(SELECT COUNT(*) FROM filtered_mappings) as total_count
FROM filtered_mappings as filtered
ORDER BY created_at DESC
LIMIT ? OFFSET ?
`).bind(...banPath, pageSize, offset).all();
if (!results.results || results.results.length === 0) {
return {
mappings: {},
total: 0,
page,
pageSize,
totalPages: 0
};
}
const total = results.results[0].total_count;
const mappings = {};
for (const row of results.results) {
mappings[row.path] = {
target: row.target,
name: row.name,
expiry: row.expiry,
enabled: row.enabled === 1,
isWechat: row.isWechat === 1,
qrCodeData: row.qrCodeData
};
}
return {
mappings,
total,
page,
pageSize,
totalPages: Math.ceil(total / pageSize)
};
}
async function createMapping(path, target, name, expiry, enabled = true, isWechat = false, qrCodeData = null) {
if (!path || !target || typeof path !== 'string' || typeof target !== 'string') {
throw new Error('Invalid input');
}
// 检查短链名是否在禁用列表中
if (banPath.includes(path)) {
throw new Error('该短链名已被系统保留,请使用其他名称');
}
if (expiry && isNaN(Date.parse(expiry))) {
throw new Error('Invalid expiry date');
}
// 如果是微信二维码,必须提供二维码数据
if (isWechat && !qrCodeData) {
throw new Error('微信二维码必须提供原始二维码数据');
}
await DB.prepare(`
INSERT INTO mappings (path, target, name, expiry, enabled, isWechat, qrCodeData)
VALUES (?, ?, ?, ?, ?, ?, ?)
`).bind(
path,
target,
name || null,
expiry || null,
enabled ? 1 : 0,
isWechat ? 1 : 0,
qrCodeData
).run();
}
async function deleteMapping(path) {
if (!path || typeof path !== 'string') {
throw new Error('Invalid input');
}
// 检查是否在禁用列表中
if (banPath.includes(path)) {
throw new Error('系统保留的短链名无法删除');
}
await DB.prepare('DELETE FROM mappings WHERE path = ?').bind(path).run();
}
async function updateMapping(originalPath, newPath, target, name, expiry, enabled = true, isWechat = false, qrCodeData = null) {
if (!originalPath || !newPath || !target) {
throw new Error('Invalid input');
}
// 检查新短链名是否在禁用列表中
if (banPath.includes(newPath)) {
throw new Error('该短链名已被系统保留,请使用其他名称');
}
if (expiry && isNaN(Date.parse(expiry))) {
throw new Error('Invalid expiry date');
}
// 如果没有提供新的二维码数据,获取原有的二维码数据
if (!qrCodeData && isWechat) {
const existingMapping = await DB.prepare(`
SELECT qrCodeData
FROM mappings
WHERE path = ?
`).bind(originalPath).first();
if (existingMapping) {
qrCodeData = existingMapping.qrCodeData;
}
}
// 如果是微信二维码,必须有二维码数据
if (isWechat && !qrCodeData) {
throw new Error('微信二维码必须提供原始二维码数据');
}
const stmt = DB.prepare(`
UPDATE mappings
SET path = ?, target = ?, name = ?, expiry = ?, enabled = ?, isWechat = ?, qrCodeData = ?
WHERE path = ?
`);
await stmt.bind(
newPath,
target,
name || null,
expiry || null,
enabled ? 1 : 0,
isWechat ? 1 : 0,
qrCodeData,
originalPath
).run();
}
async function getExpiringMappings() {
// 获取今天的日期(设置为今天的23:59:59)
const today = new Date();
today.setHours(23, 59, 59, 999);
const now = today.toISOString();
// 获取今天的开始时间(00:00:00)
const todayStart = new Date();
todayStart.setHours(0, 0, 0, 0);
const dayStart = todayStart.toISOString();
// 修改为3天后的23:59:59
const threeDaysFromNow = new Date(todayStart);
threeDaysFromNow.setDate(todayStart.getDate() + 3);
threeDaysFromNow.setHours(23, 59, 59, 999);
const threeDaysLater = threeDaysFromNow.toISOString();
// 使用单个查询获取所有过期和即将过期的映射
const results = await DB.prepare(`
WITH categorized_mappings AS (
SELECT
path, name, target, expiry, enabled, isWechat, qrCodeData,
CASE
WHEN datetime(expiry) < datetime(?) THEN 'expired'
WHEN datetime(expiry) <= datetime(?) THEN 'expiring'
END as status
FROM mappings
WHERE expiry IS NOT NULL
AND datetime(expiry) <= datetime(?)
AND enabled = 1
)
SELECT * FROM categorized_mappings
ORDER BY expiry ASC
`).bind(dayStart, threeDaysLater, threeDaysLater).all();
const mappings = {
expiring: [],
expired: []
};
for (const row of results.results) {
const mapping = {
path: row.path,
name: row.name,
target: row.target,
expiry: row.expiry,
enabled: row.enabled === 1,
isWechat: row.isWechat === 1,
qrCodeData: row.qrCodeData
};
if (row.status === 'expired') {
mappings.expired.push(mapping);
} else {
mappings.expiring.push(mapping);
}
}
return mappings;
}
// 添加新的批量清理过期映射的函数
async function cleanupExpiredMappings(batchSize = 100) {
const now = new Date().toISOString();
while (true) {
// 获取一批过期的映射
const batch = await DB.prepare(`
SELECT path
FROM mappings
WHERE expiry IS NOT NULL
AND expiry < ?
LIMIT ?
`).bind(now, batchSize).all();
if (!batch.results || batch.results.length === 0) {
break;
}
// 批量删除这些映射
const paths = batch.results.map(row => row.path);
const placeholders = paths.map(() => '?').join(',');
await DB.prepare(`
DELETE FROM mappings
WHERE path IN (${placeholders})
`).bind(...paths).run();
// 如果获取的数量小于 batchSize,说明已经处理完所有过期映射
if (batch.results.length < batchSize) {
break;
}
}
}
// 数据迁移函数
async function migrateFromKV() {
let cursor = null;
do {
const listResult = await KV_BINDING.list({ cursor, limit: 1000 });
for (const key of listResult.keys) {
if (!banPath.includes(key.name)) {
const value = await KV_BINDING.get(key.name, { type: "json" });
if (value) {
try {
await createMapping(
key.name,
value.target,
value.name,
value.expiry,
value.enabled,
value.isWechat,
value.qrCodeData
);
} catch (e) {
console.error(`Failed to migrate ${key.name}:`, e);
}
}
}
}
cursor = listResult.cursor;
} while (cursor);
}
export default {
async fetch(request, env) {
KV_BINDING = env.KV_BINDING;
DB = env.DB;
// 初始化数据库
await initDatabase();
const url = new URL(request.url);
const path = url.pathname.slice(1);
// 根目录跳转到 管理后台
if (path === '') {
return Response.redirect(url.origin + '/admin.html', 302);
}
// API 路由处理
if (path.startsWith('api/')) {
// 登录 API
if (path === 'api/login' && request.method === 'POST') {
const { password } = await request.json();
if (password === env.PASSWORD) {
return new Response(JSON.stringify({ success: true }), {
headers: setAuthCookie(password)
});
}
return new Response('Unauthorized', { status: 401 });
}
// 登出 API
if (path === 'api/logout' && request.method === 'POST') {
return new Response(JSON.stringify({ success: true }), {
headers: clearAuthCookie()
});
}
// 需要认证的 API
if (!verifyAuthCookie(request, env)) {
return new Response('Unauthorized', { status: 401 });
}
try {
// 获取即将过期和已过期的映射
if (path === 'api/expiring-mappings') {
const result = await getExpiringMappings();
return new Response(JSON.stringify(result), {
headers: { 'Content-Type': 'application/json' }
});
}
// 获取映射列表
if (path === 'api/mappings') {
const params = new URLSearchParams(url.search);
const page = parseInt(params.get('page')) || 1;
const pageSize = parseInt(params.get('pageSize')) || 10;
const result = await listMappings(page, pageSize);
return new Response(JSON.stringify(result), {
headers: { 'Content-Type': 'application/json' }
});
}
// 映射管理 API
if (path === 'api/mapping') {
// 获取单个映射
if (request.method === 'GET') {
const params = new URLSearchParams(url.search);
const mappingPath = params.get('path');
if (!mappingPath) {
return new Response(JSON.stringify({ error: 'Missing path parameter' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
const mapping = await DB.prepare(`
SELECT path, target, name, expiry, enabled, isWechat, qrCodeData
FROM mappings
WHERE path = ?
`).bind(mappingPath).first();
if (!mapping) {
return new Response(JSON.stringify({ error: 'Mapping not found' }), {
status: 404,
headers: { 'Content-Type': 'application/json' }
});
}
return new Response(JSON.stringify(mapping), {
headers: { 'Content-Type': 'application/json' }
});
}
// 创建映射
if (request.method === 'POST') {
const data = await request.json();
await createMapping(data.path, data.target, data.name, data.expiry, data.enabled, data.isWechat, data.qrCodeData);
return new Response(JSON.stringify({ success: true }), {
headers: { 'Content-Type': 'application/json' }
});
}
// 更新映射
if (request.method === 'PUT') {
const data = await request.json();
await updateMapping(
data.originalPath,
data.path,
data.target,
data.name,
data.expiry,
data.enabled,
data.isWechat,
data.qrCodeData
);
return new Response(JSON.stringify({ success: true }), {
headers: { 'Content-Type': 'application/json' }
});
}
// 删除映射
if (request.method === 'DELETE') {
const { path } = await request.json();
await deleteMapping(path);
return new Response(JSON.stringify({ success: true }), {
headers: { 'Content-Type': 'application/json' }
});
}
}
return new Response('Not Found', { status: 404 });
} catch (error) {
console.error('API operation error:', error);
return new Response(JSON.stringify({
error: error.message || 'Internal Server Error'
}), {
status: error.message === 'Invalid input' ? 400 : 500,
headers: { 'Content-Type': 'application/json' }
});
}
}
// URL 重定向处理
if (path) {
try {
const mapping = await DB.prepare(`
SELECT path, target, name, expiry, enabled, isWechat, qrCodeData
FROM mappings
WHERE path = ?
`).bind(path).first();
if (mapping) {
// 检查是否启用
if (!mapping.enabled) {
return new Response('Not Found', { status: 404 });
}
// 检查是否过期 - 使用当天23:59:59作为失效判断时间
if (mapping.expiry) {
const today = new Date();
today.setHours(23, 59, 59, 999);
if (new Date(mapping.expiry) < today) {
const expiredHtml = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>链接已过期</title>
<style>
:root {
color-scheme: light dark;
}
body {
margin: 0;
padding: 16px;
min-height: 100vh;
display: flex;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: #f7f7f7;
box-sizing: border-box;
}
.container {
margin: auto;
padding: 24px 16px;
width: calc(100% - 32px);
max-width: 320px;
text-align: center;
background: white;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.title {
font-size: 22px;
font-weight: 600;
margin: 0 0 16px;
color: #333;
}
.message {
font-size: 16px;
color: #666;
margin: 16px 0;
line-height: 1.5;
}
.info {
font-size: 14px;
color: #999;
margin-top: 20px;
}
@media (prefers-color-scheme: dark) {
body {
background: #1a1a1a;
}
.container {
background: #2a2a2a;
box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
.title {
color: #e0e0e0;
}
.message {
color: #aaa;
}
.info {
color: #777;
}
}
</style>
</head>
<body>
<div class="container">
<h1 class="title">${mapping.name ? mapping.name + ' 已过期' : '链接已过期'}</h1>
<p class="info">过期时间:${new Date(mapping.expiry).toLocaleDateString()}</p>
<p class="info">如需访问,请联系管理员更新链接</p>
</div>
</body>
</html>`;
return new Response(expiredHtml, {
status: 404,
headers: {
'Content-Type': 'text/html;charset=UTF-8',
'Cache-Control': 'no-store'
}
});
}
}
// 如果是微信二维码,返回活码页面
if (mapping.isWechat === 1 && mapping.qrCodeData) {
const wechatHtml = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${mapping.name || '微信群二维码'}</title>
<style>
:root {
color-scheme: light dark;
}
body {
margin: 0;
padding: 16px;
min-height: 100vh;
display: flex;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: #f7f7f7;
box-sizing: border-box;
}
.container {
margin: auto;
padding: 24px 16px;
width: calc(100% - 32px);
max-width: 320px;
text-align: center;
background: white;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.wechat-icon {
width: 32px;
height: 32px;
margin-bottom: 12px;
}
.title {
font-size: 22px;
font-weight: 600;
margin: 0 0 8px;
color: #333;
}
.qr-code {
width: 100%;
max-width: 240px;
border-radius: 8px;
margin: 20px 0;
}
.notice {
font-size: 16px;
color: #666;
margin: 16px 0 0;
line-height: 1.5;
}
.footer {
font-size: 14px;
color: #999;
margin-top: 20px;
}
@media (prefers-color-scheme: dark) {
body {
background: #1a1a1a;
}
.container {
background: #2a2a2a;
box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
.title {
color: #e0e0e0;
}
.notice {
color: #aaa;
}
.footer {
color: #777;
}
.qr-code {
background: white;
padding: 8px;
}
}
</style>
</head>
<body>
<div class="container">
<img class="wechat-icon" src="wechat.svg" alt="WeChat">
<h1 class="title">${mapping.name ? mapping.name : '微信二维码'}</h1>
<p class="notice">请长按识别下方二维码</p>
<img class="qr-code" src="${mapping.qrCodeData}" alt="微信群二维码">
<p class="footer">二维码失效请联系作者更新</p>
</div>
</body>
</html>`;
return new Response(wechatHtml, {
headers: {
'Content-Type': 'text/html;charset=UTF-8',
'Cache-Control': 'no-store'
}
});
}
// 如果不是微信二维码,执行普通重定向
return Response.redirect(mapping.target, 302);
}
return new Response('Not Found', { status: 404 });
} catch (error) {
console.error('Redirect error:', error);
return new Response('Internal Server Error', { status: 500 });
}
}
},
async scheduled(controller, env, ctx) {
KV_BINDING = env.KV_BINDING;
DB = env.DB;
// 初始化数据库
await initDatabase();
// 获取过期和即将过期的映射报告
const result = await getExpiringMappings();
console.log(`Cron job report: Found ${result.expired.length} expired mappings`);
if (result.expired.length > 0) {
console.log('Expired mappings:', JSON.stringify(result.expired, null, 2));
}
console.log(`Found ${result.expiring.length} mappings expiring in 2 days`);
if (result.expiring.length > 0) {
console.log('Expiring soon mappings:', JSON.stringify(result.expiring, null, 2));
}
},
};