Browse Source

feat: dynamic rate service

https://github.com/Anankke/SSPanel-Uim/issues/2174
M1Screw 2 years ago
parent
commit
4d31d34356
5 changed files with 118 additions and 16 deletions
  1. 2 2
      .gitignore
  2. 12 12
      composer.lock
  3. 103 0
      src/Services/DynamicRate.php
  4. 1 2
      src/Services/Subscribe/Clash.php
  5. 0 0
      storage/framework/twig/.gitkeep

+ 2 - 2
.gitignore

@@ -28,11 +28,11 @@ public/clients/*.*
 
 storage/framework/smarty/cache/*
 storage/framework/smarty/compile/*
-storage/framework/views/*
+storage/framework/twig/*
 storage/*.*
 !storage/framework/smarty/cache/.gitkeep
 !storage/framework/smarty/compile/.gitkeep
-!storage/framework/views/.gitkeep
+!storage/framework/twig/.gitkeep
 
 .user.ini
 public/.user.ini

+ 12 - 12
composer.lock

@@ -123,16 +123,16 @@
         },
         {
             "name": "aws/aws-sdk-php",
-            "version": "3.283.3",
+            "version": "3.283.8",
             "source": {
                 "type": "git",
                 "url": "https://github.com/aws/aws-sdk-php.git",
-                "reference": "4cc8d6c7e856de80d9316f659c4c626d3713f6c1"
+                "reference": "0f73ec85852312a6e971cfe2eebfd7c8091cca34"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/4cc8d6c7e856de80d9316f659c4c626d3713f6c1",
-                "reference": "4cc8d6c7e856de80d9316f659c4c626d3713f6c1",
+                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/0f73ec85852312a6e971cfe2eebfd7c8091cca34",
+                "reference": "0f73ec85852312a6e971cfe2eebfd7c8091cca34",
                 "shasum": ""
             },
             "require": {
@@ -212,9 +212,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.283.3"
+                "source": "https://github.com/aws/aws-sdk-php/tree/3.283.8"
             },
-            "time": "2023-10-12T18:14:56+00:00"
+            "time": "2023-10-19T19:26:52+00:00"
         },
         {
             "name": "bacon/bacon-qr-code",
@@ -4840,16 +4840,16 @@
         },
         {
             "name": "stripe/stripe-php",
-            "version": "v12.7.0",
+            "version": "v12.8.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/stripe/stripe-php.git",
-                "reference": "01a4a316d7c724fd1a111f36f037b698ca425f2c"
+                "reference": "6b6f4a775ad46fee4b1df2df4fdfa574365b1621"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/stripe/stripe-php/zipball/01a4a316d7c724fd1a111f36f037b698ca425f2c",
-                "reference": "01a4a316d7c724fd1a111f36f037b698ca425f2c",
+                "url": "https://api.github.com/repos/stripe/stripe-php/zipball/6b6f4a775ad46fee4b1df2df4fdfa574365b1621",
+                "reference": "6b6f4a775ad46fee4b1df2df4fdfa574365b1621",
                 "shasum": ""
             },
             "require": {
@@ -4893,9 +4893,9 @@
             ],
             "support": {
                 "issues": "https://github.com/stripe/stripe-php/issues",
-                "source": "https://github.com/stripe/stripe-php/tree/v12.7.0"
+                "source": "https://github.com/stripe/stripe-php/tree/v12.8.0"
             },
-            "time": "2023-10-11T17:29:55+00:00"
+            "time": "2023-10-16T18:04:12+00:00"
         },
         {
             "name": "symfony/deprecation-contracts",

+ 103 - 0
src/Services/DynamicRate.php

@@ -0,0 +1,103 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Services;
+
+final class DynamicRate
+{
+    public static function getFullDayRates(
+        float $max_rate,
+        int $max_rate_time,
+        float $min_rate,
+        int $min_rate_time,
+        string $method = 'logistic',
+    ): array|string {
+        if (! self::validateData($max_rate, $max_rate_time, $min_rate, $min_rate_time)) {
+            return 'Invalid data';
+        }
+
+        $rates = [];
+
+        for ($i = 0; $i < 24; $i++) {
+            $rates[] = self::getRateByTime($max_rate, $max_rate_time, $min_rate, $min_rate_time, $i, $method);
+        }
+
+        return $rates;
+    }
+
+    public static function getRateByTime(
+        float $max_rate,
+        int $max_rate_time,
+        float $min_rate,
+        int $min_rate_time,
+        int $time,
+        string $method = 'logistic',
+    ): float|string {
+        if (! self::validateData($max_rate, $max_rate_time, $min_rate, $min_rate_time)) {
+            return 'Invalid data';
+        }
+
+        if ($time === $max_rate_time || $max_rate_time === $min_rate_time || $max_rate === $min_rate) {
+            return $max_rate;
+        }
+
+        if ($time === $min_rate_time) {
+            return $min_rate;
+        }
+
+        if ($time < $min_rate_time) {
+            $time += 24;
+        }
+
+        if ($time > $max_rate_time) {
+            $min_rate_time += 24;
+        }
+
+        return match ($method) {
+            'logistic' => self::logistic($max_rate, $max_rate_time, $min_rate, $min_rate_time, $time),
+            'linear' => self::linear($max_rate, $max_rate_time, $min_rate, $min_rate_time, $time),
+        };
+    }
+
+    public static function validateData(
+        float $max_rate,
+        int $max_rate_time,
+        float $min_rate,
+        int $min_rate_time,
+    ): bool {
+        return ! ($max_rate < 0 ||
+            $min_rate < 0 ||
+            $max_rate_time < 0 ||
+            $min_rate_time < 0 ||
+            $max_rate_time > 24 ||
+            $min_rate_time > 24 ||
+            $min_rate_time > $max_rate_time);
+    }
+
+    public static function logistic(
+        float $max_rate,
+        int $max_rate_time,
+        float $min_rate,
+        int $min_rate_time,
+        int $time,
+    ): float {
+        $k = $time < $max_rate_time ? -0.7 : 1.3;
+        $e = M_E;
+
+        return ($max_rate - $min_rate) / (1 + $e ** ($k * ($time - ($max_rate_time + $min_rate_time) / 2))) + $min_rate;
+    }
+
+    public static function linear(
+        float $max_rate,
+        int $max_rate_time,
+        float $min_rate,
+        int $min_rate_time,
+        int $time,
+    ): float {
+        $k = ($max_rate - $min_rate) / ($max_rate_time - $min_rate_time);
+        $b = $max_rate - $k * $max_rate_time;
+
+        return $k * $time + $b;
+    }
+}

+ 1 - 2
src/Services/Subscribe/Clash.php

@@ -7,7 +7,6 @@ namespace App\Services\Subscribe;
 use App\Services\Subscribe;
 use Symfony\Component\Yaml\Yaml;
 use function array_merge;
-use function in_array;
 use function json_decode;
 
 final class Clash extends Base
@@ -97,7 +96,7 @@ final class Clash extends Base
                     $host = $node_custom_config['header']['request']['headers']['Host'][0] ??
                         $node_custom_config['host'] ?? '';
                     $allow_insecure = $node_custom_config['allow_insecure'] ?? false;
-                    $tls = in_array($security, ['tls', 'xtls']);
+                    $tls = $security === 'tls';
                     // Clash 特定配置
                     $udp = $node_custom_config['udp'] ?? true;
                     $ws_opts = $node_custom_config['ws-opts'] ?? $node_custom_config['ws_opts'] ?? null;

+ 0 - 0
storage/framework/views/.gitkeep → storage/framework/twig/.gitkeep