From 401b977ec7800e486def7dd84e1bd2df649581e2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=85=94=E5=A7=AC=E6=A1=91?= <867057410@qq.com>
Date: Wed, 15 Jun 2022 22:05:24 +0800
Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E5=8C=96Ping=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 添加Ping Api;
- Ping 支持多IP;
---
app/Components/NetworkDetection.php | 43 ++++++++++++++++++-
app/Http/Controllers/Admin/NodeController.php | 39 +++++++++--------
2 files changed, 64 insertions(+), 18 deletions(-)
diff --git a/app/Components/NetworkDetection.php b/app/Components/NetworkDetection.php
index b89b5d2c..52a00ca8 100644
--- a/app/Components/NetworkDetection.php
+++ b/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 "
{$msg['ip']}
线路【{$msg['node']}】
最小值:{$msg['ping_time_min']}
平均值:{$msg['ping_time_avg']}
最大值:{$msg['ping_time_max']}";
+ }
+ }
+ Log::warning('【PING】检测'.$ip.'时,api.yum6.cn无结果');
+
+ // 发送错误
+ return false;
+ }
+
/**
* 通过众多API进行节点阻断检测.
*
diff --git a/app/Http/Controllers/Admin/NodeController.php b/app/Http/Controllers/Admin/NodeController.php
index b484a3a9..0a0c41a8 100644
--- a/app/Http/Controllers/Admin/NodeController.php
+++ b/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.'
';
+ }
+ }
+ if (isset($msg)) {
+ return Response::json(['status' => 'success', 'message' => $msg]);
+ }
}
return Response::json(['status' => 'fail', 'message' => 'Ping访问失败']);