File size: 15,442 Bytes
f2b63ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"""Grok Token 管理器模块"""

import json
import time
import asyncio
import aiofiles
from pathlib import Path
from curl_cffi.requests import AsyncSession
from typing import Dict, Any, Optional, Tuple

from app.models.grok_models import TokenType, Models
from app.core.exception import GrokApiException
from app.core.logger import logger
from app.core.config import setting
from app.services.grok.statsig import get_dynamic_headers

# 常量定义
RATE_LIMIT_ENDPOINT = "https://grok.com/rest/rate-limits"
REQUEST_TIMEOUT = 30
IMPERSONATE_BROWSER = "chrome133a"
MAX_FAILURE_COUNT = 3
TOKEN_INVALID_CODE = 401  # SSO Token失效
STATSIG_INVALID_CODE = 403  # x-statsig-id失效


class GrokTokenManager:
    """
    Grok Token管理器
    
    单例模式的Token管理器,负责:
    - Token文件的读写操作
    - Token负载均衡
    - Token状态管理
    - 支持普通Token和Super Token
    """
    
    _instance: Optional['GrokTokenManager'] = None
    _lock = asyncio.Lock()

    def __new__(cls) -> 'GrokTokenManager':
        """单例模式实现"""
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self):
        """初始化Token管理器"""
        if hasattr(self, '_initialized'):
            return

        self.token_file = Path(__file__).parents[3] / "data" / "token.json"
        self._file_lock = asyncio.Lock()
        self.token_file.parent.mkdir(parents=True, exist_ok=True)

        # 同步加载初始数据
        self._load_data()
        self._initialized = True

        logger.debug(f"[Token] 管理器初始化完成,文件: {self.token_file}")

    def _load_data(self) -> None:
        """同步加载Token数据(仅用于初始化)"""
        default_data = {
            TokenType.NORMAL.value: {},
            TokenType.SUPER.value: {}
        }

        try:
            if self.token_file.exists():
                with open(self.token_file, "r", encoding="utf-8") as f:
                    self.token_data = json.load(f)
            else:
                self.token_data = default_data
                logger.debug("[Token] 创建新的Token数据文件")
        except (json.JSONDecodeError, IOError) as e:
            logger.error(f"[Token] 加载Token数据失败: {str(e)}")
            self.token_data = default_data

    async def _save_data(self) -> None:
        """异步保存Token数据到文件"""
        try:
            async with self._file_lock:
                async with aiofiles.open(self.token_file, "w", encoding="utf-8") as f:
                    await f.write(json.dumps(self.token_data, indent=2, ensure_ascii=False))
        except IOError as e:
            logger.error(f"[Token] 保存Token数据失败: {str(e)}")
            raise GrokApiException(
                f"Token数据保存失败: {str(e)}",
                "TOKEN_SAVE_ERROR",
                {"file_path": str(self.token_file)}
            )

    @staticmethod
    def _extract_sso(auth_token: str) -> Optional[str]:
        """从认证令牌中提取SSO值"""
        if "sso=" in auth_token:
            return auth_token.split("sso=")[1].split(";")[0]
        logger.warning("[Token] 无法从认证令牌中提取SSO值")
        return None

    def _find_token(self, sso_value: str) -> Tuple[Optional[str], Optional[Dict[str, Any]]]:
        """查找Token数据,返回(token_type, token_data)"""
        for token_type in [TokenType.NORMAL.value, TokenType.SUPER.value]:
            if sso_value in self.token_data[token_type]:
                return token_type, self.token_data[token_type][sso_value]
        return None, None

    async def add_token(self, tokens: list[str], token_type: TokenType) -> None:
        """添加Token到管理器"""
        if not tokens:
            logger.debug("[Token] 尝试添加空的Token列表")
            return

        added_count = 0
        for token in tokens:
            if not token or not token.strip():
                logger.debug("[Token] 跳过空的Token")
                continue

            self.token_data[token_type.value][token] = {
                "createdTime": int(time.time() * 1000),
                "remainingQueries": -1,
                "heavyremainingQueries": -1,
                "status": "active",
                "failedCount": 0,
                "lastFailureTime": None,
                "lastFailureReason": None
            }
            added_count += 1

        await self._save_data()
        logger.info(f"[Token] 成功添加 {added_count} 个 {token_type.value} Token")

    async def delete_token(self, tokens: list[str], token_type: TokenType) -> None:
        """删除指定的Token"""
        if not tokens:
            logger.debug("[Token] 尝试删除空的Token列表")
            return

        deleted_count = 0
        for token in tokens:
            if token in self.token_data[token_type.value]:
                del self.token_data[token_type.value][token]
                deleted_count += 1
            else:
                logger.debug(f"[Token] Token不存在: {token[:10]}...")

        await self._save_data()
        logger.info(f"[Token] 成功删除 {deleted_count} 个 {token_type.value} Token")
    
    def get_tokens(self) -> Dict[str, Any]:
        """获取所有Token数据"""
        return self.token_data.copy()

    def get_token(self, model: str) -> str:
        """获取指定模型的Token"""
        jwt_token = self.select_token(model)
        return f"sso-rw={jwt_token};sso={jwt_token}"
    
    def select_token(self, model: str) -> str:
        """根据模型类型和剩余次数选择最优Token"""
        def select_best_token(tokens_dict: Dict[str, Any]) -> Tuple[Optional[str], Optional[int]]:
            """从 token 字典中选择最佳 token"""
            unused_tokens = []  # remaining = -1 的 token
            used_tokens = []    # remaining > 0 的 token

            for token_key, token_data in tokens_dict.items():
                # 跳过已失效的Token
                if token_data.get("status") == "expired":
                    continue

                remaining = int(token_data.get(remaining_field, -1))

                # 跳过已限流的 token
                if remaining == 0:
                    continue

                # 分类存储
                if remaining == -1:
                    unused_tokens.append(token_key)
                elif remaining > 0:
                    used_tokens.append((token_key, remaining))

            # 优先返回尚未使用的 token
            if unused_tokens:
                return unused_tokens[0], -1

            # 否则返回次数最多的 token
            if used_tokens:
                used_tokens.sort(key=lambda x: x[1], reverse=True)
                return used_tokens[0][0], used_tokens[0][1]

            return None, None

        max_token_key = None
        max_remaining = None

        # 深拷贝
        token_data_snapshot = {
            TokenType.NORMAL.value: self.token_data[TokenType.NORMAL.value].copy(),
            TokenType.SUPER.value: self.token_data[TokenType.SUPER.value].copy()
        }

        if model == "grok-4-heavy":
            # grok-4-heavy 只能使用Super Token + heavy remaining queries
            remaining_field = "heavyremainingQueries"
            max_token_key, max_remaining = select_best_token(token_data_snapshot[TokenType.SUPER.value])
        else:
            # 其他模型使用 remaining Queries
            remaining_field = "remainingQueries"

            # 优先使用普通Token
            max_token_key, max_remaining = select_best_token(token_data_snapshot[TokenType.NORMAL.value])

            # 如果普通Token没有可用的,尝试使用Super Token
            if max_token_key is None:
                max_token_key, max_remaining = select_best_token(token_data_snapshot[TokenType.SUPER.value])

        if max_token_key is None:
            raise GrokApiException(
                f"没有可用Token用于模型 {model}",
                "NO_AVAILABLE_TOKEN",
                {
                    "model": model,
                    "normal_count": len(token_data_snapshot[TokenType.NORMAL.value]),
                    "super_count": len(token_data_snapshot[TokenType.SUPER.value])
                }
            )

        status_text = "未使用" if max_remaining == -1 else f"剩余{max_remaining}次"
        logger.debug(f"[Token] 为模型 {model} 选择Token ({status_text})")
        return max_token_key
    
    async def check_limits(self, auth_token: str, model: str) -> Optional[Dict[str, Any]]:
        """检查并更新模型速率限制"""
        try:
            rate_limit_model_name = Models.to_rate_limit(model)
            logger.debug(f"[Token] 检查模型 {model} (接口模型: {rate_limit_model_name}) 的速率限制")

            # 准备请求
            payload = {"requestKind": "DEFAULT", "modelName": rate_limit_model_name}
            cf_clearance = setting.grok_config.get("cf_clearance", "")
            cookie = f"{auth_token};{cf_clearance}" if cf_clearance else auth_token

            headers = get_dynamic_headers("/rest/rate-limits")
            headers["Cookie"] = cookie

            # 获取代理配置
            proxy_url = setting.grok_config.get("proxy_url", "")
            proxies = {"http": proxy_url, "https": proxy_url} if proxy_url else None

            # 发送异步请求
            async with AsyncSession() as session:
                response = await session.post(
                    RATE_LIMIT_ENDPOINT,
                    headers=headers,
                    json=payload,
                    impersonate=IMPERSONATE_BROWSER,
                    timeout=REQUEST_TIMEOUT,
                    proxies=proxies
                )

                if response.status_code == 200:
                    rate_limit_data = response.json()
                    logger.debug(f"[Token] 成功获取速率限制信息")

                    # 保存速率限制信息
                    sso_value = self._extract_sso(auth_token)
                    if sso_value:
                        if model == "grok-4-heavy":
                            await self.update_limits(sso_value, normal=None, heavy=rate_limit_data.get("remainingQueries", -1))
                            logger.info(f"[Token] 已更新限制: sso={sso_value[:10]}..., heavy={rate_limit_data.get('remainingQueries', -1)}")
                        else:
                            await self.update_limits(sso_value, normal=rate_limit_data.get("remainingTokens", -1), heavy=None)
                            logger.info(f"[Token] 已更新限制: sso={sso_value[:10]}..., 通用={rate_limit_data.get('remainingTokens', -1)}")

                    return rate_limit_data
                else:
                    logger.warning(f"[Token] 获取速率限制失败,状态码: {response.status_code}")
                    return None

        except Exception as e:
            logger.error(f"[Token] 检查速率限制时发生错误: {str(e)}")
            return None

    async def update_limits(self, sso_value: str, normal: Optional[int] = None, heavy: Optional[int] = None) -> None:
        """更新Token限制信息"""
        try:
            for token_type in [TokenType.NORMAL.value, TokenType.SUPER.value]:
                if sso_value in self.token_data[token_type]:
                    if normal is not None:
                        self.token_data[token_type][sso_value]["remainingQueries"] = normal
                    if heavy is not None:
                        self.token_data[token_type][sso_value]["heavyremainingQueries"] = heavy

                    await self._save_data()
                    logger.info(f"[Token] 已更新Token {sso_value[:10]}... 的限制信息")
                    return

            logger.warning(f"[Token] 未找到SSO值为 {sso_value[:10]}... 的Token")

        except Exception as e:
            logger.error(f"[Token] 更新Token限制时发生错误: {str(e)}")
    
    async def record_failure(self, auth_token: str, status_code: int, error_message: str) -> None:
        """记录Token失败信息

        错误码说明:
        - 401: SSO Token失效,会标记Token为expired
        - 403: x-statsig-id失效,不影响Token状态

        Args:
            auth_token: 完整的认证Token (格式: sso-rw=xxx;sso=xxx)
            status_code: HTTP状态码
            error_message: 错误信息
        """
        try:
            # 403错误是x-statsig-id失效,不是Token问题
            if status_code == STATSIG_INVALID_CODE:
                logger.warning(f"[Token] x-statsig-id失效 (403),需要更新配置文件中的x_statsig_id")
                return

            sso_value = self._extract_sso(auth_token)
            if not sso_value:
                return

            _, token_data = self._find_token(sso_value)
            if not token_data:
                logger.warning(f"[Token] 未找到SSO值为 {sso_value[:10]}... 的Token")
                return

            # 更新失败计数
            token_data["failedCount"] = token_data.get("failedCount", 0) + 1
            token_data["lastFailureTime"] = int(time.time() * 1000)
            token_data["lastFailureReason"] = f"{status_code}: {error_message}"

            logger.warning(
                f"[Token] Token {sso_value[:10]}... 失败 (状态码: {status_code}), "
                f"失败次数: {token_data['failedCount']}/{MAX_FAILURE_COUNT}, "
                f"原因: {error_message}"
            )

            # 只有401错误(SSO Token失效)且失败次数达到上限时,标记为失效
            if status_code == TOKEN_INVALID_CODE and token_data["failedCount"] >= MAX_FAILURE_COUNT:
                token_data["status"] = "expired"
                logger.error(
                    f"[Token] SSO Token {sso_value[:10]}... 已被标记为失效 "
                    f"(连续401错误{token_data['failedCount']}次)"
                )

            await self._save_data()

        except Exception as e:
            logger.error(f"[Token] 记录Token失败信息时发生错误: {str(e)}")

    async def reset_failure(self, auth_token: str) -> None:
        """重置Token失败计数

        当Token成功完成请求时调用此方法,用于清除失败记录。

        Args:
            auth_token: 完整的认证Token (格式: sso-rw=xxx;sso=xxx)
        """
        try:
            sso_value = self._extract_sso(auth_token)
            if not sso_value:
                return

            _, token_data = self._find_token(sso_value)
            if not token_data:
                logger.warning(f"[Token] 未找到SSO值为 {sso_value[:10]}... 的Token")
                return

            # 只有在有失败记录时才重置并保存
            if token_data.get("failedCount", 0) > 0:
                token_data["failedCount"] = 0
                token_data["lastFailureTime"] = None
                token_data["lastFailureReason"] = None

                await self._save_data()
                logger.info(f"[Token] Token {sso_value[:10]}... 失败计数已重置")

        except Exception as e:
            logger.error(f"[Token] 重置Token失败计数时发生错误: {str(e)}")


# 全局Token管理器实例
token_manager = GrokTokenManager()