NetworkDetection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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->idcoffer($ip, $is_icmp, $port);
  81. break;
  82. case 4:
  83. $ret = $this->gd($ip, $is_icmp, $port);
  84. break;
  85. case 5:
  86. $ret = $this->ip112($ip, $is_icmp, $port);
  87. break;
  88. default:
  89. return false;
  90. }
  91. if ($ret !== false) {
  92. return $ret;
  93. }
  94. $round++;
  95. }
  96. }
  97. private function idcWiki(string $ip, bool $is_icmp, int $port = null)
  98. {
  99. if ($is_icmp) {
  100. $type_string = 'icmp/';
  101. $checkName = 'ICMP';
  102. } else {
  103. $type_string = 'tcp_ack/';
  104. $checkName = 'TCP';
  105. }
  106. if ($port) {
  107. $port = '/'.$port;
  108. $type_string = 'tcp_port/';
  109. }
  110. $url = "https://api.50network.com/china-firewall/check/ip/{$type_string}{$ip}{$port}";
  111. $response = Http::timeout(15)->get($url);
  112. if ($response->ok()) {
  113. $message = $response->json();
  114. if (! $message) {
  115. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,接口返回异常访问链接:'.$url);
  116. return false;
  117. }
  118. if (! $message['success']) {
  119. if (isset($message['error']) && $message['error'] === 'execute timeout (3s)') {
  120. return false;
  121. }
  122. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.$port.'时,返回'.var_export($message, true));
  123. return false;
  124. }
  125. if ($message['firewall-enable'] && $message['firewall-disable']) {
  126. return '通讯正常'; // 正常
  127. }
  128. if ($message['firewall-enable'] && ! $message['firewall-disable']) {
  129. return '海外阻断'; // 国外访问异常
  130. }
  131. if (! $message['firewall-enable'] && $message['firewall-disable']) {
  132. return '国内阻断'; // 被墙
  133. }
  134. return '断连'; // 服务器宕机
  135. }
  136. return false;
  137. }
  138. private function flyzy2005(string $ip, bool $is_icmp, int $port = null)
  139. {
  140. $cn = "https://mini.flyzy2005.cn/ip_check.php?ip={$ip}&port={$port}";
  141. $us = "https://mini.flyzy2005.cn/ip_check_outside.php?ip={$ip}&port={$port}";
  142. $checkName = $is_icmp ? 'icmp' : 'tcp';
  143. $response_cn = Http::timeout(15)->get($cn);
  144. $response_us = Http::timeout(15)->get($us);
  145. if ($response_cn->ok() && $response_us->ok()) {
  146. $cn = $response_cn->json();
  147. $us = $response_us->json();
  148. if (! $cn || ! $us) {
  149. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  150. return false;
  151. }
  152. if ($cn[$checkName] === 'success' && $us['outside_'.$checkName] === 'success') {
  153. return '通讯正常'; // 正常
  154. }
  155. if ($cn[$checkName] === 'success' && $us['outside_'.$checkName] !== 'success') {
  156. return '海外阻断'; // 国外访问异常
  157. }
  158. if ($cn[$checkName] !== 'success' && $us['outside_'.$checkName] === 'success') {
  159. return '国内阻断'; // 被墙
  160. }
  161. return '断连'; // 服务器宕机
  162. }
  163. return false;
  164. }
  165. private function vps234(string $ip, bool $is_icmp)
  166. {
  167. $url = 'https://www.vps234.com/ipcheck/getdata/';
  168. $checkName = $is_icmp ? 'ICMP' : 'TCP';
  169. $response = Http::timeout(15)->withoutVerifying()->asForm()->post($url, ['ip' => $ip]);
  170. if ($response->ok()) {
  171. $message = $response->json();
  172. if (! $message) {
  173. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,接口返回异常访问链接:'.$url);
  174. return false;
  175. }
  176. if (! $message['data']['success']) {
  177. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,返回'.var_export($message, true));
  178. return false;
  179. }
  180. if ($message['data']['data']['inner'.$checkName] && $message['data']['data']['out'.$checkName]) {
  181. return '通讯正常'; // 正常
  182. }
  183. if ($message['data']['data']['inner'.$checkName] && ! $message['data']['data']['out'.$checkName]) {
  184. return '海外阻断'; // 国外访问异常
  185. }
  186. if (! $message['data']['data']['inner'.$checkName] && $message['data']['data']['out'.$checkName]) {
  187. return '国内阻断'; // 被墙
  188. }
  189. return '断连'; // 服务器宕机
  190. }
  191. return false;
  192. }
  193. private function idcoffer(string $ip, bool $is_icmp, int $port = null)
  194. { // 来源:https://www.idcoffer.com/ipcheck
  195. $cn = "https://api.24kplus.com/ipcheck?host={$ip}&port={$port}";
  196. $us = "https://api.idcoffer.com/ipcheck?host={$ip}&port={$port}";
  197. $checkName = $is_icmp ? 'ping' : 'tcp';
  198. $response_cn = Http::timeout(15)->get($cn);
  199. $response_us = Http::timeout(15)->get($us);
  200. if ($response_cn->ok() && $response_us->ok()) {
  201. $cn = $response_cn->json();
  202. $us = $response_us->json();
  203. if (! $cn || ! $us) {
  204. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  205. return false;
  206. }
  207. if (! $cn['code'] || ! $us['code']) {
  208. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.$port.'时,返回'.var_export($cn, true).var_export($us, true));
  209. return false;
  210. }
  211. if ($cn['data'][$checkName] && $us['data'][$checkName]) {
  212. return '通讯正常'; // 正常
  213. }
  214. if ($cn['data'][$checkName] && ! $us['data'][$checkName]) {
  215. return '海外阻断'; // 国外访问异常
  216. }
  217. if (! $cn['data'][$checkName] && $us['data'][$checkName]) {
  218. return '国内阻断'; // 被墙
  219. }
  220. return '断连'; // 服务器宕机
  221. }
  222. return false;
  223. }
  224. private function gd(string $ip, bool $is_icmp, int $port = 443)
  225. { // 来源:https://ping.gd/
  226. $url = "https://ping.gd/api/ip-test/{$ip}:".($port ?? 443);
  227. $checkName = $is_icmp ? 'ping_alive' : 'telnet_alive';
  228. $response = Http::timeout(15)->get($url);
  229. if ($response->ok()) {
  230. $message = $response->json();
  231. if (! $message) {
  232. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$url}");
  233. return false;
  234. }
  235. if ($message[0]['result'][$checkName] && $message[1]['result'][$checkName]) {
  236. return '通讯正常'; // 正常
  237. }
  238. if ($message[0]['result'][$checkName] && ! $message[1]['result'][$checkName]) {
  239. return '海外阻断'; // 国外访问异常
  240. }
  241. if (! $message[0]['result'][$checkName] && $message[1]['result'][$checkName]) {
  242. return '国内阻断'; // 被墙
  243. }
  244. return '断连'; // 服务器宕机
  245. }
  246. return false;
  247. }
  248. private function ip112(string $ip, bool $is_icmp, int $port = 443)
  249. { // 来源:https://ip112.cn/
  250. $cn = 'https://api.zhujiquanzi.com/ipcheck/ipcheck.php';
  251. $us = 'https://api.52bwg.com/ipcheck/ipcheck.php';
  252. $checkName = $is_icmp ? 'icmp' : 'tcp';
  253. $response_cn = Http::asForm()->post($cn, ['ip' => $ip, 'port' => $port]);
  254. $response_us = Http::asForm()->post($us, ['ip' => $ip, 'port' => $port]);
  255. if ($response_cn->ok() && $response_us->ok()) {
  256. $cn = $response_cn->json();
  257. $us = $response_us->json();
  258. if (! $cn || ! $us) {
  259. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  260. return false;
  261. }
  262. if (str_contains($cn[$checkName], 'green') && str_contains($us[$checkName], 'green')) {
  263. return '通讯正常'; // 正常
  264. }
  265. if (str_contains($cn[$checkName], 'green') && ! str_contains($us[$checkName], 'green')) {
  266. return '海外阻断'; // 国外访问异常
  267. }
  268. if (! str_contains($cn[$checkName], 'green') && str_contains($us[$checkName], 'green')) {
  269. return '国内阻断'; // 被墙
  270. }
  271. return '断连'; // 服务器宕机
  272. }
  273. return false;
  274. }
  275. }