1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Components;
- use Exception;
- use Log;
- class NetworkDetection
- {
-
- public static function networkCheck($ip, $type, $port = NULL)
- {
- $url = 'https://api.50network.com/china-firewall/check/ip/'.($type? 'icmp/' : ($port? 'tcp_port/' : 'tcp_ack/')).$ip.($port? '/'.$port : '');
- $checkName = $type? 'ICMP' : 'TCP';
- try{
- $ret = json_decode(Curl::send($url), TRUE);
- if(!$ret){
- Log::warning("【".$checkName."阻断检测】检测".$ip."时,接口返回异常访问链接:".$url);
- return FALSE;
- }elseif(!$ret['success']){
- if($ret['error'] == "execute timeout (3s)"){
- sleep(10);
- return self::networkCheck($ip, $type, $port);
- }else{
- Log::warning("【".$checkName."阻断检测】检测".$ip.($port? : '')."时,返回".json_encode($ret));
- }
- return FALSE;
- }
- }catch(Exception $e){
- Log::warning("【".$checkName."阻断检测】检测".$ip."时,接口请求超时".$e);
- return FALSE;
- }
- if($ret['firewall-enable'] && $ret['firewall-disable']){
- return "通讯正常";
- }elseif($ret['firewall-enable'] && !$ret['firewall-disable']){
- return "海外阻断";
- }elseif(!$ret['firewall-enable'] && $ret['firewall-disable']){
- return "国内阻断";
- }else{
- return "机器宕机";
- }
- }
-
- public static function ping($ip)
- {
- $url = 'https://api.iiwl.cc/api/ping.php?url='.$ip;
- try{
- $ret = json_decode(Curl::send($url), TRUE);
- if(!$ret){
- Log::warning("【PING】检测".$ip."时,接口返回异常访问链接:".$url);
- return FALSE;
- }elseif($ret['code'] != 1 || $ret['msg'] != "检测成功!"){
- Log::warning("【PING】检测".$ip."时,返回".json_encode($ret));
- return FALSE;
- }
- }catch(Exception $e){
- Log::warning("【Ping】检测".$ip."时,接口请求超时".$e);
- return FALSE;
- }
- return $ret['data'];
- }
- }
- ?>
|