fix: shorten New-API token names
This commit is contained in:
@@ -24,7 +24,7 @@ from app.schemas.upstream import (
|
||||
UpstreamCreate, UpstreamUpdate, UpstreamResponse, SnapshotResponse, TestResult,
|
||||
UpstreamBatchActionItem, UpstreamBatchActionSummary, UpstreamBatchActionResponse,
|
||||
)
|
||||
from app.services.upstream_client import UpstreamClient, UpstreamError, build_snapshot, mask_secret, _extract_key_value
|
||||
from app.services.upstream_client import UpstreamClient, UpstreamError, build_snapshot, build_new_api_token_name, mask_secret, _extract_key_value
|
||||
from app.services.auth_config import MASK, mask_auth_config, normalize_auth_config
|
||||
from app.services.snapshot_service import diff_snapshots
|
||||
from app.services import scheduler as sched_svc
|
||||
@@ -377,10 +377,27 @@ def _ensure_group_key(
|
||||
"""确保一个上游分组有一个 SmartUp 前缀 Key:存在则 upsert,不存在则创建。"""
|
||||
gid = _group_id(group)
|
||||
gname = _group_name(group, gid)
|
||||
# 使用稳定的 upstream_id + group_id 而非可变名称,避免因改名产生重复
|
||||
# 可读 Key 名:{prefix}-{upstream.id}-{安全的分组名}-{group_id}
|
||||
safe_group_name = re.sub(r"[^a-zA-Z0-9\u4e00-\u9fff_-]", "", gname)[:30] if gname else gid
|
||||
stable_name = f"{prefix}-{upstream.id}-{safe_group_name}-{gid}"
|
||||
|
||||
# 根据上游类型决定 token 名格式:
|
||||
# - New-API/Nox-API:用新短名(hash8,≤50 字节)
|
||||
# - Sub2API 等其他上游:保持旧格式({prefix}-{id}-{safename}-{gid})
|
||||
use_short_name = _is_new_api_user_upstream(upstream) or _is_nox_api_upstream(upstream)
|
||||
if use_short_name:
|
||||
try:
|
||||
stable_name = build_new_api_token_name(prefix, upstream.id, gid, gname)
|
||||
except ValueError as exc:
|
||||
# prefix 过长,骨架已超 50 字节,直接返回清晰错误
|
||||
return GeneratedUpstreamKeyResponse(
|
||||
upstream_id=upstream.id,
|
||||
group_id=gid,
|
||||
group_name=gname,
|
||||
key_name=f"{prefix}-{upstream.id}--??",
|
||||
status="failed",
|
||||
error=str(exc),
|
||||
)
|
||||
else:
|
||||
safe_group_name = re.sub(r"[^a-zA-Z0-9\u4e00-\u9fff_-]", "", gname)[:30] if gname else gid
|
||||
stable_name = f"{prefix}-{upstream.id}-{safe_group_name}-{gid}"
|
||||
|
||||
with _generate_key_lock:
|
||||
try:
|
||||
@@ -396,10 +413,18 @@ def _ensure_group_key(
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if row and row.key_id:
|
||||
# 本地已有记录,检查远端是否仍存在
|
||||
if row and row.key_name:
|
||||
# 本地已有记录(无论 key_id 是否存在),优先用本地已存的 key_name 查远端
|
||||
# 这样旧格式 key_name 也能找回远端对应 token,不重复创建
|
||||
lookup_name = row.key_name
|
||||
found_by_name = lookup_name # 记录实际命中的远端名称
|
||||
try:
|
||||
existing = client.find_smartup_group_key(gid, stable_name, prefix)
|
||||
existing = client.find_smartup_group_key(gid, lookup_name, prefix)
|
||||
# 若旧格式未找到,再尝试新格式(同一分组可能已在远端迁移)
|
||||
if existing is None and lookup_name != stable_name:
|
||||
existing = client.find_smartup_group_key(gid, stable_name, prefix)
|
||||
if existing is not None:
|
||||
found_by_name = stable_name
|
||||
except Exception:
|
||||
existing = None
|
||||
if existing:
|
||||
@@ -412,6 +437,10 @@ def _ensure_group_key(
|
||||
row.masked_key = masked
|
||||
elif masked:
|
||||
row.masked_key = str(masked)
|
||||
# P3:若是通过新短名找到(旧名已消失),同步更新本地 key_name
|
||||
# 保证 UI 和后续 lookup 与远端名称一致
|
||||
if found_by_name != lookup_name:
|
||||
row.key_name = found_by_name
|
||||
row.raw_json = json.dumps(existing, ensure_ascii=False)
|
||||
row.status = "exists"
|
||||
row.updated_at = datetime.now(timezone.utc)
|
||||
@@ -422,8 +451,29 @@ def _ensure_group_key(
|
||||
# 远端不存在,需要重新创建
|
||||
row.status = "replaced"
|
||||
|
||||
# 2. 查远端是否有同名 Key(防止并发时另一个请求已创建)
|
||||
existing = client.find_smartup_group_key(gid, stable_name, prefix)
|
||||
# 2. 查远端是否有同名 Key
|
||||
# 对 New-API/Nox 上游:先按旧格式查(本地无记录但远端可能仍是旧格式),
|
||||
# 再按新短名查(并发创建防重);最后才创建。
|
||||
names_to_search: list[str] = []
|
||||
if use_short_name and not row:
|
||||
# 构造旧格式 token 名,尝试导入已存在的旧格式远端 token
|
||||
old_safe = re.sub(r"[^a-zA-Z0-9\u4e00-\u9fff_-]", "", gname)[:30] if gname else gid
|
||||
old_format_name = f"{prefix}-{upstream.id}-{old_safe}-{gid}"
|
||||
if old_format_name != stable_name:
|
||||
names_to_search.append(old_format_name)
|
||||
names_to_search.append(stable_name)
|
||||
|
||||
existing = None
|
||||
found_name = stable_name
|
||||
for candidate_name in names_to_search:
|
||||
try:
|
||||
existing = client.find_smartup_group_key(gid, candidate_name, prefix)
|
||||
except Exception:
|
||||
existing = None
|
||||
if existing:
|
||||
found_name = candidate_name
|
||||
break
|
||||
|
||||
if existing:
|
||||
key_id = str(existing.get("id") or "")
|
||||
key_value = _extract_plaintext_key(existing)
|
||||
@@ -444,7 +494,7 @@ def _ensure_group_key(
|
||||
group_id=gid,
|
||||
group_name=gname,
|
||||
key_id=key_id or None,
|
||||
key_name=stable_name,
|
||||
key_name=found_name,
|
||||
key_value=key_value or "",
|
||||
masked_key=masked,
|
||||
raw_json=json.dumps(existing, ensure_ascii=False),
|
||||
@@ -456,7 +506,7 @@ def _ensure_group_key(
|
||||
db.refresh(row)
|
||||
return _key_response(row, include_value=False)
|
||||
|
||||
# 3. 远端不存在,创建新 Key
|
||||
# 3. 远端不存在,创建新 Key(使用新短名)
|
||||
created = client.create_api_key(
|
||||
stable_name,
|
||||
gid,
|
||||
@@ -502,8 +552,8 @@ def _ensure_group_key(
|
||||
group_name=gname,
|
||||
key_name=stable_name,
|
||||
status="failed",
|
||||
error=str(exc),
|
||||
)
|
||||
error=str(exc),
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{uid}/keys/generate-by-groups", response_model=GenerateKeysByGroupsResponse)
|
||||
|
||||
Reference in New Issue
Block a user