基于账号ID生成固定的 User-Agent Args: account_id: 账号标识(如 email 或 token_id),相同账号返回相同 UA Returns: User-Agent 字符串
(self, account_id: str = None)
| 66 | # 不在 flow2api 本地对提交做批次整形或排队,避免把同批请求打成阶梯。 |
| 67 | |
| 68 | def _generate_user_agent(self, account_id: str = None) -> str: |
| 69 | """基于账号ID生成固定的 User-Agent |
| 70 | |
| 71 | Args: |
| 72 | account_id: 账号标识(如 email 或 token_id),相同账号返回相同 UA |
| 73 | |
| 74 | Returns: |
| 75 | User-Agent 字符串 |
| 76 | """ |
| 77 | # 如果没有提供账号ID,生成随机UA |
| 78 | if not account_id: |
| 79 | account_id = f"random_{random.randint(1, 999999)}" |
| 80 | |
| 81 | # 如果已缓存,直接返回 |
| 82 | if account_id in self._user_agent_cache: |
| 83 | return self._user_agent_cache[account_id] |
| 84 | |
| 85 | # 使用账号ID作为随机种子,确保同一账号生成相同的UA |
| 86 | import hashlib |
| 87 | seed = int(hashlib.md5(account_id.encode()).hexdigest()[:8], 16) |
| 88 | rng = random.Random(seed) |
| 89 | |
| 90 | # fallback 仅在没有 runtime fingerprint 时兜底,直接与当前上游 Windows Chrome 风格对齐 |
| 91 | chrome_versions = ["149.0.0.0"] |
| 92 | ch_version = rng.choice(chrome_versions) |
| 93 | user_agent = ( |
| 94 | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " |
| 95 | f"(KHTML, like Gecko) Chrome/{ch_version} Safari/537.36" |
| 96 | ) |
| 97 | |
| 98 | # 缓存结果 |
| 99 | self._user_agent_cache[account_id] = user_agent |
| 100 | |
| 101 | return user_agent |
| 102 | |
| 103 | def _set_request_fingerprint(self, fingerprint: Optional[Dict[str, Any]]): |
| 104 | """设置当前请求链路的浏览器指纹上下文。""" |
no outgoing calls
no test coverage detected