-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin.html
More file actions
514 lines (477 loc) · 34.7 KB
/
admin.html
File metadata and controls
514 lines (477 loc) · 34.7 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WSTunnel Admin</title>
<style>
body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";background:#f4f7fb;margin:0;font-size:15px}
.container{max-width:1400px;margin:0 auto;padding:0 18px}
.card{background:white;border-radius:8px;padding:18px;box-shadow:0 4px 14px rgba(20,30,60,0.06)}
.page-grid > .card, .right-panel-wrapper > .card { margin-top: 12px; }
table{width:100%;border-collapse:collapse;font-size:14px;}
th,td{padding:10px 12px;border-bottom:1px solid #f1f5f9;word-wrap:break-word; text-align: center; vertical-align: middle;}
#device-list td { white-space: nowrap; }
th{white-space:nowrap}thead th{position:sticky;top:0;background:#f8fafc;border-bottom:2px solid #eef2f7;font-weight:700}
tr:last-child td {border-bottom: none;}
td:first-child, th:first-child { text-align: left; }
td:nth-child(2) { text-align: left; }
td:last-child, th:last-child { text-align: center; }
.table-container,.device-list-container{overflow-x:auto;border-radius:8px;border:1px solid #e6eef8;margin-top:8px}.table-container{max-height:calc(100vh - 400px)}.device-list-container{flex-grow: 1;}.btn{border:0;padding:8px 12px;border-radius:6px;cursor:pointer;font-weight:700;color:white;transition:opacity .2s}.btn:hover{opacity:0.85}.btn-sm{padding:5px 8px; font-size:12px;}.btn.primary{background:#2563eb}.btn.secondary{background:#10b981}.btn.danger{background:#dc2626}.btn.info{background:#0ea5e9}.btn.warning{background:#f59e0b}.status-pill{display:inline-block;padding:4px 10px;border-radius:999px;font-weight:600;font-size:12px}
.status-normal, .status-活跃 {background:#e6ffed;color:#065f46}
.status-banned {background:#fecaca;color:#991b1b}
.status-expired {background:#feecdc;color:#9a3412}
.status-握手 {background:#fffbeb;color:#b45309}
.navbar{display:flex;gap:8px;align-items:center;padding:8px 0;flex-wrap:wrap;border-bottom: 1px solid #e2e8f0;}.navbar a{padding:8px 12px;border-radius:8px;text-decoration:none;font-weight:700;color:#0f172a;transition:background-color .2s, color .2s}.navbar a.active{background:#eef2ff; color:#2563eb;}.navbar a:not(.active):hover{background-color:#f8fafc;}.form-row{display:flex;gap:10px;align-items:center;margin-top:8px;flex-wrap:wrap}.form-row label{font-weight:600}
input[type="text"],input[type="password"],input[type="number"],input[type="date"],select,textarea{font-size:14px;padding:8px;border:1px solid #e2e8f0;border-radius:6px;box-sizing:border-box}
input[type="text"],input[type="password"],input[type="number"],input[type="date"],select{height:32px; padding: 4px 8px;}
textarea { width: 100%; resize: vertical; }
.status-bar{display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:12px;padding:12px 0}.status-card{background:#fff;border-radius:8px;padding:12px 16px;box-shadow:0 4px 12px rgba(20,30,60,0.05);border:1px solid #eef2f7;display:flex;flex-direction:column;gap:4px}.status-card span{font-size:14px;color:#64748b}.status-card strong{font-size:16px;color:#0f172a;font-weight:600}.status-card small{font-size:12px;color:#94a3b8}
.settings-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(360px,1fr));gap:18px;margin-top:12px}.settings-group{border:1px solid #eef2f7;border-radius:8px;padding:12px 16px}.settings-group h3{margin-top:0;font-size:16px;border-bottom:1px solid #eef2f7;padding-bottom:10px;margin-bottom:16px}.settings-group .form-row{margin-top:12px}.settings-group .form-row label{min-width:140px;text-align:right;font-weight:normal;color:#334155}.settings-group .form-row input, .settings-group .form-row textarea{flex-grow:1;}
.toggle-switch{position:relative;display:inline-block;width:50px;height:28px}.toggle-switch input{opacity:0;width:0;height:0}.slider{position:absolute;cursor:pointer;top:0;left:0;right:0;bottom:0;background-color:#ccc;transition:.4s;border-radius:28px}.slider:before{position:absolute;content:"";height:20px;width:20px;left:4px;bottom:4px;background-color:white;transition:.4s;border-radius:50%}input:checked+.slider{background-color:#2563eb}input:checked+.slider:before{transform:translateX(22px)}
.page-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(450px, 1fr); gap: 12px; align-items: stretch; }
.page-grid > .card, .right-panel-wrapper > .card { display: flex; flex-direction: column; height: calc(100vh - 200px); min-width: 0; }
.form-panel .form-row { flex-wrap: nowrap; }.form-panel .form-row label { flex-basis: 120px; flex-shrink: 0; text-align: right; font-weight: normal; color: #334155; line-height: 32px; }.form-panel .form-row input, .form-panel .form-row select, .form-panel .form-row .input-wrapper { flex-grow: 1; }
.action-buttons { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 15px; }
#device-list-body tr { cursor: pointer; transition: background-color .2s; }#device-list-body tr:hover { background-color: #f8fafc; }#device-list-body tr.highlight { background-color: #eef2ff !important; }
#toast-container { position: fixed; top: 20px; right: 20px; z-index: 9999; display: flex; flex-direction: column; gap: 10px; }
.toast { padding: 12px 18px; border-radius: 6px; color: white; font-weight: 600; box-shadow: 0 4px 12px rgba(0,0,0,0.15); opacity: 0; transform: translateX(100%); transition: all 0.4s ease-in-out; }.toast.show { opacity: 1; transform: translateX(0); }.toast.success { background-color: #10b981; }.toast.error { background-color: #dc2626; }
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 10000; display: flex; justify-content: center; align-items: center; opacity: 0; visibility: hidden; transition: all 0.3s; }.modal-overlay.show { opacity: 1; visibility: visible; }
.modal-content { background: white; padding: 25px; border-radius: 8px; max-width: 400px; width: 90%; text-align: center; transform: scale(0.9); transition: all 0.3s; }.modal-overlay.show .modal-content { transform: scale(1); }
.modal-buttons { display: flex; justify-content: center; gap: 15px; margin-top: 20px; }
.right-panel-wrapper { display: flex; flex-direction: column; }
.right-panel-wrapper .card { margin-top: 0; }
.page-grid > .card, .right-panel-wrapper { height: calc(100vh - 200px); }
.right-panel-wrapper .card:last-child { margin-top: 12px; flex-grow: 1; display: flex; flex-direction: column; }
#log-viewer {height:calc(100vh - 280px);overflow:auto;background:#282c34;color:#abb2bf;padding:10px;border-radius:8px;font-family:monospace;font-size:12px;white-space:pre-wrap;}
/* [新增] 专门用于面板设置页的响应式布局 */
.account-settings-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 24px;
}
@media (max-width: 1024px) {
.page-grid { grid-template-columns: 1fr; }
.page-grid > .card, .right-panel-wrapper { height: auto; }
.device-list-container, .table-container { max-height: 400px; }
.form-panel .form-row, .settings-group .form-row { flex-wrap: wrap; }
.form-panel .form-row label, .settings-group .form-row label { text-align: left; flex-basis: 100%; }
/* [新增] 在小屏幕上,面板设置页变为单列布局 */
.account-settings-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div id="toast-container"></div>
<div id="modal-container"></div>
<div class="container">
<nav class="navbar">
<a href="#/status">连接状态</a>
<a href="#/device">账号管理</a>
<a href="#/account">安全设置</a>
<a href="#/settings">系统设置</a>
<a href="#/logs">系统日志</a>
<a style="margin-left:auto;background:#ff6b6b;color:#fff;border-radius:6px;padding:8px 12px;text-decoration:none" href="/logout">退出</a>
</nav>
<div class="status-bar">
<div class="status-card"><span>登录用户</span><strong id="current-user">...</strong></div>
<div class="status-card"><span>活跃连接</span><strong id="active-connections">...</strong></div>
<div class="status-card"><span>总上传</span><strong id="global-sent">...</strong></div>
<div class="status-card"><span>总下载</span><strong id="global-received">...</strong></div>
<div class="status-card"><span>运行时长</span><strong id="server-uptime">...</strong></div>
<div class="status-card"><span>CPU</span><strong id="cpu-usage">...</strong></div>
<div class="status-card"><span>内存使用</span><strong id="mem-usage">...</strong></div>
</div>
<main id="content-area"></main>
</div>
<script>
const gid = id => document.getElementById(id);
const contentArea = gid('content-area');
let appState = { accounts: {}, status: {}, connections: [], settings: null, logs: [], traffic: {}, currentUser: '' };
let refreshInterval;
const formatBytes = (b, d = 2) => { if (!isFinite(b) || b <= 0) return "0 B"; const k = 1024, s = ["B", "KB", "MB", "GB", "TB"], i = Math.floor(Math.log(b) / Math.log(k)); return `${parseFloat((b / Math.pow(k, i)).toFixed(d))} ${s[i]}`; };
const formatTime = (iso) => iso ? new Date(iso).toLocaleString() : 'N/A';
function showToast(message, type = 'success', duration = 3000) {
const container = gid('toast-container');
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.textContent = message;
container.appendChild(toast);
setTimeout(() => toast.classList.add('show'), 10);
setTimeout(() => { toast.classList.remove('show'); setTimeout(() => toast.remove(), 400); }, duration);
}
function showConfirm(title, message, onConfirm) {
const container = gid('modal-container');
container.innerHTML = `<div class="modal-overlay"><div class="modal-content"><h3>${title}</h3><p>${message}</p><div class="modal-buttons"><button class="btn secondary" id="modal-cancel">取消</button><button class="btn danger" id="modal-confirm">确认</button></div></div></div>`;
const overlay = container.querySelector('.modal-overlay');
setTimeout(() => overlay.classList.add('show'), 10);
const closeModal = () => { overlay.classList.remove('show'); setTimeout(() => container.innerHTML = '', 300); };
gid('modal-confirm').onclick = () => { onConfirm(); closeModal(); };
gid('modal-cancel').onclick = closeModal;
}
async function apiRequest(url, options = {}) {
if (options.body) options.headers = { ...options.headers, 'Content-Type': 'application/json' };
try {
const response = await fetch(url, options);
if (response.status === 401) { window.location.href = "/login.html"; return null; }
const data = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(data.message || `HTTP error ${response.status}`);
return data;
} catch (error) {
console.error(`API Error for ${url}:`, error);
showToast(error.message, 'error');
return null;
}
}
// --- Page Renderers ---
function renderStatusPage() {
contentArea.innerHTML = `<div class="card"><div style="display:flex; justify-content: space-between; align-items: center;"><h2>连接状态</h2></div><div class="table-container"><table><thead><tr><th>账号名称</th><th>状态</th><th>上传</th><th>下载</th><th>剩余流量</th><th>有效期</th><th>IP地址</th><th>连接时间</th><th>操作</th></tr></thead><tbody id="connections-body"></tbody></table></div></div>`;
renderConnectionsTable();
}
function renderDevicePage() {
contentArea.innerHTML = `<div class="page-grid">
<div class="card">
<h3 style="margin:0;">账号列表</h3>
<div class="form-row" style="padding-bottom: 10px; margin-top: 15px;">
<input id="device-search-box" type="text" placeholder="通过名称或用户名搜索..." oninput="renderDevicesTable()" style="width: 100%;">
</div>
<div class="device-list-container">
<table>
<thead><tr><th>状态</th><th>名称</th><th>用户名 (Key)</th><th>有效期</th><th>已用/总量</th><th>连接数</th><th>操作</th></tr></thead>
<tbody id="device-list-body"></tbody>
</table>
</div>
</div>
<div class="right-panel-wrapper">
<div class="card form-panel">
<h3 id="form-title" style="margin:0;">创建账号</h3>
<input type="hidden" id="current_editing_key">
<div class="form-row"><label>友好名称:</label><input id="friendly_name" type="text" placeholder="e.g. MyPhone"></div>
<div class="form-row"><label>用户名:</label><input id="username" type="text" placeholder="e.g. user01"></div>
<div class="form-row"><label>密码:</label><input id="password" type="text"></div>
<div class="form-row"><label>有效期:</label><input id="expiry_date" type="date"></div>
<div class="form-row"><label>流量 (GB):</label><input id="limit_gb" type="number" placeholder="0为无限"></div>
<div class="form-row"><label>最大连接数:</label><input id="max_sessions" type="number" value="1"></div>
<div class="form-row">
<label>启用账号:</label>
<div class="input-wrapper">
<label class="toggle-switch">
<input type="checkbox" id="enabled" checked><span class="slider"></span>
</label>
</div>
</div>
<div class="action-buttons" style="flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top:20px;">
<button class="btn info" onclick="generateNewDevice()">一键填充</button>
<button class="btn secondary" onclick="resetTraffic()">重置流量</button>
<button class="btn primary" style="flex-grow: 1;" onclick="saveAccount()">保存设置</button>
<button class="btn" style="background:#64748b; flex-grow: 1;" onclick="clearForm()">清空表单</button>
</div>
</div>
<div class="card">
<h3 style="margin-top:0;">封禁列表</h3>
<div class="device-list-container" style="max-height: 200px;">
<table id="blacklist-table">
<thead><tr><th>名称</th><th>用户名</th><th>操作</th></tr></thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
</div>`;
renderDevicesTable();
renderBlacklistTable();
}
function renderAccountPage() {
const s = appState.settings;
if (!s) { contentArea.innerHTML = `<div class="card">正在加载设置...</div>`; return; }
const allowedSniText = (s.allowed_sni || []).join('\n');
// [修改] 使用新的CSS类来实现响应式布局
contentArea.innerHTML = `<div class="card">
<h2>访问控制设置</h2>
<div class="account-settings-grid">
<div class="settings-group" style="border: none; padding: 0;">
<h3>面板密码设置</h3>
<p style="color:#64748b; font-size:13px; margin-top:0;">在这里修改您当前登录的管理面板账号 (${appState.currentUser}) 的密码。</p>
<div class="form-row" style="flex-direction:column; align-items:stretch;"><label>旧密码:</label><input id="old_pass" type="password"></div>
<div class="form-row" style="flex-direction:column; align-items:stretch;"><label>新密码:</label><input id="new_pass" type="password"></div>
<div class="form-row" style="margin-top:20px;"><button class="btn secondary" onclick="updateAdminPassword()">更新密码</button></div>
</div>
<div class="settings-group" style="border: none; padding: 0;">
<h3>SNI 白名单设置</h3>
<div class="form-row" style="flex-direction:column; align-items:stretch;">
<label>SNI 白名单 (每行一个):</label>
<textarea id="s_allowed_sni" rows="5" placeholder="e.g.\nyour.domain.com">${allowedSniText}</textarea>
<p style="font-size: 12px; color: #64748b; margin-top: 8px;">只有客户端 SNI 在此列表中的 TLS 连接才会被接受。留空则允许所有。</p>
</div>
<div class="form-row" style="margin-top:20px;"><button class="btn warning" onclick="saveSettings()">保存SNI设置</button></div>
</div>
</div>
</div>`;
}
function renderSettingsPage() {
const s = appState.settings;
if (!s) { contentArea.innerHTML = `<div class="card">正在加载设置...</div>`; return; }
contentArea.innerHTML = `<div class="card"><h2>系统设置</h2><p style="font-size:13px;color:#64748b;">修改这些设置后将保存到 config.json。有些设置可能需要重启程序才能完全生效。</p><div class="settings-grid"><div class="settings-group"><h3>握手与连接</h3><div class="form-row"><label>HTTP握手超时 (秒):</label><input id="s_handshake_timeout" type="number" value="${s.handshake_timeout}"></div><div class="form-row"><label>连接 User-Agent:</label><input id="s_connect_ua" type="text" value="${s.connect_ua}"></div><div class="form-row"><label>连接空闲超时 (秒):</label><input id="s_idle_timeout_seconds" type="number" value="${s.idle_timeout_seconds}"></div><div class="form-row"><label>目标连接超时 (秒):</label><input id="s_target_connect_timeout_seconds" type="number" value="${s.target_connect_timeout_seconds}"></div></div><div class="settings-group"><h3>性能与转发</h3><div class="form-row"><label>缓冲区大小 (KB):</label><input id="s_buffer_size_kb" type="number" value="${s.buffer_size_kb}"></div><div class="form-row"><label>网络错误重试次数:</label><input id="s_tolerant_copy_max_retries" type="number" value="${s.tolerant_copy_max_retries}"></div><div class="form-row"><label>错误重试延迟 (毫秒):</label><input id="s_tolerant_copy_retry_delay_ms" type="number" value="${s.tolerant_copy_retry_delay_ms}"></div></div><div class="settings-group"><h3>新用户默认值</h3><div class="form-row"><label>默认有效期 (天):</label><input id="s_default_expiry_days" type="number" value="${s.default_expiry_days}"></div><div class="form-row"><label>默认流量 (GB):</label><input id="s_default_limit_gb" type="number" value="${s.default_limit_gb}"></div></div></div><div class="action-buttons" style="justify-content: center; margin-top: 20px;"><button class="btn warning" onclick="saveSettings()">保存所有设置</button></div></div>`;
}
function renderLogsPage() {
contentArea.innerHTML = `<div class="card"><h2>系统日志 (最新200条)</h2><div id="log-viewer"></div></div>`;
renderLogs();
}
// --- Table & Content Updaters ---
function updateStatusBar() {
const s = appState.status;
if (!s) return;
gid('current-user').textContent = appState.currentUser;
// [修正] 修复 active_cons 的拼写错误
gid('active-connections').textContent = s.active_conns || 0;
gid('global-sent').textContent = formatBytes(s.global_sent);
gid('global-received').textContent = formatBytes(s.global_rcvd);
gid('server-uptime').textContent = s.uptime || '...';
gid('cpu-usage').textContent = s.cpu_percent ? `${s.cpu_percent.toFixed(1)}%` : '...';
gid('mem-usage').textContent = s.mem_percent ? `${(s.mem_used_bytes / 1024**3).toFixed(1)}GB (${s.mem_percent.toFixed(1)}%)` : '...';
}
function renderConnectionsTable() {
const tbody = gid('connections-body'); if (!tbody) return;
tbody.innerHTML = appState.connections.map(c => {
const acc = appState.accounts[c.username] || {};
const sent = formatBytes(c.sent_bytes);
const received = formatBytes(c.received_bytes);
const remaining = c.remaining_bytes === -1 ? '无限' : formatBytes(c.remaining_bytes);
return `<tr><td>${acc.friendly_name || c.username}</td><td><span class="status-pill status-活跃">活跃</span></td><td>${sent}</td><td>${received}</td><td>${remaining}</td><td>${c.expiry_date || '无'}</td><td>${c.ip}</td><td>${formatTime(c.connect_time)}</td><td><button class="btn warning btn-sm btn-ban" data-username="${c.username}">封禁</button></td></tr>`;
}).join('') || '<tr><td colspan="9" style="text-align:center;">暂无连接</td></tr>';
}
function renderDevicesTable() {
const tbody = gid('device-list-body'); if (!tbody) return;
const searchTerm = gid('device-search-box')?.value.toLowerCase() || '';
const accountsToShow = Object.entries(appState.accounts)
.filter(([_, acc]) => acc.enabled)
.filter(([user, acc]) => {
if (!searchTerm) return true;
const friendlyName = (acc.friendly_name || '').toLowerCase();
const username = user.toLowerCase();
return friendlyName.includes(searchTerm) || username.includes(searchTerm);
})
.sort((a,b) => (a[1].friendly_name || "").localeCompare(b[1].friendly_name || ""));
tbody.innerHTML = accountsToShow.map(([user, acc]) => {
const t = appState.traffic[user] || { sent: 0, received: 0 };
const used = formatBytes(t.sent + t.received);
const limit = acc.limit_gb > 0 ? `${acc.limit_gb} GB` : '无限';
const isExpired = acc.expiry_date && (new Date(acc.expiry_date).getTime() < new Date().setHours(0,0,0,0));
const status = isExpired ? 'expired' : 'normal';
const statusText = isExpired ? '过期' : '正常';
return `<tr id="device-row-${user}" onclick="fillDeviceForm('${user}')"><td><span class="status-pill status-${status}">${statusText}</span></td><td>${acc.friendly_name}</td><td><button class="btn info btn-sm" onclick="event.stopPropagation();copyToClipboard('${user}')">复制</button></td><td>${acc.expiry_date || '无'}</td><td>${used} / ${limit}</td><td>${acc.max_sessions > 0 ? acc.max_sessions : '不限'}</td><td><button class="btn danger btn-sm btn-delete" data-username="${user}">删除</button></td></tr>`;
}).join('') || '<tr><td colspan="7" style="text-align:center;">暂无账号</td></tr>';
}
function renderBlacklistTable() {
const tbody = gid('blacklist-table').querySelector('tbody'); if (!tbody) return;
tbody.innerHTML = Object.entries(appState.accounts).filter(([_, acc]) => !acc.enabled).map(([user, acc]) => `<tr><td>${acc.friendly_name}</td><td>${user}</td><td><button class="btn secondary btn-sm btn-unban" data-username="${user}">解封</button></td></tr>`).join('') || '<tr><td colspan="3" style="text-align:center;">暂无封禁账号</td></tr>';
}
function renderLogs() {
const viewer = gid('log-viewer'); if (!viewer) return;
viewer.textContent = appState.logs.join('\n');
viewer.scrollTop = viewer.scrollHeight;
}
// --- Data Fetching ---
async function updateDynamicData() {
const path = window.location.hash || '#/status';
const status = await apiRequest('/api/server_status');
if (status) { appState.status = status; updateStatusBar(); } else { return; }
if (path === '#/status') {
const [conns, accounts] = await Promise.all([apiRequest('/api/connections'), apiRequest('/api/accounts')]);
if (conns) appState.connections = conns;
if (accounts) appState.accounts = accounts;
renderConnectionsTable();
} else if (path === '#/device') {
const [accounts, traffic] = await Promise.all([apiRequest('/api/accounts'), apiRequest('/api/traffic')]);
if (accounts) appState.accounts = accounts;
if (traffic) appState.traffic = traffic;
renderDevicesTable();
renderBlacklistTable();
} else if (path === '#/logs') {
const logs = await apiRequest('/api/logs');
if (logs) { appState.logs = logs; renderLogs(); }
}
}
// --- Actions ---
function fillDeviceForm(user) {
const acc = appState.accounts[user]; if (!acc) return;
gid('form-title').innerText = `编辑: ${acc.friendly_name}`;
gid('current_editing_key').value = user;
gid('friendly_name').value = acc.friendly_name;
gid('username').value = user;
gid('username').disabled = true;
gid('password').value = "";
gid('password').placeholder = "留空不改";
gid('expiry_date').value = acc.expiry_date;
gid('limit_gb').value = acc.limit_gb;
gid('max_sessions').value = acc.max_sessions;
gid('enabled').checked = acc.enabled;
document.querySelectorAll('#device-list-body tr').forEach(r => r.classList.remove('highlight'));
gid(`device-row-${user}`)?.classList.add('highlight');
}
function clearForm() {
gid('form-title').innerText = '创建新账号';
['current_editing_key', 'friendly_name', 'username', 'password', 'expiry_date', 'limit_gb'].forEach(id => gid(id).value = '');
gid('max_sessions').value = 1;
gid('username').disabled = false;
gid('password').placeholder = "";
gid('enabled').checked = true;
document.querySelectorAll('#device-list-body tr').forEach(r => r.classList.remove('highlight'));
}
// [修改] 修复密码丢失BUG
async function saveAccount() {
const user = gid('username').value.trim();
if (!user) return showToast('用户名不能为空', 'error');
const isEditing = !!gid('current_editing_key').value;
const pass = gid('password').value;
if (!isEditing && !pass) return showToast('新用户必须设置密码', 'error');
const payload = {
friendly_name: gid('friendly_name').value.trim() || user,
enabled: gid('enabled').checked,
expiry_date: gid('expiry_date').value,
limit_gb: parseFloat(gid('limit_gb').value) || 0,
max_sessions: parseInt(gid('max_sessions').value) || 0,
};
// 只有在创建或密码框有内容时才发送 password 字段
if (!isEditing || pass) {
payload.password = pass;
}
const res = await apiRequest(`/api/accounts/${user}`, { method: 'POST', body: JSON.stringify(payload) });
if (res) { showToast(res.message); clearForm(); updateDynamicData(); }
}
async function deleteAccount(user) {
const name = appState.accounts[user]?.friendly_name || user;
showConfirm('确认删除', `确定要永久删除账号 "${name}" (${user}) 吗?`, async () => {
const res = await apiRequest(`/api/accounts/${user}`, { method: 'DELETE' });
if (res) { showToast(res.message); clearForm(); updateDynamicData(); }
});
}
async function setDeviceStatus(user, isEnabled) {
const action = isEnabled ? '解封' : '封禁';
const name = appState.accounts[user]?.friendly_name || user;
showConfirm(`确认${action}`, `确定要${action}账号 "${name}" (${user}) 吗?`, async () => {
const res = await apiRequest(`/api/accounts/set_status`, { method: 'POST', body: JSON.stringify({username: user, enabled: isEnabled})});
if (res) { showToast(res.message); updateDynamicData(); }
});
}
async function resetTraffic() {
const user = gid('current_editing_key').value;
if (!user) return showToast('请先在列表中选择一个用户', 'error');
showConfirm('确认重置', `确定要重置用户 "${user}" 的流量吗?`, async () => {
const res = await apiRequest(`/api/accounts/reset-traffic`, {method: 'POST', body: JSON.stringify({username: user})});
if (res) { showToast(res.message); updateDynamicData(); }
});
}
async function updateAdminPassword() {
const old_pass = gid('old_pass').value;
const new_pass = gid('new_pass').value;
if (!old_pass || !new_pass) return showToast('新旧密码都不能为空', 'error');
const res = await apiRequest('/api/admin/update_password', {method:'POST', body: JSON.stringify({oldPassword: old_pass, newPassword: new_pass})});
if (res) { showToast(res.message); gid('old_pass').value = ''; gid('new_pass').value = ''; }
}
function generateNewDevice() {
clearForm();
gid('username').value = `user_${Math.random().toString(36).substring(2, 8)}`;
gid('password').value = Math.random().toString(36).substring(2, 10);
if(appState.settings) {
const d = parseInt(appState.settings.default_expiry_days, 10) || 30;
const t = new Date();
t.setDate(t.getDate() + d);
gid('expiry_date').value = `${t.getFullYear()}-${('0'+(t.getMonth()+1)).slice(-2)}-${('0'+t.getDate()).slice(-2)}`;
gid('limit_gb').value = appState.settings.default_limit_gb || 0;
}
showToast("新账号信息已填充", "success");
}
async function saveSettings() {
const path = window.location.hash;
let payload = { ...appState.settings };
if (path === '#/account') {
const allowedSniList = gid('s_allowed_sni').value.split('\n').map(sni => sni.trim()).filter(sni => sni);
payload.allowed_sni = allowedSniList;
} else if (path === '#/settings') {
payload.handshake_timeout = parseInt(gid('s_handshake_timeout').value, 10);
payload.connect_ua = gid('s_connect_ua').value;
payload.buffer_size_kb = parseInt(gid('s_buffer_size_kb').value, 10);
payload.idle_timeout_seconds = parseInt(gid('s_idle_timeout_seconds').value, 10);
payload.tolerant_copy_max_retries = parseInt(gid('s_tolerant_copy_max_retries').value, 10);
payload.tolerant_copy_retry_delay_ms = parseInt(gid('s_tolerant_copy_retry_delay_ms').value, 10);
payload.target_connect_timeout_seconds = parseInt(gid('s_target_connect_timeout_seconds').value, 10);
payload.default_expiry_days = parseInt(gid('s_default_expiry_days').value, 10);
payload.default_limit_gb = parseFloat(gid('s_default_limit_gb').value);
}
const res = await apiRequest('/api/settings', { method: 'POST', body: JSON.stringify(payload) });
if (res) {
showToast(res.message);
appState.settings = await apiRequest('/api/settings');
}
}
function copyToClipboard(user) {
const acc = appState.accounts[user];
if (!acc) return;
const textToCopy = `用户名: ${user}\n密码: ${acc.password}`;
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(textToCopy).then(
() => showToast('用户名和密码已复制'),
() => showToast('复制失败', 'error')
);
} else {
const textArea = document.createElement('textarea');
textArea.value = textToCopy;
textArea.style.position = 'fixed';
textArea.style.left = '-9999px';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
showToast('用户名和密码已复制');
} catch (err) {
showToast('复制失败', 'error');
}
document.body.removeChild(textArea);
}
}
// --- Router and Init ---
const routes = { '#/status': renderStatusPage, '#/device': renderDevicePage, '#/account': renderAccountPage, '#/settings': renderSettingsPage, '#/logs': renderLogsPage };
async function router() {
clearInterval(refreshInterval);
const path = window.location.hash || '#/status';
document.querySelectorAll('.navbar a').forEach(a => a.classList.toggle('active', a.getAttribute('href') === path));
const renderFunc = routes[path] || routes['#/status'];
if (!appState.settings) {
appState.settings = await apiRequest('/api/settings');
}
renderFunc();
await updateDynamicData();
refreshInterval = setInterval(updateDynamicData, 5000);
}
async function initializeApp() {
contentArea.addEventListener('click', function(event){
const target = event.target.closest('button');
if(!target) return;
const username = target.dataset.username;
if (username) {
event.stopPropagation();
if(target.classList.contains('btn-ban')) {
setDeviceStatus(username, false);
} else if (target.classList.contains('btn-unban')) {
setDeviceStatus(username, true);
} else if (target.classList.contains('btn-delete')) {
deleteAccount(username);
}
}
});
const [status, settings, whoami] = await Promise.all([apiRequest('/api/server_status'), apiRequest('/api/settings'), apiRequest('/api/whoami')]);
appState.status = status || {};
appState.settings = settings;
appState.currentUser = whoami?.username || 'admin';
if (!window.location.hash) window.location.hash = '#/status';
await router();
}
window.addEventListener('hashchange', router);
document.addEventListener('DOMContentLoaded', initializeApp);
</script>
</body>
</html>