AutoCheckNodeTCP.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use Illuminate\Console\Command;
  5. use App\Components\ServerChan;
  6. use App\Http\Models\SsNode;
  7. use App\Http\Models\SsNodeInfo;
  8. use App\Mail\nodeCrashWarning;
  9. use Cache;
  10. use Mail;
  11. use Log;
  12. class AutoCheckNodeTCP extends Command
  13. {
  14. protected $signature = 'autoCheckNodeTCP';
  15. protected $description = '自动检测节点是否被TCP阻断';
  16. protected static $systemConfig;
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. self::$systemConfig = Helpers::systemConfig();
  21. }
  22. public function handle()
  23. {
  24. $jobStartTime = microtime(true);
  25. if (self::$systemConfig['is_tcp_check']) {
  26. if (!Cache::has('tcp_check_time')) {
  27. $this->checkNodes();
  28. } elseif (Cache::get('tcp_check_time') <= time()) {
  29. $this->checkNodes();
  30. } else {
  31. Log::info('下次节点TCP阻断检测时间:' . date('Y-m-d H:i:s', Cache::get('tcp_check_time')));
  32. }
  33. }
  34. $jobEndTime = microtime(true);
  35. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  36. Log::info('执行定时任务【' . $this->description . '】,耗时' . $jobUsedTime . '秒');
  37. }
  38. // 监测节点状态
  39. private function checkNodes()
  40. {
  41. $title = "节点异常警告";
  42. $nodeList = SsNode::query()->where('is_transit', 0)->where('is_nat', 0)->where('status', 1)->where('is_tcp_check', 1)->get();
  43. foreach ($nodeList as $node) {
  44. $tcpCheck = $this->tcpCheck($node->ip);
  45. if (false !== $tcpCheck) {
  46. switch ($tcpCheck) {
  47. case 1:
  48. $text = '服务器宕机';
  49. break;
  50. case 2:
  51. $text = '海外不通';
  52. break;
  53. case 3:
  54. $text = 'TCP阻断';
  55. break;
  56. case 0:
  57. default:
  58. $text = '正常';
  59. }
  60. // 异常才发通知消息
  61. if ($tcpCheck) {
  62. if (self::$systemConfig['tcp_check_warning_times']) {
  63. // 已通知次数
  64. $cacheKey = 'tcp_check_warning_times_' . $node->id;
  65. if (Cache::has($cacheKey)) {
  66. $times = Cache::get($cacheKey);
  67. } else {
  68. Cache::put($cacheKey, 1, 725); // 最多设置提醒12次,12*60=720分钟缓存时效,多5分钟防止异常
  69. $times = 1;
  70. }
  71. if ($times < self::$systemConfig['tcp_check_warning_times']) {
  72. Cache::increment($cacheKey);
  73. $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**", $node->name, $node->server);
  74. } elseif ($times >= self::$systemConfig['tcp_check_warning_times']) {
  75. Cache::forget($cacheKey);
  76. SsNode::query()->where('id', $node->id)->update(['status' => 0]);
  77. $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**,节点自动进入维护状态", $node->name, $node->server);
  78. }
  79. } else {
  80. $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**", $node->name, $node->server);
  81. }
  82. }
  83. Log::info("【TCP阻断检测】" . $node->name . ' - ' . $node->ip . ' - ' . $text);
  84. }
  85. }
  86. // 随机生成下次检测时间
  87. $nextCheckTime = time() + mt_rand(1800, 3600);
  88. Cache::put('tcp_check_time', $nextCheckTime, 60);
  89. }
  90. /**
  91. * 用ipcheck.need.sh进行TCP阻断检测
  92. *
  93. * @param string $ip 被检测的IP
  94. *
  95. * @return bool|int
  96. */
  97. private function tcpCheck($ip)
  98. {
  99. try {
  100. $url = 'https://ipcheck.need.sh/api_v2.php?ip=' . $ip;
  101. $ret = $this->curlRequest($url);
  102. $ret = json_decode($ret);
  103. if (!$ret || $ret->result != 'success') {
  104. Log::warning("【TCP阻断检测】检测" . $ip . "时,接口返回异常");
  105. return false;
  106. }
  107. } catch (\Exception $e) {
  108. Log::warning("【TCP阻断检测】检测" . $ip . "时,接口请求超时");
  109. return false;
  110. }
  111. if (!$ret->data->inside_gfw->tcp->alive && !$ret->data->outside_gfw->tcp->alive) {
  112. return 1; // 服务器宕机或者检测接口挂了
  113. } elseif ($ret->data->inside_gfw->tcp->alive && !$ret->data->outside_gfw->tcp->alive) {
  114. return 2; // 国外访问异常
  115. } elseif (!$ret->data->inside_gfw->tcp->alive && $ret->data->outside_gfw->tcp->alive) {
  116. return 3; // 被墙
  117. } else {
  118. return 0; // 正常
  119. }
  120. }
  121. /**
  122. * 通知管理员
  123. *
  124. * @param string $title 消息标题
  125. * @param string $content 消息内容
  126. * @param string $nodeName 节点名称
  127. * @param string $nodeServer 节点域名
  128. *
  129. * @throws \GuzzleHttp\Exception\GuzzleException
  130. */
  131. private function notifyMaster($title, $content, $nodeName, $nodeServer)
  132. {
  133. $this->notifyMasterByEmail($title, $content, $nodeName, $nodeServer);
  134. ServerChan::send($title, $content);
  135. }
  136. /**
  137. * 发邮件通知管理员
  138. *
  139. * @param string $title 消息标题
  140. * @param string $content 消息内容
  141. * @param string $nodeName 节点名称
  142. * @param string $nodeServer 节点域名
  143. */
  144. private function notifyMasterByEmail($title, $content, $nodeName, $nodeServer)
  145. {
  146. if (self::$systemConfig['crash_warning_email']) {
  147. $logId = Helpers::addEmailLog(self::$systemConfig['crash_warning_email'], $title, $content);
  148. Mail::to(self::$systemConfig['crash_warning_email'])->send(new nodeCrashWarning($logId, $nodeName, $nodeServer));
  149. }
  150. }
  151. /**
  152. * 发起一个CURL请求
  153. *
  154. * @param string $url 请求地址
  155. * @param array $data POST数据,留空则为GET
  156. *
  157. * @return mixed
  158. */
  159. private function curlRequest($url, $data = [])
  160. {
  161. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  162. $ch = curl_init();
  163. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  164. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  165. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  166. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  167. curl_setopt($ch, CURLOPT_URL, $url);
  168. // curl_setopt($ch, CURLOPT_HTTPHEADER, [
  169. // 'Accept: application/json', // 请求报头
  170. // 'Content-Type: application/json', // 实体报头
  171. // 'Content-Length: ' . strlen($data)
  172. // ]);
  173. // 如果data有数据,则用POST请求
  174. if ($data) {
  175. curl_setopt($ch, CURLOPT_POST, 1);
  176. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  177. }
  178. $result = curl_exec($ch);
  179. curl_close($ch);
  180. return $result;
  181. }
  182. }