Bladeren bron

更新对应节点 array 获取

GeekQuerxy 5 jaren geleden
bovenliggende
commit
ca40dc0e49

+ 3 - 3
resources/views/material/user/node.tpl

@@ -12,7 +12,7 @@
 </script>
 
 {function displayV2rayNode node=null}
-    {$v2server=URL::getV2Url($user, $node['raw_node'], true)}
+    {$v2server = $node['raw_node']->getV2RayItem($user)}
     <p>节点地址:<span class="card-tag tag-blue">{$v2server['add']}</span></p>
     <p>节点端口:<span class="card-tag tag-volcano">{$v2server['port']}</span></p>
     <p>AlterId:<span class="card-tag tag-purple">{$v2server['aid']}</span></p>
@@ -37,7 +37,7 @@
 {/function}
 
 {function displaySort13Node node=null}
-    {$sort13Node = URL::getV2RayPluginItem($user, $node['raw_node'], false)}
+    {$sort13Node = $node['raw_node']->getV2RayPluginItem($user)}
     {if $sort13Node === null}
         <p>您的加密方式非 AEAD 系列,无法使用此节点.</p>
     {else}
@@ -51,7 +51,7 @@
 {/function}
 
 {function displaySort14Node node=null}
-    {$sort14Node = URL::getTrojanItem($user, $node['raw_node'], false)}
+    {$sort14Node = $node['raw_node']->getTrojanItem($user)}
     <p>节点地址:<span class="card-tag tag-blue">{$sort14Node['address']}</span></p>
     <p>节点端口:<span class="card-tag tag-volcano">{$sort14Node['port']}</span></p>
     <p>连接密码:<span class="card-tag tag-geekblue">{$sort14Node['passwd']}</span></p>

+ 2 - 2
resources/views/material/user/nodeinfo.tpl

@@ -48,7 +48,7 @@
                                         </nav>
                                         <div class="tab-pane fade {if $ssr_prefer}active in{/if}" id="ssr_info">
                                             {if URL::SSRCanConnect($user, $mu)}
-                                                {$ssr_item = URL::getItem($user, $node, $mu, $relay_rule_id, 0)}
+                                                {$ssr_item = $node->getItem($user, $mu, $relay_rule_id, 0)}
                                                     <p>服务器地址:{$ssr_item['address']}<br>
                                                         服务器端口:{$ssr_item['port']}<br>
                                                         加密方式:{$ssr_item['method']}<br>
@@ -65,7 +65,7 @@
                                         </div>
                                         <div class="tab-pane fade {if !$ssr_prefer}active in{/if}" id="ss_info">
                                             {if URL::SSCanConnect($user, $mu)}
-                                                {$ss_item = URL::getItem($user, $node, $mu, $relay_rule_id, 1)}
+                                                {$ss_item = $node->getItem($user, $mu, $relay_rule_id, 1)}
                                                     <p>服务器地址:{$ss_item['address']}<br>
                                                         服务器端口:{$ss_item['port']}<br>
                                                         加密方式:{$ss_item['method']}<br>

+ 4 - 4
src/Controllers/VueController.php

@@ -496,14 +496,14 @@ class VueController extends BaseController
         $mu = $request->getQueryParam('ismu');
         $relay_rule_id = $request->getQueryParam('relay_rule');
         $node = Node::find($id);
-        
+
 
         if ($node == null) {
             return $response->withJson([null]);
         }
 
-        $ssr_item = URL::getItem($user, $node, $mu, $relay_rule_id, 0);
-        $ss_item = URL::getItem($user, $node, $mu, $relay_rule_id, 1);
+        $ssr_item = $node->getItem($user, $mu, $relay_rule_id, 0);
+        $ss_item = $node->getItem($user, $mu, $relay_rule_id, 1);
 
         switch ($node->sort) {
             case 0:
@@ -707,7 +707,7 @@ class VueController extends BaseController
         $res['port_price_specify'] = $_ENV['port_price_specify'];
         $res['min_port'] = $_ENV['min_port'];
         $res['max_port'] = $_ENV['max_port'];
-        
+
         return $response->withJson($res);
     }
 }

+ 161 - 0
src/Models/Node.php

@@ -209,4 +209,165 @@ class Node extends Model
     {
         return Tools::OutPort($this->attributes['server'], $this->attributes['name'], $port)['port'];
     }
+
+    /**
+     * 获取 SS/SSR 节点
+     *
+     * @param User $user
+     * @param int  $mu_port
+     * @param int  $relay_rule_id
+     * @param int  $is_ss
+     * @param bool $emoji
+     */
+    public function getItem(User $user, int $mu_port = 0, int $relay_rule_id = 0, int $is_ss = 0, bool $emoji = false):? array
+    {
+        $relay_rule = Relay::where('id', $relay_rule_id)
+            ->where(
+                static function ($query) use ($user) {
+                    $query->Where('user_id', '=', $user->id)
+                        ->orWhere('user_id', '=', 0);
+                }
+            )
+            ->orderBy('priority', 'DESC')
+            ->orderBy('id')
+            ->first();
+        $node_name = $this->name;
+        if ($relay_rule != null) {
+            $node_name .= ' - ' . $relay_rule->dist_node()->name;
+        }
+        if ($mu_port != 0) {
+            $mu_user = User::where('port', '=', $mu_port)->where('is_multi_user', '<>', 0)->first();
+            if ($mu_user == null) {
+                return null;
+            }
+            $mu_user->obfs_param = $user->getMuMd5();
+            $mu_user->protocol_param = $user->id . ':' . $user->passwd;
+            $user = $mu_user;
+            $node_name .= ($_ENV['disable_sub_mu_port'] ? '' : ' - ' . $mu_port . ' 单端口');
+        }
+        if ($is_ss) {
+            if (!URL::SSCanConnect($user)) {
+                return null;
+            }
+            $user = URL::getSSConnectInfo($user);
+            $return_array['type'] = 'ss';
+        } else {
+            if (!URL::SSRCanConnect($user)) {
+                return null;
+            }
+            $user = URL::getSSRConnectInfo($user);
+            $return_array['type'] = 'ssr';
+        }
+        $return_array['address']        = $this->getServer();
+        $return_array['port']           = $user->port;
+        $return_array['protocol']       = $user->protocol;
+        $return_array['protocol_param'] = $user->protocol_param;
+        $return_array['obfs']           = $user->obfs;
+        $return_array['obfs_param']     = $user->obfs_param;
+        if ($mu_port != 0 && strpos($this->server, ';') !== false) {
+            $node_tmp             = Tools::OutPort($this->server, $this->name, $mu_port);
+            $return_array['port'] = $node_tmp['port'];
+            $node_name            = $node_tmp['name'];
+        }
+        $return_array['passwd'] = $user->passwd;
+        $return_array['method'] = $user->method;
+        $return_array['remark'] = ($emoji ? Tools::addEmoji($node_name) : $node_name);
+        $return_array['class']  = $this->node_class;
+        $return_array['group']  = $_ENV['appName'];
+        $return_array['ratio']  = ($relay_rule != null ? $this->traffic_rate + $relay_rule->dist_node()->traffic_rate : $this->traffic_rate);
+
+        return $return_array;
+    }
+
+    /**
+     * 获取 V2Ray 节点
+     *
+     * @param User $user
+     * @param int  $mu_port
+     * @param int  $relay_rule_id
+     * @param int  $is_ss
+     * @param bool $emoji
+     */
+    public function getV2RayItem(User $user, int $mu_port = 0, int $relay_rule_id = 0, int $is_ss = 0, bool $emoji = false): array
+    {
+        $item           = Tools::v2Array($this->server);
+        $item['type']   = 'vmess';
+        $item['remark'] = ($emoji ? Tools::addEmoji($this->name) : $this->name);
+        $item['id']     = $user->getUuid();
+        $item['class']  = $this->node_class;
+        return $item;
+    }
+
+    /**
+     * 获取 V2RayPlugin | obfs 节点
+     *
+     * @param User $user 用户
+     * @param int  $mu_port
+     * @param int  $relay_rule_id
+     * @param int  $is_ss
+     * @param bool $emoji
+     *
+     * @return array|null
+     */
+    public function getV2RayPluginItem(User $user, int $mu_port = 0, int $relay_rule_id = 0, int $is_ss = 0, bool $emoji = false)
+    {
+        $return_array = Tools::ssv2Array($this->server);
+        // 非 AEAD 加密无法使用
+        if ($return_array['net'] != 'obfs' && !in_array($user->method, Config::getSupportParam('ss_aead_method'))) {
+            return null;
+        }
+        $return_array['remark']         = ($emoji ? Tools::addEmoji($this->name) : $this->name);
+        $return_array['address']        = $return_array['add'];
+        $return_array['method']         = $user->method;
+        $return_array['passwd']         = $user->passwd;
+        $return_array['protocol']       = 'origin';
+        $return_array['protocol_param'] = '';
+        if ($return_array['net'] == 'obfs') {
+            $return_array['obfs_param'] = $user->getMuMd5();
+        } else {
+            $return_array['obfs'] = 'v2ray';
+            if ($return_array['tls'] == 'tls' && $return_array['net'] == 'ws') {
+                $return_array['obfs_param'] = ('mode=ws;security=tls;path=' . $return_array['path'] .
+                    ';host=' . $return_array['host']);
+            } else {
+                $return_array['obfs_param'] = ('mode=ws;security=none;path=' . $return_array['path'] .
+                    ';host=' . $return_array['host']);
+            }
+            $return_array['path'] = ($return_array['path'] . '?redirect=' . $user->getMuMd5());
+        }
+        $return_array['class'] = $this->node_class;
+        $return_array['group'] = $_ENV['appName'];
+        $return_array['type'] = 'ss';
+        $return_array['ratio'] = $this->traffic_rate;
+
+        return $return_array;
+    }
+
+    /**
+     * Trojan 节点
+     *
+     * @param User $user 用户
+     * @param int  $mu_port
+     * @param int  $relay_rule_id
+     * @param int  $is_ss
+     * @param bool $emoji
+     */
+    public function getTrojanItem(User $user, int $mu_port = 0, int $relay_rule_id = 0, int $is_ss = 0, bool $emoji = false): array
+    {
+        $server = explode(';', $this->server);
+        $opt    = [];
+        if (isset($server[1])) {
+            $opt = URL::parse_args($server[1]);
+        }
+        $item['remark']   = ($emoji ? Tools::addEmoji($this->name) : $this->name);
+        $item['type']     = 'trojan';
+        $item['address']  = $server[0];
+        $item['port']     = (isset($opt['port']) ? (int) $opt['port'] : 443);
+        $item['passwd']   = $user->getUuid();
+        $item['host']     = $item['address'];
+        if (isset($opt['host'])) {
+            $item['host'] = $opt['address'];
+        }
+        return $item;
+    }
 }

+ 9 - 164
src/Utils/URL.php

@@ -179,7 +179,7 @@ class URL
 
     /**
      * 获取全部节点
-     * 
+     *
      * ```
      * $Rule = [
      *      'type'    => 'all | ss | ssr | vmess | trojan',
@@ -275,7 +275,7 @@ class URL
                     14 => 'getTrojanItem',          // Trojan
                 ];
                 $class = $node_class[$node->sort];
-                $item = self::$class($user, $node, $emoji);
+                $item = $node->$class($user, 0, 0, 0, $emoji);
                 if ($item != null) {
                     $return_array[] = $item;
                 }
@@ -295,10 +295,10 @@ class URL
                             if ($relay_rule != null && $relay_rule->dist_node() != null) {
                                 $relay_rule_id = $relay_rule->id;
                             }
-                            $item = self::getItem($user, $node, 0, $relay_rule_id, $ss, $emoji);
+                            $item = $node->getItem($user, 0, $relay_rule_id, $ss, $emoji);
                         } else {
                             // SS 非中转
-                            $item = self::getItem($user, $node, 0, 0, $ss, $emoji);
+                            $item = $node->getItem($user, 0, 0, $ss, $emoji);
                         }
                         if ($item != null) {
                             $return_array[] = $item;
@@ -318,10 +318,10 @@ class URL
                                 if ($relay_rule != null && $relay_rule->dist_node() != null) {
                                     $relay_rule_id = $relay_rule->id;
                                 }
-                                $item = self::getItem($user, $node, $mu_node->server, $relay_rule_id, $ss, $emoji);
+                                $item = $node->getItem($user, $mu_node->server, $relay_rule_id, $ss, $emoji);
                             } else {
                                 // SS 非中转
-                                $item = self::getItem($user, $node, $mu_node->server, 0, $ss, $emoji);
+                                $item = $node->getItem($user, $mu_node->server, 0, $ss, $emoji);
                             }
                             if ($item != null) {
                                 $return_array[] = $item;
@@ -339,7 +339,7 @@ class URL
 
     /**
      * 获取全部节点 Url
-     * 
+     *
      * ```
      *  $Rule = [
      *      'type'    => 'ss | ssr | vmess',
@@ -407,7 +407,7 @@ class URL
         $return_array = array();
         $nodes = self::getNodes($user, 13);
         foreach ($nodes as $node) {
-            $item = self::getV2RayPluginItem($user, $node, $emoji);
+            $item = $node->getV2RayPluginItem($user, 0, 0, 0, $emoji);
             if ($item != null) {
                 $return_array[] = $item;
             }
@@ -416,49 +416,6 @@ class URL
         return $return_array;
     }
 
-    /**
-     * 获取 V2RayPlugin | obfs 节点
-     *
-     * @param User $user 用户
-     * @param Node $node 节点
-     * @param bool $emoji
-     *
-     * @return array|null
-     */
-    public static function getV2RayPluginItem(User $user, Node $node, bool $emoji = false)
-    {
-        $return_array = Tools::ssv2Array($node->server);
-        // 非 AEAD 加密无法使用
-        if ($return_array['net'] != 'obfs' && !in_array($user->method, Config::getSupportParam('ss_aead_method'))) {
-            return null;
-        }
-        $return_array['remark']         = ($emoji ? Tools::addEmoji($node->name) : $node->name);
-        $return_array['address']        = $return_array['add'];
-        $return_array['method']         = $user->method;
-        $return_array['passwd']         = $user->passwd;
-        $return_array['protocol']       = 'origin';
-        $return_array['protocol_param'] = '';
-        if ($return_array['net'] == 'obfs') {
-            $return_array['obfs_param'] = $user->getMuMd5();
-        } else {
-            $return_array['obfs'] = 'v2ray';
-            if ($return_array['tls'] == 'tls' && $return_array['net'] == 'ws') {
-                $return_array['obfs_param'] = ('mode=ws;security=tls;path=' . $return_array['path'] .
-                    ';host=' . $return_array['host']);
-            } else {
-                $return_array['obfs_param'] = ('mode=ws;security=none;path=' . $return_array['path'] .
-                    ';host=' . $return_array['host']);
-            }
-            $return_array['path'] = ($return_array['path'] . '?redirect=' . $user->getMuMd5());
-        }
-        $return_array['class'] = $node->node_class;
-        $return_array['group'] = $_ENV['appName'];
-        $return_array['type'] = 'ss';
-        $return_array['ratio'] = $node->traffic_rate;
-
-        return $return_array;
-    }
-
     /**
      * 获取 V2Ray 节点
      *
@@ -486,23 +443,6 @@ class URL
         return $item;
     }
 
-    /**
-     * 获取 V2Ray 节点
-     *
-     * @param User $user
-     * @param Node $node
-     * @param bool $emoji
-     */
-    public static function getV2RayItem(User $user, Node $node, bool $emoji = false): array
-    {
-        $item           = Tools::v2Array($node->server);
-        $item['type']   = 'vmess';
-        $item['remark'] = ($emoji ? Tools::addEmoji($node->name) : $node->name);
-        $item['id']     = $user->getUuid();
-        $item['class']  = $node->node_class;
-        return $item;
-    }
-
     /**
      * 获取全部 V2Ray 节点
      *
@@ -563,7 +503,7 @@ class URL
         $return_array = array();
         $nodes = self::getNodes($user, 14);
         foreach ($nodes as $node) {
-            $item = self::getTrojanItem($user, $node, $emoji);
+            $item = $node->getTrojanItem($user, 0, 0, 0, $emoji);
             if ($item != null) {
                 $return_array[] = $item;
             }
@@ -572,33 +512,6 @@ class URL
         return $return_array;
     }
 
-    /**
-     * Trojan 节点
-     *
-     * @param User $user 用户
-     * @param Node $node
-     * @param bool $emoji
-     */
-    public static function getTrojanItem(User $user, Node $node, bool $emoji = false): array
-    {
-        $server = explode(';', $node->server);
-        $opt    = [];
-        if (isset($server[1])) {
-            $opt = self::parse_args($server[1]);
-        }
-        $item['remark']   = ($emoji ? Tools::addEmoji($node->name) : $node->name);
-        $item['type']     = 'trojan';
-        $item['address']  = $server[0];
-        $item['port']     = (isset($opt['port']) ? (int) $opt['port'] : 443);
-        $item['passwd']   = $user->getUuid();
-        $item['host']     = $item['address'];
-        if (isset($opt['host'])) {
-            $item['host'] = $opt['address'];
-        }
-
-        return $item;
-    }
-
     public static function getJsonObfs(array $item): string
     {
         $ss_obfs_list = Config::getSupportParam('ss_obfs');
@@ -635,74 +548,6 @@ class URL
         return $plugin;
     }
 
-    /*
-    * Conn info
-    * address
-    * port
-    * passwd
-    * method
-    * remark
-    * protocol
-    * protocol_param
-    * obfs
-    * obfs_param
-    */
-    public static function getItem(User $user, Node $node, int $mu_port = 0, int $relay_rule_id = 0, int $is_ss = 0, bool $emoji = false):? array
-    {
-        $relay_rule = Relay::where('id', $relay_rule_id)->where(
-            static function ($query) use ($user) {
-                $query->Where('user_id', '=', $user->id)
-                    ->orWhere('user_id', '=', 0);
-            }
-        )->orderBy('priority', 'DESC')->orderBy('id')->first();
-        $node_name = $node->name;
-        if ($relay_rule != null) {
-            $node_name .= ' - ' . $relay_rule->dist_node()->name;
-        }
-        if ($mu_port != 0) {
-            $mu_user = User::where('port', '=', $mu_port)->where('is_multi_user', '<>', 0)->first();
-            if ($mu_user == null) {
-                return null;
-            }
-            $mu_user->obfs_param = $user->getMuMd5();
-            $mu_user->protocol_param = $user->id . ':' . $user->passwd;
-            $user = $mu_user;
-            $node_name .= ($_ENV['disable_sub_mu_port'] ? '' : ' - ' . $mu_port . ' 单端口');
-        }
-        if ($is_ss) {
-            if (!self::SSCanConnect($user)) {
-                return null;
-            }
-            $user = self::getSSConnectInfo($user);
-            $return_array['type'] = 'ss';
-        } else {
-            if (!self::SSRCanConnect($user)) {
-                return null;
-            }
-            $user = self::getSSRConnectInfo($user);
-            $return_array['type'] = 'ssr';
-        }
-        $return_array['address']        = $node->getServer();
-        $return_array['port']           = $user->port;
-        $return_array['protocol']       = $user->protocol;
-        $return_array['protocol_param'] = $user->protocol_param;
-        $return_array['obfs']           = $user->obfs;
-        $return_array['obfs_param']     = $user->obfs_param;
-        if ($mu_port != 0 && strpos($node->server, ';') !== false) {
-            $node_tmp             = Tools::OutPort($node->server, $node->name, $mu_port);
-            $return_array['port'] = $node_tmp['port'];
-            $node_name            = $node_tmp['name'];
-        }
-        $return_array['passwd'] = $user->passwd;
-        $return_array['method'] = $user->method;
-        $return_array['remark'] = ($emoji ? Tools::addEmoji($node_name) : $node_name);
-        $return_array['class']  = $node->node_class;
-        $return_array['group']  = $_ENV['appName'];
-        $return_array['ratio']  = ($relay_rule != null ? $node->traffic_rate + $relay_rule->dist_node()->traffic_rate : $node->traffic_rate);
-
-        return $return_array;
-    }
-
     public static function cloneUser(User $user): User
     {
         return clone $user;