浏览代码

自动更新失效的 aws waf token

luolongfei 1 年之前
父节点
当前提交
7aca3a7351
共有 7 个文件被更改,包括 217 次插入15 次删除
  1. 1 1
      .env.example
  2. 3 8
      app/Console/FreeNom.php
  3. 83 0
      app/Console/GlobalValue.php
  4. 17 0
      app/Constants/CommonConst.php
  5. 111 6
      app/helpers.php
  6. 1 0
      resources/lang/en.php
  7. 1 0
      resources/lang/zh.php

+ 1 - 1
.env.example

@@ -157,7 +157,7 @@ MOSAIC_SENSITIVE_INFO=0
 # 每次重试间隔根据次数递增
 # Maximum request retry count
 # Retry interval increases based on the number of attempts
-MAX_REQUEST_RETRY_COUNT=2
+MAX_REQUEST_RETRY_COUNT=4
 
 ######################  AWS WAF SOLVER  #########################
 # 识别方案 1:Wit.ai 2:OpenAI whisper(离线版)

+ 3 - 8
app/Console/FreeNom.php

@@ -437,9 +437,6 @@ class FreeNom extends Base
 
         system_log(sprintf(lang('100049'), $totalAccounts));
 
-        $awsWafToken = getAwsWafToken();
-        system_log(sprintf(lang('100139'), $awsWafToken));
-
         foreach ($accounts as $index => $account) {
             try {
                 $this->username = $account['username'];
@@ -449,11 +446,9 @@ class FreeNom extends Base
                 system_log(sprintf(lang('100050'), get_local_num($num), $this->username, $num, $totalAccounts));
 
                 $this->jar = new CookieJar(); // 所有请求共用一个 CookieJar 实例
-                $cookie = new SetCookie();
-                $cookie->setName('aws-waf-token');
-                $cookie->setValue($awsWafToken);
-                $cookie->setDomain('.my.freenom.com');
-                $this->jar->setCookie($cookie);
+
+                $awsWafToken = getAwsWafToken();
+                $this->jar->setCookie(buildAwsWafCookie($awsWafToken));
 
                 $this->login($this->username, $this->password);
 

+ 83 - 0
app/Console/GlobalValue.php

@@ -0,0 +1,83 @@
+<?php
+/**
+ * @author luolongf <[email protected]>
+ * @date 2024-01-25
+ * @time 18:22
+ */
+
+namespace Luolongfei\App\Console;
+
+class GlobalValue extends Base
+{
+    /**
+     * @var GlobalValue
+     */
+    private static $instance;
+
+    /**
+     * @var array $values
+     */
+    private $values = [];
+
+    /**
+     * @return GlobalValue|self
+     */
+    public static function getInstance()
+    {
+        if (!self::$instance instanceof self) {
+            self::$instance = new self();
+        }
+
+        return self::$instance;
+    }
+
+    private function __construct()
+    {
+    }
+
+    private function __clone()
+    {
+    }
+
+    /**
+     * @param string $name
+     * @param string $value
+     *
+     * @return void
+     */
+    public function set(string $name, string $value)
+    {
+        $this->values[$name] = $value;
+    }
+
+    /**
+     * @param string $name
+     * @param string|null $default
+     *
+     * @return string|null
+     */
+    public function get(string $name, ?string $default = null)
+    {
+        return isset($this->values[$name]) ? $this->values[$name] : $default;
+    }
+
+    /**
+     * @param string $name
+     *
+     * @return void
+     */
+    public function del(string $name)
+    {
+        unset($this->values[$name]);
+    }
+
+    /**
+     * @param string $name
+     *
+     * @return bool
+     */
+    public function has(string $name)
+    {
+        return isset($this->values[$name]);
+    }
+}

+ 17 - 0
app/Constants/CommonConst.php

@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * @author luolongf <[email protected]>
+ * @date 2024-01-26
+ * @time 12:22
+ */
+
+namespace Luolongfei\App\Constants;
+
+class CommonConst
+{
+    /**
+     * @var string aws waf token 的 key
+     */
+    public const AWS_WAF_TOKEN = 'AWS_WAF_TOKEN';
+}

+ 111 - 6
app/helpers.php

@@ -7,16 +7,20 @@
  * @time 16:34
  */
 
+use GuzzleHttp\Client;
+use GuzzleHttp\Cookie\CookieJar;
+use GuzzleHttp\Cookie\SetCookie;
+use Luolongfei\App\Console\GlobalValue;
+use Luolongfei\App\Console\MigrateEnvFile;
+use Luolongfei\App\Console\Upgrade;
+use Luolongfei\App\Constants\CommonConst;
 use Luolongfei\App\Exceptions\LlfException;
 use Luolongfei\Libs\Argv;
 use Luolongfei\Libs\Config;
-use Luolongfei\Libs\Log;
 use Luolongfei\Libs\Env;
 use Luolongfei\Libs\Lang;
+use Luolongfei\Libs\Log;
 use Luolongfei\Libs\PhpColor;
-use Luolongfei\App\Console\MigrateEnvFile;
-use Luolongfei\App\Console\Upgrade;
-use GuzzleHttp\Client;
 
 if (!function_exists('config')) {
     /**
@@ -1946,7 +1950,20 @@ if (!function_exists('autoRetry')) {
                 $sleepTime = getSleepTime($retryCount);
 
                 if (stripos($e->getMessage(), '405') !== false) {
-                    system_log(sprintf(lang('exception_msg.34520015'), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
+                    // aws waf token 失效,将重新获取新的 token
+                    $handleInvalidToken = false;
+                    foreach ($params as &$param) {
+                        if ($param instanceof CookieJar) {
+                            $handleInvalidToken = true;
+                            $sleepTime = 1;
+                            delGlobalValue(CommonConst::AWS_WAF_TOKEN);
+                            $param->setCookie(buildAwsWafCookie(getAwsWafToken()));
+
+                            break;
+                        }
+                    }
+
+                    system_log($handleInvalidToken ? \lang('exception_msg.34520019') : sprintf(lang('exception_msg.34520015'), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
                 } else {
                     system_log(sprintf(lang('exception_msg.34520016'), $e->getMessage(), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
                 }
@@ -1957,6 +1974,25 @@ if (!function_exists('autoRetry')) {
     }
 }
 
+if (!function_exists('buildAwsWafCookie')) {
+    /**
+     * 构建 aws waf cookie
+     *
+     * @param string $awsWafToken
+     *
+     * @return SetCookie
+     */
+    function buildAwsWafCookie(string $awsWafToken)
+    {
+        $cookie = new SetCookie();
+        $cookie->setName('aws-waf-token');
+        $cookie->setValue($awsWafToken);
+        $cookie->setDomain('.my.freenom.com');
+
+        return $cookie;
+    }
+}
+
 if (!function_exists('getSleepTime')) {
     /**
      * 获取睡眠秒数
@@ -1988,6 +2024,13 @@ if (!function_exists('getAwsWafToken')) {
      */
     function getAwsWafToken()
     {
+        // 优先从全局变量中获取
+        $AWS_WAF_TOKEN = getGlobalValue(CommonConst::AWS_WAF_TOKEN);
+        if ($AWS_WAF_TOKEN) {
+            return $AWS_WAF_TOKEN;
+        }
+
+        // 调用自建接口获取
         $AWS_WAF_SOLVER_URL = \env('AWS_WAF_SOLVER_URL');
         if (!$AWS_WAF_SOLVER_URL) {
             throw new LlfException('34520017');
@@ -2046,7 +2089,12 @@ if (!function_exists('getAwsWafToken')) {
                         throw new \Exception('no result');
                     }
 
-                    return $body['data']['result'];
+                    $awsWafToken = $body['data']['result'] ?? '';
+                    setGlobalValue(CommonConst::AWS_WAF_TOKEN, $awsWafToken);
+
+                    system_log(sprintf(lang('100139'), $awsWafToken));
+
+                    return $awsWafToken;
                 }
             } catch (\Exception $e) {
                 system_log('<red>getAwsWafToken error:</red> ' . $e->getMessage());
@@ -2060,3 +2108,60 @@ if (!function_exists('getAwsWafToken')) {
         throw new LlfException('34520018');
     }
 }
+
+if (!function_exists('getGlobalValue')) {
+    /**
+     * 获取全局变量
+     *
+     * @param string $name
+     *
+     * @return string|null
+     */
+    function getGlobalValue(string $name, ?string $default = null)
+    {
+        return GlobalValue::getInstance()->get($name, $default);
+    }
+}
+
+if (!function_exists('setGlobalValue')) {
+    /**
+     * 设置全局变量
+     *
+     * @param string $name
+     * @param string $value
+     *
+     * @return void
+     */
+    function setGlobalValue(string $name, string $value)
+    {
+        GlobalValue::getInstance()->set($name, $value);
+    }
+}
+
+if (!function_exists('hasGlobalValue')) {
+    /**
+     * 是否存在全局变量
+     *
+     * @param string $name
+     *
+     * @return bool
+     */
+    function hasGlobalValue(string $name)
+    {
+        return GlobalValue::getInstance()->has($name);
+    }
+}
+
+if (!function_exists('delGlobalValue')) {
+    /**
+     * 删除全局变量
+     *
+     * @param string $name
+     *
+     * @return void
+     */
+    function delGlobalValue(string $name)
+    {
+        GlobalValue::getInstance()->del($name);
+    }
+}

+ 1 - 0
resources/lang/en.php

@@ -26,6 +26,7 @@ return [
         '34520016' => 'Request failed: %s, will retry in %d seconds, up to %d times. [%d/%d]',
         '34520017' => 'The AWS_WAF_SOLVER_URL environment variable is not configured',
         '34520018' => 'Failed to get AWS WAF Token',
+        '34520019' => '<light_green>Will automatically use the new AWS WAF Token</light_green>',
     ],
     'messages' => [
         '100001' => 'The cookie value named WHMCSZH5eHTGhfvzP could not be obtained, so this login is not valid, please check if your account or password is correct',

+ 1 - 0
resources/lang/zh.php

@@ -26,6 +26,7 @@ return [
         '34520016' => '请求失败,原因为 %s,将在 %d 秒后重试,最多重试 %d 次。[%d/%d]',
         '34520017' => '未配置 AWS_WAF_SOLVER_URL 环境变量',
         '34520018' => '未能获取到 AWS WAF Token',
+        '34520019' => '<light_green>将自动使用新的 AWS WAF Token</light_green>',
     ],
     'messages' => [
         '100001' => '未能取得名为 WHMCSZH5eHTGhfvzP 的 cookie 值,故本次登录无效,请检查你的账户或密码是否正确。',