NetworkDetection.php 8.3 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 = 1;
  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. //china-firewall/check/ip/ICMP-1.1.1.1-53
  105. $url = "https://api.50network.com/china-firewall/check/ip/{$type_string}{$ip}{$port}";
  106. $response = Http::timeout(15)->get($url);
  107. if ($response->ok()) {
  108. $message = $response->json();
  109. if (! $message) {
  110. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,接口返回异常访问链接:'.$url);
  111. return false;
  112. }
  113. if (! $message['success']) {
  114. if ($message['error'] && $message['error'] === 'execute timeout (3s)') {
  115. return false;
  116. }
  117. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.$port.'时,返回'.var_export($message, true));
  118. return false;
  119. }
  120. if ($message['firewall-enable'] && $message['firewall-disable']) {
  121. return '通讯正常'; // 正常
  122. }
  123. if ($message['firewall-enable'] && ! $message['firewall-disable']) {
  124. return '海外阻断'; // 国外访问异常
  125. }
  126. if (! $message['firewall-enable'] && $message['firewall-disable']) {
  127. return '国内阻断'; // 被墙
  128. }
  129. return '机器宕机'; // 服务器宕机
  130. }
  131. return false;
  132. }
  133. private function flyzy2005(string $ip, bool $is_icmp, int $port = null)
  134. {
  135. $cn = "https://mini.flyzy2005.cn/ip_check.php?ip={$ip}&port={$port}";
  136. $us = "https://mini.flyzy2005.cn/ip_check_outside.php?ip={$ip}&port={$port}";
  137. $checkName = $is_icmp ? 'icmp' : 'tcp';
  138. $response_cn = Http::timeout(15)->get($cn);
  139. $response_us = Http::timeout(15)->get($us);
  140. if ($response_cn->ok() && $response_us->ok()) {
  141. $cn = $response_cn->json();
  142. $us = $response_us->json();
  143. if (! $cn || ! $us) {
  144. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  145. return false;
  146. }
  147. if ($cn[$checkName] === 'success' && $us['outside_'.$checkName] === 'success') {
  148. return '通讯正常'; // 正常
  149. }
  150. if ($cn[$checkName] === 'success' && $us['outside_'.$checkName] !== 'success') {
  151. return '海外阻断'; // 国外访问异常
  152. }
  153. if ($cn[$checkName] !== 'success' && $us['outside_'.$checkName] === 'success') {
  154. return '国内阻断'; // 被墙
  155. }
  156. return '机器宕机'; // 服务器宕机
  157. }
  158. return false;
  159. }
  160. private function vps234(string $ip, bool $is_icmp)
  161. {
  162. $url = 'https://www.vps234.com/ipcheck/getdata/';
  163. $checkName = $is_icmp ? 'ICMP' : 'TCP';
  164. $response = Http::timeout(15)->withoutVerifying()->asForm()->post($url, ['ip' => $ip]);
  165. if ($response->ok()) {
  166. $message = $response->json();
  167. if (! $message) {
  168. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,接口返回异常访问链接:'.$url);
  169. return false;
  170. }
  171. if (! $message['data']['success']) {
  172. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,返回'.var_export($message, true));
  173. return false;
  174. }
  175. if ($message['data']['data']['inner'.$checkName] && $message['data']['data']['out'.$checkName]) {
  176. return '通讯正常'; // 正常
  177. }
  178. if ($message['data']['data']['inner'.$checkName] && ! $message['data']['data']['out'.$checkName]) {
  179. return '海外阻断'; // 国外访问异常
  180. }
  181. if (! $message['data']['data']['inner'.$checkName] && $message['data']['data']['out'.$checkName]) {
  182. return '国内阻断'; // 被墙
  183. }
  184. return '机器宕机'; // 服务器宕机
  185. }
  186. return false;
  187. }
  188. private function vpsaff(string $ip, bool $is_icmp, int $port = null)
  189. {
  190. $cn = "https://api.24kplus.com/ipcheck?host={$ip}&port={$port}";
  191. $us = "https://api.vpsaff.net/ipcheck?host={$ip}&port={$port}";
  192. $checkName = $is_icmp ? 'ping' : 'tcp';
  193. $response_cn = Http::timeout(15)->get($cn);
  194. $response_us = Http::timeout(15)->get($us);
  195. if ($response_cn->ok() && $response_us->ok()) {
  196. $cn = $response_cn->json();
  197. $us = $response_us->json();
  198. if (! $cn || ! $us) {
  199. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  200. return false;
  201. }
  202. if (! $cn['code'] || ! $us['code']) {
  203. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.$port.'时,返回'.var_export($cn, true).var_export($us, true));
  204. return false;
  205. }
  206. if ($cn['data'][$checkName] && $us['data'][$checkName]) {
  207. return '通讯正常'; // 正常
  208. }
  209. if ($cn['data'][$checkName] && ! $us['data'][$checkName]) {
  210. return '海外阻断'; // 国外访问异常
  211. }
  212. if (! $cn['data'][$checkName] && $us['data'][$checkName]) {
  213. return '国内阻断'; // 被墙
  214. }
  215. return '机器宕机'; // 服务器宕机
  216. }
  217. return false;
  218. }
  219. }