Quellcode durchsuchen

优化Geo更新操作

兔姬桑 vor 4 Jahren
Ursprung
Commit
77ec618f25

+ 19 - 12
app/Http/Controllers/Admin/NodeController.php

@@ -12,7 +12,6 @@ use App\Models\Level;
 use App\Models\Node;
 use App\Models\NodeCertificate;
 use App\Models\RuleGroup;
-use App\Services\NodeService;
 use Arr;
 use Exception;
 use Illuminate\Http\JsonResponse;
@@ -56,11 +55,11 @@ class NodeController extends Controller
     public function create()
     {
         return view('admin.node.info', [
-            'countries' => Country::orderBy('code')->get(),
-            'levels' => Level::orderBy('level')->get(),
+            'countries'  => Country::orderBy('code')->get(),
+            'levels'     => Level::orderBy('level')->get(),
             'ruleGroups' => RuleGroup::orderBy('id')->get(),
-            'labels' => Label::orderByDesc('sort')->orderBy('id')->get(),
-            'certs' => NodeCertificate::orderBy('id')->get(),
+            'labels'     => Label::orderByDesc('sort')->orderBy('id')->get(),
+            'certs'      => NodeCertificate::orderBy('id')->get(),
         ]);
     }
 
@@ -91,12 +90,12 @@ class NodeController extends Controller
     public function edit(Node $node)
     {
         return view('admin.node.info', [
-            'node' => $node,
-            'countries' => Country::orderBy('code')->get(),
-            'levels' => Level::orderBy('level')->get(),
+            'node'       => $node,
+            'countries'  => Country::orderBy('code')->get(),
+            'levels'     => Level::orderBy('level')->get(),
             'ruleGroups' => RuleGroup::orderBy('id')->get(),
-            'labels' => Label::orderByDesc('sort')->orderBy('id')->get(),
-            'certs' => NodeCertificate::orderBy('id')->get(),
+            'labels'     => Label::orderByDesc('sort')->orderBy('id')->get(),
+            'certs'      => NodeCertificate::orderBy('id')->get(),
         ]);
     }
 
@@ -158,7 +157,15 @@ class NodeController extends Controller
     // 刷新节点地理位置
     public function refreshGeo($id): JsonResponse
     {
-        if ((new NodeService())->getNodeGeo($id)) {
+        if ($id) {
+            $ret = Node::findOrFail($id)->refresh_geo();
+        } else {
+            foreach (Node::whereStatus(1)->get() as $node) {
+                $ret = $node->refresh_geo();
+            }
+        }
+
+        if ($ret) {
             return Response::json(['status' => 'success', 'message' => '获取地理位置更新成功!']);
         }
 
@@ -186,7 +193,7 @@ class NodeController extends Controller
     {
         if ($result = (new NetworkDetection)->ping($node->is_ddns ? $node->server : $node->ip)) {
             return Response::json([
-                'status' => 'success',
+                'status'  => 'success',
                 'message' => $result,
             ]);
         }

+ 38 - 22
app/Models/Node.php

@@ -2,6 +2,7 @@
 
 namespace App\Models;
 
+use App\Components\IP;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@@ -85,48 +86,63 @@ class Node extends Model
             ->get();
     }
 
+    public function refresh_geo()
+    {
+        $data = IP::IPSB($this->is_ddns ? gethostbyname($this->server) : $this->ip);
+
+        if ($data) {
+            self::withoutEvents(function () use ($data) {
+                $this->update(['geo' => $data['latitude'].','.$data['longitude']]);
+            });
+
+            return 1;
+        }
+
+        return 0;
+    }
+
     public function config(User $user)
     {
         $config = [
-            'id' => $this->id,
-            'name' => $this->name,
-            'host' => $this->is_relay ? $this->relay_server : ($this->server ?: $this->ip),
+            'id'    => $this->id,
+            'name'  => $this->name,
+            'host'  => $this->is_relay ? $this->relay_server : ($this->server ?: $this->ip),
             'group' => sysConfig('website_name'),
         ];
         switch ($this->type) {
             case 2:
                 $config = array_merge($config, [
-                    'type' => 'v2ray',
-                    'port' => $this->is_relay ? $this->relay_port : $this->v2_port,
-                    'uuid' => $user->vmess_id,
-                    'method' => $this->v2_method,
+                    'type'        => 'v2ray',
+                    'port'        => $this->is_relay ? $this->relay_port : $this->v2_port,
+                    'uuid'        => $user->vmess_id,
+                    'method'      => $this->v2_method,
                     'v2_alter_id' => $this->v2_alter_id,
-                    'v2_net' => $this->v2_net,
-                    'v2_type' => $this->v2_type,
-                    'v2_host' => $this->v2_host,
-                    'v2_path' => $this->v2_path,
-                    'v2_tls' => $this->v2_tls ? 'tls' : '',
-                    'udp' => $this->is_udp,
+                    'v2_net'      => $this->v2_net,
+                    'v2_type'     => $this->v2_type,
+                    'v2_host'     => $this->v2_host,
+                    'v2_path'     => $this->v2_path,
+                    'v2_tls'      => $this->v2_tls ? 'tls' : '',
+                    'udp'         => $this->is_udp,
                 ]);
                 break;
             case 3:
                 $config = array_merge($config, [
-                    'type' => 'trojan',
-                    'port' => $this->is_relay ? $this->relay_port : $this->v2_port,
+                    'type'   => 'trojan',
+                    'port'   => $this->is_relay ? $this->relay_port : $this->v2_port,
                     'passwd' => $user->passwd,
-                    'sni' => $this->is_relay ? $this->server : '',
-                    'udp' => $this->is_udp,
+                    'sni'    => $this->is_relay ? $this->server : '',
+                    'udp'    => $this->is_udp,
                 ]);
                 break;
             case 1:
             case 4:
                 $config = array_merge($config, [
-                    'type' => $this->compatible ? 'shadowsocks' : 'shadowsocksr',
-                    'method' => $this->method,
-                    'protocol' => $this->protocol,
-                    'obfs' => $this->obfs,
+                    'type'       => $this->compatible ? 'shadowsocks' : 'shadowsocksr',
+                    'method'     => $this->method,
+                    'protocol'   => $this->protocol,
+                    'obfs'       => $this->obfs,
                     'obfs_param' => $this->obfs_param,
-                    'udp' => $this->is_udp,
+                    'udp'        => $this->is_udp,
                 ]);
                 if ($this->single) {
                     //单端口使用中转的端口

+ 1 - 2
app/Observers/NodeObserver.php

@@ -5,7 +5,6 @@ namespace App\Observers;
 use App\Components\DDNS;
 use App\Jobs\VNet\reloadNode;
 use App\Models\Node;
-use App\Services\NodeService;
 use Arr;
 use Log;
 use Str;
@@ -14,7 +13,7 @@ class NodeObserver
 {
     public function saved(Node $node): void
     {
-        (new NodeService())->getNodeGeo($node->id);
+        $node->refresh_geo();
     }
 
     public function created(Node $node): void

+ 0 - 28
app/Services/NodeService.php

@@ -1,28 +0,0 @@
-<?php
-
-namespace App\Services;
-
-use App\Components\IP;
-use App\Models\Node;
-
-class NodeService
-{
-    public function getNodeGeo($id = false): int
-    {
-        if ($id) {
-            $nodes = Node::whereStatus(1)->whereId($id)->get();
-        } else {
-            $nodes = Node::whereStatus(1)->get();
-        }
-
-        $result = 0;
-        foreach ($nodes as $node) {
-            $data = IP::IPSB($node->is_ddns ? gethostbyname($node->server) : $node->ip);
-            if ($data && Node::whereId($node->id)->update(['geo' => $data['latitude'].','.$data['longitude']])) {
-                $result++;
-            }
-        }
-
-        return $result;
-    }
-}