NodeBlockedDetection.php 3.9 KB

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