NodeBlockedDetection.php 4.7 KB

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