NodeBlockedDetection.php 4.6 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. // 节点检测次数
  71. if ($info && $detectionCheckTimes) {
  72. // 已通知次数
  73. $cacheKey = 'detection_check_times'.$node->id;
  74. if (Cache::has($cacheKey)) {
  75. $times = Cache::get($cacheKey);
  76. } else {
  77. // 键将保留12小时,多10分钟防意外
  78. Cache::put($cacheKey, 1, 43800);
  79. $times = 1;
  80. }
  81. if ($times < $detectionCheckTimes) {
  82. Cache::increment($cacheKey);
  83. } else {
  84. Cache::forget($cacheKey);
  85. Node::find($node->id)->update(['status' => 0]);
  86. $additionalMessage .= "\r\n节点【{$node->name}】自动进入维护状态\r\n";
  87. }
  88. }
  89. }
  90. //只有在出现阻断线路时,才会发出警报
  91. if ($sendText) {
  92. $this->notifyMaster("节点阻断警告", "阻断日志: \r\n\r\n".$message.$additionalMessage);
  93. Log::info("阻断日志: \r\n".$message.$additionalMessage);
  94. }
  95. // 随机生成下次检测时间
  96. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700);
  97. }
  98. /**
  99. * 通知管理员
  100. *
  101. * @param string $title 消息标题
  102. * @param string $content 消息内容
  103. *
  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. }