NodeBlockedDetection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\NetworkDetection;
  4. use App\Models\Node;
  5. use App\Models\User;
  6. use App\Notifications\NodeBlocked;
  7. use Cache;
  8. use Illuminate\Console\Command;
  9. use Log;
  10. use Notification;
  11. class NodeBlockedDetection extends Command
  12. {
  13. protected $signature = 'nodeBlockedDetection';
  14. protected $description = '节点阻断检测';
  15. public function handle(): void
  16. {
  17. $jobStartTime = microtime(true);
  18. if (sysConfig('nodes_detection')) {
  19. if (! Cache::has('LastCheckTime')) {
  20. $this->checkNodes();
  21. } elseif (Cache::get('LastCheckTime') <= time()) {
  22. $this->checkNodes();
  23. } else {
  24. Log::info('下次节点阻断检测时间:'.date('Y-m-d H:i:s', Cache::get('LastCheckTime')));
  25. }
  26. }
  27. $jobEndTime = microtime(true);
  28. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  29. Log::info("---【{$this->description}】完成---,耗时 {$jobUsedTime} 秒");
  30. }
  31. // 监测节点状态
  32. private function checkNodes(): void
  33. {
  34. $detectionCheckTimes = sysConfig('detection_check_times');
  35. $sendText = false;
  36. $message = "| 线路 | 协议 | 状态 |\r\n| ------ | ------ | ------ |\r\n";
  37. $additionalMessage = '';
  38. foreach (Node::whereIsRelay(0)->whereStatus(1)->where('detection_type', '>', 0)->get() as $node) {
  39. $info = false;
  40. if ($node->detection_type === 0) {
  41. continue;
  42. }
  43. // 使用DDNS的node先通过gethostbyname获取ipv4地址
  44. if ($node->is_ddns) {
  45. $ip = gethostbyname($node->server);
  46. if (strcmp($ip, $node->server) !== 0) {
  47. $node->ip = $ip;
  48. } else {
  49. Log::warning('【节点阻断检测】检测'.$node->server.'时,IP获取失败'.$ip.' | '.$node->server);
  50. }
  51. }
  52. if ($node->detection_type !== 1) {
  53. $icmpCheck = (new NetworkDetection)->networkCheck($node->ip, true);
  54. if ($icmpCheck !== false && $icmpCheck !== '通讯正常') {
  55. $message .= '| '.$node->name.' | ICMP | '.$icmpCheck." |\r\n";
  56. $sendText = true;
  57. $info = true;
  58. }
  59. }
  60. if ($node->detection_type !== 2) {
  61. $tcpCheck = (new NetworkDetection)->networkCheck($node->ip, false, $node->single ? $node->port : 22);
  62. if ($tcpCheck !== false && $tcpCheck !== '通讯正常') {
  63. $message .= '| '.$node->name.' | TCP | '.$tcpCheck." |\r\n";
  64. $sendText = true;
  65. $info = true;
  66. }
  67. }
  68. sleep(5);
  69. // 节点检测次数
  70. if ($info && $detectionCheckTimes) {
  71. // 已通知次数
  72. $cacheKey = 'detection_check_times'.$node->id;
  73. if (Cache::has($cacheKey)) {
  74. $times = Cache::get($cacheKey);
  75. } else {
  76. // 键将保留12小时,多10分钟防意外
  77. Cache::put($cacheKey, 1, 43800);
  78. $times = 1;
  79. }
  80. if ($times < $detectionCheckTimes) {
  81. Cache::increment($cacheKey);
  82. } else {
  83. Cache::forget($cacheKey);
  84. $node->update(['status' => 0]);
  85. $additionalMessage .= "\r\n节点【{$node->name}】自动进入维护状态\r\n";
  86. }
  87. }
  88. }
  89. //只有在出现阻断线路时,才会发出警报
  90. if ($sendText) {
  91. Notification::send(User::permission('admin.node.edit,update')->orWhere(function ($query) {
  92. return $query->role('Super Admin');
  93. })->get(), new NodeBlocked($message.$additionalMessage));
  94. Log::info("阻断日志: \r\n".$message.$additionalMessage);
  95. }
  96. // 随机生成下次检测时间
  97. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700);
  98. }
  99. }