Skip to content

Commit

Permalink
清理 link 表的无用字段
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekQuerxy committed Nov 11, 2020
1 parent 16ed407 commit 407c1be
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 51 deletions.
51 changes: 22 additions & 29 deletions sql/glzjin_all.sql
Expand Up @@ -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` (
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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` (
Expand All @@ -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` (
Expand All @@ -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` (
Expand All @@ -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` (
Expand Down Expand Up @@ -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`;

--
Expand Down
35 changes: 20 additions & 15 deletions src/Controllers/LinkController.php
Expand Up @@ -17,6 +17,10 @@
};
use voku\helper\AntiXSS;
use Psr\Http\Message\ResponseInterface;
use Slim\Http\{
Request,
Response
};

/**
* LinkController
Expand All @@ -36,26 +40,28 @@ public static function GenerateRandomLink()
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']) {
Expand All @@ -64,13 +70,12 @@ public static function GetContent($request, $response, $args)

$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;
}
Expand Down Expand Up @@ -132,7 +137,7 @@ function ($item) {
$opts['sub'] = 3;
break;
case 3:
$opts['ssd'] = 1;//deprecated
$opts['ssd'] = 1; //deprecated
break;
case 4:
$opts['clash'] = 1;
Expand Down Expand Up @@ -431,7 +436,7 @@ public static function getSubinfo($user, $int = 0)
if ($int == 0) {
$int = '';
}
$userapiUrl = $_ENV['subUrl'] . self::GenerateSSRSubCode($user->id, 0);
$userapiUrl = $_ENV['subUrl'] . self::GenerateSSRSubCode($user->id);
$return_info = [
'link' => '',
// sub
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/UserController.php
Expand Up @@ -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;
Expand Down Expand Up @@ -1532,7 +1532,7 @@ public function subscribe_log($request, $response, $args)
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/';
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/VueController.php
Expand Up @@ -114,7 +114,7 @@ public function getUserInfo($request, $response, $args)
$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'] != '') {
Expand Down Expand Up @@ -287,7 +287,7 @@ public function getNewSubToken($request, $response, $args)
}

$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,
Expand Down
5 changes: 5 additions & 0 deletions src/Models/Link.php
Expand Up @@ -6,4 +6,9 @@ class Link extends Model
{
protected $connection = 'default';
protected $table = 'link';

public function getUser(): ?User
{
return User::find($this->attributes['userid']);
}
}
4 changes: 2 additions & 2 deletions src/Utils/Telegram/Callbacks/Callback.php
Expand Up @@ -910,7 +910,7 @@ public function getUserSubscribeKeyboard()
[
'text' => 'SSR 订阅',
'callback_data' => 'user.subscribe|?sub=1'
],
],
],
[
[
Expand Down Expand Up @@ -1028,7 +1028,7 @@ public function UserSubscribe()
]
]
];
$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':
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/TelegramProcess.php
Expand Up @@ -29,7 +29,7 @@ private static function callback_bind_method($bot, $callback)
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;
Expand Down
49 changes: 49 additions & 0 deletions 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);

0 comments on commit 407c1be

Please sign in to comment.