Przeglądaj źródła

强化Ping功能

- 添加Ping Api;
- Ping 支持多IP;
兔姬桑 3 lat temu
rodzic
commit
401b977ec7

+ 42 - 1
app/Components/NetworkDetection.php

@@ -15,13 +15,19 @@ class NetworkDetection
      */
     public function ping(string $ip)
     {
-        $round = 0;
+        $round = 1;
         // 依次尝试接口
         while (true) {
             switch ($round) {
                 case 0:
                     $ret = $this->oiowebPing($ip);
                     break;
+                case 1:
+                    $ret = $this->xiaoapiPing($ip);
+                    break;
+                case 2:
+                    $ret = $this->yum6Ping($ip);
+                    break;
                 default:
                     return false;
             }
@@ -59,6 +65,41 @@ class NetworkDetection
         return false;
     }
 
+    private function xiaoapiPing(string $ip)
+    { // 来源 https://xiaoapi.cn/?action=doc&id=3
+        $msg = null;
+
+        $url = "https://xiaoapi.cn/API/sping.php?url={$ip}";
+        $response = Http::timeout(20)->get($url);
+
+        // 发送成功
+        if ($response->ok()) {
+            return $response->body();
+        }
+        Log::warning('【PING】检测'.$ip.'时,xiaoapi.cn无结果');
+
+        // 发送错误
+        return false;
+    }
+
+    private function yum6Ping(string $ip)
+    { // 来源 https://api.yum6.cn/ping.php?host=api.yum6.cn
+        $url = "https://api.yum6.cn/ping.php?host={$ip}";
+        $response = Http::timeout(20)->get($url);
+
+        // 发送成功
+        if ($response->ok()) {
+            $msg = $response->json();
+            if ($msg && $msg['state'] === '1000') {
+                return "<h4>{$msg['ip']}</h4>线路【{$msg['node']}】<br> 最小值:{$msg['ping_time_min']}<br> 平均值:{$msg['ping_time_avg']}<br> 最大值:{$msg['ping_time_max']}";
+            }
+        }
+        Log::warning('【PING】检测'.$ip.'时,api.yum6.cn无结果');
+
+        // 发送错误
+        return false;
+    }
+
     /**
      * 通过众多API进行节点阻断检测.
      *

+ 22 - 17
app/Http/Controllers/Admin/NodeController.php

@@ -197,16 +197,7 @@ class NodeController extends Controller
         foreach ($node->ips() as $ip) {
             $icmp = (new NetworkDetection)->networkCheck($ip, true, $node->port ?? 22); // ICMP
             $tcp = (new NetworkDetection)->networkCheck($ip, false, $node->port ?? 22); // TCP
-            if ($icmp) {
-                $data[$ip][0] = config('common.network_status')[$icmp];
-            } else {
-                $data[$ip][0] = ' ';
-            }
-            if ($tcp) {
-                $data[$ip][1] = config('common.network_status')[$tcp];
-            } else {
-                $data[$ip][1] = ' ';
-            }
+            $data[$ip] = [$icmp ? config('common.network_status')[$icmp] : ' ', $tcp ? config('common.network_status')[$tcp] : ' '];
         }
 
         return Response::json(['status' => 'success', 'title' => '['.$node->name.']阻断信息', 'message' => $data ?? []]);
@@ -215,15 +206,19 @@ class NodeController extends Controller
     // 刷新节点地理位置
     public function refreshGeo($id): JsonResponse
     {
+        $ret = false;
         if ($id) {
             $ret = Node::findOrFail($id)->refresh_geo();
         } else {
             foreach (Node::whereStatus(1)->get() as $node) {
-                $ret = $node->refresh_geo();
+                $result = $node->refresh_geo();
+                if ($result && ! $ret) {
+                    $ret = true;
+                }
             }
         }
 
-        if ($ret) {
+        if (! empty($ret)) {
             return Response::json(['status' => 'success', 'message' => '获取地理位置更新成功!']);
         }
 
@@ -249,11 +244,21 @@ class NodeController extends Controller
     // Ping节点延迟
     public function pingNode(Node $node): JsonResponse
     {
-        if ($result = (new NetworkDetection)->ping($node->is_ddns ? $node->server : $node->ip)) {
-            return Response::json([
-                'status'  => 'success',
-                'message' => $result,
-            ]);
+        if ($node->is_ddns) {
+            if ($result = (new NetworkDetection)->ping($node->server)) {
+                return Response::json(['status' => 'success', 'message' => $result]);
+            }
+        } else {
+            $msg = null;
+            foreach ($node->ips() as $ip) {
+                $ret = (new NetworkDetection)->ping($ip);
+                if ($ret !== false) {
+                    $msg .= $ret.' <hr>';
+                }
+            }
+            if (isset($msg)) {
+                return Response::json(['status' => 'success', 'message' => $msg]);
+            }
         }
 
         return Response::json(['status' => 'fail', 'message' => 'Ping访问失败']);