Open in github.dev
Open in a new github.dev tab
Open in codespace
/
domainkeeper.js
/
domainkeeper.js
219
async function handleAdminLogin(request) {
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
// 在文件顶部添加版本信息后台密码(不可为空)
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 });
}
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;
fetch('/api/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(':' + '${ADMIN_PASSWORD}')
},
body: JSON.stringify({
action: 'add',
domain: domain,
system: system,
registrar: registrar,
registrationDate: registrationDate,
expirationDate: expirationDate
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('添加成功');
location.reload();
} else {
alert('添加失败');
}
})
.catch(error => {
console.error('Error:', error);
alert('添加失败');
});
});
` : ''}
</script>
${footerHTML}
</body>
</html>
`;
}
function getStatusColor(daysRemaining) {
if (isNaN(daysRemaining)) return '#808080'; // 灰色表示未知状态
if (daysRemaining <= 7) return '#ff0000'; // 红色
if (daysRemaining <= 30) return '#ffa500'; // 橙色
if (daysRemaining <= 90) return '#ffff00'; // 黄色
return '#00ff00'; // 绿色
}
function getStatusTitle(daysRemaining) {
if (isNaN(daysRemaining)) return '未知状态';
if (daysRemaining <= 7) return '紧急';
if (daysRemaining <= 30) return '警告';
if (daysRemaining <= 90) return '注意';
return '正常';
}
function categorizeDomains(domains) {
if (!domains || !Array.isArray(domains)) {
console.error('Invalid domains input:', domains);
return { cfTopLevel: [], cfSecondLevelAndCustom: [] };
}
return domains.reduce((acc, domain) => {
if (domain.system === 'Cloudflare' && domain.domain.split('.').length === 2) {
acc.cfTopLevel.push(domain);
} else {
acc.cfSecondLevelAndCustom.push(domain);
}
return acc;
}, { cfTopLevel: [], cfSecondLevelAndCustom: [] });
}
Symbols
Find definitions and references for functions and other symbols in this file by clicking a symbol below or in the code.
r
- funchandleRequest
- funchandleManualQuery
- funccleanupKV
- funchandleFrontend
- funchandleAdmin
- funchandleLogin
- funchandleAdminLogin
- funchandleApiUpdate
- funcfetchCloudflareDomainsInfo
- funcfetchDomainInfo
- funchandleWhoisRequest
- funcfetchWhoisInfo
- funcformatDate
- funcgetCachedWhoisInfo
- funccacheWhoisInfo
- funcgenerateLoginHTML
- funcgetStatusColor
- funcgetStatusTitle
- funcgenerateHTML
- funcgenerateTable
- funcgetStatusColor
- funcgetStatusTitle
- funccategorizeDomains