-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.php
More file actions
200 lines (171 loc) · 5.83 KB
/
index.php
File metadata and controls
200 lines (171 loc) · 5.83 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
<?php
session_start();
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
ini_set('display_errors', 0);
// 设置默认时区为中国时区
date_default_timezone_set('Asia/Shanghai');
require_once 'AliyunTrafficCheck.php';
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
$app = new AliyunTrafficCheck();
$action = $_GET['action'] ?? 'view';
// ---------------- 公开接口 ----------------
if ($action === 'check_init') {
header('Content-Type: application/json');
$initError = $app->getInitError();
if ($initError) {
echo json_encode(['initialized' => false, 'error' => $initError]);
} else {
echo json_encode(['initialized' => $app->isInitialized()]);
}
exit;
}
if ($action === 'setup') {
header('Content-Type: application/json');
if ($app->isInitialized()) {
http_response_code(403);
echo json_encode(['success' => false, 'message' => 'System already initialized']);
exit;
}
$data = json_decode(file_get_contents('php://input'), true);
try {
if ($app->setup($data)) {
$_SESSION['is_admin'] = true;
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'message' => 'Setup failed']);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
exit;
}
if ($action === 'login') {
$data = json_decode(file_get_contents('php://input'), true);
try {
if ($app->login($data['password'] ?? '')) {
$_SESSION['is_admin'] = true;
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'message' => '密码错误']);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
exit;
}
if ($action === 'check_login') {
echo json_encode(['logged_in' => isset($_SESSION['is_admin']) && $_SESSION['is_admin'] === true]);
exit;
}
if ($action === 'get_status') {
header('Content-Type: application/json; charset=utf-8');
$initError = $app->getInitError();
if ($initError) {
echo json_encode(['error' => $initError]);
} else {
echo json_encode($app->getStatusForFrontend());
}
exit;
}
// ---------------- 需鉴权接口 ----------------
if ($action !== 'view' && !isset($_SESSION['is_admin'])) {
http_response_code(403);
echo json_encode(['error' => 'Unauthorized']);
exit;
}
// 新增:处理手工控制实例开关机请求
if ($action === 'control_instance') {
$data = json_decode(file_get_contents('php://input'), true);
$id = $data['id'] ?? 0;
$actionType = $data['action'] ?? ''; // 期望值: Start 或 Stop
if (!$id || !$actionType) {
echo json_encode(['success' => false, 'message' => '参数缺失']);
exit;
}
try {
$result = $app->controlInstance($id, $actionType);
if ($result === true) {
echo json_encode(['success' => true, 'message' => '指令发送成功']);
} else {
echo json_encode(['success' => false, 'message' => $result]);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
exit;
}
if ($action === 'get_config') {
echo json_encode($app->getConfigForFrontend());
exit;
}
if ($action === 'save_config') {
$data = json_decode(file_get_contents('php://input'), true);
if ($app->updateConfig($data)) {
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'message' => '保存失败']);
}
exit;
}
if ($action === 'send_test_email') {
$data = json_decode(file_get_contents('php://input'), true);
$result = $app->sendTestEmail($data['email'] ?? '');
echo json_encode(['success' => $result === true, 'message' => $result]);
exit;
}
if ($action === 'send_test_telegram') {
$data = json_decode(file_get_contents('php://input'), true);
$result = $app->sendTestTelegram($data['telegram'] ?? []);
echo json_encode(['success' => $result === true, 'message' => $result]);
exit;
}
if ($action === 'send_test_webhook') {
$data = json_decode(file_get_contents('php://input'), true);
$result = $app->sendTestWebhook($data['webhook'] ?? []);
echo json_encode(['success' => $result === true, 'message' => $result]);
exit;
}
if ($action === 'refresh_account') {
$data = json_decode(file_get_contents('php://input'), true);
$id = $data['id'] ?? 0;
$result = $app->refreshAccount($id);
if ($result === false) {
echo json_encode(['success' => false, 'message' => 'Refresh failed']);
} elseif (is_array($result)) {
// 流量/状态刷新成功,但账单获取失败
echo json_encode($result);
} else {
echo json_encode(['success' => true]);
}
exit;
}
// 修改:获取系统日志,支持 Tab
if ($action === 'get_logs') {
header('Content-Type: application/json; charset=utf-8');
$tab = $_GET['tab'] ?? 'action'; // 默认是动作日志
echo json_encode(['data' => $app->getSystemLogs($tab)]);
exit;
}
// 新增:清空日志
if ($action === 'clear_logs') {
$data = json_decode(file_get_contents('php://input'), true);
$tab = $data['tab'] ?? 'action';
if ($app->clearSystemLogs($tab)) {
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'message' => 'Clear failed']);
}
exit;
}
if ($action === 'get_history') {
header('Content-Type: application/json; charset=utf-8');
$id = $_GET['id'] ?? 0;
echo json_encode(['data' => $app->getAccountHistory($id)]);
exit;
}
if ($action === 'logout') {
session_destroy();
echo json_encode(['success' => true]);
exit;
}
echo $app->renderTemplate();