-
Notifications
You must be signed in to change notification settings - Fork 14
/
domainkeeper.js
1070 lines (949 loc) · 30.7 KB
/
domainkeeper.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
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 在文件顶部添加版本信息后台密码(不可为空)
const VERSION = "1.6.8";
// 自定义标题
const CUSTOM_TITLE = "我的域名管理";
// 在这里设置你的 Cloudflare API Token
const CF_API_KEY = "";
// 自建 WHOIS 代理服务地址
const WHOIS_PROXY_URL = "";
// 访问密码(可为空)
const ACCESS_PASSWORD = "";
// 后台密码(不可为空)
const ADMIN_PASSWORD = "";
// KV 命名空间绑定名称
const KV_NAMESPACE = DOMAIN_INFO;
// footerHTML
const footerHTML = `
<footer style="
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: #f8f9fa;
color: #6c757d;
text-align: center;
padding: 10px 0;
font-size: 14px;
">
Powered by DomainKeeper v${VERSION} <span style="margin: 0 10px;">|</span> © 2023 bacon159. All rights reserved.
</footer>
`;
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
async function handleRequest(request) {
// 清理KV中的错误内容
await cleanupKV();
const url = new URL(request.url);
const path = url.pathname;
if (path === "/api/manual-query") {
return handleManualQuery(request);
}
if (path === "/") {
return handleFrontend(request);
} else if (path === "/admin") {
return handleAdmin(request);
} else if (path === "/api/update") {
return handleApiUpdate(request);
} else if (path === "/login") {
return handleLogin(request);
} else if (path === "/admin-login") {
return handleAdminLogin(request);
} else if (path.startsWith("/whois/")) {
const domain = path.split("/")[2];
return handleWhoisRequest(domain);
} else {
return new Response("Not Found", { status: 404 });
}
}
async function handleManualQuery(request) {
if (request.method !== "POST") {
return new Response("Method Not Allowed", { status: 405 });
}
const data = await request.json();
const { domain, apiKey } = data;
try {
const whoisInfo = await fetchWhoisInfo(domain, apiKey);
await cacheWhoisInfo(domain, whoisInfo);
return new Response(JSON.stringify(whoisInfo), {
headers: { 'Content-Type': 'application/json' }
});
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}
async function cleanupKV() {
const list = await KV_NAMESPACE.list();
for (const key of list.keys) {
const value = await KV_NAMESPACE.get(key.name);
if (value) {
const { data } = JSON.parse(value);
if (data.whoisError) {
await KV_NAMESPACE.delete(key.name);
}
}
}
}
const adminScript = `
<script>
async function editDomain(domain, button) {
const row = button.closest('tr');
const cells = row.querySelectorAll('.editable');
if (button.textContent === '编辑') {
button.textContent = '保存';
cells.forEach(cell => {
const input = document.createElement('input');
input.value = cell.textContent;
cell.textContent = '';
cell.appendChild(input);
});
} else {
button.textContent = '编辑';
const updatedData = {
domain: domain,
registrar: cells[0].querySelector('input').value,
registrationDate: cells[1].querySelector('input').value,
expirationDate: cells[2].querySelector('input').value
};
try {
const response = await fetch('/api/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(':${ADMIN_PASSWORD}')
},
body: JSON.stringify(updatedData)
});
if (response.ok) {
cells.forEach(cell => {
cell.textContent = cell.querySelector('input').value;
});
alert('更新成功');
} else {
throw new Error('更新失败');
}
} catch (error) {
alert('更新失败: ' + error.message);
location.reload();
}
}
}
</script>
`;
async function handleFrontend(request) {
const cookie = request.headers.get("Cookie");
if (ACCESS_PASSWORD && (!cookie || !cookie.includes(`access_token=${ACCESS_PASSWORD}`))) {
return Response.redirect(`${new URL(request.url).origin}/login`, 302);
}
console.log("Fetching Cloudflare domains info...");
const domains = await fetchCloudflareDomainsInfo();
console.log("Cloudflare domains:", domains);
console.log("Fetching domain info...");
const domainsWithInfo = await fetchDomainInfo(domains);
console.log("Domains with info:", domainsWithInfo);
return new Response(generateHTML(domainsWithInfo, false), {
headers: { 'Content-Type': 'text/html' },
});
}
async function handleAdmin(request) {
const cookie = request.headers.get("Cookie");
if (!cookie || !cookie.includes(`admin_token=${ADMIN_PASSWORD}`)) {
return Response.redirect(`${new URL(request.url).origin}/admin-login`, 302);
}
const domains = await fetchCloudflareDomainsInfo();
const domainsWithInfo = await fetchDomainInfo(domains);
return new Response(generateHTML(domainsWithInfo, true), {
headers: { 'Content-Type': 'text/html' },
});
}
async function handleLogin(request) {
if (request.method === "POST") {
const formData = await request.formData();
const password = formData.get("password");
console.log("Entered password:", password);
console.log("Expected password:", ACCESS_PASSWORD);
if (password === ACCESS_PASSWORD) {
return new Response("Login successful", {
status: 302,
headers: {
"Location": "/",
"Set-Cookie": `access_token=${ACCESS_PASSWORD}; HttpOnly; Path=/; SameSite=Strict`
}
});
} else {
return new Response(generateLoginHTML("前台登录", "/login", "密码错误,请重试。"), {
headers: { "Content-Type": "text/html" },
status: 401
});
}
}
return new Response(generateLoginHTML("前台登录", "/login"), {
headers: { "Content-Type": "text/html" }
});
}
async function handleAdminLogin(request) {
console.log("Handling admin login request");
console.log("Request method:", request.method);
if (request.method === "POST") {
console.log("Processing POST request for admin login");
const formData = await request.formData();
console.log("Form data:", formData);
const password = formData.get("password");
console.log("Entered admin password:", password);
console.log("Expected admin password:", ADMIN_PASSWORD);
if (password === ADMIN_PASSWORD) {
return new Response("Admin login successful", {
status: 302,
headers: {
"Location": "/admin",
"Set-Cookie": `admin_token=${ADMIN_PASSWORD}; HttpOnly; Path=/; SameSite=Strict`
}
});
} else {
return new Response(generateLoginHTML("后台登录", "/admin-login", "密码错误,请重试。"), {
headers: { "Content-Type": "text/html" },
status: 401
});
}
}
return new Response(generateLoginHTML("后台登录", "/admin-login"), {
headers: { "Content-Type": "text/html" }
});
}
async function handleApiUpdate(request) {
if (request.method !== "POST") {
return new Response("Method Not Allowed", { status: 405 });
}
const auth = request.headers.get("Authorization");
if (!auth || auth !== `Basic ${btoa(`:${ADMIN_PASSWORD}`)}`) {
return new Response("Unauthorized", { status: 401 });
}
try {
const data = await request.json();
const { action, domain, system, registrar, registrationDate, expirationDate } = data;
if (action === 'delete') {
// 删除自定义域名
await KV_NAMESPACE.delete(`whois_${domain}`);
} else if (action === 'update-whois') {
// 更新 WHOIS 信息
const whoisInfo = await fetchWhoisInfo(domain);
await cacheWhoisInfo(domain, whoisInfo);
} else if (action === 'add') {
// 添加新域名
const newDomainInfo = {
domain,
system,
registrar,
registrationDate,
expirationDate,
isCustom: true
};
await cacheWhoisInfo(domain, newDomainInfo);
} else {
// 更新域名信息
let domainInfo = await getCachedWhoisInfo(domain) || {};
domainInfo = {
...domainInfo,
registrar,
registrationDate,
expirationDate
};
await cacheWhoisInfo(domain, domainInfo);
}
return new Response(JSON.stringify({ success: true }), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
} catch (error) {
console.error('Error in handleApiUpdate:', error);
return new Response(JSON.stringify({ success: false, error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}
async function fetchCloudflareDomainsInfo() {
const response = await fetch('https://api.cloudflare.com/client/v4/zones', {
headers: {
'Authorization': `Bearer ${CF_API_KEY}`,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error('Failed to fetch domains from Cloudflare');
}
const data = await response.json();
if (!data.success) {
throw new Error('Cloudflare API request failed');
}
return data.result.map(zone => ({
domain: zone.name,
registrationDate: new Date(zone.created_on).toISOString().split('T')[0],
system: 'Cloudflare',
}));
}
async function fetchDomainInfo(domains) {
const result = [];
// 获取所有域名信息,包括自定义域名
const allDomainKeys = await KV_NAMESPACE.list({ prefix: 'whois_' });
const allDomains = await Promise.all(allDomainKeys.keys.map(async (key) => {
const value = await KV_NAMESPACE.get(key.name);
if (value) {
try {
const parsedValue = JSON.parse(value);
return parsedValue.data;
} catch (error) {
console.error(`Error parsing data for ${key.name}:`, error);
return null;
}
}
return null;
}));
// 过滤掉无效的域名数据
const validAllDomains = allDomains.filter(d => d && d.isCustom);
// 合并 Cloudflare 域名和自定义域名
const mergedDomains = [...domains, ...validAllDomains];
for (const domain of mergedDomains) {
if (!domain) continue; // 跳过无效的域名数据
let domainInfo = { ...domain };
const cachedInfo = await getCachedWhoisInfo(domain.domain || domain);
if (cachedInfo) {
domainInfo = { ...domainInfo, ...cachedInfo };
} else if (!domainInfo.isCustom && domainInfo.domain && domainInfo.domain.split('.').length === 2 && WHOIS_PROXY_URL) {
try {
const whoisInfo = await fetchWhoisInfo(domainInfo.domain);
domainInfo = { ...domainInfo, ...whoisInfo };
if (!whoisInfo.whoisError) {
await cacheWhoisInfo(domainInfo.domain, whoisInfo);
}
} catch (error) {
console.error(`Error fetching WHOIS info for ${domainInfo.domain}:`, error);
domainInfo.whoisError = error.message;
}
}
result.push(domainInfo);
}
return result;
}
async function handleWhoisRequest(domain) {
console.log(`Handling WHOIS request for domain: ${domain}`);
try {
console.log(`Fetching WHOIS data from: ${WHOIS_PROXY_URL}/whois/${domain}`);
const response = await fetch(`${WHOIS_PROXY_URL}/whois/${domain}`);
if (!response.ok) {
throw new Error(`WHOIS API responded with status: ${response.status}`);
}
const whoisData = await response.json();
console.log(`Received WHOIS data:`, whoisData);
return new Response(JSON.stringify({
error: false,
rawData: whoisData.rawData
}), {
headers: { 'Content-Type': 'application/json' }
});
} catch (error) {
console.error(`Error fetching WHOIS data for ${domain}:`, error);
return new Response(JSON.stringify({
error: true,
message: `Failed to fetch WHOIS data for ${domain}. Error: ${error.message}`
}), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
}
}
async function fetchWhoisInfo(domain) {
try {
const response = await fetch(`${WHOIS_PROXY_URL}/whois/${domain}`);
const whoisData = await response.json();
console.log('Raw WHOIS proxy response:', JSON.stringify(whoisData, null, 2));
if (whoisData) {
return {
registrar: whoisData.registrar || 'Unknown',
registrationDate: formatDate(whoisData.creationDate) || 'Unknown',
expirationDate: formatDate(whoisData.expirationDate) || 'Unknown'
};
} else {
console.warn(`Incomplete WHOIS data for ${domain}`);
return {
registrar: 'Unknown',
registrationDate: 'Unknown',
expirationDate: 'Unknown',
whoisError: 'Incomplete WHOIS data'
};
}
} catch (error) {
console.error('Error fetching WHOIS info:', error);
return {
registrar: 'Unknown',
registrationDate: 'Unknown',
expirationDate: 'Unknown',
whoisError: error.message
};
}
}
function formatDate(dateString) {
if (!dateString) return null;
const date = new Date(dateString);
return isNaN(date.getTime()) ? dateString : date.toISOString().split('T')[0];
}
async function getCachedWhoisInfo(domain) {
const cacheKey = `whois_${domain}`;
const cachedData = await KV_NAMESPACE.get(cacheKey);
if (cachedData) {
const { data, timestamp } = JSON.parse(cachedData);
// 检查是否有错误内容,如果有,删除它
if (data.whoisError) {
await KV_NAMESPACE.delete(cacheKey);
return null;
}
// 这里可以添加缓存过期检查,如果需要的话
return data;
}
return null;
}
async function cacheWhoisInfo(domain, whoisInfo) {
const cacheKey = `whois_${domain}`;
await KV_NAMESPACE.put(cacheKey, JSON.stringify({
data: whoisInfo,
timestamp: Date.now()
}));
}
function generateLoginHTML(title, action, errorMessage = "") {
return `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title} - ${CUSTOM_TITLE}</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
.container {
max-width: 1600px;
width: 100%;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin-bottom: 60px;
}
.table-wrapper {
overflow-x: auto;
width: 100%;
}
table {
width: 100%;
table-layout: auto;
}
thead {
position: sticky;
top: 0;
background-color: #f2f2f2;
z-index: 1;
}
th, td {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 8px;
}
.status-column { width: 30px; min-width: 30px; max-width: 50px; }
.domain-column { max-width: 200px; }
.system-column, .registrar-column { max-width: 150px; }
.date-column { max-width: 100px; }
.days-column { max-width: 80px; }
.progress-column { max-width: 150px; }
.operation-column { max-width: 200px; }
@media (max-width: 768px) {
.container {
padding: 0 10px;
}
table {
table-layout: auto;
font-size: 12px;
}
th, td {
padding: 6px;
}
.system-column, .registrar-column {
display: none;
}
.domain-column,
.date-column,
.days-column,
.progress-column,
.operation-column {
width: auto;
}
button {
padding: 3px 6px;
font-size: 12px;
}
}
@media (min-width: 1921px) {
.container {
max-width: 1800px;
}
}
.status-dot {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
}
.progress-bar {
width: 100%;
background-color: #e0e0e0;
border-radius: 5px;
overflow: hidden;
}
.progress {
height: 20px;
background-color: #4CAF50;
transition: width 0.5s ease-in-out;
}
button {
padding: 5px 10px;
margin: 2px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="login-container">
<h1>${title}</h1>
${errorMessage ? `<p class="error-message">${errorMessage}</p>` : ''}
<form method="POST" action="${action}">
<input type="password" name="password" placeholder="请输入密码" required>
<input type="submit" value="登录">
</form>
</div>
${footerHTML}
</body>
</html>
`;
}
function getStatusColor(daysRemaining) {
if (daysRemaining === 'N/A' || daysRemaining <= 0) return '#e74c3c'; // 红色
if (daysRemaining <= 30) return '#f1c40f'; // 黄色
return '#2ecc71'; // 绿色
}
function getStatusTitle(daysRemaining) {
if (daysRemaining === 'N/A') return '无效的到期日期';
if (daysRemaining <= 0) return '已过期';
if (daysRemaining <= 30) return '即将过期';
return '正常';
}
function generateHTML(domains, isAdmin) {
const categorizedDomains = categorizeDomains(domains);
console.log("Categorized domains:", categorizedDomains);
const generateTable = (domainList, isCFTopLevel) => {
if (!domainList || !Array.isArray(domainList)) {
console.error('Invalid domainList:', domainList);
return '';
}
return domainList.map(info => {
const today = new Date();
const expirationDate = new Date(info.expirationDate);
const daysRemaining = info.expirationDate === 'Unknown' ? 'N/A' : Math.ceil((expirationDate - today) / (1000 * 60 * 60 * 24));
const totalDays = info.registrationDate === 'Unknown' || info.expirationDate === 'Unknown' ? 'N/A' : Math.ceil((expirationDate - new Date(info.registrationDate)) / (1000 * 60 * 60 * 24));
const progressPercentage = isNaN(daysRemaining) || isNaN(totalDays) ? 0 : 100 - (daysRemaining / totalDays * 100);
const whoisErrorMessage = info.whoisError
? `<br><span style="color: red;">WHOIS错误: ${info.whoisError}</span><br><span style="color: blue;">建议:请检查域名状态或API配置</span>`
: '';
let operationButtons = '';
if (isAdmin) {
if (isCFTopLevel) {
operationButtons = `
<button onclick="editDomain('${info.domain}', this)">编辑</button>
<button data-action="update-whois" data-domain="${info.domain}">更新WHOIS信息</button>
<button data-action="query-whois" data-domain="${info.domain}">查询WHOIS信息</button>
`;
} else {
operationButtons = `
<button onclick="editDomain('${info.domain}', this)">编辑</button>
<button onclick="deleteDomain('${info.domain}')">删除</button>
`;
}
}
return `
<tr data-domain="${info.domain}">
<td class="status-column"><span class="status-dot" style="background-color: ${getStatusColor(daysRemaining)};" title="${getStatusTitle(daysRemaining)}"></span></td>
<td class="domain-column" title="${info.domain}">${info.domain}</td>
<td class="system-column" title="${info.system}">${info.system}</td>
<td class="registrar-column editable" title="${info.registrar}${whoisErrorMessage}">${info.registrar}${whoisErrorMessage}</td>
<td class="date-column editable" title="${info.registrationDate}">${info.registrationDate}</td>
<td class="date-column editable" title="${info.expirationDate}">${info.expirationDate}</td>
<td class="days-column" title="${daysRemaining}">${daysRemaining}</td>
<td class="progress-column">
<div class="progress-bar">
<div class="progress" style="width: ${progressPercentage}%;" title="${progressPercentage.toFixed(2)}%"></div>
</div>
</td>
${isAdmin ? `<td class="operation-column">${operationButtons}</td>` : ''}
</tr>
`;
}).join('');
};
const cfTopLevelTable = generateTable(categorizedDomains.cfTopLevel, true);
const cfSecondLevelAndCustomTable = generateTable(categorizedDomains.cfSecondLevelAndCustom, false);
const adminLink = isAdmin
? '<span>当前为后台管理页面</span> | <a href="/">返回前台</a>'
: '<a href="/admin">进入后台管理</a>';
return `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${CUSTOM_TITLE}${isAdmin ? ' - 后台管理' : ''}</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
.container {
margin: 0 auto;
padding: 0 15px;
}
.container {
padding-bottom: 60px; /* 根据页脚高度调整 */
}
footer {
position: relative;
left: 0;
bottom: 0;
width: 100%;
}
.table-wrapper {
width: 100%;
overflow-x: auto;
}
h2.table-title {
font-size: 1.5em;
margin-top: 30px;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 2px solid #ddd;
}
.table-separator {
height: 2px;
background-color: #eee;
margin: 30px 0;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
table-layout: auto;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
.status-column { width: 30px; min-width: 30px; max-width: 50px; }
.domain-column { min-width: 120px; max-width: 25%; }
.system-column, .registrar-column { min-width: 80px; max-width: 15%; }
.date-column { min-width: 90px; max-width: 12%; }
.days-column { min-width: 60px; max-width: 10%; }
.progress-column { min-width: 100px; max-width: 20%; }
.operation-column { min-width: 120px; max-width: 20%; }
.status-dot {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
}
.progress-bar {
width: 100%;
background-color: #e0e0e0;
border-radius: 5px;
overflow: hidden;
}
.progress {
height: 20px;
background-color: #4CAF50;
transition: width 0.5s ease-in-out;
}
button {
padding: 5px 10px;
margin: 2px;
cursor: pointer;
}
.section-header {
background-color: #e9ecef;
font-weight: bold;
}
.section-header td {
padding: 10px;
}
@media (max-width: 768px) {
table {
font-size: 12px;
}
th, td {
padding: 6px;
}
.system-column, .registrar-column {
display: none;
}
.operation-column {
width: auto;
}
button {
padding: 3px 6px;
font-size: 12px;
}
.less-important-column {
display: none;
}
}
</style>
</head>
<body>
<div class="container">
<h1>${CUSTOM_TITLE}${isAdmin ? ' - 后台管理' : ''}</h1>
<div class="admin-link">${adminLink}</div>
<div class="table-wrapper">
<table>
<thead>
<tr>
<th class="status-column">状态</th>
<th class="domain-column">域名</th>
<th class="system-column">系统</th>
<th class="registrar-column">注册商</th>
<th class="date-column">注册日期</th>
<th class="date-column">到期日期</th>
<th class="days-column">剩余天数</th>
<th class="progress-column">进度</th>
${isAdmin ? '<th class="operation-column">操作</th>' : ''}
</tr>
</thead>
<tbody>
<tr class="section-header"><td colspan="${isAdmin ? '9' : '8'}"><h2>CF顶级域名</h2></td></tr>
${cfTopLevelTable}
<tr class="section-separator"><td colspan="${isAdmin ? '9' : '8'}"></td></tr>
<tr class="section-header"><td colspan="${isAdmin ? '9' : '8'}"><h2>CF二级域名or自定义域名</h2></td></tr>
${cfSecondLevelAndCustomTable}
</tbody>
</table>
</div>
${isAdmin ? `
<div>
<h2>添加CF二级域名or自定义域名</h2>
<form id="addCustomDomainForm">
<input type="text" id="newDomain" placeholder="域名" required>
<input type="text" id="newSystem" placeholder="系统" required>
<input type="text" id="newRegistrar" placeholder="注册商" required>
<input type="date" id="newRegistrationDate" required>
<input type="date" id="newExpirationDate" required>
<button type="submit">添加</button>
</form>
</div>
` : ''}
</div>
<script>
async function editDomain(domain, button) {
const row = button.closest('tr');
const cells = row.querySelectorAll('.editable');
if (button.textContent === '编辑') {
button.textContent = '保存';
cells.forEach(cell => {
const input = document.createElement('input');
input.value = cell.textContent;
cell.textContent = '';
cell.appendChild(input);
});
} else {
button.textContent = '编辑';
const updatedData = {
domain: domain,
registrar: cells[0].querySelector('input').value,
registrationDate: cells[1].querySelector('input').value,
expirationDate: cells[2].querySelector('input').value
};
try {
const response = await fetch('/api/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(':${ADMIN_PASSWORD}')
},
body: JSON.stringify(updatedData)
});
if (response.ok) {
cells.forEach(cell => {
cell.textContent = cell.querySelector('input').value;
});
alert('更新成功');
} else {
throw new Error('更新失败');
}
} catch (error) {
alert('更新失败: ' + error.message);
location.reload();
}
}
}
async function deleteDomain(domain) {
if (confirm('确定要删除这个域名吗?')) {
try {
const response = await fetch('/api/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(':${ADMIN_PASSWORD}')
},
body: JSON.stringify({
action: 'delete',
domain: domain
})
});
if (response.ok) {
alert('删除成功');
location.reload();
} else {
throw new Error('删除失败');
}
} catch (error) {
alert('删除失败: ' + error.message);
}
}
}
document.addEventListener('click', function(event) {
if (event.target.dataset.action === 'update-whois') {
updateWhoisInfo(event.target.dataset.domain);
} else if (event.target.dataset.action === 'query-whois') {
queryWhoisInfo(event.target.dataset.domain);
}
});
async function updateWhoisInfo(domain) {
try {
const response = await fetch('/api/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(':${ADMIN_PASSWORD}')
},
body: JSON.stringify({
action: 'update-whois',
domain: domain
})
});
if (response.ok) {
alert('WHOIS信息更新成功');
location.reload();
} else {
throw new Error('WHOIS信息更新失败');
}
} catch (error) {
alert('WHOIS信息更新失败: ' + error.message);
}
}
async function queryWhoisInfo(domain) {
try {
const response = await fetch('/whois/' + domain);
const data = await response.json();
if (data.error) {
alert('查询WHOIS信息失败: ' + data.message);
} else {
alert('WHOIS信息:\\n' + data.rawData);
}
} catch (error) {
alert('查询WHOIS信息失败: ' + error.message);
}
}
${isAdmin ? `
document.getElementById('addCustomDomainForm').addEventListener('submit', function(e) {
e.preventDefault();
const domain = document.getElementById('newDomain').value;
const system = document.getElementById('newSystem').value;
const registrar = document.getElementById('newRegistrar').value;
const registrationDate = document.getElementById('newRegistrationDate').value;
const expirationDate = document.getElementById('newExpirationDate').value;