Commit Graph
59 Commits
Author SHA1 Message Date
SmartUp Developer 5e99abcbc5 feat: 财务对账页面 — GET /api/finance/daily-summary + Finance.vue 2026-07-03 15:28:06 +08:00
SmartUp Developer 1cb13e878e style: remove trailing whitespace in website_sync.py 2026-07-02 21:45:02 +08:00
SmartUp Developer fdbcca4d95 fix: delay binding.percent update until remote sync succeeds and update UI labels to 自动保底盈利 2026-07-02 21:27:52 +08:00
SmartUp Developer 0e77de31ae feat: implement automatic margin pricing and margin reverse engineering in sync 2026-07-02 21:22:37 +08:00
SmartUp Developer 9011c68b9d fix: use equal weight sharing for remaining 15% and quantize min_rate floor comparison to 2 decimal places 2026-07-02 20:45:40 +08:00
SmartUp Developer 8746068cd4 feat: implement priority_weighted_plus_percent pricing algorithm 2026-07-02 20:23:20 +08:00
liumangmang 63cbdf4dca feat: 实现一键同步网站账号上游模型功能 2026-07-02 15:43:00 +08:00
SmartUp Developer e2129d3796 fix: 防止分页漏读和上游异常快照误标 orphaned 2026-07-02 14:21:09 +08:00
SmartUp Developer 00af34c487 fix: Sub2ApiWebsiteClient.list_accounts 支持分页拉取,避免只拿到默认第一页 20 条导致误建重复账号 2026-07-02 11:04:46 +08:00
liumangmang 608ef00920 feat: 实现设置账号并发数功能(支持 bulk-update 与 fallback 逐个更新) 2026-07-02 09:48:59 +08:00
SmartUp DeveloperandClaude Sonnet 4.6 a296525d4e fix: 将 refresh_path 加入 login_password auth_config 白名单
自定义 refresh 路径会被 normalize_auth_config 丢弃,导致只有默认
/auth/refresh 生效。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-02 09:34:53 +08:00
SmartUp DeveloperandClaude Sonnet 4.6 c96d55665d 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>
2026-07-02 09:26:19 +08:00
liumangmang 9c9a4b3423 fix: 对齐 test_account 在 404 响应下的日志成功标志及错误类型口径,与标准接口行为一致 2026-07-02 00:28:34 +08:00
liumangmang 3b23f21ef3 fix: 优化清理失效账号执行响应的 success 标志,对 test_account/delete_account 引入外部 API 日志审计,并对账号 ID 进行安全转义 2026-07-02 00:25:24 +08:00
liumangmang 536c0aa3d4 feat: 实现清理失效账号安全方案(包含预览、SSE测试、安全删除以及本地标记清空) 2026-07-02 00:19:26 +08:00
liumangmang 94d9550f77 feat: 调整新建/重建账号命名格式为 {上游网站名}-{上游分组名/ID}-{KeyID} 2026-07-01 21:11:37 +08:00
liumangmang 7ae9ad9292 fix: 删除 routers/websites 中残留的死代码函数 _build_rate_priority_map,并为 build_target_group_priority_map 引入严格的分组倍率解析,防止非法/缺失倍率的分组以 1.0 的降级值参与排序 2026-07-01 20:26:23 +08:00
liumangmang dd3649a6ad feat: 修正优先级自动计算算法为分目标分组独立排序,并优化一键分组整理完成后仅触发一次网站级优先级重排 2026-07-01 20:19:54 +08:00
liumangmang 8c2f1c13bb feat: 支持网站目标分组轻量管理(新增、编辑、描述展示),不修改和不提交倍率 2026-07-01 17:47:36 +08:00
liumangmang 8abfa4c5ef feat: 重构接口日志页面与接口监控看板,支持HTTP方法过滤和实时轮询 2026-07-01 15:04:32 +08:00
liumangmang bf5470caf3 fix(sync): 优先级同步前置过滤远端失效账号防排序污染,移除 locals 隐患,并将自愈结果纳入本地日志与摘要统计 2026-07-01 10:20:13 +08:00
liumangmang d92537688a fix(priority): keep readable priority sync details 2026-07-01 09:12:39 +08:00
liumangmang fe40fcd4c9 feat(priority): group priority changed notifications by target group and fetch remote account details 2026-07-01 09:00:29 +08:00
liumangmang edc00444dc fix(nox-api): address duplicate creation risks, pending upgrading issues, cache warm error handling, test speedups, and frontend display support for pending keys 2026-06-30 17:25:18 +08:00
liumangmang 4fa6a79c5a feat(nox-api): implement batch creation rate limit resilience and pending backfilling 2026-06-30 17:00:26 +08:00
liumangmang 854a640d48 fix: shorten New-API token names 2026-06-30 16:12:46 +08:00
liumangmang c5bb63cf82 feat: support upstream token refresh 2026-06-30 14:34:40 +08:00
SmartUp Developer 2d1dcb8f9f fix: round website rates to two decimals 2026-06-30 11:26:48 +08:00
liumangmang a914d3d222 feat: add Nox-API upstream support 2026-06-30 11:22:38 +08:00
SmartUp Developer 9600e4ceba fix: backfill account groups before priority reorder 2026-06-03 18:39:21 +08:00
SmartUp Developer b866b387e0 feat: sync upstream keys and reorder priorities 2026-06-03 17:03:11 +08:00
liumangmang a42bcba483 Remove server remote browser support 2026-06-02 19:25:20 +08:00
liumangmang 84148f4a69 feat: support real browser auth import 2026-06-02 13:51:29 +08:00
liumangmang 4f9acdc99c feat(auth-capture): full cookie bundle extraction + richer refresh-auth
Problem: Meow upstream uses Cloudflare, which sets cf_clearance + session
cookies that must all be sent together. The old code only captured a single
session-named cookie via a whitelist, discarding cf_clearance entirely, and
wrote back only 'name=value' instead of the full cookie string.

Changes:

auth_capture_service.py:
  - Add _cookie_matches_hostname(): hostname suffix matching supporting
    dot-prefixed domains (.saki.lat matches api.saki.lat)
  - Add _build_cookie_bundle(): collects ALL cookies matching the current
    page's hostname, returns complete 'name1=v1; name2=v2' string
  - _curate_candidates(): new 'cookie_bundle' candidate type (type=0 in sort,
    highest priority), carries cookie_count + cookie_names in extra fields
  - extract_all(): obtain real-time page URL from session.page.url and pass
    to _curate_candidates so cookie domain filtering is accurate
  - Sort order: cookie_bundle > cookie > bearer_token/api_key > credential
  - Fix bug in original JWT dedup check (was assigning instead of checking)

custom_pages.py:
  - Add logging import + logger
  - _pick_best_candidate(): cookie preferred_auth_type now tries cookie_bundle
    first, then single cookie; bearer/api_key use existing type_map logic
  - RefreshAuthResponse: add optional 'warning' field
  - refresh_auth(): handle ctype='cookie_bundle' same as 'cookie'; always
    write full candidate.value as cookie_string (works for both types)
  - Post-write validation: attempt get_available_groups with new credentials;
    on failure, still commit (lenient mode) but set warning message explaining
    cf_clearance IP-binding as the likely cause; success logs at INFO level

Tests (test_auth_capture.py, 19 cases):
  - _cookie_matches_hostname: exact, dot-prefix subdomain, empty domain,
    different domain, evil-subdomain partial match rejection
  - _build_cookie_bundle: cf_clearance included, cross-domain excluded,
    single cookie, empty value excluded, no cookies
  - _curate_candidates: bundle ranks first, value is full string, bundle
    beats single session cookie, bearer wins when no cookies, empty case,
    cookie_count/cookie_names in extra, session fallback preserved,
    new_api_user propagation to bundle

All 46 tests pass.
2026-06-02 09:32:23 +08:00
liumangmang f17317b13c fix(priority-sync): handle missing rate data and backfill target group on re-import
P1 - Missing rate data now skips account instead of falling back to 1.0:
  In sync_account_priorities_for_upstream(), the rated list now filters
  out accounts whose upstream snapshot has no rate entry for their group_id.
  If after filtering a competitive bucket has fewer than 2 accounts with
  valid rate data, the entire bucket is silently skipped (no update_account
  call, no webhook) rather than treating missing rates as 1.0 and
  potentially triggering spurious notifications.

P2 - Re-importing an existing account now backfills imported_target_group_id:
  In the exists-is-True idempotency branch of import_upstream_keys_as_accounts(),
  if the current request supplies a target_group_id for the account's source group
  and it differs from what is stored, the field is written back and committed.
  This lets operators fix old data by simply re-running the import dialog.

Tests added:
  - test_missing_rate_skips_entire_competitive_group: all accounts in
    competitive group lack snapshot → bucket skipped, no update called
  - test_partial_missing_rate_sufficient_accounts_still_updates: 3 accounts
    in same bucket, 1 missing rate → the 2 with rates still compete normally

All 27 tests pass.
2026-06-01 19:27:35 +08:00
liumangmang e519d1804b fix(priority-sync): narrow account priority update to competitive groups only
Root cause: sync_account_priorities_for_upstream() was doing a global
priority re-rank across ALL imported accounts on a website whenever any
upstream rate changed, triggering spurious account_priority_changed
notifications for accounts in different target groups with no competition.

Fix:
- Add imported_target_group_id / imported_target_group_name to
  UpstreamGeneratedKey (nullable; old data falls back to group_id)
- Writ imported_target_group_id on account import in websites.py
- Rewrite sync_account_priorities_for_upstream():
  * bucket accounts by competition_group = imported_target_group_id or group_id
  * only process buckets with count > 1 (genuine competition)
  * each competitive bucket independently sorted by rate; priority starts at 1
  * single-account groups: completely skipped (no update_account, no notification)
  * no competitive groups at all: early return, no log, no notification
- Remove auto priority update in re-import idempotency path (was also
  incorrect; now fully delegated to sync_account_priorities_for_upstream)
- Fix Sub2ApiWebsiteClient local import in sync fn → use module-level name
  so monkeypatch works correctly in tests

Tests: rewrite test_priority_sync.py
- REMOVED: test_priority_sync_full_website_update (was asserting the buggy behavior)
- NEW: test_no_update_when_different_groups_single_account_each
- NEW: test_same_target_group_two_accounts_updated
- NEW: test_two_target_groups_independent_priority
- NEW: test_old_data_null_target_group_fallback
- NEW: test_single_account_in_mixed_website
- UPDATED: test_priority_sync_log_structure (now requires competitive group)
- KEPT: test_priority_sync_cross_upstream_group, test_import_auto_priority_by_rate

All 25 tests pass (8 priority_sync + 17 existing upstream tests).
2026-06-01 19:13:14 +08:00
liumangmang a949969c4d perf: remote browser CPU / zombie optimization
- Add tini (init: true in compose) to reap orphan Chromium processes
- Reduce screenshot push frequency (active 0.12→0.20s, idle 0.35→1.00s,
  deep idle 1.00→5.00s, backoff 0.60→2.00s)
- Add 5s timeout to screenshot in WebSocket push loop
- close() now wraps context.close() in asyncio.wait_for(10s)
  with browser.close() fallback on timeout
- Two-phase close logging (closing → closed / close_failed)
- Auth-capture sessions evicted after 10min TTL
- shutdown() with timeout protection and logging
- close_ok correctly tracks success through browser fallback path
2026-06-01 15:47:08 +08:00
liumangmang c8ba25f08e feat: live remote key list with auto-upsert and safe group name extraction
- list_generated_keys now fetches live keys from upstream API, merges with
  local DB: remote keys with plaintext values are auto-upserted (by
  group_id+managed_prefix), remote-only keys shown as unimportable
- Use _fetch_remote_managed_prefixes to support custom key prefixes
- Group remote keys by (group_id, prefix), pick latest by key_id
- Extract _remote_group_name helper for safe group name parsing
  (handles dict group field from Meow upstream)
- Frontend excludes orphaned keys from importable list
- Backend import endpoint reconciles upstream before importing
2026-06-01 14:53:40 +08:00
liumangmang bea4344bb3 fix: reconcile upstream keys on list/generate/import to prevent stale key imports
- Extract reconcile_upstream_keys() to website_sync.py (shared scheduler + on-demand)
- Add reconcile_upstream_keys_full() for on-demand reconciliation at three entry points:
  list_generated_keys, generate_keys_by_groups, import_upstream_keys_as_accounts
- Safe on failure: active_group_ids=None / remote_key_ids=None skip cleanup
- Support custom managed_prefix via _fetch_remote_managed_key_ids() helper
- Exclude orphaned keys from frontend importable list
- Remove hardcoded search='SmartUp' from scheduler path
2026-06-01 11:29:37 +08:00
liumangmang 5268f1119b fix: multi-tab concurrency and metadata sync improvements 2026-05-30 10:08:55 +08:00
liumangmang 3ab3a5e26f feat: add multi-tab support to remote browser 2026-05-30 09:51:51 +08:00
liumangmang 5c20ddc8e6 feat: sync account priorities after rate changes 2026-05-29 17:51:12 +08:00
liumangmang c5778bb3e7 feat: persist browser sessions and update admin workflows 2026-05-29 16:00:43 +08:00
SmartUp Developer 42d8731ff7 perf: website_sync 批量查询 + custom_pages origin TTL 缓存 2026-05-25 00:52:38 +08:00
SmartUp Developer 41a439d830 perf: SQLite WAL + 复合索引 + GZip + scheduler jitter + 构建缓存 2026-05-25 00:08:10 +08:00
SmartUp Developer 3a31d185a4 fix: reuse upstream keys for account import 2026-05-24 23:18:40 +08:00
liumangmang 6044b00685 feat: 上游 Key 唯一化、分组导入跳过、账号导入平台识别&远端校验&base_url 注入
- 上游 Key 命名改为 {prefix}-{upstream.id}-{safe_group_name}-{group_id}
- 唯一约束 (upstream_id, group_id, managed_prefix) 加 managed_prefix 列
- 上游检测成功时同步 Key 状态,远端已删/分组已删自动清理
- 重复分组导入跳过,目标网站已存在同名分组返回 exists
- 账号导入平台自动识别(auto/manual 模式)
- 全选可导入 Key 按钮 + 目标分组自动匹配
- 导入幂等:已导入过的 Key 校验远端账号,不存在则重建
- 新增同步接口 POST /sync-imported-upstream-keys
- account_exists() 通过拉取账号列表判断,避免 404 误判
- credentials.base_url 注入来源上游地址,避免 401
- 前端导入弹窗自动同步+刷新按钮+并发/优先级设置
- 新增 12 个测试覆盖同步、幂等、远端删除、校验失败路径
2026-05-21 01:16:39 +08:00
liumangmang 0a27bba296 fix: 修复远程浏览器登录态保留 & 剪贴板同步问题 2026-05-20 10:13:13 +08:00
liumangmang 6cc797f915 feat: remote browser login persistence + balance display + UI consistency
- Retain login state in remote browser profiles (don't delete on disconnect)
- Add GET /api/browser-sessions/{id}/clipboard for clipboard sync
- Add POST /api/browser-sessions/{id}/autofill-login for manual credential fill
- Add DELETE /api/browser-sessions/profiles/{custom_page_id} for login clear
- Add balance tracking with configurable divisor (balance_divisor)
- Health check on session reuse, idle TTL eviction, background cleanup
- Add first-frame watchdog (10s timeout) to prevent infinite loading
- Reconnect browser on active=true when session was closed
- UI: uniform text-only inline buttons (websites + upstreams pages)
- Fix page switch race with closingRemoteSessionPromise
2026-05-20 09:44:20 +08:00
SmartUp DeveloperandClaude Opus 4.7 4c71148ff9 feat: one-click upstream auth refresh from custom page viewer
- Add linked_upstream_id to CustomPage model with DB migration
- New POST /api/custom-pages/{pid}/refresh-auth endpoint extracts
  credentials from active remote browser and updates linked upstream
- PageViewer toolbar shows key icon button when page has linked upstream
- CustomPages form adds upstream dropdown for remote_browser pages
- Auth capture extracts New-Api-User from localStorage uid/user/self API
- Upstream client sends New-Api-User header in cookie auth mode
- Fix auth capture dialog: transparent background, field persistence,
  login URL defaults to base_url/login, focus on click for keyboard input
- Fix upstream test ASCII encoding with non-header characters validation

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-19 09:27:14 +08:00