NetworkDetection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 ($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. // 来源:https://www.idcoffer.com/ipcheck
  194. private function idcoffer(string $ip, bool $is_icmp, int $port = null)
  195. {
  196. $cn = "https://api.24kplus.com/ipcheck?host={$ip}&port={$port}";
  197. $us = "https://api.idcoffer.com/ipcheck?host={$ip}&port={$port}";
  198. $checkName = $is_icmp ? 'ping' : 'tcp';
  199. $response_cn = Http::timeout(15)->get($cn);
  200. $response_us = Http::timeout(15)->get($us);
  201. if ($response_cn->ok() && $response_us->ok()) {
  202. $cn = $response_cn->json();
  203. $us = $response_us->json();
  204. if (! $cn || ! $us) {
  205. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  206. return false;
  207. }
  208. if (! $cn['code'] || ! $us['code']) {
  209. Log::warning('【'.$checkName.'阻断检测】检测'.$ip.$port.'时,返回'.var_export($cn, true).var_export($us, true));
  210. return false;
  211. }
  212. if ($cn['data'][$checkName] && $us['data'][$checkName]) {
  213. return '通讯正常'; // 正常
  214. }
  215. if ($cn['data'][$checkName] && ! $us['data'][$checkName]) {
  216. return '海外阻断'; // 国外访问异常
  217. }
  218. if (! $cn['data'][$checkName] && $us['data'][$checkName]) {
  219. return '国内阻断'; // 被墙
  220. }
  221. return '断连'; // 服务器宕机
  222. }
  223. return false;
  224. }
  225. // 来源:https://ping.gd/
  226. private function gd(string $ip, bool $is_icmp, int $port = 443)
  227. {
  228. $url = "https://ping.gd/api/ip-test/{$ip}:".($port ?? 443);
  229. $checkName = $is_icmp ? 'ping_alive' : 'telnet_alive';
  230. $response = Http::timeout(15)->get($url);
  231. if ($response->ok()) {
  232. $message = $response->json();
  233. if (! $message) {
  234. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$url}");
  235. return false;
  236. }
  237. if ($message[0]['result'][$checkName] && $message[1]['result'][$checkName]) {
  238. return '通讯正常'; // 正常
  239. }
  240. if ($message[0]['result'][$checkName] && ! $message[1]['result'][$checkName]) {
  241. return '海外阻断'; // 国外访问异常
  242. }
  243. if (! $message[0]['result'][$checkName] && $message[1]['result'][$checkName]) {
  244. return '国内阻断'; // 被墙
  245. }
  246. return '断连'; // 服务器宕机
  247. }
  248. return false;
  249. }
  250. // 来源:https://ip112.cn/
  251. private function ip112(string $ip, bool $is_icmp, int $port = 443)
  252. {
  253. $cn = 'https://api.zhujiquanzi.com/ipcheck/ipcheck.php';
  254. $us = 'https://api.52bwg.com/ipcheck/ipcheck.php';
  255. $checkName = $is_icmp ? 'icmp' : 'tcp';
  256. $response_cn = Http::asForm()->post($cn, ['ip' => $ip, 'port' => $port]);
  257. $response_us = Http::asForm()->post($us, ['ip' => $ip, 'port' => $port]);
  258. if ($response_cn->ok() && $response_us->ok()) {
  259. $cn = $response_cn->json();
  260. $us = $response_us->json();
  261. if (! $cn || ! $us) {
  262. Log::warning("【{$checkName}阻断检测】检测{$ip}时,接口返回异常访问链接:{$cn} | {$us}");
  263. return false;
  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. if (! str_contains($cn[$checkName], 'green') && str_contains($us[$checkName], 'green')) {
  272. return '国内阻断'; // 被墙
  273. }
  274. return '断连'; // 服务器宕机
  275. }
  276. return false;
  277. }
  278. }