1
0
Эх сурвалжийг харах

feat: add user ip limit in webapi

M1Screw 1 жил өмнө
parent
commit
fad3e56789

+ 33 - 32
composer.lock

@@ -124,16 +124,16 @@
         },
         {
             "name": "aws/aws-sdk-php",
-            "version": "3.300.2",
+            "version": "3.300.4",
             "source": {
                 "type": "git",
                 "url": "https://github.com/aws/aws-sdk-php.git",
-                "reference": "61d3d0179eff76793e338349c40c73271854fd1c"
+                "reference": "27d59c22c121ce9c0041c563dc9d7270e180925c"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/61d3d0179eff76793e338349c40c73271854fd1c",
-                "reference": "61d3d0179eff76793e338349c40c73271854fd1c",
+                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/27d59c22c121ce9c0041c563dc9d7270e180925c",
+                "reference": "27d59c22c121ce9c0041c563dc9d7270e180925c",
                 "shasum": ""
             },
             "require": {
@@ -213,9 +213,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.300.2"
+                "source": "https://github.com/aws/aws-sdk-php/tree/3.300.4"
             },
-            "time": "2024-02-21T19:07:54+00:00"
+            "time": "2024-02-23T19:10:30+00:00"
         },
         {
             "name": "bacon/bacon-qr-code",
@@ -530,16 +530,16 @@
         },
         {
             "name": "composer/ca-bundle",
-            "version": "1.4.0",
+            "version": "1.4.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/composer/ca-bundle.git",
-                "reference": "b66d11b7479109ab547f9405b97205640b17d385"
+                "reference": "3ce240142f6d59b808dd65c1f52f7a1c252e6cfd"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/composer/ca-bundle/zipball/b66d11b7479109ab547f9405b97205640b17d385",
-                "reference": "b66d11b7479109ab547f9405b97205640b17d385",
+                "url": "https://api.github.com/repos/composer/ca-bundle/zipball/3ce240142f6d59b808dd65c1f52f7a1c252e6cfd",
+                "reference": "3ce240142f6d59b808dd65c1f52f7a1c252e6cfd",
                 "shasum": ""
             },
             "require": {
@@ -586,7 +586,7 @@
             "support": {
                 "irc": "irc://irc.freenode.org/composer",
                 "issues": "https://github.com/composer/ca-bundle/issues",
-                "source": "https://github.com/composer/ca-bundle/tree/1.4.0"
+                "source": "https://github.com/composer/ca-bundle/tree/1.4.1"
             },
             "funding": [
                 {
@@ -602,7 +602,7 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2023-12-18T12:05:55+00:00"
+            "time": "2024-02-23T10:16:52+00:00"
         },
         {
             "name": "dasprid/enum",
@@ -4422,16 +4422,16 @@
         },
         {
             "name": "stripe/stripe-php",
-            "version": "v13.11.0",
+            "version": "v13.12.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/stripe/stripe-php.git",
-                "reference": "d92a95bd61be5d3141d86986c3b454065f9fcc13"
+                "reference": "8052da9058caae10c7297f85821f652b38e31d85"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/stripe/stripe-php/zipball/d92a95bd61be5d3141d86986c3b454065f9fcc13",
-                "reference": "d92a95bd61be5d3141d86986c3b454065f9fcc13",
+                "url": "https://api.github.com/repos/stripe/stripe-php/zipball/8052da9058caae10c7297f85821f652b38e31d85",
+                "reference": "8052da9058caae10c7297f85821f652b38e31d85",
                 "shasum": ""
             },
             "require": {
@@ -4475,9 +4475,9 @@
             ],
             "support": {
                 "issues": "https://github.com/stripe/stripe-php/issues",
-                "source": "https://github.com/stripe/stripe-php/tree/v13.11.0"
+                "source": "https://github.com/stripe/stripe-php/tree/v13.12.0"
             },
-            "time": "2024-02-16T00:23:50+00:00"
+            "time": "2024-02-22T22:19:58+00:00"
         },
         {
             "name": "symfony/deprecation-contracts",
@@ -6507,16 +6507,16 @@
         },
         {
             "name": "friendsofphp/php-cs-fixer",
-            "version": "v3.49.0",
+            "version": "v3.50.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer.git",
-                "reference": "8742f7aa6f72a399688b65e4f58992c2d4681fc2"
+                "reference": "dbea11dcb6d9a1f6c8d51c0e580ab4a8876f524c"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/8742f7aa6f72a399688b65e4f58992c2d4681fc2",
-                "reference": "8742f7aa6f72a399688b65e4f58992c2d4681fc2",
+                "url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/dbea11dcb6d9a1f6c8d51c0e580ab4a8876f524c",
+                "reference": "dbea11dcb6d9a1f6c8d51c0e580ab4a8876f524c",
                 "shasum": ""
             },
             "require": {
@@ -6526,7 +6526,7 @@
                 "ext-json": "*",
                 "ext-tokenizer": "*",
                 "php": "^7.4 || ^8.0",
-                "sebastian/diff": "^4.0 || ^5.0",
+                "sebastian/diff": "^4.0 || ^5.0 || ^6.0",
                 "symfony/console": "^5.4 || ^6.0 || ^7.0",
                 "symfony/event-dispatcher": "^5.4 || ^6.0 || ^7.0",
                 "symfony/filesystem": "^5.4 || ^6.0 || ^7.0",
@@ -6547,7 +6547,8 @@
                 "php-cs-fixer/accessible-object": "^1.1",
                 "php-cs-fixer/phpunit-constraint-isidenticalstring": "^1.4",
                 "php-cs-fixer/phpunit-constraint-xmlmatchesxsd": "^1.4",
-                "phpunit/phpunit": "^9.6 || ^10.5.5",
+                "phpunit/phpunit": "^9.6 || ^10.5.5 || ^11.0.2",
+                "symfony/var-dumper": "^5.4 || ^6.0 || ^7.0",
                 "symfony/yaml": "^5.4 || ^6.0 || ^7.0"
             },
             "suggest": {
@@ -6586,7 +6587,7 @@
             ],
             "support": {
                 "issues": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues",
-                "source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.49.0"
+                "source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.50.0"
             },
             "funding": [
                 {
@@ -6594,7 +6595,7 @@
                     "type": "github"
                 }
             ],
-            "time": "2024-02-02T00:41:40+00:00"
+            "time": "2024-02-23T23:17:45+00:00"
         },
         {
             "name": "justinrainbow/json-schema",
@@ -7141,16 +7142,16 @@
         },
         {
             "name": "phpstan/phpdoc-parser",
-            "version": "1.25.0",
+            "version": "1.26.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/phpstan/phpdoc-parser.git",
-                "reference": "bd84b629c8de41aa2ae82c067c955e06f1b00240"
+                "reference": "231e3186624c03d7e7c890ec662b81e6b0405227"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/bd84b629c8de41aa2ae82c067c955e06f1b00240",
-                "reference": "bd84b629c8de41aa2ae82c067c955e06f1b00240",
+                "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/231e3186624c03d7e7c890ec662b81e6b0405227",
+                "reference": "231e3186624c03d7e7c890ec662b81e6b0405227",
                 "shasum": ""
             },
             "require": {
@@ -7182,9 +7183,9 @@
             "description": "PHPDoc parser with support for nullable, intersection and generic types",
             "support": {
                 "issues": "https://github.com/phpstan/phpdoc-parser/issues",
-                "source": "https://github.com/phpstan/phpdoc-parser/tree/1.25.0"
+                "source": "https://github.com/phpstan/phpdoc-parser/tree/1.26.0"
             },
-            "time": "2024-01-04T17:06:16+00:00"
+            "time": "2024-02-23T16:05:55+00:00"
         },
         {
             "name": "phpunit/php-code-coverage",

+ 0 - 14
src/Command/ClientDownload.php

@@ -32,8 +32,6 @@ final class ClientDownload extends Command
     private array $version;
 
     /**
-     * @return void
-     *
      * @throws GuzzleException
      */
     public function boot(): void
@@ -68,8 +66,6 @@ final class ClientDownload extends Command
      * @param string $fileName
      * @param string $savePath
      * @param string $url
-     *
-     * @return bool
      */
     private function getSourceFile(string $fileName, string $savePath, string $url): bool
     {
@@ -104,8 +100,6 @@ final class ClientDownload extends Command
      *
      * @param string $repo
      *
-     * @return string
-     *
      * @throws GuzzleException
      */
     private function getLatestReleaseTagName(string $repo): string
@@ -125,8 +119,6 @@ final class ClientDownload extends Command
      *
      * @param string $repo
      *
-     * @return string
-     *
      * @throws GuzzleException
      */
     private function getLatestPreReleaseTagName(string $repo): string
@@ -184,8 +176,6 @@ final class ClientDownload extends Command
      * 储存本地软体版本库
      *
      * @param array $versions
-     *
-     * @return void
      */
     private function setLocalVersions(array $versions): void
     {
@@ -204,8 +194,6 @@ final class ClientDownload extends Command
      * @param $name
      * @param $taskName
      * @param $tagName
-     *
-     * @return array|string
      */
     private static function getNames($name, $taskName, $tagName): array|string
     {
@@ -227,8 +215,6 @@ final class ClientDownload extends Command
     /**
      * @param array $task
      *
-     * @return void
-     *
      * @throws GuzzleException
      */
     private function getSoft(array $task): void

+ 0 - 2
src/Controllers/Admin/AnnController.php

@@ -71,8 +71,6 @@ final class AnnController extends BaseController
      * @param ServerRequest $request
      * @param Response $response
      * @param array $args
-     *
-     * @return Response|ResponseInterface
      */
     public function add(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {

+ 0 - 2
src/Controllers/Admin/DocsController.php

@@ -125,8 +125,6 @@ final class DocsController extends BaseController
      * @param ServerRequest $request
      * @param Response $response
      * @param array $args
-     *
-     * @return Response|ResponseInterface
      */
     public function update(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {

+ 0 - 2
src/Controllers/Admin/UserController.php

@@ -108,8 +108,6 @@ final class UserController extends BaseController
      * @param Response $response
      * @param array $args
      *
-     * @return Response|ResponseInterface
-     *
      * @throws Exception
      */
     public function create(ServerRequest $request, Response $response, array $args): Response|ResponseInterface

+ 11 - 0
src/Controllers/Api/AdminApiV1Controller.php

@@ -0,0 +1,11 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Controllers\Api;
+
+use App\Controllers\BaseController;
+
+final class AdminApiV1Controller extends BaseController
+{
+}

+ 11 - 0
src/Controllers/Api/NodeApiV1Controller.php

@@ -0,0 +1,11 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Controllers\Api;
+
+use App\Controllers\BaseController;
+
+final class NodeApiV1Controller extends BaseController
+{
+}

+ 11 - 0
src/Controllers/Api/UserApiV1Controller.php

@@ -0,0 +1,11 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Controllers\Api;
+
+use App\Controllers\BaseController;
+
+final class UserApiV1Controller extends BaseController
+{
+}

+ 0 - 6
src/Controllers/AuthController.php

@@ -57,8 +57,6 @@ final class AuthController extends BaseController
      * @param ServerRequest $request
      * @param Response $response
      * @param array $args
-     *
-     * @return Response|ResponseInterface
      */
     public function loginHandle(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {
@@ -209,8 +207,6 @@ final class AuthController extends BaseController
      * @param $money
      * @param $is_admin_reg
      *
-     * @return ResponseInterface
-     *
      * @throws Exception
      */
     public function registerHelper(
@@ -305,8 +301,6 @@ final class AuthController extends BaseController
      * @param Response $response
      * @param array $args
      *
-     * @return Response|ResponseInterface
-     *
      * @throws RedisException
      * @throws Exception
      */

+ 0 - 2
src/Controllers/SubController.php

@@ -25,8 +25,6 @@ final class SubController extends BaseController
      * @param $response
      * @param $args
      *
-     * @return ResponseInterface
-     *
      * @throws ClientExceptionInterface
      * @throws GuzzleException
      * @throws RedisException

+ 11 - 9
src/Controllers/WebAPI/UserController.php

@@ -31,8 +31,6 @@ final class UserController extends BaseController
      * @param ServerRequest   $request
      * @param Response  $response
      * @param array     $args
-     *
-     * @return ResponseInterface
      */
     public function index(ServerRequest $request, Response $response, array $args): ResponseInterface
     {
@@ -104,6 +102,16 @@ final class UserController extends BaseController
                 }
             }
 
+            if ($user_raw->node_iplimit !== 0 &&
+                $user_raw->node_iplimit <
+                (new OnlineLog())
+                    ->where('user_id', $user_raw->id)
+                    ->where('last_time', '>', time() - 90)
+                    ->count()
+            ) {
+                continue;
+            }
+
             if ($node->sort === 1) {
                 $method = json_decode($node->custom_config)->method ?? '2022-blake3-aes-128-gcm';
 
@@ -132,8 +140,6 @@ final class UserController extends BaseController
      * @param ServerRequest   $request
      * @param Response  $response
      * @param array     $args
-     *
-     * @return ResponseInterface
      */
     public function addTraffic(ServerRequest $request, Response $response, array $args): ResponseInterface
     {
@@ -221,8 +227,6 @@ final class UserController extends BaseController
      * @param ServerRequest   $request
      * @param Response  $response
      * @param array     $args
-     *
-     * @return ResponseInterface
      */
     public function addAliveIp(ServerRequest $request, Response $response, array $args): ResponseInterface
     {
@@ -250,7 +254,7 @@ final class UserController extends BaseController
 
             if (Tools::isIPv4($ip)) {
                 // convert IPv4 Address to IPv4-mapped IPv6 Address
-                $ip = "::ffff:{$ip}";
+                $ip = '::ffff:' . $ip;
             } elseif (! Tools::isIPv6($ip)) {
                 // either IPv4 or IPv6 Address
                 continue;
@@ -278,8 +282,6 @@ final class UserController extends BaseController
      * @param ServerRequest   $request
      * @param Response  $response
      * @param array     $args
-     *
-     * @return ResponseInterface
      */
     public function addDetectLog(ServerRequest $request, Response $response, array $args): ResponseInterface
     {

+ 1 - 3
src/Middleware/AdminApi.php

@@ -16,10 +16,8 @@ final class AdminApi
      * @param ServerRequest $request
      * @param Response $response
      * @param callable $next
-     *
-     * @return ResponseInterface
      */
-    public function __invoke($request, $response, $next)
+    public function __invoke($request, $response, $next): ResponseInterface
     {
     }
 }

+ 1 - 3
src/Middleware/NodeApi.php

@@ -16,10 +16,8 @@ final class NodeApi
      * @param ServerRequest $request
      * @param Response $response
      * @param callable $next
-     *
-     * @return ResponseInterface
      */
-    public function __invoke($request, $response, $next)
+    public function __invoke($request, $response, $next): ResponseInterface
     {
     }
 }

+ 1 - 3
src/Middleware/UserApi.php

@@ -16,10 +16,8 @@ final class UserApi
      * @param ServerRequest $request
      * @param Response $response
      * @param callable $next
-     *
-     * @return ResponseInterface
      */
-    public function __invoke($request, $response, $next)
+    public function __invoke($request, $response, $next): ResponseInterface
     {
     }
 }

+ 0 - 2
src/Models/LoginIp.php

@@ -48,8 +48,6 @@ final class LoginIp extends Model
      * @param string $ip IP
      * @param int $type 1 = failed, 0 = success
      * @param int $user_id User ID
-     *
-     * @return void
      */
     public function collectLoginIP(string $ip, int $type = 0, int $user_id = 0): void
     {

+ 0 - 2
src/Models/User.php

@@ -96,8 +96,6 @@ final class User extends Model
 
     /**
      * @param $len
-     *
-     * @return string
      */
     public function getSs2022Pk($len): string
     {

+ 4 - 6
src/Services/Analytics.php

@@ -24,8 +24,6 @@ final class Analytics
      * 获取累计收入
      *
      * @param string $req
-     *
-     * @return float
      */
     public static function getIncome(string $req): float
     {
@@ -72,7 +70,7 @@ final class Analytics
         return Tools::autoBytes((new User())->sum('transfer_today'));
     }
 
-    public static function getRawTodayTrafficUsage()
+    public static function getRawTodayTrafficUsage(): int
     {
         return (new User())->sum('transfer_today');
     }
@@ -87,7 +85,7 @@ final class Analytics
         return Tools::autoBytes((new User())->sum('u') + (new User())->sum('d') - (new User())->sum('transfer_today'));
     }
 
-    public static function getRawLastTrafficUsage()
+    public static function getRawLastTrafficUsage(): int
     {
         return (new User())->sum('u') + (new User())->sum('d') - (new User())->sum('transfer_today');
     }
@@ -102,7 +100,7 @@ final class Analytics
         return Tools::autoBytes((new User())->sum('transfer_enable') - (new User())->sum('u') - (new User())->sum('d'));
     }
 
-    public static function getRawUnusedTrafficUsage()
+    public static function getRawUnusedTrafficUsage(): int
     {
         return (new User())->sum('transfer_enable') - (new User())->sum('u') - (new User())->sum('d');
     }
@@ -117,7 +115,7 @@ final class Analytics
         return Tools::autoBytes((new User())->sum('transfer_enable'));
     }
 
-    public static function getRawTotalTraffic()
+    public static function getRawTotalTraffic(): int
     {
         return (new User())->sum('transfer_enable');
     }

+ 0 - 2
src/Services/Subscribe.php

@@ -38,8 +38,6 @@ final class Subscribe
     /**
      * @param $user
      * @param bool $show_all_nodes
-     *
-     * @return Collection
      */
     public static function getUserNodes($user, bool $show_all_nodes = false): Collection
     {

+ 0 - 6
src/Utils/ResponseHelper.php

@@ -24,8 +24,6 @@ final class ResponseHelper
      * @param Response $response
      * @param string $msg
      * @param array $data
-     *
-     * @return ResponseInterface
      */
     public static function successWithData(Response $response, string $msg = '', array $data = []): ResponseInterface
     {
@@ -44,8 +42,6 @@ final class ResponseHelper
      * @param RequestInterface $request
      * @param ResponseInterface $response
      * @param mixed $data
-     *
-     * @return ResponseInterface
      */
     public static function successWithDataEtag(
         RequestInterface $request,
@@ -76,8 +72,6 @@ final class ResponseHelper
      * @param Response $response
      * @param string $msg
      * @param array $data
-     *
-     * @return ResponseInterface
      */
     public static function errorWithData(Response $response, string $msg = '', array $data = []): ResponseInterface
     {

+ 0 - 28
src/Utils/Tools.php

@@ -48,8 +48,6 @@ final class Tools
      * 查询IP归属
      *
      * @param string $ip
-     *
-     * @return string
      */
     public static function getIpLocation(string $ip): string
     {
@@ -93,8 +91,6 @@ final class Tools
      *
      * @param $size
      * @param int $precision
-     *
-     * @return string
      */
     public static function autoBytes($size, int $precision = 2): string
     {
@@ -116,8 +112,6 @@ final class Tools
      * 根据含单位的流量值转换 B 输出
      *
      * @param $size
-     *
-     * @return int|null
      */
     public static function autoBytesR($size): ?int
     {
@@ -141,8 +135,6 @@ final class Tools
      *
      * @param $size
      * @param int $precision
-     *
-     * @return string
      */
     public static function autoMbps($size, int $precision = 2): string
     {
@@ -164,8 +156,6 @@ final class Tools
      * 虽然名字是toMB,但是实际上功能是from MB to B
      *
      * @param $traffic
-     *
-     * @return int
      */
     public static function toMB($traffic): int
     {
@@ -176,8 +166,6 @@ final class Tools
      * 虽然名字是toGB,但是实际上功能是from GB to B
      *
      * @param $traffic
-     *
-     * @return int
      */
     public static function toGB($traffic): int
     {
@@ -186,8 +174,6 @@ final class Tools
 
     /**
      * @param $traffic
-     *
-     * @return float
      */
     public static function flowToMB($traffic): float
     {
@@ -196,8 +182,6 @@ final class Tools
 
     /**
      * @param $traffic
-     *
-     * @return float
      */
     public static function flowToGB($traffic): float
     {
@@ -294,8 +278,6 @@ final class Tools
     /**
      * @param $type
      * @param $str
-     *
-     * @return bool
      */
     public static function isParamValidate($type, $str): bool
     {
@@ -372,8 +354,6 @@ final class Tools
 
     /**
      * @param $input
-     *
-     * @return bool
      */
     public static function isEmail($input): bool
     {
@@ -386,8 +366,6 @@ final class Tools
 
     /**
      * @param $input
-     *
-     * @return bool
      */
     public static function isIPv4($input): bool
     {
@@ -400,8 +378,6 @@ final class Tools
 
     /**
      * @param $input
-     *
-     * @return bool
      */
     public static function isIPv6($input): bool
     {
@@ -414,8 +390,6 @@ final class Tools
 
     /**
      * @param $input
-     *
-     * @return bool
      */
     public static function isInt($input): bool
     {
@@ -431,8 +405,6 @@ final class Tools
      * TODO: Remove this function when PHP 8.3 is minimum requirement and replace it with native function
      *
      * @param string $string
-     *
-     * @return bool
      */
     public static function isJson(string $string): bool
     {