classify)){ $this->classify(); } if($type){ return $this->classify[$type]; }else{ return $this->classify; } } public function classify(){ if(!isset($this->ping)){ $this->ping(); } $classify = []; foreach($this->ping as $id=>$node){ $isp = $node['nodeinfo']->nodename; $isp = preg_replace('/\(.*?\)/', '', $isp); $isp=explode('-',$isp); if(in_array(end($isp),['电信','联通','移动','BGP'])){ $isp = '中国'.end($isp); }else{ $isp = $isp[0]; } if(!isset($classify[$isp])){ $classify[$isp]=[]; } array_push($classify[$isp],$node); } $this->classify=$classify; return $this; } public function node() { $nodeinfo = $this->callWebSocet('{"msg":"getnodelist"}'); $this->nodeinfo=$nodeinfo; return $this; } public function getNode(){ if(!isset($this->nodeinfo)){ $this->node(); } return $this->nodeinfo; } public function ping($host,$count) { if(!isset($this->nodeinfo)){ $this->node(); } $ping = $this->callWebSocet('{"msg":"ping","target":"'.$host.'","type":"ICMP","count":'.$count.'}'); foreach($this->nodeinfo as $node){ $ping[$node->nodeid]['nodeinfo']=$node; } $alldelay = 0; foreach($ping as $id=>$node){ if((!isset($node['nodeinfo'])) || (!isset($node['delay'])) || $node['delay']<=10){ unset($ping[$id]); }else{ $alldelay += $node['delay']; } } $this->ping=$ping; $this->delay=$alldelay/count($this->ping); return $this; } public function getPing(){ if(!isset($this->ping)){ $this->ping(); } return $this->ping; } public function getDelay() { if(!isset($this->delay)){ $this->ping(); } return $this->delay; } private function callWebSocet($msg){ $client = new Client($this->dest); // 发送消息 $client->send($msg); $response = []; // 接收消息 while (true) { try { $receive=$client->receive(); $receive = json_decode($receive); if($receive->msg="setnodes"&&isset($receive->nodes)){ $response = $receive->nodes; break; }elseif(isset($receive->delay)){ $response[$receive->nodeid]['delay'] = $receive->delay/1000; }else{ $response[$receive->nodeid]['ip'] = $receive->ip; $response[$receive->nodeid]['loc'] = $receive->loc; $response[$receive->nodeid]['asn'] = $receive->asn; $response[$receive->nodeid]['org'] = $receive->org; } } catch (\WebSocket\ConnectionException $e) { break; } } // 关闭连接 $client->close(); return $response; } }