NetworkDetection.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace App\Utils;
  3. use Exception;
  4. use Http;
  5. use Illuminate\Http\Client\PendingRequest;
  6. use Log;
  7. class NetworkDetection
  8. {
  9. private static PendingRequest $basicRequest;
  10. public function ping(string $ip): ?string
  11. { // 用外部API进行Ping检测. TODO: 无权威外部API,功能缺失
  12. $testers = ['oiowebPing', 'xiaoapiPing', 'yum6Ping'];
  13. self::$basicRequest = Http::timeout(20)->withOptions(['http_errors' => false])->withUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36');
  14. foreach ($testers as $tester) {
  15. try {
  16. $result = $this->callLatencyTester($tester, $ip);
  17. if ($result !== null) {
  18. return $result;
  19. }
  20. } catch (Exception $e) {
  21. Log::error("[$tester] 网络延迟测试报错: ".$e->getMessage());
  22. continue;
  23. }
  24. }
  25. return null;
  26. }
  27. private function callLatencyTester(string $tester, string $ip): ?array
  28. {
  29. return match ($tester) {
  30. 'oiowebPing' => $this->oiowebPing($ip),
  31. 'xiaoapiPing' => $this->xiaoapiPing($ip),
  32. 'yum6Ping' => $this->yum6Ping($ip),
  33. };
  34. }
  35. private function oiowebPing(string $ip)
  36. {
  37. $msg = null;
  38. foreach ([1, 6, 14] as $line) {
  39. $response = self::$basicRequest->get("https://api.oioweb.cn/api/hostping.php?host=$ip&node=$line"); // https://api.iiwl.cc/api/ping.php?host=
  40. // 发送成功
  41. if ($response->ok()) {
  42. $message = $response->json();
  43. if ($message && $message['code']) {
  44. $msg .= "{$message['node']}:{$message['data']['Time']}<br>";
  45. }
  46. } else {
  47. return false;
  48. }
  49. }
  50. if ($msg) {
  51. return $msg;
  52. }
  53. Log::warning('【PING】检测'.$ip.'时,api.oioweb.cn无结果');
  54. // 发送错误
  55. return false;
  56. }
  57. private function xiaoapiPing(string $ip)
  58. { // 开发依据 https://xiaoapi.cn/?action=doc&id=3
  59. $response = self::$basicRequest->get("https://xiaoapi.cn/API/sping.php?url=$ip");
  60. // 发送成功
  61. if ($response->ok()) {
  62. return $response->body();
  63. }
  64. Log::warning("【PING】检测{$ip}时,xiaoapi.cn无结果");
  65. // 发送错误
  66. return false;
  67. }
  68. private function yum6Ping(string $ip)
  69. { // 来源 https://api.yum6.cn/ping.php?host=api.yum6.cn
  70. $response = self::$basicRequest->get("https://api.yum6.cn/ping.php?host=$ip");
  71. // 发送成功
  72. if ($response->ok()) {
  73. $msg = $response->json();
  74. if ($msg && $msg['state'] === '1000') {
  75. return "<h4>{$msg['ip']}</h4>线路【{$msg['node']}】<br> 最小值:{$msg['ping_time_min']}<br> 平均值:{$msg['ping_time_avg']}<br> 最大值:{$msg['ping_time_max']}";
  76. }
  77. }
  78. Log::warning('【PING】检测'.$ip.'时,api.yum6.cn无结果');
  79. return false; // 发送错误
  80. }
  81. public function networkStatus(string $ip, int $port): ?array
  82. {
  83. $status = $this->networkCheck($ip, $port);
  84. if ($status) {
  85. $ret = [];
  86. foreach (['icmp', 'tcp'] as $protocol) {
  87. if ($status['in'][$protocol] && $status['out'][$protocol]) {
  88. $check = 1; // 正常
  89. }
  90. if ($status['in'][$protocol] && ! $status['out'][$protocol]) {
  91. $check = 2; // 国外访问异常
  92. }
  93. if (! $status['in'][$protocol] && $status['out'][$protocol]) {
  94. $check = 3; // 被墙
  95. }
  96. $ret[$protocol] = $check ?? 4; // 服务器宕机
  97. }
  98. return $ret;
  99. }
  100. return null;
  101. }
  102. private function networkCheck(string $ip, int $port): ?array
  103. { // 通过众多API进行节点阻断检测.
  104. $checkers = ['toolsdaquan', 'flyzy2005', 'idcoffer', 'ip112', 'upx8', 'vps234', 'rss', 'gd', 'vps1352'];
  105. self::$basicRequest = Http::timeout(15)->retry(2)->withOptions(['http_errors' => false])->withoutVerifying()->withUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36');
  106. foreach ($checkers as $checker) {
  107. try {
  108. $result = $this->callChecker($checker, $ip, $port);
  109. if ($result !== null) {
  110. return $result;
  111. }
  112. } catch (Exception $e) {
  113. Log::error("[$checker] 网络阻断测试报错: ".$e->getMessage());
  114. continue;
  115. }
  116. }
  117. return null;
  118. }
  119. private function callChecker(string $checker, string $ip, int $port): ?array
  120. {
  121. return match ($checker) {
  122. 'toolsdaquan' => $this->toolsdaquan($ip, $port),
  123. 'gd' => $this->gd($ip, $port),
  124. 'vps234' => $this->vps234($ip),
  125. 'flyzy2005' => $this->flyzy2005($ip, $port),
  126. 'idcoffer' => $this->idcoffer($ip, $port),
  127. 'ip112' => $this->ip112($ip, $port),
  128. 'upx8' => $this->upx8($ip, $port),
  129. 'vps1352' => $this->vps1352($ip, $port),
  130. 'rss' => $this->rss($ip, $port),
  131. };
  132. }
  133. private function toolsdaquan(string $ip, int $port): ?array
  134. { // 开发依据: https://www.toolsdaquan.com/ipcheck/
  135. $response_inner = self::$basicRequest->withHeaders(['Referer' => 'https://www.toolsdaquan.com/ipcheck/'])->get("https://www.toolsdaquan.com/toolapi/public/ipchecking/$ip/$port");
  136. $response_outer = self::$basicRequest->withHeaders(['Referer' => 'https://www.toolsdaquan.com/ipcheck/'])->get("https://www.toolsdaquan.com/toolapi/public/ipchecking2/$ip/$port");
  137. if ($response_inner->ok() && $response_outer->ok()) {
  138. return $this->common_detection($response_inner->json(), $response_outer->json(), $ip);
  139. }
  140. return null;
  141. }
  142. private function common_detection(array $inner, array $outer, string $ip): ?array
  143. {
  144. if (empty($inner) || empty($outer)) {
  145. Log::warning("【阻断检测】检测{$ip}时,接口返回异常");
  146. return null;
  147. }
  148. return [
  149. 'in' => [
  150. 'icmp' => $inner['icmp'] === 'success',
  151. 'tcp' => $inner['tcp'] === 'success',
  152. ],
  153. 'out' => [
  154. 'icmp' => $outer['outside_icmp'] === 'success',
  155. 'tcp' => $outer['outside_tcp'] === 'success',
  156. ],
  157. ];
  158. }
  159. private function gd(string $ip, int $port): ?array
  160. { // 开发依据: https://ping.gd/
  161. $response = self::$basicRequest->get("https://ping.gd/api/ip-test/$ip:$port");
  162. if ($response->ok()) {
  163. $data = $response->json();
  164. if ($data) {
  165. return [
  166. 'in' => [
  167. 'icmp' => $data[0]['result']['ping_alive'],
  168. 'tcp' => $data[0]['result']['telnet_alive'],
  169. ],
  170. 'out' => [
  171. 'icmp' => $data[1]['result']['ping_alive'],
  172. 'tcp' => $data[1]['result']['telnet_alive'],
  173. ],
  174. ];
  175. }
  176. }
  177. Log::warning("【阻断检测】检测{$ip}时,[ping.gd]接口返回异常");
  178. return null;
  179. }
  180. private function vps234(string $ip): ?array
  181. { // 开发依据: https://www.vps234.com/ipchecker/
  182. $response = self::$basicRequest->asForm()->post('https://www.vps234.com/ipcheck/getdata/', ['ip' => $ip]);
  183. if ($response->ok()) {
  184. $data = $response->json();
  185. if ($data) {
  186. if ($data['error'] === false && $data['data']['success']) {
  187. $result = $data['data']['data'];
  188. return [
  189. 'in' => [
  190. 'icmp' => $result['innerICMP'],
  191. 'tcp' => $result['innerTCP'],
  192. ],
  193. 'out' => [
  194. 'icmp' => $result['outICMP'],
  195. 'tcp' => $result['outTCP'],
  196. ],
  197. ];
  198. }
  199. Log::warning('【阻断检测】检测'.$ip.'时,[vps234]接口返回'.var_export($data, true));
  200. }
  201. Log::warning("【阻断检测】检测{$ip}时, [vps234]接口返回异常");
  202. }
  203. return null;
  204. }
  205. private function flyzy2005(string $ip, int $port): ?array
  206. { // 开发依据: https://www.flyzy2005.cn/tech/ip-check/
  207. $response_inner = self::$basicRequest->get("https://mini.flyzy2005.cn/ip_check.php?ip=$ip&port=$port");
  208. $response_outer = self::$basicRequest->get("https://mini.flyzy2005.cn/ip_check_outside.php?ip=$ip&port=$port");
  209. if ($response_inner->ok() && $response_outer->ok()) {
  210. return $this->common_detection($response_inner->json(), $response_outer->json(), $ip);
  211. }
  212. return null;
  213. }
  214. private function idcoffer(string $ip, int $port): ?array
  215. { // 开发依据: https://www.idcoffer.com/ipcheck
  216. $response_inner = self::$basicRequest->get("https://api.24kplus.com/ipcheck?host=$ip&port=$port");
  217. $response_outer = self::$basicRequest->get("https://api.idcoffer.com/ipcheck?host=$ip&port=$port");
  218. if ($response_inner->ok() && $response_outer->ok()) {
  219. $inner = $response_inner->json();
  220. $outer = $response_outer->json();
  221. if ($inner && $outer) {
  222. if ($inner['code'] && $outer['code']) {
  223. return [
  224. 'in' => [
  225. 'icmp' => $inner['data']['ping'],
  226. 'tcp' => $inner['data']['tcp'],
  227. ],
  228. 'out' => [
  229. 'icmp' => $outer['data']['ping'],
  230. 'tcp' => $outer['data']['tcp'],
  231. ],
  232. ];
  233. }
  234. Log::warning('【阻断检测】检测'.$ip.$port.'时,[idcoffer]接口返回'.var_export($inner, true).PHP_EOL.var_export($outer, true));
  235. }
  236. Log::warning("【阻断检测】检测{$ip}时,[idcoffer]接口返回异常");
  237. }
  238. return null;
  239. }
  240. private function ip112(string $ip, int $port = 443): ?array
  241. { // 开发依据: https://ip112.cn/
  242. $response_inner = self::$basicRequest->asForm()->post('https://api.ycwxgzs.com/ipcheck/index.php', ['ip' => $ip, 'port' => $port]);
  243. $response_outer = self::$basicRequest->asForm()->post('https://api.52bwg.com/ipcheck/ipcheck.php', ['ip' => $ip, 'port' => $port]);
  244. if ($response_inner->ok() && $response_outer->ok()) {
  245. $inner = $response_inner->json();
  246. $outer = $response_outer->json();
  247. if ($inner && $outer) {
  248. return [
  249. 'in' => [
  250. 'icmp' => str_contains($inner['icmp'], 'green'),
  251. 'tcp' => str_contains($inner['tcp'], 'green'),
  252. ],
  253. 'out' => [
  254. 'icmp' => str_contains($outer['icmp'], 'green'),
  255. 'tcp' => str_contains($outer['tcp'], 'green'),
  256. ],
  257. ];
  258. }
  259. }
  260. Log::warning("【阻断检测】检测{$ip}时,[ip112]接口返回异常");
  261. return null;
  262. }
  263. private function upx8(string $ip, int $port = 443): ?array
  264. { // 开发依据: https://blog.upx8.com/ipcha.html
  265. $response_inner = self::$basicRequest->asForm()->post('https://ip.upx8.com/check.php', ['ip' => $ip, 'port' => $port]);
  266. $response_outer = self::$basicRequest->asForm()->post('https://ip.7761.cf/check.php', ['ip' => $ip, 'port' => $port]);
  267. if ($response_inner->ok() && $response_outer->ok()) {
  268. $inner = $response_inner->json();
  269. $outer = $response_outer->json();
  270. if ($inner && $outer) {
  271. return [
  272. 'in' => [
  273. 'icmp' => $inner['icmp'] === '正常',
  274. 'tcp' => $inner['tcp'] === '正常',
  275. ],
  276. 'out' => [
  277. 'icmp' => $outer['icmp'] === '正常',
  278. 'tcp' => $outer['tcp'] === '正常',
  279. ],
  280. ];
  281. }
  282. }
  283. Log::warning("【阻断检测】检测{$ip}时,[upx8]接口返回异常");
  284. return null;
  285. }
  286. private function vps1352(string $ip, int $port): ?array
  287. { // 开发依据: https://www.51vps.info/ipcheck.html https://www.vps1352.com/ipcheck.html 有缺陷api,查不了海外做判断 备用
  288. $response = self::$basicRequest->asForm()->withHeaders(['Referer' => 'https://www.51vps.info'])->post('https://www.vps1352.com/check.php', ['ip' => $ip, 'port' => $port]);
  289. if ($response->ok()) {
  290. $data = $response->json();
  291. if ($data) {
  292. return [
  293. 'in' => [
  294. 'icmp' => $data['icmp'] === '开放',
  295. 'tcp' => $data['tcp'] === '开放',
  296. ],
  297. 'out' => [
  298. 'icmp' => true,
  299. 'tcp' => true,
  300. ],
  301. ];
  302. }
  303. }
  304. Log::warning("【阻断检测】检测{$ip}时, [vps1352.com]接口返回异常");
  305. return null;
  306. }
  307. private function rss(string $ip, int $port): ?array
  308. { // https://ip.rss.ink/index/check
  309. $client = self::$basicRequest->withHeaders(['X-Token' => '5AXfB1xVfuq5hxv4']);
  310. foreach (['in', 'out'] as $type) {
  311. foreach (['icmp', 'tcp'] as $protocol) {
  312. $response = $client->get('https://ip.rss.ink/netcheck/'.($type === 'in' ? 'cn' : 'global')."/api/check/$protocol?ip=$ip".($protocol === 'tcp' ? "&port=$port" : ''));
  313. if ($response->ok()) {
  314. $data = $response->json();
  315. $ret[$type][$protocol] = $data['msg'] === 'success';
  316. }
  317. }
  318. }
  319. if (! isset($ret)) {
  320. Log::warning("【阻断检测】检测{$ip}时, [rss]接口返回异常");
  321. }
  322. return $ret ?? null;
  323. }
  324. }