-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAliyunTrafficCheck.php
More file actions
835 lines (714 loc) · 32.3 KB
/
Copy pathAliyunTrafficCheck.php
File metadata and controls
835 lines (714 loc) · 32.3 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
<?php
require 'vendor/autoload.php';
require_once 'Database.php';
require_once 'ConfigManager.php';
require_once 'AliyunService.php';
require_once 'NotificationService.php';
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class AliyunTrafficCheck
{
private $db;
private $configManager;
private $aliyunService;
private $notificationService;
private $initError = null;
public function __construct()
{
try {
$this->db = new Database();
$this->configManager = new ConfigManager($this->db);
$this->aliyunService = new AliyunService();
$this->notificationService = new NotificationService();
// 注入配置到通知服务
$this->notificationService->setConfig($this->configManager->getAllSettings());
} catch (Exception $e) {
$this->initError = $e->getMessage();
}
}
public function getInitError()
{
return $this->initError;
}
public function isInitialized()
{
if ($this->initError)
return false;
return $this->configManager->isInitialized();
}
public function getAdminPassword()
{
return $this->configManager->get('admin_password', '');
}
public function login($password)
{
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = trim($ips[0]);
}
$attempts = $this->db->getRecentFailedAttempts($ip, 900);
if ($attempts >= 5) {
$this->db->addLog('warning', "登录被锁定: IP {$ip} 尝试次数过多");
throw new Exception("错误次数过多,请 15 分钟后再试。");
}
$adminPass = $this->getAdminPassword();
if (empty($adminPass))
return false;
if (hash_equals((string) $adminPass, (string) $password)) {
$this->db->clearLoginAttempts($ip);
$this->db->addLog('info', "管理员登录成功 [IP: {$ip}]");
return true;
}
$this->db->recordLoginAttempt($ip);
$this->db->addLog('warning', "管理员登录失败 [IP: {$ip}]");
return false;
}
public function setup($data)
{
if ($this->initError)
throw new Exception($this->initError);
if ($this->isInitialized())
return false;
return $this->configManager->updateConfig($data);
}
public function updateConfig($data)
{
$success = $this->configManager->updateConfig($data);
if ($success) {
$this->notificationService->setConfig($this->configManager->getAllSettings());
}
return $success;
}
public function getConfigForFrontend()
{
if ($this->initError)
return [];
$settings = $this->configManager->getAllSettings();
$accounts = $this->configManager->getAccounts();
$config = [
'admin_password' => $settings['admin_password'] ?? '',
'traffic_threshold' => (int) ($settings['traffic_threshold'] ?? 95),
'enable_schedule_email' => ($settings['enable_schedule_email'] ?? '0') === '1',
'shutdown_mode' => $settings['shutdown_mode'] ?? 'KeepCharging',
'threshold_action' => $settings['threshold_action'] ?? 'stop_and_notify',
'keep_alive' => ($settings['keep_alive'] ?? '0') === '1',
'api_interval' => (int) ($settings['api_interval'] ?? 600),
'enable_billing' => ($settings['enable_billing'] ?? '0') === '1',
'Notification' => [
'email_enabled' => ($settings['notify_email_enabled'] ?? '1') === '1',
'email' => $settings['notify_email'] ?? '',
'host' => $settings['notify_host'] ?? '',
'port' => $settings['notify_port'] ?? 465,
'username' => $settings['notify_username'] ?? '',
'password' => $settings['notify_password'] ?? '',
'secure' => $settings['notify_secure'] ?? 'ssl',
'telegram' => [
'enabled' => ($settings['notify_tg_enabled'] ?? '0') === '1',
'token' => $settings['notify_tg_token'] ?? '',
'chat_id' => $settings['notify_tg_chat_id'] ?? '',
'proxy_type' => $settings['notify_tg_proxy_type'] ?? 'none',
'proxy_url' => $settings['notify_tg_proxy_url'] ?? '',
'proxy_ip' => $settings['notify_tg_proxy_ip'] ?? '',
'proxy_port' => $settings['notify_tg_proxy_port'] ?? '',
'proxy_user' => $settings['notify_tg_proxy_user'] ?? '',
'proxy_pass' => $settings['notify_tg_proxy_pass'] ?? ''
],
'webhook' => [
'enabled' => ($settings['notify_wh_enabled'] ?? '0') === '1',
'url' => $settings['notify_wh_url'] ?? '',
'method' => $settings['notify_wh_method'] ?? 'GET',
'request_type' => $settings['notify_wh_request_type'] ?? 'JSON',
'headers' => $settings['notify_wh_headers'] ?? '',
'body' => $settings['notify_wh_body'] ?? ''
]
],
'Accounts' => []
];
foreach ($accounts as $row) {
$config['Accounts'][] = [
'AccessKeyId' => $row['access_key_id'],
'AccessKeySecret' => $row['access_key_secret'],
'regionId' => $row['region_id'],
'instanceId' => $row['instance_id'],
'maxTraffic' => (float) $row['max_traffic'],
'schedule' => [
'enabled' => $row['schedule_enabled'] == 1,
'startTime' => $row['start_time'],
'stopTime' => $row['stop_time']
],
'remark' => $row['remark'] ?? '',
'siteType' => $row['site_type'] ?? 'china'
];
}
return $config;
}
// --- 修改:支持按 Tab 获取日志 ---
public function getSystemLogs($tab = 'action')
{
if ($this->initError)
return [];
if ($tab === 'heartbeat') {
// 心跳日志:只看 heartbeat 类型
$types = ['heartbeat'];
} else {
// 动作日志:只看 info 和 warning,排除 error (超时/接口错误)
$types = ['info', 'warning'];
}
// 仅返回最近 20 条
$logs = $this->db->getLogsByTypes($types, 20);
foreach ($logs as &$log) {
$log['time_str'] = date('Y-m-d H:i:s', $log['created_at']);
}
return $logs;
}
// --- 新增:清空日志并重排 ID ---
public function clearSystemLogs($tab = 'action')
{
if ($this->initError)
return false;
$result = false;
if ($tab === 'heartbeat') {
$result = $this->db->clearLogsByTypes(['heartbeat']);
} else {
$result = $this->db->clearLogsByTypes(['info', 'warning', 'error']);
}
// 关键改动:清空后立即重排剩余 ID
if ($result) {
$this->db->reorderLogsIds();
}
return $result;
}
public function getAccountHistory($id)
{
if ($this->initError)
return [];
$account = $this->configManager->getAccountById($id);
if (!$account)
return ['error' => 'Account not found'];
if (!$account)
return ['error' => 'Account not found'];
// Use account ID for stats query
$rawHourly = $this->db->getHourlyStats($id);
$chartHourly = [];
foreach ($rawHourly as $row) {
$chartHourly[] = [
'time' => date('H:00', $row['recorded_at']),
'full_time' => date('Y-m-d H:i', $row['recorded_at']),
'value' => round($row['traffic'], 3)
];
}
$rawDaily = $this->db->getDailyStats($id);
$chartDaily = [];
foreach ($rawDaily as $row) {
$chartDaily[] = [
'date' => date('Y-m-d', $row['recorded_at']),
'value' => round($row['traffic'], 3)
];
}
return [
'history_24h' => $chartHourly,
'history_30d' => $chartDaily
];
}
// --- 核心监控逻辑 ---
public function monitor()
{
if ($this->initError)
return "Error: " . $this->initError;
// 优化:分级清理日志
// 普通/重要日志保留 30 天,高频心跳日志仅保留 3 天
$this->db->pruneLogs(30, 3);
// 关键改动:每次清理后重排 ID,保证 ID 永远紧凑
$this->db->reorderLogsIds();
$this->db->pruneStats();
// 优化:每天凌晨 04:xx 执行一次 VACUUM 整理数据库碎片
if (date('H') === '04' && date('i') === '00') {
$this->db->vacuum();
}
$logs = [];
$currentUserTime = date('H:i');
$currentTime = time();
$threshold = (int) $this->configManager->get('traffic_threshold', 95);
$shutdownMode = $this->configManager->get('shutdown_mode', 'KeepCharging');
$thresholdAction = $this->configManager->get('threshold_action', 'stop_and_notify');
$keepAlive = $this->configManager->get('keep_alive', '0') === '1';
$userInterval = (int) $this->configManager->get('api_interval', 600);
$accounts = $this->configManager->getAccounts();
foreach ($accounts as $account) {
$logPrefix = "[{$account['access_key_id']}]";
$actions = [];
$forceRefresh = false;
$statusTransformed = false;
// 1. 定时任务
if ($account['schedule_enabled'] == 1) {
if ($account['start_time'] && $currentUserTime === $account['start_time']) {
if ($this->safeControlInstance($account, 'start')) {
$actions[] = "定时启动";
$this->db->addLog('info', "执行定时启动 [{$account['access_key_id']}]");
$mailRes = $this->notificationService->notifySchedule("定时启动", $account, "计划任务已触发,实例正在启动。");
$this->logNotificationResult($mailRes, $account['access_key_id']);
$forceRefresh = true;
$statusTransformed = true;
}
}
if ($account['stop_time'] && $currentUserTime === $account['stop_time']) {
if ($this->safeControlInstance($account, 'stop', $shutdownMode)) {
$actions[] = "定时停止({$shutdownMode})";
$this->db->addLog('info', "执行定时停止 [{$account['access_key_id']}]");
$mailRes = $this->notificationService->notifySchedule("定时停止", $account, "计划任务已触发,实例已停止。");
$this->logNotificationResult($mailRes, $account['access_key_id']);
$forceRefresh = true;
$statusTransformed = true;
}
}
}
// 2. 自适应心跳
$lastUpdate = $account['updated_at'] ?? 0;
$cachedStatus = $account['instance_status'] ?? 'Unknown';
$isTransientState = in_array($cachedStatus, ['Starting', 'Stopping', 'Pending', 'Unknown']);
$currentInterval = ($isTransientState || $statusTransformed) ? 60 : $userInterval;
$shouldCheckApi = $forceRefresh || (($currentTime - $lastUpdate) > $currentInterval);
if (date('i') === '00') {
$shouldCheckApi = true;
}
$newUpdateTime = $currentTime;
if ($shouldCheckApi) {
$newTraffic = $this->safeGetTraffic($account);
$status = $this->safeGetInstanceStatus($account);
if ($status === 'Unknown') {
usleep(500000);
$status = $this->safeGetInstanceStatus($account);
}
if ($newTraffic < 0) {
$traffic = $account['traffic_used'];
$apiStatusLog = "流量API异常";
$newUpdateTime = $lastUpdate;
} else {
$traffic = $newTraffic;
$apiStatusLog = "已更新";
$this->db->addHourlyStat($account['id'], $traffic);
$this->db->addDailyStat($account['id'], $traffic);
}
if ($status === 'Unknown') {
$newUpdateTime = $lastUpdate;
$apiStatusLog .= "(状态Unknown)";
} else {
$apiStatusLog .= in_array($status, ['Starting', 'Stopping', 'Pending']) ? " [过渡态]" : " [稳定态]";
}
$this->configManager->updateAccountStatus($account['id'], $traffic, $status, $newUpdateTime);
} else {
$traffic = $account['traffic_used'];
$status = $account['instance_status'];
$timeLeft = $currentInterval - ($currentTime - $lastUpdate);
$apiStatusLog = "缓存({$timeLeft}s)";
}
$maxTraffic = $account['max_traffic'];
$usagePercent = ($maxTraffic > 0) ? round(($traffic / $maxTraffic) * 100, 2) : 0;
$trafficDesc = "流量:{$usagePercent}%";
$isOverThreshold = $usagePercent >= $threshold;
// 3. 流量熔断
if ($isOverThreshold) {
$trafficDesc .= "[警告]";
if ($shouldCheckApi) {
if ($thresholdAction === 'stop_and_notify') {
if ($status !== 'Stopped') {
if ($this->safeControlInstance($account, 'stop', $shutdownMode)) {
$actions[] = "超限关机";
$this->db->addLog('warning', "流量超限自动关机 [{$account['access_key_id']}] 使用率:{$usagePercent}%");
$this->configManager->updateAccountStatus($account['id'], $traffic, 'Stopping', $currentTime);
$status = 'Stopping';
}
}
} else {
$actions[] = "超限告警";
$this->db->addLog('warning', "流量超限触发告警 [{$account['access_key_id']}] 使用率:{$usagePercent}%");
}
$mailRes = $this->notificationService->sendTrafficWarning($account['access_key_id'], $traffic, $usagePercent, implode(',', $actions), $threshold);
$this->logNotificationResult($mailRes, $account['access_key_id']);
}
}
// 4. 保活逻辑 (跳过已被定时任务操作的实例)
if ($keepAlive && !$isOverThreshold && !$statusTransformed) {
if ($account['schedule_enabled'] == 0 || $this->isTimeInRange($currentUserTime, $account['start_time'], $account['stop_time'])) {
if ($status === 'Stopped') {
if ($this->safeControlInstance($account, 'start')) {
$actions[] = "保活启动";
$this->db->addLog('info', "执行保活启动 [{$account['access_key_id']}]");
$mailRes = $this->notificationService->notifySchedule("保活启动", $account, "检测到实例在工作时段非预期关机,已尝试自动启动。");
$this->logNotificationResult($mailRes, $account['access_key_id']);
$this->configManager->updateAccountStatus($account['id'], $traffic, 'Starting', $currentTime);
$status = 'Starting';
} else {
$apiStatusLog .= " [保活启动失败,下次重试]";
}
}
}
}
if ($statusTransformed) {
$tempStatus = in_array("定时启动", $actions) ? 'Starting' : 'Stopping';
$this->configManager->updateAccountStatus($account['id'], $traffic, $tempStatus, $currentTime);
$apiStatusLog .= " -> 强制过渡态";
}
$actionLog = empty($actions) ? "无动作" : implode(", ", $actions);
$logLine = sprintf("%s %s | %s | %s | %s", $logPrefix, $actionLog, $trafficDesc, $status, $apiStatusLog);
// --- 修改:将心跳日志写入数据库 ---
$this->db->addLog('heartbeat', $logLine);
$logs[] = $logLine;
}
$this->configManager->updateLastRunTime(time());
return implode(PHP_EOL, $logs);
}
public function getStatusForFrontend()
{
if ($this->initError)
return ['error' => $this->initError];
$data = [];
$threshold = (int) $this->configManager->get('traffic_threshold', 95);
$userInterval = (int) $this->configManager->get('api_interval', 600);
$billingEnabled = $this->configManager->get('enable_billing', '0') === '1';
$currentTime = time();
$accounts = $this->configManager->getAccounts();
$billingCycle = date('Y-m');
foreach ($accounts as $account) {
$lastUpdate = $account['updated_at'] ?? 0;
$cachedStatus = $account['instance_status'] ?? 'Unknown';
$newUpdateTime = $currentTime;
$isTransientState = in_array($cachedStatus, ['Starting', 'Stopping', 'Pending', 'Unknown']);
$checkInterval = $isTransientState ? 60 : $userInterval;
if (($currentTime - $lastUpdate) > $checkInterval) {
$newTraffic = $this->safeGetTraffic($account);
$status = $this->safeGetInstanceStatus($account);
if ($status === 'Unknown') {
usleep(500000);
$status = $this->safeGetInstanceStatus($account);
}
if ($newTraffic < 0) {
$traffic = $account['traffic_used'];
$newUpdateTime = $lastUpdate;
} else {
$traffic = $newTraffic;
$this->db->addHourlyStat($account['id'], $traffic);
$this->db->addDailyStat($account['id'], $traffic);
}
if ($status === 'Unknown') {
$newUpdateTime = $lastUpdate;
}
$this->configManager->updateAccountStatus($account['id'], $traffic, $status, $newUpdateTime);
} else {
$traffic = $account['traffic_used'];
$status = $account['instance_status'];
}
$usagePercent = ($account['max_traffic'] > 0) ? round(($traffic / $account['max_traffic']) * 100, 2) : 0;
$isFull = $usagePercent >= $threshold;
$item = [
'id' => $account['id'],
'account' => substr($account['access_key_id'], 0, 7) . '***',
'flow_total' => (float) $account['max_traffic'],
'flow_used' => round($traffic, 2),
'percentageOfUse' => $usagePercent,
'region' => $account['region_id'],
'regionName' => $this->getRegionName($account['region_id']),
'rate95' => $isFull,
'threshold' => $threshold,
'instanceStatus' => $status,
'lastUpdated' => date('Y-m-d H:i:s', $lastUpdate > 0 ? $lastUpdate : $currentTime),
'remark' => $account['remark'] ?? ''
];
// 注入费用数据 (如果启用)
if ($billingEnabled) {
$item['cost'] = $this->safeGetBillingInfo($account, $billingCycle);
}
$data[] = $item;
}
return [
'data' => $data,
'system_last_run' => $this->configManager->getLastRunTime()
];
}
public function refreshAccount($id)
{
if ($this->initError)
return false;
$targetAccount = $this->configManager->getAccountById($id);
if (!$targetAccount)
return false;
$currentTime = time();
$traffic = $this->safeGetTraffic($targetAccount);
$status = $this->safeGetInstanceStatus($targetAccount);
if ($traffic < 0) {
$traffic = $targetAccount['traffic_used'];
} else {
$this->db->addHourlyStat($targetAccount['id'], $traffic);
$this->db->addDailyStat($targetAccount['id'], $traffic);
}
$this->configManager->updateAccountStatus($id, $traffic, $status, $currentTime);
// 刷新账单数据:仅在启用费用监控 且 无有效缓存时调用 BSS API
$billingError = null;
$billingEnabled = $this->configManager->get('enable_billing', '0') === '1';
if ($billingEnabled) {
$billingCycle = date('Y-m');
// 余额:无有效缓存时重新获取
$balanceCache = $this->db->getBillingCache($targetAccount['id'], 'balance', '', 21600);
if (!$balanceCache) {
try {
$balance = $this->aliyunService->getAccountBalance(
$targetAccount['access_key_id'],
$targetAccount['access_key_secret'],
$targetAccount['site_type'] ?? 'china'
);
$this->db->setBillingCache($targetAccount['id'], 'balance', '', $balance);
} catch (\Exception $e) {
$billingError = '余额查询失败: ' . $e->getMessage();
}
}
// 实例账单:无有效缓存时重新获取
if (!empty($targetAccount['instance_id'])) {
$billCache = $this->db->getBillingCache($targetAccount['id'], 'instance_bill', $billingCycle, 21600);
if (!$billCache) {
try {
$bill = $this->aliyunService->getInstanceBill(
$targetAccount['access_key_id'],
$targetAccount['access_key_secret'],
$targetAccount['instance_id'],
$billingCycle,
$targetAccount['site_type'] ?? 'china'
);
$this->db->setBillingCache($targetAccount['id'], 'instance_bill', $billingCycle, $bill);
} catch (\Exception $e) {
$billingError = ($billingError ? $billingError . '; ' : '') . '账单查询失败: ' . $e->getMessage();
}
}
}
}
if ($billingError) {
$this->db->addLog('warning', "账单刷新异常 [{$targetAccount['access_key_id']}]: {$billingError}");
return ['success' => true, 'billing_error' => $billingError];
}
return true;
}
/**
* 公共接口:控制实例开关机
* @param int $id 账户ID
* @param string $action 'Start' 或 'Stop'
*/
public function controlInstance($id, $action)
{
// 1. 强制设置 JSON 响应头
header('Content-Type: application/json');
if ($this->initError) {
echo json_encode(['success' => false, 'message' => "系统未初始化"]);
exit;
}
// 2. 获取账户配置
$account = $this->configManager->getAccountById($id);
if (!$account) {
echo json_encode(['success' => false, 'message' => "账户配置未找到"]);
exit;
}
// 3. 获取当前实例状态 (从数据库读取最新状态,确保实时性)
$currentStatus = $account['instance_status'] ?? 'Unknown';
// --- 拦截逻辑 1: 状态冲突拦截 (Pending/Starting/Stopping) ---
// 防止在状态变更中重复发送指令
$transientStates = ['Pending', 'Starting', 'Stopping'];
if (in_array($currentStatus, $transientStates)) {
echo json_encode([
'success' => false,
'message' => "实例状态更新中 ({$currentStatus}),请稍后刷新页面查看最新状态,不要重复操作。"
]);
exit;
}
// --- 拦截逻辑 2: 保活模式拦截 ---
// 如果开启了“实例保活”,且用户尝试关机,则拒绝操作
$keepAlive = $this->configManager->get('keep_alive', '0') === '1';
if ($keepAlive && strtolower($action) === 'stop') {
$this->db->addLog('warning', "拒绝手动关机请求 [{$account['access_key_id']}]: 实例保活功能已开启");
echo json_encode([
'success' => false,
'message' => "操作被拒绝:当前开启了“实例保活”模式,不允许手动关机。"
]);
exit;
}
// 4. 获取关机模式配置 (仅 Stop 时有效)
$shutdownMode = $this->configManager->get('shutdown_mode', 'KeepCharging');
// 5. 调用内部安全方法执行操作
$result = $this->safeControlInstance($account, strtolower($action), $shutdownMode);
if ($result === true) {
$this->db->addLog('info', "手动控制实例 [{$account['access_key_id']}] 执行: {$action}");
echo json_encode(['success' => true, 'message' => '指令发送成功']);
} else {
echo json_encode(['success' => false, 'message' => "操作执行失败: " . $result]);
}
exit;
}
public function sendTestEmail($to)
{
return $this->notificationService->sendTestEmail($to);
}
public function sendTestTelegram($data)
{
return $this->notificationService->sendTestTelegram($data);
}
public function sendTestWebhook($data)
{
return $this->notificationService->sendTestWebhook($data);
}
private function logNotificationResult($result, $key)
{
if ($result === true) {
$this->db->addLog('info', "通知推送成功 [$key]");
} elseif ($result !== false && $result !== true) {
$this->db->addLog('warning', "通知推送异常/失败 [$key]: " . strip_tags($result));
}
}
private function safeGetTraffic($account)
{
try {
return $this->aliyunService->getTraffic($account['access_key_id'], $account['access_key_secret'], $account['region_id']);
} catch (ClientException $e) {
$code = $e->getErrorCode();
$this->db->addLog('error', "流量查询配置错误: " . ($code ?: "鉴权失败"));
return -1;
} catch (ServerException $e) {
$this->db->addLog('error', "流量查询失败: 阿里云接口超时");
return -1;
} catch (\Exception $e) {
if (strpos($e->getMessage(), 'cURL error') !== false) {
$this->db->addLog('error', "流量查询失败: 网络连接超时");
} else {
$this->db->addLog('error', "流量查询失败: 系统未知错误");
}
return -1;
}
}
private function safeGetInstanceStatus($account)
{
try {
return $this->aliyunService->getInstanceStatus($account);
} catch (\Exception $e) {
if (strpos($e->getMessage(), 'cURL error') !== false) {
} elseif ($e instanceof ClientException) {
$this->db->addLog('error', "实例状态查询配置错误: 鉴权失败");
} else {
}
return 'Unknown';
}
}
private function safeControlInstance($account, $action, $shutdownMode = 'KeepCharging')
{
try {
return $this->aliyunService->controlInstance($account, $action, $shutdownMode);
} catch (ClientException $e) {
$this->db->addLog('error', "实例操作失败 [{$action}]: 权限不足或配置错误");
return false;
} catch (ServerException $e) {
$this->db->addLog('error', "实例操作失败 [{$action}]: 阿里云服务无响应");
return false;
} catch (\Exception $e) {
$this->db->addLog('error', "实例操作失败 [{$action}]: 无法连接API");
return false;
}
}
private function isTimeInRange($current, $start, $end)
{
if (!$start || !$end)
return false;
if ($start < $end) {
return $current >= $start && $current < $end;
} else {
return $current >= $start || $current < $end;
}
}
private function getRegionName($regionId)
{
$regions = [
'cn-hongkong' => '中国香港',
'ap-southeast-1' => '新加坡',
'us-west-1' => '美国(硅谷)',
'us-east-1' => '美国(弗吉尼亚)',
'cn-hangzhou' => '华东1(杭州)',
'cn-shanghai' => '华东2(上海)',
'cn-qingdao' => '华北1(青岛)',
'cn-beijing' => '华北2(北京)',
'cn-zhangjiakou' => '华北3(张家口)',
'cn-huhehaote' => '华北5(呼和浩特)',
'cn-wulanchabu' => '华北6(乌兰察布)',
'cn-shenzhen' => '华南1(深圳)',
'cn-heyuan' => '华南2(河源)',
'cn-guangzhou' => '华南3(广州)',
'cn-chengdu' => '西南1(成都)',
'ap-northeast-1' => '日本(东京)',
];
return $regions[$regionId] ?? $regionId;
}
// ==================== 费用分析 ====================
/**
* 安全获取账户费用摘要信息 (带缓存)
* 用于实例卡片上显示
*/
private function safeGetBillingInfo($account, $billingCycle)
{
$costInfo = [
'enabled' => true,
'monthly_cost' => null,
'balance' => null,
'currency' => 'CNY',
'last_updated' => null,
'error' => null
];
// 1. 尝试读取余额缓存
$balanceCache = $this->db->getBillingCache($account['id'], 'balance', '', 21600);
if ($balanceCache) {
$costInfo['balance'] = $balanceCache['AvailableAmount'];
$costInfo['currency'] = $balanceCache['Currency'] ?? 'CNY';
} else {
try {
$balance = $this->aliyunService->getAccountBalance(
$account['access_key_id'],
$account['access_key_secret'],
$account['site_type'] ?? 'china'
);
$costInfo['balance'] = $balance['AvailableAmount'];
$costInfo['currency'] = $balance['Currency'] ?? 'CNY';
$this->db->setBillingCache($account['id'], 'balance', '', $balance);
} catch (\Exception $e) {
$costInfo['error'] = '余额查询失败';
}
}
// 2. 尝试读取实例账单缓存
if (!empty($account['instance_id'])) {
$billCache = $this->db->getBillingCache($account['id'], 'instance_bill', $billingCycle, 21600);
if ($billCache) {
$costInfo['monthly_cost'] = $billCache['TotalCost'];
} else {
try {
$bill = $this->aliyunService->getInstanceBill(
$account['access_key_id'],
$account['access_key_secret'],
$account['instance_id'],
$billingCycle,
$account['site_type'] ?? 'china'
);
$costInfo['monthly_cost'] = $bill['TotalCost'];
$this->db->setBillingCache($account['id'], 'instance_bill', $billingCycle, $bill);
} catch (\Exception $e) {
if ($costInfo['error']) {
$costInfo['error'] = 'BSS权限不足';
} else {
$costInfo['error'] = '账单查询失败';
}
}
}
}
$costInfo['last_updated'] = date('Y-m-d H:i:s');
return $costInfo;
}
public function renderTemplate()
{
if (!file_exists('template.html'))
return "File not found";
ob_start();
include 'template.html';
return ob_get_clean();
}
}