-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.html
More file actions
474 lines (436 loc) · 19.6 KB
/
dashboard.html
File metadata and controls
474 lines (436 loc) · 19.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Script Execution</title>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container py-5">
<div class="row justify-content-center mb-4">
<div class="col-lg-6 col-md-8">
<div class="text-center mb-4">
<h2 class="font-weight-bold">VPS 管理面板</h2>
<p class="text-muted mb-0">一键创建、安装、删除 Vultr VPS</p>
</div>
<!-- 登录界面 -->
<div id="loginSection" class="card shadow-sm mb-3">
<div class="card-body">
<form id="loginForm" method="post" autocomplete="off">
<div class="form-group mb-3">
<label for="loginPassword">请输入管理密码</label>
<input type="password" class="form-control" id="loginPassword" placeholder="管理密码" autofocus>
</div>
<button type="submit" class="btn btn-primary btn-block">登录</button>
</form>
<div id="loginError" class="alert alert-danger mt-3" style="display: none;"></div>
</div>
</div>
<!-- 功能界面(登录后显示) -->
<div id="dashboardSection" style="display: none;">
<div class="card shadow-sm mb-3">
<div class="card-body">
<form id="createForm" method="post">
<div class="form-row">
<div class="form-group col-4 mb-2">
<select class='form-control' id='regions'>
<option value='nrt'>Tokyo</option>
<option value='sgp'>Singapore</option>
<option value='bom'>Mumbai</option>
<option value='icn'>Seoul</option>
</select>
</div>
<div class="form-group col-4 mb-2">
<select class='form-control' id='createXraySchema'>
<option value='reality'>Reality</option>
<option value='tcp'>TCP</option>
</select>
</div>
<div class="form-group col-4 mb-2">
<input type="text" class="form-control" id="duration" value="55" placeholder="运行时长(min)">
</div>
</div>
<div class="row">
<div class="col-6">
<button type="submit" class="btn btn-primary btn-block">创建 VPS</button>
</div>
<div class="col-6">
<button type="button" id="createAndInstallBtn" class="btn btn-success btn-block">一键创建</button>
</div>
</div>
</form>
</div>
</div>
<div class="card shadow-sm mb-3">
<div class="card-body">
<form id="removeForm" method="post">
<button type="submit" class="btn btn-danger btn-block">删除所有 VPS</button>
</form>
</div>
</div>
<div class="card shadow-sm mb-3">
<div class="card-body">
<form id='reinstallForm' method='post'>
<div class="form-row">
<div class="form-group col-12 mb-2">
<select class='form-control' id='xraySchema'>
<option value='reality'>Reality</option>
<option value='tcp'>TCP</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-info btn-block">重装 Xray</button>
</form>
</div>
</div>
<!-- 定时任务管理卡片 -->
<div class="card shadow-sm mb-3">
<div class="card-header bg-warning text-dark d-flex justify-content-between align-items-center">
<span><strong>定时删除任务</strong></span>
<button id="refreshScheduleBtn" class="btn btn-sm btn-outline-dark">刷新</button>
</div>
<div class="card-body p-0">
<div id="scheduleList">
<div class="text-center text-muted py-3">加载中...</div>
</div>
</div>
</div>
<div id="loader" class="text-center" style="display: none;">
<div class="spinner-border text-primary" role="status">
<span class="sr-only">Loading...</span>
</div>
</div>
<div id='result' class="mt-3"></div>
<div class="text-center mt-3">
<button id="logoutBtn" class="btn btn-secondary btn-sm">退出登录</button>
</div>
</div>
</div>
</div>
</div>
<script type='text/javascript'>
function showLoader() {
$('#loader').show();
}
function hideLoader() {
$('#loader').hide();
}
function checkAuthStatus() {
$.ajax({
url: '/vps/api/check-auth',
type: 'GET',
success: function (response) {
if (response.logged_in) {
showDashboard();
} else {
showLogin();
}
},
error: function () {
showLogin();
}
});
}
function showLogin() {
$('#loginSection').show();
$('#dashboardSection').hide();
}
function showDashboard() {
$('#loginSection').hide();
$('#dashboardSection').show();
loadSchedule(); // 登录后加载定时任务
}
// 格式化剩余时间
function formatRemainingTime(seconds) {
if (seconds <= 0) return '已到期';
var hours = Math.floor(seconds / 3600);
var minutes = Math.floor((seconds % 3600) / 60);
var secs = seconds % 60;
if (hours > 0) {
return hours + '时' + minutes + '分';
} else if (minutes > 0) {
return minutes + '分' + secs + '秒';
} else {
return secs + '秒';
}
}
// 格式化时间
function formatDateTime(isoString) {
var date = new Date(isoString);
return date.toLocaleString('zh-CN', {
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
}
// 加载定时任务列表
function loadSchedule() {
$.ajax({
url: '/vps/api/schedule',
type: 'GET',
success: function (response) {
renderScheduleList(response);
},
error: function (xhr) {
if (xhr.status === 401) {
showLogin();
} else {
$('#scheduleList').html('<div class="text-center text-danger py-3">加载失败</div>');
}
}
});
}
// 渲染定时任务列表
function renderScheduleList(tasks) {
if (!tasks || tasks.length === 0) {
$('#scheduleList').html('<div class="text-center text-muted py-3">暂无定时任务</div>');
return;
}
var html = '<ul class="list-group list-group-flush">';
tasks.forEach(function(task) {
var displayId = task.is_all_instances ? '所有实例' : task.instance_id.substring(0, 8) + '...';
var remainingText = formatRemainingTime(task.remaining_seconds);
var badgeClass = task.remaining_seconds < 300 ? 'badge-danger' : 'badge-warning';
html += '<li class="list-group-item d-flex justify-content-between align-items-center py-2">';
html += '<div class="small">';
html += '<div><strong>' + displayId + '</strong></div>';
html += '<div class="text-muted">到期: ' + formatDateTime(task.expire_time) + '</div>';
html += '</div>';
html += '<div class="d-flex align-items-center">';
html += '<span class="badge ' + badgeClass + ' mr-2">' + remainingText + '</span>';
html += '<button class="btn btn-sm btn-outline-danger cancel-schedule-btn" data-id="' + task.instance_id + '" title="取消定时删除">×</button>';
html += '</div>';
html += '</li>';
});
html += '</ul>';
$('#scheduleList').html(html);
}
// 取消定时任务
function cancelSchedule(instanceId) {
if (!confirm('确定要取消该实例的定时删除任务吗?')) {
return;
}
$.ajax({
url: '/vps/api/schedule/' + encodeURIComponent(instanceId),
type: 'DELETE',
success: function (response) {
loadSchedule(); // 重新加载列表
},
error: function (xhr) {
if (xhr.status === 401) {
showLogin();
} else {
alert('取消失败: ' + (xhr.responseJSON ? xhr.responseJSON.error : '未知错误'));
}
}
});
}
// 定时刷新任务列表(每30秒)
var scheduleRefreshInterval = null;
function startScheduleRefresh() {
if (scheduleRefreshInterval) clearInterval(scheduleRefreshInterval);
scheduleRefreshInterval = setInterval(function() {
if ($('#dashboardSection').is(':visible')) {
loadSchedule();
}
}, 30000);
}
function checkStatus() {
$.ajax({
url: '/vps/api/status',
type: 'GET',
success: function (response) {
if (response.in_progress) {
var stageText = {
'creating': '正在创建VPS...',
'waiting': '等待VPS启动...',
'installing': '正在安装Xray...'
};
var statusMsg = stageText[response.stage] || '任务进行中...';
$('#result').html('<pre>' + statusMsg + ' 请稍候</pre>');
setTimeout(checkStatus, 5000); // 5秒后再次检查
} else {
$('#result').html('<pre>任务已完成!</pre>');
loadSchedule(); // 任务完成后刷新定时任务列表
}
},
error: function (xhr) {
if (xhr.status === 401) {
showLogin();
$('#result').html('<pre>登录已过期,请重新登录</pre>');
} else {
$('#result').html('<pre>检查状态时出错</pre>');
}
}
});
}
$(document).ready(function () {
// 页面加载时检查登录状态
checkAuthStatus();
// 启动定时刷新
startScheduleRefresh();
// 刷新定时任务按钮
$('#refreshScheduleBtn').on('click', function() {
loadSchedule();
});
// 取消定时任务按钮(事件委托)
$(document).on('click', '.cancel-schedule-btn', function() {
var instanceId = $(this).data('id');
cancelSchedule(instanceId);
});
// 登录表单提交
$('#loginForm').on('submit', function (e) {
e.preventDefault();
var password = $('#loginPassword').val();
$('#loginError').hide();
$.ajax({
url: '/vps/api/login',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ password: password }),
success: function (response) {
if (response.success) {
showDashboard();
$('#loginPassword').val('');
} else {
$('#loginError').text(response.error || '登录失败').show();
}
},
error: function (xhr) {
var errorMsg = '登录失败';
if (xhr.responseJSON && xhr.responseJSON.error) {
errorMsg = xhr.responseJSON.error;
}
$('#loginError').text(errorMsg).show();
}
});
});
// 退出登录
$('#logoutBtn').on('click', function () {
$.ajax({
url: '/vps/api/logout',
type: 'POST',
success: function () {
showLogin();
}
});
});
// Create script AJAX call
$('#createForm').on('submit', function (e) {
e.preventDefault();
showLoader();
var region = $('#regions').val();
var duration = $('#duration').val();
//清空结果
$('#result').html('<pre></pre>');
$.ajax({
url: '/vps/api/create',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ region: region, duration: duration}),
success: function (response) {
hideLoader();
$('#result').html('<pre>' + JSON.stringify(response, null, 2) + '</pre>');
},
error: function (xhr) {
hideLoader();
if (xhr.status === 401) {
showLogin();
$('#result').html('<pre>登录已过期,请重新登录</pre>');
} else {
$('#result').html('<pre>Error occurred!</pre>');
}
}
});
});
// 一键创建按钮处理
$('#createAndInstallBtn').on('click', function (e) {
e.preventDefault();
showLoader();
var region = $('#regions').val();
var xrayschema = $('#createXraySchema').val();
var duration = $('#duration').val();
//清空结果
$('#result').html('<pre></pre>');
$.ajax({
url: '/vps/api/create-and-install',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ region: region, xrayschema: xrayschema, duration: duration}),
success: function (response) {
hideLoader();
$('#result').html('<pre>' + JSON.stringify(response, null, 2) + '</pre>');
// 启动状态检查
checkStatus();
// 延迟刷新定时任务列表(等待后台任务完成)
setTimeout(loadSchedule, 65000);
},
error: function (xhr) {
hideLoader();
if (xhr.status === 401) {
showLogin();
$('#result').html('<pre>登录已过期,请重新登录</pre>');
} else {
$('#result').html('<pre>Error occurred!</pre>');
}
}
});
});
$('#removeForm').on('submit', function (e) {
e.preventDefault();
$('#result').html('<pre></pre>');
showLoader();
$.ajax({
url: '/vps/api/remove',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({}),
success: function (response) {
hideLoader();
$('#result').html('<pre>' + JSON.stringify(response, null, 2) + '</pre>');
// 删除后刷新定时任务列表
loadSchedule();
},
error: function (xhr) {
hideLoader();
if (xhr.status === 401) {
showLogin();
$('#result').html('<pre>登录已过期,请重新登录</pre>');
} else {
$('#result').html('<pre>Error occurred!</pre>');
}
}
});
});
$('#reinstallForm').on('submit', function (e) {
e.preventDefault();
showLoader();
var xrayschema = $('#xraySchema').val();
$('#result').html('<pre></pre>');
$.ajax({
url: '/vps/api/xray',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ xrayschema: xrayschema }),
success: function (response) {
hideLoader();
$('#result').html('<pre>' + JSON.stringify(response, null, 2) + '</pre>');
},
error: function (xhr) {
hideLoader();
if (xhr.status === 401) {
showLogin();
$('#result').html('<pre>登录已过期,请重新登录</pre>');
} else {
$('#result').html('<pre>Error occurred!</pre>');
}
}
});
});
});
</script>
</body>
</html>