Browse Source

refactor: combine duplicated code

Cat 2 years ago
parent
commit
f46e0cc75c
4 changed files with 70 additions and 65 deletions
  1. 22 22
      composer.lock
  2. 17 21
      src/Controllers/LinkController.php
  3. 23 22
      src/Controllers/SubController.php
  4. 8 0
      src/Models/Link.php

+ 22 - 22
composer.lock

@@ -123,16 +123,16 @@
         },
         {
             "name": "aws/aws-sdk-php",
-            "version": "3.262.1",
+            "version": "3.262.2",
             "source": {
                 "type": "git",
                 "url": "https://github.com/aws/aws-sdk-php.git",
-                "reference": "42ca7ade60a775fc5eb103d4631df3d366b48a29"
+                "reference": "6359e467b6f099149f93769e014416b5f841bb42"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/42ca7ade60a775fc5eb103d4631df3d366b48a29",
-                "reference": "42ca7ade60a775fc5eb103d4631df3d366b48a29",
+                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/6359e467b6f099149f93769e014416b5f841bb42",
+                "reference": "6359e467b6f099149f93769e014416b5f841bb42",
                 "shasum": ""
             },
             "require": {
@@ -211,9 +211,9 @@
             "support": {
                 "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
                 "issues": "https://github.com/aws/aws-sdk-php/issues",
-                "source": "https://github.com/aws/aws-sdk-php/tree/3.262.1"
+                "source": "https://github.com/aws/aws-sdk-php/tree/3.262.2"
             },
-            "time": "2023-03-24T18:20:43+00:00"
+            "time": "2023-03-27T18:22:32+00:00"
         },
         {
             "name": "bacon/bacon-qr-code",
@@ -4056,32 +4056,31 @@
         },
         {
             "name": "sentry/sentry",
-            "version": "3.13.0",
+            "version": "3.17.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/getsentry/sentry-php.git",
-                "reference": "a046ff5a37f5a0a0c285a6543dc17a7fc93b47f8"
+                "reference": "95d2e932383cf684f77acff0d2a5aef5ad2f1933"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/a046ff5a37f5a0a0c285a6543dc17a7fc93b47f8",
-                "reference": "a046ff5a37f5a0a0c285a6543dc17a7fc93b47f8",
+                "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/95d2e932383cf684f77acff0d2a5aef5ad2f1933",
+                "reference": "95d2e932383cf684f77acff0d2a5aef5ad2f1933",
                 "shasum": ""
             },
             "require": {
                 "ext-json": "*",
                 "ext-mbstring": "*",
                 "guzzlehttp/promises": "^1.4",
-                "guzzlehttp/psr7": "^1.8.4|^2.1.1",
                 "jean85/pretty-package-versions": "^1.5|^2.0.4",
                 "php": "^7.2|^8.0",
                 "php-http/async-client-implementation": "^1.0",
                 "php-http/client-common": "^1.5|^2.0",
-                "php-http/discovery": "^1.11",
+                "php-http/discovery": "^1.15",
                 "php-http/httplug": "^1.1|^2.0",
                 "php-http/message": "^1.5",
                 "psr/http-factory": "^1.0",
-                "psr/http-message-implementation": "^1.0",
+                "psr/http-factory-implementation": "^1.0",
                 "psr/log": "^1.0|^2.0|^3.0",
                 "symfony/options-resolver": "^3.4.43|^4.4.30|^5.0.11|^6.0",
                 "symfony/polyfill-php80": "^1.17"
@@ -4092,6 +4091,7 @@
             },
             "require-dev": {
                 "friendsofphp/php-cs-fixer": "^2.19|3.4.*",
+                "guzzlehttp/psr7": "^1.8.4|^2.1.1",
                 "http-interop/http-factory-guzzle": "^1.0",
                 "monolog/monolog": "^1.6|^2.0|^3.0",
                 "nikic/php-parser": "^4.10.3",
@@ -4123,7 +4123,7 @@
             },
             "notification-url": "https://packagist.org/downloads/",
             "license": [
-                "BSD-3-Clause"
+                "MIT"
             ],
             "authors": [
                 {
@@ -4144,7 +4144,7 @@
             ],
             "support": {
                 "issues": "https://github.com/getsentry/sentry-php/issues",
-                "source": "https://github.com/getsentry/sentry-php/tree/3.13.0"
+                "source": "https://github.com/getsentry/sentry-php/tree/3.17.0"
             },
             "funding": [
                 {
@@ -4156,7 +4156,7 @@
                     "type": "custom"
                 }
             ],
-            "time": "2023-02-03T10:03:13+00:00"
+            "time": "2023-03-26T21:54:06+00:00"
         },
         {
             "name": "slim/http",
@@ -7570,16 +7570,16 @@
         },
         {
             "name": "slevomat/coding-standard",
-            "version": "8.9.0",
+            "version": "8.9.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/slevomat/coding-standard.git",
-                "reference": "8f11e0f5ff984d6862bb9d83aa513dc05a1773ef"
+                "reference": "3d4fe0c803ae15829ef72d90d3d4eee3dd9f79b2"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/slevomat/coding-standard/zipball/8f11e0f5ff984d6862bb9d83aa513dc05a1773ef",
-                "reference": "8f11e0f5ff984d6862bb9d83aa513dc05a1773ef",
+                "url": "https://api.github.com/repos/slevomat/coding-standard/zipball/3d4fe0c803ae15829ef72d90d3d4eee3dd9f79b2",
+                "reference": "3d4fe0c803ae15829ef72d90d3d4eee3dd9f79b2",
                 "shasum": ""
             },
             "require": {
@@ -7619,7 +7619,7 @@
             ],
             "support": {
                 "issues": "https://github.com/slevomat/coding-standard/issues",
-                "source": "https://github.com/slevomat/coding-standard/tree/8.9.0"
+                "source": "https://github.com/slevomat/coding-standard/tree/8.9.1"
             },
             "funding": [
                 {
@@ -7631,7 +7631,7 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2023-03-25T15:52:37+00:00"
+            "time": "2023-03-27T11:00:16+00:00"
         },
         {
             "name": "squizlabs/php_codesniffer",

+ 17 - 21
src/Controllers/LinkController.php

@@ -37,25 +37,15 @@ final class LinkController extends BaseController
         $token = $args['token'];
         $params = $request->getQueryParams();
 
-        $Elink = Link::where('token', $token)->first();
-        if ($Elink === null) {
-            return $response->withJson([
-                'ret' => 0,
-            ]);
-        }
+        $link = Link::where('token', $token)->first();
 
-        $user = $Elink->user();
-        if ($user === null) {
+        if (! $link->isValid()) {
             return $response->withJson([
                 'ret' => 0,
             ]);
         }
 
-        if ((int) $user->is_banned === 1) {
-            return $response->withJson([
-                'ret' => 0,
-            ]);
-        }
+        $user = $link->user();
 
         $sub_type = '';
         $sub_info = [];
@@ -214,7 +204,8 @@ final class LinkController extends BaseController
                 $server = $node_custom_config['server_user'];
             }
             if ((int) $node_raw->sort === 11) {
-                $v2_port = $node_custom_config['v2_port'] ?? ($node_custom_config['offset_port_user'] ?? ($node_custom_config['offset_port_node'] ?? 443));
+                $v2_port = $node_custom_config['v2_port'] ?? ($node_custom_config['offset_port_user']
+                    ?? ($node_custom_config['offset_port_node'] ?? 443));
                 //默認值有問題的請懂 V2 怎麽用的人來改一改。
                 $alter_id = $node_custom_config['alter_id'] ?? '0';
                 $security = $node_custom_config['security'] ?? 'none';
@@ -270,22 +261,27 @@ final class LinkController extends BaseController
                 $server = $node_custom_config['server_user'];
             }
             if ((int) $node_raw->sort === 14) {
-                $trojan_port = $node_custom_config['trojan_port'] ?? ($node_custom_config['offset_port_user'] ?? ($node_custom_config['offset_port_node'] ?? 443));
+                $trojan_port = $node_custom_config['trojan_port'] ?? ($node_custom_config['offset_port_user']
+                    ?? ($node_custom_config['offset_port_node'] ?? 443));
                 $host = $node_custom_config['host'] ?? '';
                 $allow_insecure = $node_custom_config['allow_insecure'] ?? '0';
-                $security = $node_custom_config['security'] ?? array_key_exists('enable_xtls', $node_custom_config) && $node_custom_config['enable_xtls'] === '1' ? 'xtls' : 'tls';
+                $security = $node_custom_config['security']
+                    ?? array_key_exists('enable_xtls', $node_custom_config)
+                    && $node_custom_config['enable_xtls'] === '1' ? 'xtls' : 'tls';
                 $mux = $node_custom_config['mux'] ?? '';
-                $transport = $node_custom_config['transport'] ?? array_key_exists('grpc', $node_custom_config) && $node_custom_config['grpc'] === '1' ? 'grpc' : 'tcp';
+                $transport = $node_custom_config['transport']
+                    ?? array_key_exists('grpc', $node_custom_config)
+                    && $node_custom_config['grpc'] === '1' ? 'grpc' : 'tcp';
 
                 $transport_plugin = $node_custom_config['transport_plugin'] ?? '';
                 $transport_method = $node_custom_config['transport_method'] ?? '';
                 $servicename = $node_custom_config['servicename'] ?? '';
                 $path = $node_custom_config['path'] ?? '';
 
-                $links .= 'trojan://' . $user->uuid . '@' . $server . ':' . $trojan_port . '?peer=' . $host . '&sni=' . $host .
-                    '&obfs=' . $transport_plugin . '&path=' . $path . '&mux=' . $mux . '&allowInsecure=' . $allow_insecure .
-                    '&obfsParam=' . $transport_method . '&type=' . $transport . '&security=' . $security . '&serviceName=' . $servicename . '#' .
-                    $node_raw->name . PHP_EOL;
+                $links .= 'trojan://' . $user->uuid . '@' . $server . ':' . $trojan_port . '?peer=' . $host . '&sni='
+                    . $host . '&obfs=' . $transport_plugin . '&path=' . $path . '&mux=' . $mux . '&allowInsecure='
+                    . $allow_insecure . '&obfsParam=' . $transport_method . '&type=' . $transport . '&security='
+                    . $security . '&serviceName=' . $servicename . '#' . $node_raw->name . PHP_EOL;
             }
         }
 

+ 23 - 22
src/Controllers/SubController.php

@@ -32,33 +32,24 @@ final class SubController extends BaseController
         $token = $args['token'];
         $subtype = $args['subtype'];
 
-        $sub_token = Link::where('token', $token)->first();
-        if ($sub_token === null) {
-            return $response->withJson([
-                'ret' => 0,
-            ]);
-        }
+        $subtype_list = ['json', 'clash', 'sip008'];
 
-        $user = $sub_token->user();
-        if ($user === null) {
+        if (! in_array($subtype, $subtype_list)) {
             return $response->withJson([
                 'ret' => 0,
             ]);
         }
 
-        if ((int) $user->is_banned === 1) {
-            return $response->withJson([
-                'ret' => 0,
-            ]);
-        }
+        $link = Link::where('token', $token)->first();
 
-        $subtype_list = ['json', 'clash', 'sip008'];
-        if (! in_array($subtype, $subtype_list)) {
+        if (! $link->isValid()) {
             return $response->withJson([
                 'ret' => 0,
             ]);
         }
 
+        $user = $link->user();
+
         match ($subtype) {
             'json' => $sub_info = self::getJson($user),
             'clash' => $sub_info = self::getClash($user),
@@ -122,7 +113,8 @@ final class SubController extends BaseController
                     ];
                     break;
                 case 11:
-                    $v2_port = $node_custom_config['v2_port'] ?? ($node_custom_config['offset_port_user'] ?? ($node_custom_config['offset_port_node'] ?? 443));
+                    $v2_port = $node_custom_config['v2_port'] ?? ($node_custom_config['offset_port_user']
+                        ?? ($node_custom_config['offset_port_node'] ?? 443));
                     //默認值有問題的請懂 V2 怎麽用的人來改一改。
                     $alter_id = $node_custom_config['alter_id'] ?? '0';
                     $security = $node_custom_config['security'] ?? 'none';
@@ -159,12 +151,17 @@ final class SubController extends BaseController
                     ];
                     break;
                 case 14:
-                    $trojan_port = $node_custom_config['trojan_port'] ?? ($node_custom_config['offset_port_user'] ?? ($node_custom_config['offset_port_node'] ?? 443));
+                    $trojan_port = $node_custom_config['trojan_port'] ?? ($node_custom_config['offset_port_user']
+                        ?? ($node_custom_config['offset_port_node'] ?? 443));
                     $host = $node_custom_config['host'] ?? '';
                     $allow_insecure = $node_custom_config['allow_insecure'] ?? '0';
-                    $security = $node_custom_config['security'] ?? array_key_exists('enable_xtls', $node_custom_config) && $node_custom_config['enable_xtls'] === '1' ? 'xtls' : 'tls';
+                    $security = $node_custom_config['security']
+                        ?? array_key_exists('enable_xtls', $node_custom_config)
+                        && $node_custom_config['enable_xtls'] === '1' ? 'xtls' : 'tls';
                     $mux = $node_custom_config['mux'] ?? '';
-                    $transport = $node_custom_config['transport'] ?? array_key_exists('grpc', $node_custom_config) && $node_custom_config['grpc'] === '1' ? 'grpc' : 'tcp';
+                    $transport = $node_custom_config['transport']
+                        ?? array_key_exists('grpc', $node_custom_config)
+                        && $node_custom_config['grpc'] === '1' ? 'grpc' : 'tcp';
 
                     $transport_plugin = $node_custom_config['transport_plugin'] ?? '';
                     $transport_method = $node_custom_config['transport_method'] ?? '';
@@ -260,7 +257,8 @@ final class SubController extends BaseController
 
                     break;
                 case 11:
-                    $v2_port = $node_custom_config['v2_port'] ?? ($node_custom_config['offset_port_user'] ?? ($node_custom_config['offset_port_node'] ?? 443));
+                    $v2_port = $node_custom_config['v2_port'] ?? ($node_custom_config['offset_port_user']
+                        ?? ($node_custom_config['offset_port_node'] ?? 443));
                     $alter_id = $node_custom_config['alter_id'] ?? '0';
                     $security = $node_custom_config['security'] ?? 'none';
                     $encryption = $node_custom_config['encryption'] ?? 'auto';
@@ -296,8 +294,11 @@ final class SubController extends BaseController
 
                     break;
                 case 14:
-                    $trojan_port = $node_custom_config['trojan_port'] ?? ($node_custom_config['offset_port_user'] ?? ($node_custom_config['offset_port_node'] ?? 443));
-                    $network = $node_custom_config['network'] ?? array_key_exists('grpc', $node_custom_config) && $node_custom_config['grpc'] === '1' ? 'grpc' : 'tcp';
+                    $trojan_port = $node_custom_config['trojan_port'] ?? ($node_custom_config['offset_port_user']
+                        ?? ($node_custom_config['offset_port_node'] ?? 443));
+                    $network = $node_custom_config['network']
+                        ?? array_key_exists('grpc', $node_custom_config)
+                        && $node_custom_config['grpc'] === '1' ? 'grpc' : 'tcp';
                     $host = $node_custom_config['host'] ?? '';
                     $allow_insecure = $node_custom_config['allow_insecure'] ?? false;
                     // Clash 特定配置

+ 8 - 0
src/Models/Link.php

@@ -13,4 +13,12 @@ final class Link extends Model
     {
         return User::find($this->attributes['userid']);
     }
+
+    public function isValid(): bool
+    {
+        if ($this !== null && $this->user() !== null && $this->user()->is_banned === 0) {
+            return true;
+        }
+        return false;
+    }
 }