-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex1.html
215 lines (177 loc) · 6.19 KB
/
index1.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>File Browser</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
.file-list {
list-style-type: none;
padding: 0;
}
.file-item, .directory-item {
margin: 5px 0;
}
.back-button a {
color: #007BFF;
text-decoration: none;
}
.file-item a, .directory-item a {
color: #007BFF;
text-decoration: none;
}
.password-input {
margin-bottom: 20px;
}
.error-message {
color: red;
}
</style>
</head>
<body>
<div class="password-input" id="passwordInputDiv">
<label for="password">请输入密码:</label>
<input type="password" id="password" />
<button onclick="decryptFiles()">解密并加载文件</button>
<p id="error-message" class="error-message"></p>
</div>
<ul class="file-list">
<!-- 文件 / 目录 列表将由JS动态填充 -->
</ul>
<script>
let filesData = [];
let encryptedData = null;
let currentPassword = null;
// 检查是否已经存储密码
function checkStoredPassword() {
const storedPassword = localStorage.getItem('userPassword');
if (storedPassword) {
currentPassword = storedPassword;
document.getElementById('passwordInputDiv').style.display = 'none'; // 隐藏密码输入框
decryptAndLoadFiles(); // 自动解密并加载文件
} else {
document.getElementById('passwordInputDiv').style.display = 'block'; // 显示密码输入框
}
}
// 异步加载加密文件
async function loadEncryptedFile() {
const response = await fetch('./files.json.enc');
if (!response.ok) {
throw new Error('无法加载加密文件 files.json.enc');
}
encryptedData = await response.arrayBuffer();
}
// 解密并加载文件数据
async function decryptFiles() {
const password = document.getElementById('password').value;
const errorMessage = document.getElementById('error-message');
if (!password) {
errorMessage.textContent = "密码不能为空!";
return;
}
try {
// 加载加密文件
await loadEncryptedFile();
// 解密数据
const decryptedData = await decryptAes256(new Uint8Array(encryptedData), password);
// 解析解密后的 JSON 数据
filesData = JSON.parse(new TextDecoder().decode(decryptedData));
// 存储密码并隐藏输入框
localStorage.setItem('userPassword', password);
currentPassword = password;
document.getElementById('passwordInputDiv').style.display = 'none';
// 加载文件列表
const path = getCurrentDirectory();
await loadDirectory(path);
errorMessage.textContent = ''; // 清除错误消息
} catch (error) {
errorMessage.textContent = "解密失败,请检查密码或文件!";
}
}
// 自动解密并加载文件
async function decryptAndLoadFiles() {
try {
await loadEncryptedFile();
const decryptedData = await decryptAes256(new Uint8Array(encryptedData), currentPassword);
filesData = JSON.parse(new TextDecoder().decode(decryptedData));
// 加载文件列表
const path = getCurrentDirectory();
await loadDirectory(path);
} catch (error) {
console.error('解密失败', error);
document.getElementById('error-message').textContent = "解密失败,请检查密码或文件!";
}
}
// AES256 解密算法
async function decryptAes256(encryptedData, password) {
const aesKey = await window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(password));
const iv = encryptedData.slice(0, 16);
const ciphertext = encryptedData.slice(16);
const cryptoKey = await window.crypto.subtle.importKey(
'raw', aesKey, { name: 'AES-CBC' }, false, ['decrypt']
);
const decryptedBuffer = await window.crypto.subtle.decrypt(
{ name: 'AES-CBC', iv: iv }, cryptoKey, ciphertext
);
return new Uint8Array(decryptedBuffer);
}
// 获取当前目录的路径
function getCurrentDirectory() {
const params = new URLSearchParams(window.location.search);
return params.get('path') || '';
}
// 获取上一级目录路径
function getParentPath(path) {
const segments = path.split('/').filter(Boolean);
segments.pop();
return segments.join('/');
}
// 递归过滤出指定 path 下的内容
function filterFilesByPath(files, path) {
const segments = path.split('/').filter(Boolean);
let current = files;
for (const segment of segments) {
const match = current.find(
(item) => item.name === segment && item.type === 'directory'
);
if (match && match.children) {
current = match.children;
} else {
return [];
}
}
return current;
}
// 根据当前目录文件数据生成 HTML 列表
function generateFileList(files, basePath) {
let html = '';
files.forEach(item => {
if (item.type === 'file') {
html += `<li class="file-item"><a href="${item.path}" target="_blank">${item.name}</a></li>`;
} else if (item.type === 'directory') {
const subPath = basePath ? `${basePath}/${item.name}` : item.name;
html += `<li class="directory-item"><a href="?path=${subPath}">${item.name}/</a></li>`;
}
});
return html;
}
// 加载并渲染指定路径下的所有文件和文件夹
async function loadDirectory(path) {
const container = document.querySelector('.file-list');
const directoryContent = filterFilesByPath(filesData, path);
container.innerHTML = '';
if (path) {
const parentPath = getParentPath(path);
container.innerHTML += `<li class="back-button"><a href="?path=${parentPath}">../</a></li>`;
}
container.innerHTML += generateFileList(directoryContent, path);
}
// 页面初始化
checkStoredPassword();
</script>
</body>
</html>