NetworkDetection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. * @return bool
  12. */
  13. public function ping(string $ip)
  14. {
  15. $round = 0;
  16. // 依次尝试接口
  17. while (true) {
  18. switch ($round) {
  19. case 0:
  20. $ret = $this->oiowebPing($ip);
  21. break;
  22. default:
  23. return false;
  24. }
  25. if ($ret !== false) {
  26. return $ret;
  27. }
  28. $round++;
  29. }
  30. }
  31. private function oiowebPing(string $ip)
  32. {
  33. $msg = null;
  34. foreach ([1, 6, 14] as $line) {
  35. $url = "https://api.oioweb.cn/api/hostping.php?host={$ip}&node={$line}"; // https://api.iiwl.cc/api/ping.php?host=
  36. $response = Http::timeout(20)->get($url);
  37. // 发送成功
  38. if ($response->ok()) {
  39. $message = $response->json();
  40. if ($message && $message['code']) {
  41. $msg .= "{$message['node']}:{$message['data']['Time']}<br>";
  42. }
  43. } else {
  44. return false;
  45. }
  46. }
  47. if ($msg) {
  48. return $msg;
  49. }
  50. Log::warning('【PING】检测'.$ip.'时,api.oioweb.cn无结果');
  51. // 发送错误
  52. return false;
  53. }
  54. /**
  55. * 通过众多API进行节点阻断检测.
  56. *
  57. * @param string $ip 被检测的IP
  58. * @param bool $is_icmp TRUE 为ICMP,FALSE 为tcp
  59. * @param int|null $port 检测端口,默认为空
  60. * @return bool
  61. */
  62. public function networkCheck(string $ip, bool $is_icmp, int $port = null)
  63. {
  64. $round = 1;
  65. // 依次尝试接口
  66. while (true) {
  67. switch ($round) {
  68. case 0:
  69. $ret = $this->idcWiki($ip, $is_icmp, $port);
  70. break;
  71. case 1:
  72. $ret = $this->flyzy2005($ip, $is_icmp, $port);
  73. break;
  74. case 2:
  75. $ret = $this->vps234($ip, $is_icmp);
  76. break;
  77. case 3:
  78. $ret = $this->idcoffer($ip, $is_icmp, $port);
  79. break;
  80. case 4:
  81. $ret = $this->gd($ip, $is_icmp, $port);
  82. break;
  83. case 5:
  84. $ret = $this->ip112($ip, $is_icmp, $port);
  85. break;
  86. default:
  87. return false;
  88. }
  89. if ($ret !== false) {
  90. return $ret;
  91. }
  92. $round++;
  93. }
  94. }
  95. private function idcWiki(string $ip, bool $is_icmp, int $port = null)
  96. {
  97. if ($is_icmp) {
  98. $type_string = 'icmp/';
  99. $checkName = 'ICMP';
  100. } else {
  101. $type_string = 'tcp_ack/';
  102. $checkName = 'TCP';
  103. }
  104. if ($port) {
  105. $port = '/'.$port;
  106. $type_string = 'tcp_port/';
  107. }
  108. $url = "https://api.50network.com/china-firewall/check/ip/{$type_string}{$ip}{$port}";
  109. $response = Http::timeout(20)->get($url);
  110. if ($response->ok()) {
  111. $message = $response->json();
  112. if (! $message) {
  113. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,接口返回异常访问链接:'.$url);
  114. return false;
  115. }
  116. if (! $message['success']) {
  117. if (isset($message['error']) && $message['error'] === 'execute timeout (3s)') {
  118. return false;
  119. }
  120. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.$port.'时,返回'.var_export($message, true));
  121. return false;
  122. }
  123. if ($message['firewall-enable'] && $message['firewall-disable']) {
  124. return 1; // 正常
  125. }
  126. if ($message['firewall-enable'] && ! $message['firewall-disable']) {
  127. return 2; // 国外访问异常
  128. }
  129. if (! $message['firewall-enable'] && $message['firewall-disable']) {
  130. return 3; // 被墙
  131. }
  132. return 4; // 服务器宕机
  133. }
  134. return false;
  135. }
  136. private function flyzy2005(string $ip, bool $is_icmp, int $port = null)
  137. {
  138. $cn = "https://mini.flyzy2005.cn/ip_check.php?ip={$ip}&port={$port}";
  139. $us = "https://mini.flyzy2005.cn/ip_check_outside.php?ip={$ip}&port={$port}";
  140. $checkName = $is_icmp ? 'icmp' : 'tcp';
  141. $response_cn = Http::timeout(20)->get($cn);
  142. $response_us = Http::timeout(20)->get($us);
  143. if ($response_cn->ok() && $response_us->ok()) {
  144. $cn = $response_cn->json();
  145. $us = $response_us->json();
  146. if (! $cn || ! $us) {
  147. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  148. return false;
  149. }
  150. if ($cn[$checkName] === 'success' && $us['outside_'.$checkName] === 'success') {
  151. return 1; // 正常
  152. }
  153. if ($cn[$checkName] === 'success' && $us['outside_'.$checkName] !== 'success') {
  154. return 2; // 国外访问异常
  155. }
  156. if ($cn[$checkName] !== 'success' && $us['outside_'.$checkName] === 'success') {
  157. return 3; // 被墙
  158. }
  159. return 4; // 服务器宕机
  160. }
  161. return false;
  162. }
  163. private function vps234(string $ip, bool $is_icmp)
  164. {
  165. $url = 'https://www.vps234.com/ipcheck/getdata/';
  166. $checkName = $is_icmp ? 'ICMP' : 'TCP';
  167. $response = Http::timeout(20)->withoutVerifying()->asForm()->post($url, ['ip' => $ip]);
  168. if ($response->ok()) {
  169. $message = $response->json();
  170. if (! $message) {
  171. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,接口返回异常访问链接:'.$url);
  172. return false;
  173. }
  174. if (! $message['data']['success']) {
  175. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.'时,返回'.var_export($message, true));
  176. return false;
  177. }
  178. if ($message['data']['data']['inner'.$checkName] && $message['data']['data']['out'.$checkName]) {
  179. return 1; // 正常
  180. }
  181. if ($message['data']['data']['inner'.$checkName] && ! $message['data']['data']['out'.$checkName]) {
  182. return 2; // 国外访问异常
  183. }
  184. if (! $message['data']['data']['inner'.$checkName] && $message['data']['data']['out'.$checkName]) {
  185. return 3; // 被墙
  186. }
  187. return 4; // 服务器宕机
  188. }
  189. return false;
  190. }
  191. private function idcoffer(string $ip, bool $is_icmp, int $port = null)
  192. { // 来源:https://www.idcoffer.com/ipcheck
  193. $cn = "https://api.24kplus.com/ipcheck?host={$ip}&port={$port}";
  194. $us = "https://api.idcoffer.com/ipcheck?host={$ip}&port={$port}";
  195. $checkName = $is_icmp ? 'ping' : 'tcp';
  196. $response_cn = Http::timeout(20)->get($cn);
  197. $response_us = Http::timeout(20)->get($us);
  198. if ($response_cn->ok() && $response_us->ok()) {
  199. $cn = $response_cn->json();
  200. $us = $response_us->json();
  201. if (! $cn || ! $us) {
  202. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  203. return false;
  204. }
  205. if (! $cn['code'] || ! $us['code']) {
  206. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.$port.'时,返回'.var_export($cn, true).var_export($us, true));
  207. return false;
  208. }
  209. if ($cn['data'][$checkName] && $us['data'][$checkName]) {
  210. return 1; // 正常
  211. }
  212. if ($cn['data'][$checkName] && ! $us['data'][$checkName]) {
  213. return 2; // 国外访问异常
  214. }
  215. if (! $cn['data'][$checkName] && $us['data'][$checkName]) {
  216. return 3; // 被墙
  217. }
  218. return 4; // 服务器宕机
  219. }
  220. return false;
  221. }
  222. private function gd(string $ip, bool $is_icmp, int $port = 443)
  223. { // 来源:https://ping.gd/
  224. $url = "https://ping.gd/api/ip-test/{$ip}:".($port ?? 443);
  225. $checkName = $is_icmp ? 'ping_alive' : 'telnet_alive';
  226. $response = Http::timeout(20)->get($url);
  227. if ($response->ok()) {
  228. $message = $response->json();
  229. if (! $message) {
  230. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$url}");
  231. return false;
  232. }
  233. if ($message[0]['result'][$checkName] && $message[1]['result'][$checkName]) {
  234. return 1; // 正常
  235. }
  236. if ($message[0]['result'][$checkName] && ! $message[1]['result'][$checkName]) {
  237. return 2; // 国外访问异常
  238. }
  239. if (! $message[0]['result'][$checkName] && $message[1]['result'][$checkName]) {
  240. return 3; // 被墙
  241. }
  242. return 4; // 服务器宕机
  243. }
  244. return false;
  245. }
  246. private function ip112(string $ip, bool $is_icmp, int $port = 443)
  247. { // 来源:https://ip112.cn/
  248. $cn = 'https://api.zhujiquanzi.com/ipcheck/ipcheck.php';
  249. $us = 'https://api.52bwg.com/ipcheck/ipcheck.php';
  250. $checkName = $is_icmp ? 'icmp' : 'tcp';
  251. $response_cn = Http::asForm()->post($cn, ['ip' => $ip, 'port' => $port]);
  252. $response_us = Http::asForm()->post($us, ['ip' => $ip, 'port' => $port]);
  253. if ($response_cn->ok() && $response_us->ok()) {
  254. $cn = $response_cn->json();
  255. $us = $response_us->json();
  256. if (! $cn || ! $us) {
  257. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  258. return false;
  259. }
  260. if (str_contains($cn[$checkName], 'green') && str_contains($us[$checkName], 'green')) {
  261. return 1; // 正常
  262. }
  263. if (str_contains($cn[$checkName], 'green') && ! str_contains($us[$checkName], 'green')) {
  264. return 2; // 国外访问异常
  265. }
  266. if (! str_contains($cn[$checkName], 'green') && str_contains($us[$checkName], 'green')) {
  267. return 3; // 被墙
  268. }
  269. return 4; // 服务器宕机
  270. }
  271. return false;
  272. }
  273. }