NetworkDetection.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace App\Components;
  3. use Http;
  4. use Log;
  5. class NetworkDetection
  6. {
  7. /**
  8. * 用外部API进行Ping检测.
  9. *
  10. * @param string $ip 被检测的IP或者域名
  11. *
  12. * @return bool|array
  13. */
  14. public function ping(string $ip)
  15. {
  16. $round = 0;
  17. // 依次尝试接口
  18. while (true) {
  19. switch ($round) {
  20. case 0:
  21. $ret = $this->oiowebPing($ip);
  22. break;
  23. default:
  24. return false;
  25. }
  26. if ($ret !== false) {
  27. return $ret;
  28. }
  29. $round++;
  30. }
  31. }
  32. private function oiowebPing(string $ip)
  33. {
  34. $msg = null;
  35. foreach ([1, 6, 14] as $line) {
  36. $url = "https://api.oioweb.cn/api/hostping.php?host={$ip}&node={$line}"; // https://api.iiwl.cc/api/ping.php?host=
  37. $response = Http::timeout(15)->get($url);
  38. // 发送成功
  39. if ($response->ok()) {
  40. $message = $response->json();
  41. if ($message && $message['code']) {
  42. $msg .= "{$message['node']}:{$message['data']['Time']}<br>";
  43. }
  44. } else {
  45. return false;
  46. }
  47. }
  48. if ($msg) {
  49. return $msg;
  50. }
  51. Log::warning('【PING】检测'.$ip.'时,api.oioweb.cn无结果');
  52. // 发送错误
  53. return false;
  54. }
  55. /**
  56. * 通过众多API进行节点阻断检测.
  57. *
  58. * @param string $ip 被检测的IP
  59. * @param bool $is_icmp TRUE 为ICMP,FALSE 为tcp
  60. * @param int|null $port 检测端口,默认为空
  61. *
  62. * @return bool|string
  63. */
  64. public function networkCheck(string $ip, bool $is_icmp, int $port = null)
  65. {
  66. $round = 0;
  67. // 依次尝试接口
  68. while (true) {
  69. switch ($round) {
  70. case 0:
  71. $ret = $this->idcWiki($ip, $is_icmp, $port);
  72. break;
  73. case 1:
  74. $ret = $this->flyzy2005($ip, $is_icmp, $port);
  75. break;
  76. case 2:
  77. $ret = $this->vps234($ip, $is_icmp);
  78. break;
  79. case 3:
  80. $ret = $this->vpsaff($ip, $is_icmp, $port);
  81. break;
  82. default:
  83. return false;
  84. }
  85. if ($ret !== false) {
  86. return $ret;
  87. }
  88. $round++;
  89. }
  90. }
  91. private function idcWiki(string $ip, bool $is_icmp, int $port = null)
  92. {
  93. if ($is_icmp) {
  94. $type_string = 'icmp/';
  95. $checkName = 'ICMP';
  96. } else {
  97. $type_string = 'tcp_ack/';
  98. $checkName = 'TCP';
  99. }
  100. if ($port) {
  101. $port = '/'.$port;
  102. $type_string = 'tcp_port/';
  103. }
  104. $url = "https://api.50network.com/china-firewall/check/ip/{$type_string}{$ip}{$port}";
  105. $response = Http::timeout(15)->get($url);
  106. if ($response->ok()) {
  107. $message = $response->json();
  108. if (! $message) {
  109. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,接口返回异常访问链接:'.$url);
  110. return false;
  111. }
  112. if (! $message['success']) {
  113. if ($message['error'] && $message['error'] === 'execute timeout (3s)') {
  114. return false;
  115. }
  116. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.$port.'时,返回'.var_export($message, true));
  117. return false;
  118. }
  119. if ($message['firewall-enable'] && $message['firewall-disable']) {
  120. return '通讯正常'; // 正常
  121. }
  122. if ($message['firewall-enable'] && ! $message['firewall-disable']) {
  123. return '海外阻断'; // 国外访问异常
  124. }
  125. if (! $message['firewall-enable'] && $message['firewall-disable']) {
  126. return '国内阻断'; // 被墙
  127. }
  128. return '机器宕机'; // 服务器宕机
  129. }
  130. return false;
  131. }
  132. private function flyzy2005(string $ip, bool $is_icmp, int $port = null)
  133. {
  134. $cn = "https://mini.flyzy2005.cn/ip_check.php?ip={$ip}&port={$port}";
  135. $us = "https://mini.flyzy2005.cn/ip_check_outside.php?ip={$ip}&port={$port}";
  136. $checkName = $is_icmp ? 'icmp' : 'tcp';
  137. $response_cn = Http::timeout(15)->get($cn);
  138. $response_us = Http::timeout(15)->get($us);
  139. if ($response_cn->ok() && $response_us->ok()) {
  140. $cn = $response_cn->json();
  141. $us = $response_us->json();
  142. if (! $cn || ! $us) {
  143. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  144. return false;
  145. }
  146. if ($cn[$checkName] === 'success' && $us['outside_'.$checkName] === 'success') {
  147. return '通讯正常'; // 正常
  148. }
  149. if ($cn[$checkName] === 'success' && $us['outside_'.$checkName] !== 'success') {
  150. return '海外阻断'; // 国外访问异常
  151. }
  152. if ($cn[$checkName] !== 'success' && $us['outside_'.$checkName] === 'success') {
  153. return '国内阻断'; // 被墙
  154. }
  155. return '机器宕机'; // 服务器宕机
  156. }
  157. return false;
  158. }
  159. private function vps234(string $ip, bool $is_icmp)
  160. {
  161. $url = 'https://www.vps234.com/ipcheck/getdata/';
  162. $checkName = $is_icmp ? 'ICMP' : 'TCP';
  163. $response = Http::timeout(15)->withoutVerifying()->asForm()->post($url, ['ip' => $ip]);
  164. if ($response->ok()) {
  165. $message = $response->json();
  166. if (! $message) {
  167. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,接口返回异常访问链接:'.$url);
  168. return false;
  169. }
  170. if (! $message['data']['success']) {
  171. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,返回'.var_export($message, true));
  172. return false;
  173. }
  174. if ($message['data']['data']['inner'.$checkName] && $message['data']['data']['out'.$checkName]) {
  175. return '通讯正常'; // 正常
  176. }
  177. if ($message['data']['data']['inner'.$checkName] && ! $message['data']['data']['out'.$checkName]) {
  178. return '海外阻断'; // 国外访问异常
  179. }
  180. if (! $message['data']['data']['inner'.$checkName] && $message['data']['data']['out'.$checkName]) {
  181. return '国内阻断'; // 被墙
  182. }
  183. return '机器宕机'; // 服务器宕机
  184. }
  185. return false;
  186. }
  187. private function vpsaff(string $ip, bool $is_icmp, int $port = null)
  188. {
  189. $cn = "https://api.24kplus.com/ipcheck?host={$ip}&port={$port}";
  190. $us = "https://api.vpsaff.net/ipcheck?host={$ip}&port={$port}";
  191. $checkName = $is_icmp ? 'ping' : 'tcp';
  192. $response_cn = Http::timeout(15)->get($cn);
  193. $response_us = Http::timeout(15)->get($us);
  194. if ($response_cn->ok() && $response_us->ok()) {
  195. $cn = $response_cn->json();
  196. $us = $response_us->json();
  197. if (! $cn || ! $us) {
  198. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  199. return false;
  200. }
  201. if (! $cn['code'] || ! $us['code']) {
  202. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.$port.'时,返回'.var_export($cn, true).var_export($us, true));
  203. return false;
  204. }
  205. if ($cn['data'][$checkName] && $us['data'][$checkName]) {
  206. return '通讯正常'; // 正常
  207. }
  208. if ($cn['data'][$checkName] && ! $us['data'][$checkName]) {
  209. return '海外阻断'; // 国外访问异常
  210. }
  211. if (! $cn['data'][$checkName] && $us['data'][$checkName]) {
  212. return '国内阻断'; // 被墙
  213. }
  214. return '机器宕机'; // 服务器宕机
  215. }
  216. return false;
  217. }
  218. }