Browse Source

清理 link 表的无用字段

GeekQuerxy 5 năm trước cách đây
mục cha
commit
407c1bee6f

+ 22 - 29
sql/glzjin_all.sql

@@ -39,15 +39,8 @@ CREATE TABLE IF NOT EXISTS `code` (
 
 CREATE TABLE IF NOT EXISTS `link` (
   `id` bigint(20) NOT NULL,
-  `type` int(11) NOT NULL,
-  `address` text NOT NULL,
-  `port` int(11) NOT NULL,
   `token` text NOT NULL,
-  `ios` int(11) NOT NULL DEFAULT '0',
-  `userid` bigint(20) NOT NULL,
-  `isp` text,
-  `geo` int(11) DEFAULT NULL,
-  `method` text
+  `userid` bigint(20) NOT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
 CREATE TABLE IF NOT EXISTS `login_ip` (
@@ -322,7 +315,7 @@ CREATE TABLE `shop` (
 ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
 CREATE TABLE `coupon` (
-  `id` BIGINT NOT NULL AUTO_INCREMENT, 
+  `id` BIGINT NOT NULL AUTO_INCREMENT,
   `code` TEXT NOT NULL,
   `onetime` INT NOT NULL,
   `expire` BIGINT NOT NULL,
@@ -398,12 +391,12 @@ CREATE TABLE `disconnect_ip` (
 
 ALTER TABLE `user`
   CHANGE `node_speedlimit` `node_speedlimit` DECIMAL(12,2) NOT NULL DEFAULT '0.00';
-ALTER TABLE `ss_node` 
+ALTER TABLE `ss_node`
   CHANGE `node_speedlimit` `node_speedlimit` DECIMAL(12,2) NOT NULL DEFAULT '0.00';
 ALTER TABLE `user`
   DROP `relay_enable`,
   DROP `relay_info`;
-ALTER TABLE `user` 
+ALTER TABLE `user`
   CHANGE `protocol` `protocol` VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT 'origin', CHANGE `obfs` `obfs` VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT 'plain';
 
 CREATE TABLE `email_verify` (
@@ -415,7 +408,7 @@ CREATE TABLE `email_verify` (
   PRIMARY KEY (`id`)
 ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
-ALTER TABLE `user` 
+ALTER TABLE `user`
   ADD `is_hide` INT NOT NULL DEFAULT '0' AFTER `disconnect_ip`;
 
 CREATE TABLE `detect_list` (
@@ -427,22 +420,22 @@ CREATE TABLE `detect_list` (
 ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
 CREATE TABLE `detect_log` (
-  `id` BIGINT NOT NULL AUTO_INCREMENT, 
-  `user_id` BIGINT NOT NULL, 
-  `list_id` BIGINT NOT NULL, 
-  `datetime` BIGINT NOT NULL, 
+  `id` BIGINT NOT NULL AUTO_INCREMENT,
+  `user_id` BIGINT NOT NULL,
+  `list_id` BIGINT NOT NULL,
+  `datetime` BIGINT NOT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
 ALTER TABLE `detect_list`
   ADD `type` INT NOT NULL AFTER `regex`;
-ALTER TABLE `detect_log` 
+ALTER TABLE `detect_log`
   ADD `node_id` INT NOT NULL AFTER `datetime`;
-ALTER TABLE `user` 
+ALTER TABLE `user`
   ADD `is_multi_user` INT NOT NULL DEFAULT '0' AFTER `is_hide`;
-ALTER TABLE `ss_node` 
+ALTER TABLE `ss_node`
   ADD `mu_only` INT NULL DEFAULT '0' AFTER `custom_rss`;
-ALTER TABLE `ss_node` 
+ALTER TABLE `ss_node`
   ADD `online` BOOLEAN NOT NULL DEFAULT TRUE AFTER `mu_only`, ADD `gfw_block` BOOLEAN NOT NULL DEFAULT FALSE AFTER `online`;
 
 CREATE TABLE IF NOT EXISTS `relay` (
@@ -461,17 +454,17 @@ ALTER TABLE `relay`
   MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;
 
 CREATE TABLE `telegram_session` (
-  `id` BIGINT NOT NULL AUTO_INCREMENT, 
-  `user_id` BIGINT NOT NULL, 
-  `type` INT NOT NULL, 
-  `session_content` TEXT NOT NULL, 
-  `datetime` BIGINT NOT NULL, 
+  `id` BIGINT NOT NULL AUTO_INCREMENT,
+  `user_id` BIGINT NOT NULL,
+  `type` INT NOT NULL,
+  `session_content` TEXT NOT NULL,
+  `datetime` BIGINT NOT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
-ALTER TABLE `user` 
+ALTER TABLE `user`
   ADD `telegram_id` BIGINT NULL AFTER `is_multi_user`;
-ALTER TABLE `user` 
+ALTER TABLE `user`
   ADD `expire_notified` BOOLEAN NOT NULL DEFAULT FALSE AFTER `telegram_id`, ADD `traffic_notified` BOOLEAN NULL DEFAULT FALSE AFTER `expire_notified`;
 
 CREATE TABLE IF NOT EXISTS `paylist` (
@@ -560,9 +553,9 @@ CREATE TABLE IF NOT EXISTS `telegram_tasks` (
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Telegram 任务列表';
 
 
-ALTER TABLE `detect_log` 
+ALTER TABLE `detect_log`
   ADD `status` int(2) NOT NULL DEFAULT '0' AFTER `node_id`;
-ALTER TABLE `user` 
+ALTER TABLE `user`
   ADD COLUMN `uuid` TEXT NULL DEFAULT NULL COMMENT 'uuid' AFTER `passwd`;
 
 --

+ 20 - 15
src/Controllers/LinkController.php

@@ -17,6 +17,10 @@ use App\Utils\{
 };
 use voku\helper\AntiXSS;
 use Psr\Http\Message\ResponseInterface;
+use Slim\Http\{
+    Request,
+    Response
+};
 
 /**
  *  LinkController
@@ -36,26 +40,28 @@ class LinkController extends BaseController
         return "couldn't alloc token";
     }
 
-    public static function GenerateSSRSubCode($userid, $without_mu)
+    /**
+     * @param int $userid
+     */
+    public static function GenerateSSRSubCode(int $userid): string
     {
-        $Elink = Link::where('type', 11)->where('userid', $userid)->where('geo', $without_mu)->first();
+        $Elink = Link::where('userid', $userid)->first();
         if ($Elink != null) {
             return $Elink->token;
         }
-        $NLink = new Link();
-        $NLink->type = 11;
-        $NLink->address = '';
-        $NLink->port = 0;
-        $NLink->ios = 0;
-        $NLink->geo = $without_mu;
-        $NLink->method = '';
+        $NLink         = new Link();
         $NLink->userid = $userid;
-        $NLink->token = self::GenerateRandomLink();
+        $NLink->token  = self::GenerateRandomLink();
         $NLink->save();
 
         return $NLink->token;
     }
 
+    /**
+     * @param Request   $request
+     * @param Response  $response
+     * @param array     $args
+     */
     public static function GetContent($request, $response, $args)
     {
         if (!$_ENV['Subscribe']) {
@@ -64,13 +70,12 @@ class LinkController extends BaseController
 
         $token = $args['token'];
 
-        //$builder->getPhrase();
-        $Elink = Link::where('type', 11)->where('token', $token)->first();
+        $Elink = Link::where('token', $token)->first();
         if ($Elink == null) {
             return null;
         }
 
-        $user = User::where('id', $Elink->userid)->first();
+        $user = $Elink->getUser();
         if ($user == null) {
             return null;
         }
@@ -132,7 +137,7 @@ class LinkController extends BaseController
                     $opts['sub'] = 3;
                     break;
                 case 3:
-                    $opts['ssd'] = 1;//deprecated
+                    $opts['ssd'] = 1; //deprecated
                     break;
                 case 4:
                     $opts['clash'] = 1;
@@ -431,7 +436,7 @@ class LinkController extends BaseController
         if ($int == 0) {
             $int = '';
         }
-        $userapiUrl = $_ENV['subUrl'] . self::GenerateSSRSubCode($user->id, 0);
+        $userapiUrl = $_ENV['subUrl'] . self::GenerateSSRSubCode($user->id);
         $return_info = [
             'link'            => '',
             // sub

+ 2 - 2
src/Controllers/UserController.php

@@ -60,7 +60,7 @@ class UserController extends BaseController
 {
     public function index($request, $response, $args)
     {
-        $ssr_sub_token = LinkController::GenerateSSRSubCode($this->user->id, 0);
+        $ssr_sub_token = LinkController::GenerateSSRSubCode($this->user->id);
 
         $GtSdk = null;
         $recaptcha_sitekey = null;
@@ -1532,7 +1532,7 @@ class UserController extends BaseController
     public function getPcClient($request, $response, $args)
     {
         $zipArc = new \ZipArchive();
-        $user_token = LinkController::GenerateSSRSubCode($this->user->id, 0);
+        $user_token = LinkController::GenerateSSRSubCode($this->user->id);
         $type = trim($request->getQueryParams()['type']);
         // 临时文件存放路径
         $temp_file_path = BASE_PATH . '/storage/';

+ 2 - 2
src/Controllers/VueController.php

@@ -114,7 +114,7 @@ class VueController extends BaseController
         $ssinfo = URL::getSSConnectInfo($pre_user);
         $user->vmess_url_all = URL::getAllVMessUrl($user);
         $user->isAbleToCheckin = $user->isAbleToCheckin();
-        $ssr_sub_token = LinkController::GenerateSSRSubCode($this->user->id, 0);
+        $ssr_sub_token = LinkController::GenerateSSRSubCode($this->user->id);
         $GtSdk = null;
         $recaptcha_sitekey = null;
         if ($_ENV['captcha_provider'] != '') {
@@ -287,7 +287,7 @@ class VueController extends BaseController
         }
 
         $user->clean_link();
-        $ssr_sub_token = LinkController::GenerateSSRSubCode($this->user->id, 0);
+        $ssr_sub_token = LinkController::GenerateSSRSubCode($this->user->id);
 
         $res['arr'] = array(
             'ssr_sub_token' => $ssr_sub_token,

+ 5 - 0
src/Models/Link.php

@@ -6,4 +6,9 @@ class Link extends Model
 {
     protected $connection = 'default';
     protected $table = 'link';
+
+    public function getUser(): ?User
+    {
+        return User::find($this->attributes['userid']);
+    }
 }

+ 2 - 2
src/Utils/Telegram/Callbacks/Callback.php

@@ -910,7 +910,7 @@ class Callback
                 [
                     'text'          => 'SSR 订阅',
                     'callback_data' => 'user.subscribe|?sub=1'
-                ],        
+                ],
             ],
             [
                 [
@@ -1028,7 +1028,7 @@ class Callback
                     ]
                 ]
             ];
-            $token      = LinkController::GenerateSSRSubCode($this->User->id, 0);
+            $token      = LinkController::GenerateSSRSubCode($this->User->id);
             $UserApiUrl = LinkController::getSubinfo($this->User, 0)['link'];
             switch ($CallbackDataExplode[1]) {
                 case '?clash=1':

+ 1 - 1
src/Utils/TelegramProcess.php

@@ -29,7 +29,7 @@ class TelegramProcess
         if ($user != null) {
             switch (true) {
                 case (strpos($callback_data, 'mu')):
-                    $ssr_sub_token = LinkController::GenerateSSRSubCode($user->id, 0);
+                    $ssr_sub_token = LinkController::GenerateSSRSubCode($user->id);
                     $subUrl = $_ENV['subUrl'];
                     $reply_message = self::$all_rss[$callback_data] . ': ' . $subUrl . $ssr_sub_token . $callback_data . PHP_EOL;
                     break;

+ 49 - 0
utils/clean_link.php

@@ -0,0 +1,49 @@
+#!/usr/bin/env php
+<?php
+
+declare(strict_types=1);
+
+use App\Models\Link;
+use App\Services\Boot;
+use Illuminate\Database\Capsule\Manager as Capsule;
+use Illuminate\Database\Schema\Blueprint;
+
+require __DIR__ . '/../app/predefine.php';
+require __DIR__ . '/../vendor/autoload.php';
+require __DIR__ . '/../config/.config.php';
+
+Boot::setTime();
+Boot::bootDb();
+
+echo PHP_EOL;
+
+try {
+    Link::where('type', '<>', 11)->delete();
+    echo '所有类型非 11 的订阅链接已全部清理.' . PHP_EOL;
+} catch (\Throwable $th) {
+    $th->getMessage();
+}
+
+try {
+    Capsule::schema()->table(
+        'link',
+        function (Blueprint $table) {
+            $table->dropColumn(
+                [
+                    'type',
+                    'address',
+                    'port',
+                    'ios',
+                    'isp',
+                    'geo',
+                    'method',
+                ]
+            );
+        }
+    );
+    echo 'link 表无用的字段已全部清理.' . PHP_EOL;
+} catch (\Throwable $th) {
+    $th->getMessage();
+}
+
+exit(0);