feat: 优化上游认证复用与 wangwang888 分组接口
## 上游认证优化 ### 核心改进 - login_password 类型上游优先复用已保存 token,过期后自动 refresh,失败再重新登录 - 新增 ensure_authenticated() 方法替代直接调用 login(),减少不必要的登录请求 - 初始化时从 auth_config 自动加载已保存的 token 和 user_id - 请求遇到 401 时自动尝试 refresh 或 login 并重试一次 ### 实现细节 - UpstreamClient.__init__: 初始化时加载 auth_config 中的 token/new_api_user - _is_login_password_with_refresh(): 判断是否支持 refresh - _token_is_expired(): 检查 token 是否过期(提前 60 秒) - _refresh_login_password_token(): 刷新 login_password 类型的 token - ensure_authenticated(): 优先复用 token,过期后 refresh,失败再 login - _send_request(): 401 时针对 login_password 类型先 refresh 再 login 并重试 ### 调用点更新 - scheduler.py: _check_upstream, _sync_upstream_keys - website_sync.py: reconcile_upstream_keys_full - upstreams.py: list_generated_keys, generate_keys_by_groups, test_all, check_now ## wangwang888 分组接口优化 - 提供数据库更新脚本 update_wangwang888_groups_endpoint.py - 将 wangwang888 的 groups_endpoint 从 /groups 改为 /groups/all - 减少不必要的 /api/v1/admin/groups 统计开销 - website_client 已有 fallback 逻辑保证兼容性 ## 测试 - 新增 test_upstream_login_password_refresh.py(7 个测试用例) - 验证 token 复用、refresh、401 重试等逻辑 - 更新 test_upstream_key_sync.py 的 FakeClient 增加 ensure_authenticated - 所有现有测试保持通过(87 passed) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
9c9a4b3423
commit
c96d55665d
@@ -198,7 +198,7 @@ def list_generated_keys(uid: int, db: Session = Depends(get_db), _=Depends(get_c
|
||||
target_id=upstream.id,
|
||||
target_name=upstream.name,
|
||||
) as client:
|
||||
client.login()
|
||||
client.ensure_authenticated()
|
||||
for prefix in website_sync._fetch_remote_managed_prefixes(db, uid):
|
||||
# list_api_keys 的参数在 Sub2API 中通常是 search 参数
|
||||
remote_keys_list.extend(client.list_api_keys(search=prefix, status="active"))
|
||||
@@ -634,7 +634,7 @@ def generate_keys_by_groups(
|
||||
target_name=u.name,
|
||||
) as client:
|
||||
try:
|
||||
client.login()
|
||||
client.ensure_authenticated()
|
||||
groups = client.get_available_groups(u.groups_endpoint)
|
||||
# New-API/Nox 上游:批量前预热 token 列表缓存(一次 /api/token/ 拉取),
|
||||
# 避免每个分组都调 /api/token/search 触发 Nox 搜索限流(10 次/60 秒)。
|
||||
@@ -686,7 +686,7 @@ def _test_upstream_core(db: Session, u: Upstream) -> UpstreamBatchActionItem:
|
||||
target_id=u.id,
|
||||
target_name=u.name,
|
||||
) as client:
|
||||
client.login()
|
||||
client.ensure_authenticated()
|
||||
groups = client.get_available_groups(u.groups_endpoint)
|
||||
# 余额(与单行 test_upstream 保持一致)
|
||||
if u.balance_endpoint and u.balance_response_path:
|
||||
@@ -728,7 +728,7 @@ def _check_now_core(db: Session, u: Upstream) -> tuple[str, bool]:
|
||||
target_id=u.id,
|
||||
target_name=u.name,
|
||||
) as client:
|
||||
client.login()
|
||||
client.ensure_authenticated()
|
||||
groups = client.get_available_groups(u.groups_endpoint)
|
||||
raw_rates = client.get_group_rates(u.rate_endpoint)
|
||||
snapshot = build_snapshot(uid, u.base_url, u.api_prefix, groups, raw_rates)
|
||||
|
||||
Reference in New Issue
Block a user