NodeBlockedDetection.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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(
  26. '下次节点阻断检测时间:' . date(
  27. 'Y-m-d H:i:s',
  28. Cache::get('LastCheckTime')
  29. )
  30. );
  31. }
  32. }
  33. $jobEndTime = microtime(true);
  34. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  35. Log::info("---【{$this->description}】完成---,耗时 {$jobUsedTime} 秒");
  36. }
  37. // 监测节点状态
  38. private function checkNodes(): void
  39. {
  40. $detectionCheckTimes = sysConfig('detection_check_times');
  41. $sendText = false;
  42. $message = "| 线路 | 协议 | 状态 |\r\n| ------ | ------ | ------ |\r\n";
  43. $additionalMessage = '';
  44. foreach (
  45. Node::whereIsRelay(0)->whereStatus(1)->where(
  46. 'detection_type',
  47. '>',
  48. 0
  49. )->get() as $node
  50. ) {
  51. $info = false;
  52. if ($node->detection_type == 0) {
  53. continue;
  54. }
  55. // 使用DDNS的node先通过gethostbyname获取ipv4地址
  56. if ($node->is_ddns) {
  57. $ip = gethostbyname($node->server);
  58. if (strcmp($ip, $node->server) != 0) {
  59. $node->ip = $ip;
  60. } else {
  61. Log::warning(
  62. "【节点阻断检测】检测" . $node->server . "时,IP获取失败" . $ip . " | " . $node->server
  63. );
  64. $this->notifyMaster(
  65. "{$node->name}动态IP获取失败",
  66. "节点 {$node->name} : IP获取失败 "
  67. );
  68. }
  69. }
  70. if ($node->detection_type != 1) {
  71. $icmpCheck = NetworkDetection::networkCheck($node->ip, true);
  72. if ($icmpCheck != false && $icmpCheck !== "通讯正常") {
  73. $message .= "| " . $node->name . " | ICMP | " . $icmpCheck . " |\r\n";
  74. $sendText = true;
  75. $info = true;
  76. }
  77. }
  78. if ($node->detection_type != 2) {
  79. $tcpCheck = NetworkDetection::networkCheck(
  80. $node->ip,
  81. false,
  82. $node->single ? $node->port : null
  83. );
  84. if ($tcpCheck != false && $tcpCheck !== "通讯正常") {
  85. $message .= "| " . $node->name . " | TCP | " . $tcpCheck . " |\r\n";
  86. $sendText = true;
  87. $info = true;
  88. }
  89. }
  90. // 节点检测次数
  91. if ($info && $detectionCheckTimes) {
  92. // 已通知次数
  93. $cacheKey = 'detection_check_times' . $node->id;
  94. if (Cache::has($cacheKey)) {
  95. $times = Cache::get($cacheKey);
  96. } else {
  97. // 键将保留12小时,多10分钟防意外
  98. Cache::put($cacheKey, 1, 43800);
  99. $times = 1;
  100. }
  101. if ($times < $detectionCheckTimes) {
  102. Cache::increment($cacheKey);
  103. } else {
  104. Cache::forget($cacheKey);
  105. Node::find($node->id)->update(['status' => 0]);
  106. $additionalMessage .= "\r\n节点【{$node->name}】自动进入维护状态\r\n";
  107. }
  108. }
  109. }
  110. //只有在出现阻断线路时,才会发出警报
  111. if ($sendText) {
  112. $this->notifyMaster(
  113. "节点阻断警告",
  114. "阻断日志: \r\n\r\n" . $message . $additionalMessage
  115. );
  116. Log::info("阻断日志: \r\n" . $message . $additionalMessage);
  117. }
  118. // 随机生成下次检测时间
  119. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700);
  120. }
  121. /**
  122. * 通知管理员
  123. *
  124. * @param string $title 消息标题
  125. * @param string $content 消息内容
  126. *
  127. */
  128. private function notifyMaster(string $title, string $content): void
  129. {
  130. $result = PushNotification::send($title, $content);
  131. if ( ! $result && sysConfig('webmaster_email')) {
  132. $logId = Helpers::addNotificationLog(
  133. $title,
  134. $content,
  135. 1,
  136. sysConfig('webmaster_email')
  137. );
  138. Mail::to(sysConfig('webmaster_email'))->send(
  139. new nodeCrashWarning($logId)
  140. );
  141. }
  142. }
  143. }