diff --git a/README.md b/README.md index 20688fe..7045a5c 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,12 @@ The project was originally named `cc-gateway`; it was renamed to avoid confusion - OpenAI base64 data `image_url` conversion to Command Code / Anthropic-style image blocks - Multiple Command Code accounts with round-robin rotation and automatic failover (401/403/429/5xx), including per-account 429 cooldown - Multiple local client API keys with per-key usage tracking, managed in the WebUI or `config.yaml` -- Embedded single-file WebUI: usage dashboard, account/key management, model exposure editor, live settings, and log tail +- Embedded single-file WebUI: usage dashboard, account/key management, model exposure editor, live settings, quota dashboard, and log tail - Browser OAuth helper for obtaining a Command Code API key (CLI or from the WebUI); each OAuth run adds an account - Local bearer-token auth for clients and a separate admin password for the WebUI - CORS enabled for local UI clients -- Usage counters (global, per-account, and per-client-key) persisted to `usage.json` +- Usage counters (global, per-account, and per-client-key) and cached quota snapshots persisted to `usage.json` +- Command Code quota dashboard: 5-hour rolling / weekly / estimated monthly progress bars, credit balances, plan and billing period, refreshed in the background every 5 minutes - Health endpoint: `GET /health` - Usage endpoint: `GET /usage` @@ -241,6 +242,14 @@ Set the base URL to your local gateway: http://localhost:11434/v1 ``` +If nginx and cmdcode2api run on the same host, keep forwarding Cloudflare's +`CF-Connecting-IP` and `X-Forwarded-For` headers. The server accepts these +headers only from loopback proxy connections, then uses the resolved address +for HTTP logs and admin login rate limiting. Direct connections with forged +proxy headers continue to use their TCP peer address. If nginx itself also +needs `$remote_addr` to represent the end user, configure +`real_ip_header CF-Connecting-IP` and Cloudflare's published proxy CIDRs. + Use any key from the `api_keys` list in `config.yaml` as the bearer token. ### curl example @@ -344,8 +353,8 @@ pointed at any running instance. Features: -- **Overview** — version, uptime, listen address, usage counters, account/key/model summaries -- **Accounts** — add (paste a key or run OAuth with an optional callback URL), edit name/key, enable/disable, connectivity test, delete; per-account requests, tokens, errors, cooldown state, and last error. OAuth-added accounts are named after the Command Code user automatically +- **Overview** — version, uptime, listen address, usage counters, account/key/model summaries, and a quota sync summary (synced / exceeded / low-balance accounts, last refresh) +- **Accounts** — add (paste a key or run OAuth with an optional callback URL), edit name/key, enable/disable, connectivity test, quota refresh, delete; per-account requests, tokens, errors, cooldown state, last error, and quota (5-hour / weekly / estimated monthly bars, balances, plan, billing period). OAuth-added accounts are named after the Command Code user automatically - **Models** — checkbox list of upstream models; checked = exposed via `/v1/models` and callable, unchecked = hidden. This is the editor for `exclude_models` and applies live - **Keys** — create local client API keys (always server-generated), enable/disable, copy, delete; per-key request and token usage. Keys are masked in the list — reveal or copy them on demand (the full value is shown once at creation) - **Settings** — edit `base_url` (live), `host`/`port`/`webui` (persisted, applied on restart), and change the admin password (requires the current password; every existing admin session is kicked afterwards) @@ -353,6 +362,31 @@ Features: Changes to accounts and settings are written back to `config.yaml` immediately. +### Quota display + +The Accounts tab shows each account's Command Code quota, read with the same +API key from the undocumented `/alpha/*` endpoints (`whoami`, +`billing/credits`, `billing/subscriptions`, `usage/summary`): + +- **5-hour** and **weekly** bars come straight from the upstream + `windowLimits` objects. Bars grade amber at ≥50% used, heavier amber at + ≥75%, and red at ≥90%. +- **Monthly** is *derived*: the API exposes no monthly window, so the cap + comes from the community CLI's plan mapping (`individual-pro` → 30, + `individual-pro-v1` → 80, …), with `used = cap − remaining credits`. It is + labelled as estimated; unknown plans fall back to balance-only display. +- Credit balances (monthly remaining / purchased / free), plan name and + status, billing-period end, and billing-period totals. +- A per-account **refresh quota** button and a **refresh all** button. + +Quota refresh runs once shortly after startup and every 5 minutes afterwards; +the latest snapshot is cached in `usage.json` so it survives restarts. A failed +query keeps the last successful snapshot and only records the error and check +time. These endpoints come from +[commandcode-usage](https://github.com/MAXeaglet/commandcode-usage); they are +unofficial, so the parser tolerates field drift (camelCase or snake_case, epoch +seconds / milliseconds / ISO timestamps, flat or `data`-wrapped responses). + ### Admin API The UI talks to a JSON API under `/admin/api/*`, authenticated with @@ -365,6 +399,8 @@ POST /admin/api/accounts {"name": "...", "api_key": "..."} PATCH /admin/api/accounts/{id} {"enabled": true}, {"name": "..."} or {"api_key": "..."} DELETE /admin/api/accounts/{id} POST /admin/api/accounts/{id}/test +POST /admin/api/accounts/{id}/quota/refresh +POST /admin/api/quotas/refresh {"id": "..."} optional — omit to refresh every account GET /admin/api/models PUT /admin/api/models {"exposed": ["model-id", ...]} GET /admin/api/keys @@ -380,6 +416,10 @@ GET /admin/api/oauth/status POST /admin/api/oauth/cancel ``` +`GET /admin/api/accounts` includes a nested `quota` object per account; +`GET /admin/api/overview` includes a `quotas` summary (`synced`, `exceeded`, +`low_balance`, `last_checked_at`). + Security notes: admin authentication is rate limited per source IP (5 failed attempts in 10 minutes locks the source out for 15 minutes), responses carry hardening headers (CSP, `X-Frame-Options: DENY`, diff --git a/README.zh-CN.md b/README.zh-CN.md index b6b3dc8..2aea715 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -142,8 +142,8 @@ http://localhost:11434/webui 功能: -- **概览**:版本、运行时长、监听地址、用量统计、账号/密钥/模型概览 -- **账号**:添加(粘贴 Key 或 OAuth,OAuth 支持填写回调地址)、编辑名称/Key、启用/禁用、连通性测试、删除;展示每账号请求数、tokens、错误、冷却状态、最近错误。OAuth 添加的账号按登录账号名自动命名 +- **概览**:版本、运行时长、监听地址、用量统计、账号/密钥/模型概览、额度同步汇总(已同步 / 超限 / 低余额账号数、最近刷新时间) +- **账号**:添加(粘贴 Key 或 OAuth,OAuth 支持填写回调地址)、编辑名称/Key、启用/禁用、连通性测试、刷新额度、删除;展示每账号请求数、tokens、错误、冷却状态、最近错误,以及额度(5 小时 / 周 / 按月估算进度条、余额、套餐、账期)。OAuth 添加的账号按登录账号名自动命名 - **模型**:上游模型复选框列表,勾选 = 对外提供(`/v1/models` 可见、可调用),取消勾选 = 隐藏并拒绝调用;本页即 exclude_models 的可视化编辑器,改动即时生效 - **密钥**:新建调用本网关的客户端 API Key(服务端自动生成,不支持手动指定值)、复制、启用/禁用、删除;每把密钥独立的请求与 token 统计。列表中密钥默认打码,可按需显示/复制(完整值仅在创建时展示一次) - **设置**:`base_url`(即时生效)、`host`/`port`/`webui`(写盘后重启生效)、修改管理密码(需提供原密码,成功后踢出所有已登录管理会话);exclude_models 已移至「模型」页维护 @@ -151,6 +151,27 @@ http://localhost:11434/webui 账号与设置的修改会立即写回 `config.yaml`,无需重启。 +### 额度展示 + +「账号」页展示每个账号的 Command Code 额度,使用同一个 API Key 读取未公开的 +`/alpha/*` 接口(`whoami`、`billing/credits`、`billing/subscriptions`、 +`usage/summary`): + +- **5 小时**、**本周**进度条直接来自上游 `windowLimits`;已用 ≥50% 显示黄色、 + ≥75% 深黄、≥90% 红色。 +- **月度**为推算值:接口没有月度窗口对象,上限来自社区 CLI 的套餐映射 + (`individual-pro` → 30、`individual-pro-v1` → 80 等),已用 = 上限 − 剩余 + 月度额度,界面上标注“按套餐估算”;未知套餐仅显示余额。 +- 余额(月度剩余 / 充值 / 免费)、套餐名称与状态、账期结束时间、账期用量统计。 +- 账号页提供单账号「刷新额度」与「刷新全部额度」按钮。 + +额度会在启动后不久自动刷新一次,之后每 5 分钟刷新一次,快照缓存在 +`usage.json` 中,重启后仍然保留。查询失败时保留上一次成功的数据,只更新错误与 +查询时间。接口路径取自 +[commandcode-usage](https://github.com/MAXeaglet/commandcode-usage),属未公开接口, +解析层兼容字段漂移(camelCase / snake_case、秒 / 毫秒 / ISO 时间、顶层或 `data` +嵌套)。 + ### 管理 API UI 通过 `/admin/api/*` 访问管理接口,鉴权方式为 @@ -163,6 +184,8 @@ POST /admin/api/accounts {"name": "...", "api_key": "..."} PATCH /admin/api/accounts/{id} {"enabled": true} 或 {"name": "..."} DELETE /admin/api/accounts/{id} POST /admin/api/accounts/{id}/test +POST /admin/api/accounts/{id}/quota/refresh +POST /admin/api/quotas/refresh {"id": "..."} 可选,省略则刷新全部账号 GET /admin/api/models PUT /admin/api/models {"exposed": ["model-id", ...]} GET /admin/api/keys @@ -178,6 +201,9 @@ GET /admin/api/oauth/status POST /admin/api/oauth/cancel ``` +`GET /admin/api/accounts` 每个账号带嵌套 `quota` 对象;`GET /admin/api/overview` +带 `quotas` 汇总(`synced`、`exceeded`、`low_balance`、`last_checked_at`)。 + WebUI 安全机制:管理接口按来源 IP 限速(10 分钟内失败 5 次锁定 15 分钟)、 响应携带安全头(CSP、`X-Frame-Options: DENY`、`nosniff`、 `Referrer-Policy: no-referrer`)且禁用缓存、修改密码需原密码并使所有会话 @@ -280,6 +306,13 @@ http://localhost:11434/v1 https://example.com/ai/v1 ``` +如果 nginx 与 cmdcode2api 在同一台主机,请继续转发 Cloudflare 的 +`CF-Connecting-IP` 和 `X-Forwarded-For`。服务端只接受来自本机回环代理连接的 +这些请求头,并将解析后的地址用于 HTTP 日志和管理登录限流;直连请求即使携带 +伪造请求头,也仍使用 TCP 对端地址。若还需要让 nginx 自身的 `$remote_addr` +表示最终用户,请在 nginx 中配置 `real_ip_header CF-Connecting-IP`,并填写 +Cloudflare 官方公布的代理网段。 + 客户端 Bearer Token 使用 `config.yaml` 里 `api_keys` 列表中的任意一把密钥。 ## 模型 ID @@ -333,4 +366,3 @@ usage.json .oauth_state .oauth_url ``` - diff --git a/internal/app/accounts.go b/internal/app/accounts.go index f03cdb4..af65f49 100644 --- a/internal/app/accounts.go +++ b/internal/app/accounts.go @@ -200,6 +200,14 @@ func (p *AccountPool) Len() int { return len(p.accounts) } +// List returns a snapshot of the pool's accounts for background jobs that +// iterate without holding the pool lock. +func (p *AccountPool) List() []*Account { + p.mu.RLock() + defer p.mu.RUnlock() + return append([]*Account(nil), p.accounts...) +} + func (p *AccountPool) EnabledCount() int { p.mu.RLock() defer p.mu.RUnlock() diff --git a/internal/app/admin.go b/internal/app/admin.go index cc6a304..a1e1358 100644 --- a/internal/app/admin.go +++ b/internal/app/admin.go @@ -14,13 +14,15 @@ import ( // registerAdminRoutes wires the admin JSON API used by the WebUI. It must be // mounted behind adminAuth. -func registerAdminRoutes(mux *http.ServeMux, cc *CCClient, pool *AccountPool, keys *ClientKeyPool, cfg *Config, usage *UsageTracker, ring *logRing) { +func registerAdminRoutes(mux *http.ServeMux, cc *CCClient, pool *AccountPool, keys *ClientKeyPool, cfg *Config, usage *UsageTracker, ring *logRing, quotas *QuotaService) { mux.HandleFunc("GET /admin/api/overview", handleAdminOverview(cfg, pool, keys, usage)) mux.HandleFunc("GET /admin/api/accounts", handleAdminAccountsList(pool, usage)) - mux.HandleFunc("POST /admin/api/accounts", handleAdminAccountAdd(pool, cfg, usage)) - mux.HandleFunc("PATCH /admin/api/accounts/{id}", handleAdminAccountPatch(pool, cfg, usage)) + mux.HandleFunc("POST /admin/api/accounts", handleAdminAccountAdd(pool, cfg, usage, quotas)) + mux.HandleFunc("PATCH /admin/api/accounts/{id}", handleAdminAccountPatch(pool, cfg, usage, quotas)) mux.HandleFunc("DELETE /admin/api/accounts/{id}", handleAdminAccountDelete(pool, cfg, usage)) + mux.HandleFunc("POST /admin/api/accounts/{id}/quota/refresh", handleAdminAccountQuotaRefresh(pool, usage, quotas)) mux.HandleFunc("POST /admin/api/accounts/{id}/test", handleAdminAccountTest(pool, cc)) + mux.HandleFunc("POST /admin/api/quotas/refresh", handleAdminQuotaRefreshAll(pool, usage, quotas)) mux.HandleFunc("GET /admin/api/models", handleAdminModelsGet(cfg)) mux.HandleFunc("PUT /admin/api/models", handleAdminModelsPut(cfg)) mux.HandleFunc("GET /admin/api/keys", handleAdminKeysList(keys, usage)) @@ -31,7 +33,7 @@ func registerAdminRoutes(mux *http.ServeMux, cc *CCClient, pool *AccountPool, ke mux.HandleFunc("GET /admin/api/settings", handleAdminSettingsGet(cfg)) mux.HandleFunc("PUT /admin/api/settings", handleAdminSettingsPut(cfg, cc, pool)) mux.HandleFunc("GET /admin/api/logs", handleAdminLogs(ring)) - mux.HandleFunc("POST /admin/api/oauth/start", handleAdminOAuthStart(pool, cfg)) + mux.HandleFunc("POST /admin/api/oauth/start", handleAdminOAuthStart(pool, cfg, quotas)) mux.HandleFunc("GET /admin/api/oauth/status", handleAdminOAuthStatus(pool)) mux.HandleFunc("POST /admin/api/oauth/cancel", handleAdminOAuthCancel()) } @@ -52,17 +54,23 @@ func subtleConstantTimeEqual(a, b string) bool { return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1 } -// adminAccount merges the ephemeral health view with durable usage counters. +// adminAccount merges the ephemeral health view with durable usage counters +// and the cached quota snapshot. type adminAccount struct { AccountView UsageSnapshotEntry + Quota *QuotaSnapshot `json:"quota,omitempty"` } func adminAccountViews(pool *AccountPool, usage *UsageTracker) []adminAccount { views := pool.Views() out := make([]adminAccount, 0, len(views)) for _, v := range views { - out = append(out, adminAccount{AccountView: v, UsageSnapshotEntry: usage.AccountUsage(v.ID)}) + out = append(out, adminAccount{ + AccountView: v, + UsageSnapshotEntry: usage.AccountUsage(v.ID), + Quota: usage.Quota(v.ID), + }) } return out } @@ -88,6 +96,7 @@ func handleAdminOverview(cfg *Config, pool *AccountPool, keys *ClientKeyPool, us overview.Accounts = adminAccountsSummary{Total: total, Enabled: enabled, RateLimited: rateLimited} overview.Keys = adminKeysSummary{Total: keyTotal, Enabled: keyEnabled} overview.Models = adminModelsSummary{Loaded: loaded, Available: available} + overview.Quotas = quotaSummary(pool, usage) writeAdminJSON(w, 200, overview) } } @@ -101,6 +110,7 @@ type adminOverview struct { Accounts adminAccountsSummary `json:"accounts"` Keys adminKeysSummary `json:"keys"` Models adminModelsSummary `json:"models"` + Quotas adminQuotaSummary `json:"quotas"` } type adminKeysSummary struct { @@ -119,13 +129,46 @@ type adminModelsSummary struct { Available int `json:"available"` } +type adminQuotaSummary struct { + Synced int `json:"synced"` + Exceeded int `json:"exceeded"` + LowBalance int `json:"low_balance"` + LastCheckedAt *time.Time `json:"last_checked_at,omitempty"` +} + +// quotaSummary aggregates the cached quota snapshots across the pool. +func quotaSummary(pool *AccountPool, usage *UsageTracker) adminQuotaSummary { + var summary adminQuotaSummary + var latest time.Time + for _, view := range pool.Views() { + snap := usage.Quota(view.ID) + if snap == nil { + continue + } + summary.Synced++ + if snap.Blocked() { + summary.Exceeded++ + } + if snap.BelowThreshold { + summary.LowBalance++ + } + if snap.LastChecked != nil && snap.LastChecked.After(latest) { + latest = *snap.LastChecked + } + } + if !latest.IsZero() { + summary.LastCheckedAt = &latest + } + return summary +} + func handleAdminAccountsList(pool *AccountPool, usage *UsageTracker) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { writeAdminJSON(w, 200, map[string]any{"accounts": adminAccountViews(pool, usage)}) } } -func handleAdminAccountAdd(pool *AccountPool, cfg *Config, usage *UsageTracker) http.HandlerFunc { +func handleAdminAccountAdd(pool *AccountPool, cfg *Config, usage *UsageTracker, quotas *QuotaService) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var body struct { Name string `json:"name"` @@ -153,12 +196,15 @@ func handleAdminAccountAdd(pool *AccountPool, cfg *Config, usage *UsageTracker) if len(modelCatalog) == 0 && acct.Enabled { FetchProviderModels(cfg.UpstreamBaseURL(), acct.APIKey) } + // The first quota query runs in the background so adding an account + // stays fast; the UI picks the snapshot up on its next poll. + quotas.RefreshAsync(acct) log.Printf("account %q added via webui", acct.Name) - writeAdminJSON(w, 201, adminAccount{AccountView: acct.View(), UsageSnapshotEntry: usage.AccountUsage(acct.ID)}) + writeAdminJSON(w, 201, adminAccount{AccountView: acct.View(), UsageSnapshotEntry: usage.AccountUsage(acct.ID), Quota: usage.Quota(acct.ID)}) } } -func handleAdminAccountPatch(pool *AccountPool, cfg *Config, usage *UsageTracker) http.HandlerFunc { +func handleAdminAccountPatch(pool *AccountPool, cfg *Config, usage *UsageTracker, quotas *QuotaService) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") var body struct { @@ -180,22 +226,29 @@ func handleAdminAccountPatch(pool *AccountPool, cfg *Config, usage *UsageTracker return } } + keyChanged := false if body.APIKey != nil { newID, err := pool.SetKey(id, *body.APIKey) if err != nil { writeAdminError(w, http.StatusConflict, err.Error()) return } - // The ID derives from the key; carry the usage history over. + // The ID derives from the key; carry the usage history over, but + // the cached quota belongs to the old credential and is dropped. usage.MoveAccount(id, newID) + usage.DropQuota(id) id = newID + keyChanged = true } if err := persistPool(pool, cfg); err != nil { writeAdminError(w, 500, "saving config failed: "+err.Error()) return } acct := pool.Get(id) - writeAdminJSON(w, 200, adminAccount{AccountView: acct.View(), UsageSnapshotEntry: usage.AccountUsage(id)}) + if keyChanged && quotas != nil { + quotas.RefreshAsync(acct) + } + writeAdminJSON(w, 200, adminAccount{AccountView: acct.View(), UsageSnapshotEntry: usage.AccountUsage(id), Quota: usage.Quota(id)}) } } @@ -215,6 +268,52 @@ func handleAdminAccountDelete(pool *AccountPool, cfg *Config, usage *UsageTracke } } +// handleAdminAccountQuotaRefresh refreshes one account's quota synchronously +// and returns the updated account row. +func handleAdminAccountQuotaRefresh(pool *AccountPool, usage *UsageTracker, quotas *QuotaService) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + acct := pool.Get(r.PathValue("id")) + if acct == nil { + writeAdminError(w, 404, "account not found") + return + } + if quotas == nil { + writeAdminError(w, http.StatusServiceUnavailable, "quota service unavailable") + return + } + quotas.RefreshAccount(r.Context(), acct) + writeAdminJSON(w, 200, adminAccount{AccountView: acct.View(), UsageSnapshotEntry: usage.AccountUsage(acct.ID), Quota: usage.Quota(acct.ID)}) + } +} + +// handleAdminQuotaRefreshAll refreshes every account's quota, or a single one +// when the body carries an id. It returns the full account list. +func handleAdminQuotaRefreshAll(pool *AccountPool, usage *UsageTracker, quotas *QuotaService) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if quotas == nil { + writeAdminError(w, http.StatusServiceUnavailable, "quota service unavailable") + return + } + var body struct { + ID string `json:"id"` + } + if r.Body != nil { + _ = json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body) + } + if body.ID != "" { + acct := pool.Get(body.ID) + if acct == nil { + writeAdminError(w, 404, "account not found") + return + } + quotas.RefreshAccount(r.Context(), acct) + } else { + quotas.RefreshAll(r.Context()) + } + writeAdminJSON(w, 200, map[string]any{"accounts": adminAccountViews(pool, usage)}) + } +} + // ====== model exposure ====== type adminModel struct { @@ -600,7 +699,7 @@ var ( webOAuthFlow *OAuthFlow ) -func handleAdminOAuthStart(pool *AccountPool, cfg *Config) http.HandlerFunc { +func handleAdminOAuthStart(pool *AccountPool, cfg *Config, quotas *QuotaService) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var body struct { CallbackURL string `json:"callback_url"` @@ -658,6 +757,7 @@ func handleAdminOAuthStart(pool *AccountPool, cfg *Config) http.HandlerFunc { if len(modelCatalog) == 0 && acct.Enabled { FetchProviderModels(cfg.UpstreamBaseURL(), acct.APIKey) } + quotas.RefreshAsync(acct) log.Printf("✓ OAuth account %q added via webui (user %s)", acct.Name, cb.UserName) }() diff --git a/internal/app/admin_test.go b/internal/app/admin_test.go index fd9d8a4..44c684b 100644 --- a/internal/app/admin_test.go +++ b/internal/app/admin_test.go @@ -20,23 +20,36 @@ func newAdminTestEnv(t *testing.T) (*httptest.Server, *AccountPool, *ClientKeyPo configFile = filepath.Join(t.TempDir(), "config.yaml") t.Cleanup(func() { configFile = oldConfigFile }) + oldUsageFile := usageFile + usageFile = filepath.Join(t.TempDir(), "usage.json") + t.Cleanup(func() { usageFile = oldUsageFile }) + + // Stub upstream: keeps background refreshes off the network and fast. + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.NotFound(w, r) + })) + t.Cleanup(upstream.Close) + cfg := &Config{APIKey: "client-key", Host: "localhost", Port: 11434} - cfg.SetUpstreamBaseURL("https://api.commandcode.test") + cfg.SetUpstreamBaseURL(upstream.URL) cfg.setAdminPassword("admin-pass-123") pool := NewAccountPool(nil) keys := NewClientKeyPool(nil) - cc := NewCCClientWithPool(pool, cfg.UpstreamBaseURL()) + cc := NewCCClientWithPool(pool, upstream.URL) usage := &UsageTracker{} ring := newLogRing() + quotas := NewQuotaService(cc, pool, usage) mux := http.NewServeMux() - registerAdminRoutes(mux, cc, pool, keys, cfg, usage, ring) + registerAdminRoutes(mux, cc, pool, keys, cfg, usage, ring, quotas) // Mirror runServer: the public OAuth callback lives outside adminAuth. root := http.NewServeMux() root.HandleFunc("POST /admin/api/oauth/callback", handleWebOAuthCallback()) root.Handle("/admin/", adminAuth(cfg, nil)(mux)) srv := httptest.NewServer(root) t.Cleanup(srv.Close) + // Drain background quota refreshes before restoring the shared globals. + t.Cleanup(quotas.Wait) return srv, pool, keys, cfg, usage, ring } diff --git a/internal/app/logging.go b/internal/app/logging.go index 235fcc4..7023fb3 100644 --- a/internal/app/logging.go +++ b/internal/app/logging.go @@ -103,6 +103,53 @@ func clientIP(remoteAddr string) string { return remoteAddr } +// requestClientIP returns the client address for logging and admin rate +// limiting. Proxy headers are trusted only when the direct peer is a loopback +// address, which is the expected nginx-to-app deployment on one host. +func requestClientIP(r *http.Request) string { + remote := clientIP(r.RemoteAddr) + peer := net.ParseIP(remote) + if peer == nil || !peer.IsLoopback() { + return remote + } + + if ip := parseHeaderIP(r.Header.Get("CF-Connecting-IP")); ip != "" { + return ip + } + if ip := firstForwardedIP(r.Header.Get("X-Forwarded-For")); ip != "" { + return ip + } + if ip := parseHeaderIP(r.Header.Get("X-Real-IP")); ip != "" { + return ip + } + return remote +} + +func parseHeaderIP(value string) string { + value = strings.TrimSpace(value) + if value == "" { + return "" + } + if ip := net.ParseIP(value); ip != nil { + return ip.String() + } + if host, _, err := net.SplitHostPort(value); err == nil { + if ip := net.ParseIP(host); ip != nil { + return ip.String() + } + } + return "" +} + +func firstForwardedIP(value string) string { + for _, part := range strings.Split(value, ",") { + if ip := parseHeaderIP(part); ip != "" { + return ip + } + } + return "" +} + func colorize(text, color string) string { if color == "" || !useColor() { return text diff --git a/internal/app/quota.go b/internal/app/quota.go new file mode 100644 index 0000000..a82c0e4 --- /dev/null +++ b/internal/app/quota.go @@ -0,0 +1,615 @@ +package app + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "log" + "net/http" + "net/url" + "sort" + "strconv" + "strings" + "sync" + "time" +) + +// Command Code exposes quota data through its undocumented /alpha/* endpoints. +// Every shape below tolerates the field drift documented by the community +// commandcode-usage project: camelCase or snake_case keys, epoch seconds, +// epoch milliseconds, or ISO timestamps, and either a flat response or one +// wrapped in a "data" object. +const ( + quotaRefreshInterval = 5 * time.Minute + quotaRequestTimeout = 15 * time.Second + quotaRefreshWorkers = 4 + quotaUserAgent = "cmdcode2api/" + Version +) + +// QuotaWindow is one server-side rolling limit (the 5-hour and weekly windows). +type QuotaWindow struct { + Used float64 `json:"used"` + Cap float64 `json:"cap"` + Exceeded bool `json:"exceeded"` + ResetAt *time.Time `json:"reset_at,omitempty"` +} + +// QuotaPlan is the account's subscription and billing period. MonthlyCredits is +// the plan's estimated monthly cap: the API exposes no monthly window object, +// so the cap is derived from the plan mapping and is nil for unknown plans, +// in which case the UI falls back to balance-only display. +type QuotaPlan struct { + PlanID string `json:"plan_id,omitempty"` + Name string `json:"name,omitempty"` + Status string `json:"status,omitempty"` + MonthlyCredits *float64 `json:"monthly_credits,omitempty"` + CurrentPeriodStart *time.Time `json:"current_period_start,omitempty"` + CurrentPeriodEnd *time.Time `json:"current_period_end,omitempty"` + CancelAtPeriodEnd bool `json:"cancel_at_period_end,omitempty"` + PendingPhase string `json:"pending_phase,omitempty"` +} + +// QuotaUsage is the billing-period aggregate from /alpha/usage/summary. +type QuotaUsage struct { + TotalCount int64 `json:"total_count"` + TotalCost float64 `json:"total_cost"` + AverageCost *float64 `json:"average_cost,omitempty"` + SuccessRate float64 `json:"success_rate"` + CompletedCount int64 `json:"completed_count"` + FailedCount int64 `json:"failed_count"` + TotalTokensIn int64 `json:"total_tokens_in"` + TotalTokensOut int64 `json:"total_tokens_out"` + TotalCredits float64 `json:"total_credits"` + PeriodBasis string `json:"period_basis,omitempty"` +} + +// QuotaSnapshot is one account's cached quota view. Failures holds the +// per-endpoint errors of a partially successful query; LastError holds the +// error of a query that returned nothing, in which case the previous snapshot +// is retained and only LastError/LastChecked are updated. +type QuotaSnapshot struct { + MonthlyCredits *float64 `json:"monthly_credits,omitempty"` + PurchasedCredits *float64 `json:"purchased_credits,omitempty"` + FreeCredits *float64 `json:"free_credits,omitempty"` + FiveHour *QuotaWindow `json:"five_hour,omitempty"` + Weekly *QuotaWindow `json:"weekly,omitempty"` + Limited bool `json:"limited,omitempty"` + Exceeded string `json:"exceeded,omitempty"` + BelowThreshold bool `json:"below_threshold,omitempty"` + CreditThreshold *float64 `json:"credit_threshold,omitempty"` + Plan *QuotaPlan `json:"plan,omitempty"` + Usage *QuotaUsage `json:"usage,omitempty"` + + LastChecked *time.Time `json:"last_checked,omitempty"` + LastError string `json:"last_error,omitempty"` + Failures []string `json:"failures,omitempty"` +} + +// Blocked reports whether a window currently blocks the account. +func (q *QuotaSnapshot) Blocked() bool { + if q == nil { + return false + } + if q.Exceeded != "" { + return true + } + return (q.FiveHour != nil && q.FiveHour.Exceeded) || (q.Weekly != nil && q.Weekly.Exceeded) +} + +// ====== plan mapping ====== + +type planSpec struct { + Name string + MonthlyCredits float64 +} + +// knownPlans mirrors the community CLI's planId mapping. Unknown plans are +// reported by ID with no monthly cap, so the UI hides the derived monthly bar. +var knownPlans = map[string]planSpec{ + "individual-go": {Name: "Go", MonthlyCredits: 10}, + "individual-goat": {Name: "GOAT", MonthlyCredits: 70}, + "individual-pro": {Name: "Pro", MonthlyCredits: 30}, + "individual-pro-v1": {Name: "Pro", MonthlyCredits: 80}, + "individual-provider": {Name: "Provider", MonthlyCredits: 15}, + "individual-max": {Name: "Max", MonthlyCredits: 150}, + "individual-ultra": {Name: "Ultra", MonthlyCredits: 300}, + "teams-pro": {Name: "Teams Pro", MonthlyCredits: 40}, +} + +// planPrefixes holds the mapping keys longest-first so individual-pro-v1 wins +// over individual-pro. +var planPrefixes = func() []string { + keys := make([]string, 0, len(knownPlans)) + for key := range knownPlans { + keys = append(keys, key) + } + sort.Slice(keys, func(i, j int) bool { + if len(keys[i]) != len(keys[j]) { + return len(keys[i]) > len(keys[j]) + } + return keys[i] < keys[j] + }) + return keys +}() + +func planInfo(planID string) (planSpec, bool) { + if planID == "" { + return planSpec{}, false + } + normalized := strings.ToLower(strings.ReplaceAll(planID, "_", "-")) + for _, prefix := range planPrefixes { + if strings.HasPrefix(normalized, prefix) { + return knownPlans[prefix], true + } + } + return planSpec{}, false +} + +// ====== defensive JSON helpers ====== + +func obj(v any) map[string]any { + m, _ := v.(map[string]any) + return m +} + +func numOf(v any) (float64, bool) { + switch n := v.(type) { + case float64: + return n, true + case json.Number: + f, err := n.Float64() + return f, err == nil + case string: + s := strings.TrimSpace(n) + if s == "" { + return 0, false + } + f, err := strconv.ParseFloat(s, 64) + return f, err == nil + } + return 0, false +} + +func numField(m map[string]any, keys ...string) (float64, bool) { + for _, key := range keys { + if v, ok := m[key]; ok { + if f, ok := numOf(v); ok { + return f, true + } + } + } + return 0, false +} + +func numOr(m map[string]any, keys ...string) float64 { + v, _ := numField(m, keys...) + return v +} + +func numPtr(m map[string]any, keys ...string) *float64 { + if v, ok := numField(m, keys...); ok { + return &v + } + return nil +} + +func strField(m map[string]any, keys ...string) string { + for _, key := range keys { + if v, ok := m[key]; ok { + if s, ok := v.(string); ok && s != "" { + return s + } + } + } + return "" +} + +func boolField(m map[string]any, keys ...string) bool { + for _, key := range keys { + if v, ok := m[key]; ok { + switch b := v.(type) { + case bool: + if b { + return true + } + case string: + if b == "true" { + return true + } + } + } + } + return false +} + +func mapField(m map[string]any, keys ...string) map[string]any { + for _, key := range keys { + if rec, ok := m[key].(map[string]any); ok { + return rec + } + } + return nil +} + +func timeField(m map[string]any, keys ...string) *time.Time { + for _, key := range keys { + if v, ok := m[key]; ok { + if t := epochTime(v); t != nil { + return t + } + } + } + return nil +} + +// epochTime accepts epoch seconds, epoch milliseconds, or an ISO string. +func epochTime(v any) *time.Time { + switch t := v.(type) { + case float64: + return millisToTime(t) + case json.Number: + if f, err := t.Float64(); err == nil { + return millisToTime(f) + } + case string: + s := strings.TrimSpace(t) + if s == "" { + return nil + } + if parsed, err := time.Parse(time.RFC3339Nano, s); err == nil { + return &parsed + } + if f, err := strconv.ParseFloat(s, 64); err == nil { + return millisToTime(f) + } + } + return nil +} + +func millisToTime(value float64) *time.Time { + if value <= 0 { + return nil + } + ms := value + if value < 1e12 { + ms = value * 1000 + } + t := time.UnixMilli(int64(ms)) + return &t +} + +// ====== parsing ====== + +func parseQuotaWindow(raw map[string]any) *QuotaWindow { + if raw == nil { + return nil + } + used, hasUsed := numField(raw, "used", "usage", "usedCredits", "used_credits") + capacity, hasCap := numField(raw, "cap", "limit", "capCredits", "cap_credits") + window := &QuotaWindow{Used: used, Cap: capacity} + window.Exceeded = boolField(raw, "exceeded") || + (hasUsed && hasCap && capacity > 0 && used >= capacity) + window.ResetAt = timeField(raw, "resetAt", "reset_at", "resetsAt") + return window +} + +func buildQuotaPlan(planID string, data map[string]any) *QuotaPlan { + plan := &QuotaPlan{PlanID: planID} + if data != nil { + plan.Status = strField(data, "status") + plan.CurrentPeriodStart = timeField(data, "currentPeriodStart", "current_period_start") + plan.CurrentPeriodEnd = timeField(data, "currentPeriodEnd", "current_period_end") + plan.CancelAtPeriodEnd = boolField(data, "cancelAtPeriodEnd", "cancel_at_period_end") + plan.PendingPhase = strField(data, "pendingPhase", "pending_phase") + } + if spec, ok := planInfo(planID); ok { + plan.Name = spec.Name + credits := spec.MonthlyCredits + plan.MonthlyCredits = &credits + } else { + plan.Name = planID + } + return plan +} + +func parseQuotaUsage(raw map[string]any) *QuotaUsage { + if raw == nil { + return nil + } + return &QuotaUsage{ + TotalCount: int64(numOr(raw, "totalCount", "total_count")), + TotalCost: numOr(raw, "totalCost", "total_cost"), + AverageCost: numPtr(raw, "averageCost", "average_cost"), + SuccessRate: numOr(raw, "successRate", "success_rate"), + CompletedCount: int64(numOr(raw, "completedCount", "completed_count")), + FailedCount: int64(numOr(raw, "failedCount", "failed_count")), + TotalTokensIn: int64(numOr(raw, "totalTokensIn", "total_tokens_in")), + TotalTokensOut: int64(numOr(raw, "totalTokensOut", "total_tokens_out")), + TotalCredits: numOr(raw, "totalCredits", "total_credits"), + PeriodBasis: strField(raw, "periodBasis", "period_basis"), + } +} + +func snapshotHasData(q *QuotaSnapshot) bool { + return q.MonthlyCredits != nil || q.PurchasedCredits != nil || q.FreeCredits != nil || + q.FiveHour != nil || q.Weekly != nil || q.Plan != nil || q.Usage != nil +} + +// ====== upstream queries ====== + +type quotaHTTPError struct { + Status int + Body string +} + +func (e *quotaHTTPError) Error() string { + body := e.Body + if len(body) > 200 { + body = body[:200] + } + if body == "" { + return fmt.Sprintf("HTTP %d", e.Status) + } + return fmt.Sprintf("HTTP %d: %s", e.Status, body) +} + +func quotaGet(ctx context.Context, client *http.Client, rawURL, apiKey string) (map[string]any, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) + if err != nil { + return nil, err + } + req.Header.Set("Authorization", "Bearer "+apiKey) + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", quotaUserAgent) + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) + if err != nil { + return nil, fmt.Errorf("read response: %w", err) + } + if resp.StatusCode != http.StatusOK { + return nil, "aHTTPError{Status: resp.StatusCode, Body: strings.TrimSpace(string(body))} + } + var out map[string]any + if err := json.Unmarshal(body, &out); err != nil { + return nil, fmt.Errorf("non-JSON response (HTTP %d)", resp.StatusCode) + } + return out, nil +} + +func quotaKeyRejected(err error) bool { + var httpErr *quotaHTTPError + if errors.As(err, &httpErr) { + return httpErr.Status == http.StatusUnauthorized || httpErr.Status == http.StatusForbidden + } + return false +} + +// fetchQuotaSnapshot queries the undocumented /alpha endpoints for one key. +// Partial failures are recorded in the snapshot's Failures and the successful +// parts are kept; the query only fails when the key is rejected or no endpoint +// returned usable data. +func fetchQuotaSnapshot(ctx context.Context, client *http.Client, baseURL, apiKey string) (*QuotaSnapshot, error) { + base := strings.TrimRight(baseURL, "/") + snap := &QuotaSnapshot{} + var failures []string + + // 1. whoami → org id for the subscriptions query. + orgID := "" + if who, err := quotaGet(ctx, client, base+"/alpha/whoami", apiKey); err != nil { + if quotaKeyRejected(err) { + return nil, fmt.Errorf("API Key 被拒绝(%v)", err) + } + failures = append(failures, "whoami: "+err.Error()) + } else { + org := mapField(who, "org") + if org == nil { + org = mapField(obj(who["data"]), "org") + } + orgID = strField(org, "id") + } + + // 2. billing/credits → balances + rolling windows. + planIDFallback := "" + if creditsResp, err := quotaGet(ctx, client, base+"/alpha/billing/credits", apiKey); err != nil { + failures = append(failures, "billing/credits: "+err.Error()) + } else { + root := creditsResp + if nested := obj(creditsResp["data"]); nested != nil { + root = nested + } + credits := mapField(creditsResp, "credits") + if credits == nil { + credits = mapField(root, "credits") + } + windows := mapField(creditsResp, "windowLimits", "window_limits") + if windows == nil { + windows = mapField(root, "windowLimits", "window_limits") + } + if credits != nil { + snap.MonthlyCredits = numPtr(credits, "monthlyCredits", "monthly_credits") + snap.PurchasedCredits = numPtr(credits, "purchasedCredits", "purchased_credits") + snap.FreeCredits = numPtr(credits, "freeCredits", "free_credits") + snap.BelowThreshold = boolField(credits, "belowThreshold", "below_threshold") + snap.CreditThreshold = numPtr(credits, "creditThreshold", "credit_threshold") + planIDFallback = strField(credits, "planId", "plan_id") + } + if windows != nil { + snap.Limited = boolField(windows, "limited") + snap.Exceeded = strField(windows, "exceeded") + snap.FiveHour = parseQuotaWindow(mapField(windows, "fiveHour", "five_hour", "rolling5h", "5h")) + snap.Weekly = parseQuotaWindow(mapField(windows, "weekly", "week")) + } + } + + // 3. billing/subscriptions → plan + billing period. + subURL := base + "/alpha/billing/subscriptions" + if orgID != "" { + subURL += "?orgId=" + url.QueryEscape(orgID) + } + if sub, err := quotaGet(ctx, client, subURL, apiKey); err != nil { + if planIDFallback != "" { + snap.Plan = buildQuotaPlan(planIDFallback, nil) + } + failures = append(failures, "billing/subscriptions: "+err.Error()) + } else { + data := mapField(sub, "data") + if data == nil { + data = mapField(sub, "subscription") + } + planID := strField(data, "planId", "plan_id") + if planID == "" { + planID = planIDFallback + } + if data != nil || planID != "" { + snap.Plan = buildQuotaPlan(planID, data) + } + } + + // 4. usage/summary → billing-period aggregates. + if usageResp, err := quotaGet(ctx, client, base+"/alpha/usage/summary", apiKey); err != nil { + failures = append(failures, "usage/summary: "+err.Error()) + } else { + root := usageResp + if nested := obj(usageResp["data"]); nested != nil { + root = nested + } + snap.Usage = parseQuotaUsage(root) + } + + if len(failures) > 0 { + snap.Failures = failures + } + if !snapshotHasData(snap) { + if len(failures) == 0 { + return nil, fmt.Errorf("额度接口返回空数据") + } + return nil, fmt.Errorf("所有额度接口均无法访问:%s", strings.Join(failures, "; ")) + } + return snap, nil +} + +// ====== refresh service ====== + +// QuotaService fetches and caches per-account quota data. The background loop +// and the admin refresh endpoints share one instance so upstream requests stay +// bounded by quotaRefreshWorkers. Stored snapshots are never mutated after +// SetQuota; updates replace them wholesale. +type QuotaService struct { + cc *CCClient + pool *AccountPool + usage *UsageTracker + client *http.Client + sem chan struct{} + wg sync.WaitGroup +} + +func NewQuotaService(cc *CCClient, pool *AccountPool, usage *UsageTracker) *QuotaService { + return &QuotaService{ + cc: cc, + pool: pool, + usage: usage, + client: &http.Client{Timeout: quotaRequestTimeout}, + sem: make(chan struct{}, quotaRefreshWorkers), + } +} + +// RefreshAccount queries the upstream for one account and updates the cached +// snapshot. A failed query keeps the previous snapshot and only records the +// error and check time. +func (s *QuotaService) RefreshAccount(ctx context.Context, acct *Account) *QuotaSnapshot { + if acct == nil { + return nil + } + s.sem <- struct{}{} + defer func() { <-s.sem }() + + snap, err := fetchQuotaSnapshot(ctx, s.client, s.cc.BaseURLValue(), acct.APIKey) + now := time.Now() + if err != nil { + snap = cloneForQuotaError(s.usage.Quota(acct.ID), err.Error(), now) + s.usage.SetQuota(acct.ID, snap) + s.persist() + log.Printf("[WARN] quota refresh for account %s failed: %v", acct.Name, err) + return snap + } + snap.LastChecked = &now + snap.LastError = "" + s.usage.SetQuota(acct.ID, snap) + s.persist() + return snap +} + +// cloneForQuotaError copies a cached snapshot and stamps it with a failed +// check, preserving the last successful quota data. The copy shares the nested +// pointers, which are treated as immutable once stored. +func cloneForQuotaError(prev *QuotaSnapshot, message string, at time.Time) *QuotaSnapshot { + var snap QuotaSnapshot + if prev != nil { + snap = *prev + } + snap.LastError = message + snap.LastChecked = &at + snap.Failures = nil + return &snap +} + +// RefreshAsync refreshes one account without blocking the caller. +func (s *QuotaService) RefreshAsync(acct *Account) { + if s == nil || acct == nil { + return + } + s.wg.Add(1) + go func() { + defer s.wg.Done() + s.RefreshAccount(context.Background(), acct) + }() +} + +// RefreshAll refreshes every account, at most quotaRefreshWorkers in flight. +func (s *QuotaService) RefreshAll(ctx context.Context) int { + accounts := s.pool.List() + var wg sync.WaitGroup + for _, acct := range accounts { + wg.Add(1) + go func(a *Account) { + defer wg.Done() + s.RefreshAccount(ctx, a) + }(acct) + } + wg.Wait() + return len(accounts) +} + +// Wait blocks until every refresh started by RefreshAsync has finished. +func (s *QuotaService) Wait() { + s.wg.Wait() +} + +// Run performs one immediate refresh and then refreshes on a fixed interval +// until ctx is cancelled. +func (s *QuotaService) Run(ctx context.Context) { + s.RefreshAll(ctx) + ticker := time.NewTicker(quotaRefreshInterval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + s.RefreshAll(ctx) + } + } +} + +func (s *QuotaService) persist() { + if err := s.usage.save(); err != nil { + log.Printf("[WARN] save quota data failed: %v", err) + } +} diff --git a/internal/app/quota_test.go b/internal/app/quota_test.go new file mode 100644 index 0000000..f62e76f --- /dev/null +++ b/internal/app/quota_test.go @@ -0,0 +1,739 @@ +package app + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "path/filepath" + "sort" + "strings" + "sync" + "testing" + "time" +) + +func floatPtr(v float64) *float64 { return &v } + +func redirectUsageFile(t *testing.T) { + t.Helper() + old := usageFile + usageFile = filepath.Join(t.TempDir(), "usage.json") + t.Cleanup(func() { usageFile = old }) +} + +// ====== plan mapping ====== + +func TestPlanInfoLongestPrefix(t *testing.T) { + cases := []struct { + planID string + name string + credits float64 + known bool + }{ + {"individual-pro", "Pro", 30, true}, + {"individual-pro-v1", "Pro", 80, true}, + {"individual_pro", "Pro", 30, true}, + {"individual-goat", "GOAT", 70, true}, + {"teams-pro", "Teams Pro", 40, true}, + {"individual-max", "Max", 150, true}, + {"enterprise-unknown", "", 0, false}, + {"", "", 0, false}, + } + for _, tc := range cases { + spec, ok := planInfo(tc.planID) + if ok != tc.known { + t.Fatalf("planInfo(%q) known = %v, want %v", tc.planID, ok, tc.known) + } + if !tc.known { + continue + } + if spec.Name != tc.name || spec.MonthlyCredits != tc.credits { + t.Fatalf("planInfo(%q) = %+v, want %s/%.0f", tc.planID, spec, tc.name, tc.credits) + } + } +} + +func TestEpochTimeVariants(t *testing.T) { + seconds := epochTime(float64(1700000000)) + millis := epochTime(float64(1700000000000)) + if seconds == nil || millis == nil { + t.Fatal("numeric timestamps not parsed") + } + if !seconds.Equal(*millis) { + t.Fatalf("seconds=%v millis=%v", seconds, millis) + } + iso := epochTime("2030-01-02T03:04:05Z") + if iso == nil || iso.UTC().Year() != 2030 { + t.Fatalf("ISO timestamp = %v", iso) + } + if numeric := epochTime("1700000000"); numeric == nil || !numeric.Equal(*seconds) { + t.Fatalf("numeric string = %v", epochTime("1700000000")) + } + if epochTime(0) != nil || epochTime("") != nil || epochTime("garbage") != nil { + t.Fatal("invalid timestamps must be nil") + } +} + +// ====== fetch/parse ====== + +type quotaTestUpstream struct { + mu sync.Mutex + requests []string + auths []string + agents []string + accepts []string +} + +func (u *quotaTestUpstream) record(r *http.Request) { + u.mu.Lock() + defer u.mu.Unlock() + target := r.URL.Path + if r.URL.RawQuery != "" { + target += "?" + r.URL.RawQuery + } + u.requests = append(u.requests, target) + u.auths = append(u.auths, r.Header.Get("Authorization")) + u.agents = append(u.agents, r.Header.Get("User-Agent")) + u.accepts = append(u.accepts, r.Header.Get("Accept")) +} + +func (u *quotaTestUpstream) paths() []string { + u.mu.Lock() + defer u.mu.Unlock() + return append([]string(nil), u.requests...) +} + +func TestFetchQuotaSnapshotHappyPath(t *testing.T) { + up := "aTestUpstream{} + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + up.record(r) + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case "/alpha/whoami": + fmt.Fprint(w, `{"user":{"userName":"alice"},"org":{"id":"org_123"}}`) + case "/alpha/billing/credits": + fmt.Fprint(w, `{"credits":{"monthlyCredits":12.5,"purchasedCredits":3.25,"freeCredits":1.5,"belowThreshold":true,"creditThreshold":5},"windowLimits":{"limited":true,"exceeded":"weekly","fiveHour":{"used":4,"cap":10,"resetAt":1800000000000},"weekly":{"used":25,"cap":20,"resetAt":1800000000}}}`) + case "/alpha/billing/subscriptions": + fmt.Fprint(w, `{"data":{"planId":"individual-pro-v1","status":"active","currentPeriodEnd":1767225600000,"cancelAtPeriodEnd":true}}`) + case "/alpha/usage/summary": + fmt.Fprint(w, `{"data":{"totalCount":42,"totalCost":9.5,"averageCost":0.22,"successRate":0.98,"completedCount":40,"failedCount":2,"totalTokensIn":1000,"totalTokensOut":2000,"totalCredits":8.5,"periodBasis":"billing_period"}}`) + default: + http.NotFound(w, r) + } + })) + defer srv.Close() + + snap, err := fetchQuotaSnapshot(context.Background(), srv.Client(), srv.URL, "cc-key") + if err != nil { + t.Fatalf("fetch failed: %v", err) + } + if len(snap.Failures) != 0 || snap.LastError != "" { + t.Fatalf("unexpected failures: %+v", snap) + } + if snap.MonthlyCredits == nil || *snap.MonthlyCredits != 12.5 || + snap.PurchasedCredits == nil || *snap.PurchasedCredits != 3.25 || + snap.FreeCredits == nil || *snap.FreeCredits != 1.5 { + t.Fatalf("balances = %+v", snap) + } + if !snap.BelowThreshold || snap.CreditThreshold == nil || *snap.CreditThreshold != 5 { + t.Fatalf("threshold fields = %+v", snap) + } + if snap.FiveHour == nil || snap.FiveHour.Used != 4 || snap.FiveHour.Cap != 10 || + snap.FiveHour.Exceeded || snap.FiveHour.ResetAt == nil { + t.Fatalf("five hour window = %+v", snap.FiveHour) + } + if snap.Weekly == nil || snap.Weekly.Used != 25 || snap.Weekly.Cap != 20 || !snap.Weekly.Exceeded { + t.Fatalf("weekly window = %+v", snap.Weekly) + } + if !snap.Limited || snap.Exceeded != "weekly" { + t.Fatalf("limit flags = limited:%v exceeded:%q", snap.Limited, snap.Exceeded) + } + if snap.Plan == nil || snap.Plan.PlanID != "individual-pro-v1" || snap.Plan.Name != "Pro" || + snap.Plan.MonthlyCredits == nil || *snap.Plan.MonthlyCredits != 80 || + snap.Plan.Status != "active" || !snap.Plan.CancelAtPeriodEnd || snap.Plan.CurrentPeriodEnd == nil { + t.Fatalf("plan = %+v", snap.Plan) + } + if snap.Usage == nil || snap.Usage.TotalCount != 42 || snap.Usage.SuccessRate != 0.98 || + snap.Usage.TotalTokensOut != 2000 || snap.Usage.PeriodBasis != "billing_period" { + t.Fatalf("usage = %+v", snap.Usage) + } + + want := []string{ + "/alpha/whoami", + "/alpha/billing/credits", + "/alpha/billing/subscriptions?orgId=org_123", + "/alpha/usage/summary", + } + if got := up.paths(); !equalStrings(got, want) { + t.Fatalf("requested paths = %v, want %v", got, want) + } + up.mu.Lock() + defer up.mu.Unlock() + for i := range up.auths { + if up.auths[i] != "Bearer cc-key" { + t.Fatalf("request %d auth = %q", i, up.auths[i]) + } + if up.accepts[i] != "application/json" { + t.Fatalf("request %d accept = %q", i, up.accepts[i]) + } + if !strings.HasPrefix(up.agents[i], "cmdcode2api/") { + t.Fatalf("request %d user-agent = %q", i, up.agents[i]) + } + } +} + +func TestFetchQuotaSnapshotSnakeCaseAndNested(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case "/alpha/whoami": + fmt.Fprint(w, `{"data":{"user":{"userName":"bob"},"org":{"id":"org snake"}}}`) + case "/alpha/billing/credits": + fmt.Fprint(w, `{"data":{"credits":{"monthly_credits":12.5,"purchased_credits":3,"free_credits":1,"below_threshold":true,"credit_threshold":5,"plan_id":"individual-go"},"window_limits":{"five_hour":{"usage":4,"limit":10,"reset_at":"2030-01-02T03:04:05Z"},"weekly":{"usage":1,"cap_credits":20,"exceeded":"true"}}}}`) + case "/alpha/billing/subscriptions": + fmt.Fprint(w, `{"subscription":{"plan_id":"unknown-plan","status":"trialing","current_period_end":"2030-02-01T00:00:00Z"}}`) + case "/alpha/usage/summary": + fmt.Fprint(w, `{"total_count":7,"total_cost":0.7,"success_rate":0.5,"completed_count":3,"failed_count":4,"total_tokens_in":5,"total_tokens_out":6,"total_credits":0.3,"period_basis":"month"}`) + default: + http.NotFound(w, r) + } + })) + defer srv.Close() + + snap, err := fetchQuotaSnapshot(context.Background(), srv.Client(), srv.URL+"/", "cc-key") + if err != nil { + t.Fatalf("fetch failed: %v", err) + } + if snap.MonthlyCredits == nil || *snap.MonthlyCredits != 12.5 || !snap.BelowThreshold { + t.Fatalf("snake_case balances = %+v", snap) + } + if snap.FiveHour == nil || snap.FiveHour.Used != 4 || snap.FiveHour.Cap != 10 || snap.FiveHour.ResetAt == nil { + t.Fatalf("five_hour window = %+v", snap.FiveHour) + } + if snap.Weekly == nil || snap.Weekly.Used != 1 || snap.Weekly.Cap != 20 || !snap.Weekly.Exceeded { + t.Fatalf("weekly window = %+v", snap.Weekly) + } + if snap.Plan == nil || snap.Plan.Name != "unknown-plan" || snap.Plan.MonthlyCredits != nil { + t.Fatalf("unknown plan = %+v", snap.Plan) + } + if snap.Plan.CurrentPeriodEnd == nil || snap.Plan.CurrentPeriodEnd.UTC().Year() != 2030 { + t.Fatalf("period end = %v", snap.Plan.CurrentPeriodEnd) + } + if snap.Usage == nil || snap.Usage.TotalCount != 7 || snap.Usage.FailedCount != 4 || snap.Usage.PeriodBasis != "month" { + t.Fatalf("usage = %+v", snap.Usage) + } +} + +func TestFetchQuotaSnapshotPartialFailureKeepsData(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case "/alpha/billing/credits": + fmt.Fprint(w, `{"credits":{"monthlyCredits":7,"purchasedCredits":2,"freeCredits":0,"planId":"individual-max"},"windowLimits":{"fiveHour":{"used":1,"cap":9}}}`) + default: + http.Error(w, "boom", http.StatusInternalServerError) + } + })) + defer srv.Close() + + snap, err := fetchQuotaSnapshot(context.Background(), srv.Client(), srv.URL, "cc-key") + if err != nil { + t.Fatalf("partial failure must still succeed: %v", err) + } + if snap.MonthlyCredits == nil || *snap.MonthlyCredits != 7 { + t.Fatalf("credits lost: %+v", snap) + } + if snap.FiveHour == nil || snap.FiveHour.Cap != 9 { + t.Fatalf("window lost: %+v", snap.FiveHour) + } + // subscriptions failed, so the plan is reconstructed from the credits planId. + if snap.Plan == nil || snap.Plan.PlanID != "individual-max" || snap.Plan.Name != "Max" { + t.Fatalf("plan fallback = %+v", snap.Plan) + } + if len(snap.Failures) != 3 { + t.Fatalf("failures = %v, want 3 entries", snap.Failures) + } +} + +func TestFetchQuotaSnapshotRejectsKeyWithoutFurtherCalls(t *testing.T) { + up := "aTestUpstream{} + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + up.record(r) + http.Error(w, `{"message":"invalid key"}`, http.StatusForbidden) + })) + defer srv.Close() + + if _, err := fetchQuotaSnapshot(context.Background(), srv.Client(), srv.URL, "cc-bad"); err == nil { + t.Fatal("rejected key must fail") + } + if got := up.paths(); len(got) != 1 || got[0] != "/alpha/whoami" { + t.Fatalf("requests after 403 = %v, want only whoami", got) + } +} + +func TestFetchQuotaSnapshotNonJSONFailsWhenNoData(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + fmt.Fprint(w, "not json at all") + })) + defer srv.Close() + + _, err := fetchQuotaSnapshot(context.Background(), srv.Client(), srv.URL, "cc-key") + if err == nil || !strings.Contains(err.Error(), "所有额度接口均无法访问") { + t.Fatalf("err = %v, want aggregate failure", err) + } +} + +func TestFetchQuotaSnapshotMissingFields(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case "/alpha/billing/credits": + fmt.Fprint(w, `{}`) + case "/alpha/usage/summary": + fmt.Fprint(w, `{"data":{}}`) + default: + fmt.Fprint(w, `{}`) + } + })) + defer srv.Close() + + snap, err := fetchQuotaSnapshot(context.Background(), srv.Client(), srv.URL, "cc-key") + if err != nil { + t.Fatalf("empty-but-200 responses should still parse: %v", err) + } + if snap.MonthlyCredits != nil || snap.FiveHour != nil || snap.Weekly != nil || snap.Plan != nil { + t.Fatalf("empty response produced fields: %+v", snap) + } + if snap.Usage == nil || snap.Usage.TotalCount != 0 { + t.Fatalf("usage = %+v", snap.Usage) + } +} + +// ====== refresh service ====== + +func TestRefreshAccountKeepsSnapshotOnFailure(t *testing.T) { + redirectUsageFile(t) + var fail bool + var mu sync.Mutex + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + down := fail + mu.Unlock() + if down { + http.Error(w, "down", http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + writeQuotaFixture(w, r) + })) + defer srv.Close() + + pool := NewAccountPool(nil) + acct, err := pool.Add("main", "cc-key", true) + if err != nil { + t.Fatal(err) + } + cc := NewCCClientWithPool(pool, srv.URL) + usage := &UsageTracker{} + svc := NewQuotaService(cc, pool, usage) + + first := svc.RefreshAccount(context.Background(), acct) + if first == nil || first.MonthlyCredits == nil || *first.MonthlyCredits != 12.5 { + t.Fatalf("first refresh = %+v", first) + } + if first.LastChecked == nil || first.LastError != "" { + t.Fatalf("first refresh metadata = %+v", first) + } + + mu.Lock() + fail = true + mu.Unlock() + second := svc.RefreshAccount(context.Background(), acct) + if second == nil || second.LastError == "" { + t.Fatalf("second refresh must record the error: %+v", second) + } + if second.MonthlyCredits == nil || *second.MonthlyCredits != 12.5 { + t.Fatalf("previous quota not preserved: %+v", second) + } + if second.LastChecked == nil || !second.LastChecked.After(*first.LastChecked) { + t.Fatalf("last_checked not advanced: %+v", second.LastChecked) + } + if cached := usage.Quota(acct.ID); cached == nil || cached.LastError == "" { + t.Fatalf("cached snapshot not updated: %+v", cached) + } +} + +func TestRefreshAllRefreshesEveryAccount(t *testing.T) { + redirectUsageFile(t) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + writeQuotaFixture(w, r) + })) + defer srv.Close() + + pool := NewAccountPool(nil) + for _, key := range []string{"key-1", "key-2", "key-3"} { + if _, err := pool.Add(key, key, true); err != nil { + t.Fatal(err) + } + } + cc := NewCCClientWithPool(pool, srv.URL) + usage := &UsageTracker{} + svc := NewQuotaService(cc, pool, usage) + + if n := svc.RefreshAll(context.Background()); n != 3 { + t.Fatalf("RefreshAll returned %d, want 3", n) + } + for _, acct := range pool.List() { + if usage.Quota(acct.ID) == nil { + t.Fatalf("account %s missing snapshot", acct.Name) + } + } +} + +// ====== persistence ====== + +func TestUsageSnapshotPersistsQuotas(t *testing.T) { + redirectUsageFile(t) + usage := &UsageTracker{} + usage.SetQuota("a1", &QuotaSnapshot{ + MonthlyCredits: floatPtr(12.5), + Plan: &QuotaPlan{Name: "Pro", MonthlyCredits: floatPtr(80)}, + FiveHour: &QuotaWindow{Used: 4, Cap: 10}, + }) + if err := usage.save(); err != nil { + t.Fatal(err) + } + + reloaded := loadUsage() + snap := reloaded.Quota("a1") + if snap == nil { + t.Fatal("quota not persisted") + } + if snap.MonthlyCredits == nil || *snap.MonthlyCredits != 12.5 { + t.Fatalf("monthly credits = %+v", snap.MonthlyCredits) + } + if snap.Plan == nil || snap.Plan.Name != "Pro" || snap.Plan.MonthlyCredits == nil || *snap.Plan.MonthlyCredits != 80 { + t.Fatalf("plan = %+v", snap.Plan) + } + if snap.FiveHour == nil || snap.FiveHour.Cap != 10 { + t.Fatalf("five hour = %+v", snap.FiveHour) + } +} + +func TestDropAccountClearsQuota(t *testing.T) { + usage := &UsageTracker{} + usage.SetQuota("a1", &QuotaSnapshot{MonthlyCredits: floatPtr(1)}) + usage.DropAccount("a1") + if usage.Quota("a1") != nil { + t.Fatal("quota not cleared with the account") + } +} + +func TestKeyChangeDoesNotMigrateQuota(t *testing.T) { + usage := &UsageTracker{} + usage.SetQuota("old", &QuotaSnapshot{MonthlyCredits: floatPtr(1)}) + usage.MoveAccount("old", "new") + if usage.Quota("new") != nil { + t.Fatal("quota must not migrate to the new key") + } + if usage.Quota("old") == nil { + t.Fatal("old quota should remain until explicitly dropped") + } + usage.DropQuota("old") + if usage.Quota("old") != nil { + t.Fatal("quota not dropped") + } +} + +// ====== admin API ====== + +type quotaUpstreamStub struct { + mu sync.Mutex + fail bool + requests []string +} + +func (s *quotaUpstreamStub) ServeHTTP(w http.ResponseWriter, r *http.Request) { + s.mu.Lock() + s.requests = append(s.requests, r.URL.Path) + down := s.fail + s.mu.Unlock() + if down { + http.Error(w, "upstream down", http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + writeQuotaFixture(w, r) +} + +func (s *quotaUpstreamStub) setFail(v bool) { + s.mu.Lock() + defer s.mu.Unlock() + s.fail = v +} + +func writeQuotaFixture(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/alpha/whoami": + fmt.Fprint(w, `{"org":{"id":"org_123"}}`) + case "/alpha/billing/credits": + fmt.Fprint(w, `{"credits":{"monthlyCredits":12.5,"purchasedCredits":3,"freeCredits":1},"windowLimits":{"fiveHour":{"used":4,"cap":10},"weekly":{"used":25,"cap":20}}}`) + case "/alpha/billing/subscriptions": + fmt.Fprint(w, `{"data":{"planId":"individual-pro","status":"active","currentPeriodEnd":1767225600000}}`) + case "/alpha/usage/summary": + fmt.Fprint(w, `{"totalCount":5,"totalCost":1.5,"successRate":1,"totalTokensIn":10,"totalTokensOut":20,"totalCredits":1}`) + default: + http.NotFound(w, r) + } +} + +func newQuotaAdminTestEnv(t *testing.T) (*httptest.Server, *AccountPool, *UsageTracker, *quotaUpstreamStub) { + t.Helper() + + oldConfigFile := configFile + configFile = filepath.Join(t.TempDir(), "config.yaml") + t.Cleanup(func() { configFile = oldConfigFile }) + redirectUsageFile(t) + + stub := "aUpstreamStub{} + upstream := httptest.NewServer(stub) + t.Cleanup(upstream.Close) + + cfg := &Config{APIKey: "client-key", Host: "localhost", Port: 11434} + cfg.SetUpstreamBaseURL(upstream.URL) + cfg.setAdminPassword("admin-pass-123") + pool := NewAccountPool(nil) + keys := NewClientKeyPool(nil) + cc := NewCCClientWithPool(pool, upstream.URL) + usage := &UsageTracker{} + ring := newLogRing() + quotas := NewQuotaService(cc, pool, usage) + + mux := http.NewServeMux() + registerAdminRoutes(mux, cc, pool, keys, cfg, usage, ring, quotas) + root := http.NewServeMux() + root.HandleFunc("POST /admin/api/oauth/callback", handleWebOAuthCallback()) + root.Handle("/admin/", adminAuth(cfg, nil)(mux)) + srv := httptest.NewServer(root) + t.Cleanup(srv.Close) + t.Cleanup(quotas.Wait) + return srv, pool, usage, stub +} + +func waitForQuota(t *testing.T, usage *UsageTracker, id string) *QuotaSnapshot { + t.Helper() + deadline := time.Now().Add(3 * time.Second) + for { + if snap := usage.Quota(id); snap != nil { + return snap + } + if time.Now().After(deadline) { + t.Fatalf("quota for %s not refreshed in time", id) + } + time.Sleep(10 * time.Millisecond) + } +} + +func TestAdminAddAccountTriggersQuotaRefresh(t *testing.T) { + srv, _, usage, _ := newQuotaAdminTestEnv(t) + + resp, _ := adminRequest(t, srv, "POST", "/admin/api/accounts", "admin-pass-123", + map[string]any{"name": "main", "api_key": "cc-key-quota"}) + if resp.StatusCode != http.StatusCreated { + t.Fatalf("add status = %d", resp.StatusCode) + } + + snap := waitForQuota(t, usage, accountID("cc-key-quota")) + if snap.MonthlyCredits == nil || *snap.MonthlyCredits != 12.5 { + t.Fatalf("snapshot = %+v", snap) + } + if snap.Plan == nil || snap.Plan.Name != "Pro" { + t.Fatalf("plan = %+v", snap.Plan) + } +} + +func TestAdminAccountListIncludesQuota(t *testing.T) { + srv, pool, usage, _ := newQuotaAdminTestEnv(t) + acct, err := pool.Add("main", "cc-key-quota", true) + if err != nil { + t.Fatal(err) + } + usage.SetQuota(acct.ID, &QuotaSnapshot{MonthlyCredits: floatPtr(9.5), LastChecked: timePtr(time.Now())}) + + _, payload := adminRequest(t, srv, "GET", "/admin/api/accounts", "admin-pass-123", nil) + list := payload["accounts"].([]any) + if len(list) != 1 { + t.Fatalf("accounts = %v", list) + } + quota, ok := list[0].(map[string]any)["quota"].(map[string]any) + if !ok { + t.Fatalf("quota missing from account row: %v", list[0]) + } + if quota["monthly_credits"] != 9.5 { + t.Fatalf("quota = %v", quota) + } +} + +func TestAdminQuotaRefreshEndpoints(t *testing.T) { + srv, pool, usage, _ := newQuotaAdminTestEnv(t) + acct, err := pool.Add("main", "cc-key-quota", true) + if err != nil { + t.Fatal(err) + } + + // Single-account refresh returns the account row with a fresh snapshot. + resp, payload := adminRequest(t, srv, "POST", "/admin/api/accounts/"+acct.ID+"/quota/refresh", "admin-pass-123", nil) + if resp.StatusCode != http.StatusOK { + t.Fatalf("single refresh status = %d: %v", resp.StatusCode, payload) + } + quota, ok := payload["quota"].(map[string]any) + if !ok || quota["monthly_credits"] != 12.5 { + t.Fatalf("single refresh quota = %v", payload["quota"]) + } + + // Unknown account. + resp, _ = adminRequest(t, srv, "POST", "/admin/api/accounts/nope/quota/refresh", "admin-pass-123", nil) + if resp.StatusCode != http.StatusNotFound { + t.Fatalf("unknown account status = %d, want 404", resp.StatusCode) + } + + // Refresh all. + usage.SetQuota(acct.ID, &QuotaSnapshot{MonthlyCredits: floatPtr(0)}) + resp, payload = adminRequest(t, srv, "POST", "/admin/api/quotas/refresh", "admin-pass-123", nil) + if resp.StatusCode != http.StatusOK { + t.Fatalf("refresh all status = %d: %v", resp.StatusCode, payload) + } + list := payload["accounts"].([]any) + if len(list) != 1 || list[0].(map[string]any)["quota"].(map[string]any)["monthly_credits"] != 12.5 { + t.Fatalf("refresh all payload = %v", payload) + } + + // Refresh one via the body id. + resp, payload = adminRequest(t, srv, "POST", "/admin/api/quotas/refresh", "admin-pass-123", + map[string]any{"id": acct.ID}) + if resp.StatusCode != http.StatusOK { + t.Fatalf("refresh by id status = %d: %v", resp.StatusCode, payload) + } + resp, _ = adminRequest(t, srv, "POST", "/admin/api/quotas/refresh", "admin-pass-123", + map[string]any{"id": "missing"}) + if resp.StatusCode != http.StatusNotFound { + t.Fatalf("refresh missing id status = %d, want 404", resp.StatusCode) + } +} + +func TestAdminOverviewQuotaSummary(t *testing.T) { + srv, pool, usage, _ := newQuotaAdminTestEnv(t) + acct1, _ := pool.Add("a", "key-1-quota", true) + acct2, _ := pool.Add("b", "key-2-quota", true) + + older := time.Now().Add(-time.Hour) + newer := time.Now() + usage.SetQuota(acct1.ID, &QuotaSnapshot{ + MonthlyCredits: floatPtr(1), + BelowThreshold: true, + FiveHour: &QuotaWindow{Used: 5, Cap: 5, Exceeded: true}, + LastChecked: timePtr(older), + }) + usage.SetQuota(acct2.ID, &QuotaSnapshot{MonthlyCredits: floatPtr(50), LastChecked: timePtr(newer)}) + + _, payload := adminRequest(t, srv, "GET", "/admin/api/overview", "admin-pass-123", nil) + summary, ok := payload["quotas"].(map[string]any) + if !ok { + t.Fatalf("overview missing quotas: %v", payload) + } + if summary["synced"] != float64(2) || summary["exceeded"] != float64(1) || summary["low_balance"] != float64(1) { + t.Fatalf("quota summary = %v", summary) + } + last, ok := summary["last_checked_at"].(string) + if !ok { + t.Fatalf("last_checked_at missing: %v", summary) + } + parsed, err := time.Parse(time.RFC3339Nano, last) + if err != nil { + t.Fatalf("last_checked_at parse: %v", err) + } + if parsed.Unix() != newer.Unix() { + t.Fatalf("last_checked_at = %v, want %v", parsed, newer) + } +} + +func TestAdminOverviewQuotaSummaryEmpty(t *testing.T) { + srv, pool, _, _ := newQuotaAdminTestEnv(t) + pool.Add("a", "key-1-quota", true) + + _, payload := adminRequest(t, srv, "GET", "/admin/api/overview", "admin-pass-123", nil) + summary := payload["quotas"].(map[string]any) + if summary["synced"] != float64(0) || summary["exceeded"] != float64(0) || summary["low_balance"] != float64(0) { + t.Fatalf("empty summary = %v", summary) + } + if _, present := summary["last_checked_at"]; present { + t.Fatalf("empty summary should omit last_checked_at: %v", summary) + } +} + +func TestAdminAccountKeyChangeDropsQuota(t *testing.T) { + srv, pool, usage, _ := newQuotaAdminTestEnv(t) + acct, err := pool.Add("main", "cc-key-old", true) + if err != nil { + t.Fatal(err) + } + oldID := acct.ID + usage.SetQuota(oldID, &QuotaSnapshot{MonthlyCredits: floatPtr(4), LastChecked: timePtr(time.Now())}) + + resp, _ := adminRequest(t, srv, "PATCH", "/admin/api/accounts/"+oldID, "admin-pass-123", + map[string]any{"api_key": "cc-key-new"}) + if resp.StatusCode != http.StatusOK { + t.Fatalf("patch status = %d", resp.StatusCode) + } + if usage.Quota(oldID) != nil { + t.Fatal("quota for the old credential must be dropped") + } + newID := accountID("cc-key-new") + if newID == oldID { + t.Fatal("key change did not change the account ID") + } + // The new credential gets its own snapshot from the background refresh. + if snap := waitForQuota(t, usage, newID); snap.MonthlyCredits == nil { + t.Fatalf("new credential snapshot = %+v", snap) + } +} + +func TestAdminAccountDeleteClearsQuota(t *testing.T) { + srv, pool, usage, _ := newQuotaAdminTestEnv(t) + acct, err := pool.Add("main", "cc-key-delete", true) + if err != nil { + t.Fatal(err) + } + usage.SetQuota(acct.ID, &QuotaSnapshot{MonthlyCredits: floatPtr(4), LastChecked: timePtr(time.Now())}) + + resp, _ := adminRequest(t, srv, "DELETE", "/admin/api/accounts/"+acct.ID, "admin-pass-123", nil) + if resp.StatusCode != http.StatusOK { + t.Fatalf("delete status = %d", resp.StatusCode) + } + if usage.Quota(acct.ID) != nil { + t.Fatal("quota not cleared with the account") + } +} + +// ====== small helpers ====== + +func timePtr(t time.Time) *time.Time { return &t } + +func equalStrings(a, b []string) bool { + if len(a) != len(b) { + return false + } + left := append([]string(nil), a...) + right := append([]string(nil), b...) + sort.Strings(left) + sort.Strings(right) + for i := range left { + if left[i] != right[i] { + return false + } + } + return true +} diff --git a/internal/app/ratelimit_test.go b/internal/app/ratelimit_test.go index 8c0c501..0200223 100644 --- a/internal/app/ratelimit_test.go +++ b/internal/app/ratelimit_test.go @@ -66,3 +66,35 @@ func TestAdminAuthRateLimitsBruteForce(t *testing.T) { t.Fatalf("fresh limiter status = %d, want 200", rec.Code) } } + +func TestAdminAuthRateLimitsByForwardedClientIP(t *testing.T) { + cfg := &Config{} + cfg.setAdminPassword("correct-horse") + mux := http.NewServeMux() + mux.HandleFunc("GET /admin/api/overview", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + handler := adminAuth(cfg, nil)(mux) + + attempt := func(ip, password string) int { + req := httptest.NewRequest(http.MethodGet, "/admin/api/overview", nil) + req.RemoteAddr = "127.0.0.1:11434" + req.Header.Set("CF-Connecting-IP", ip) + req.Header.Set("Authorization", "Bearer "+password) + rec := httptest.NewRecorder() + handler.ServeHTTP(rec, req) + return rec.Code + } + + for i := 0; i < adminFailLimit; i++ { + if code := attempt("203.0.113.11", "wrong"); code != http.StatusUnauthorized { + t.Fatalf("attempt %d: status = %d, want 401", i+1, code) + } + } + if code := attempt("203.0.113.11", "correct-horse"); code != http.StatusTooManyRequests { + t.Fatalf("locked forwarded IP status = %d, want 429", code) + } + if code := attempt("203.0.113.12", "correct-horse"); code != http.StatusOK { + t.Fatalf("different forwarded IP status = %d, want 200", code) + } +} diff --git a/internal/app/server.go b/internal/app/server.go index 0450f5b..124acb6 100644 --- a/internal/app/server.go +++ b/internal/app/server.go @@ -91,7 +91,7 @@ func adminAuth(cfg *Config, limiter *ipRateLimiter) func(http.Handler) http.Hand } return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ip := clientIP(r.RemoteAddr) + ip := requestClientIP(r) if ok, retryAfter := limiter.Allow(ip); !ok { seconds := int(retryAfter.Seconds()) + 1 w.Header().Set("Retry-After", strconv.Itoa(seconds)) @@ -158,7 +158,7 @@ func loggingMiddleware(next http.Handler) http.Handler { recorder, wrapped := newStatusRecorder(w) next.ServeHTTP(wrapped, r) if r.URL.Path != "/health" { - log.Print(formatHTTPLog(r.Method, r.URL.Path, recorder.status, time.Since(start), r.RemoteAddr)) + log.Print(formatHTTPLog(r.Method, r.URL.Path, recorder.status, time.Since(start), requestClientIP(r))) } }) } @@ -171,6 +171,7 @@ func runServer(cc *CCClient, cfg *Config, usage *UsageTracker, ring *logRing) er pool = NewAccountPool(nil) } keys := NewClientKeyPool(cfg.APIKeys) + quotas := NewQuotaService(cc, pool, usage) mux := http.NewServeMux() @@ -187,7 +188,7 @@ func runServer(cc *CCClient, cfg *Config, usage *UsageTracker, ring *logRing) er // WebUI:管理 API 与内嵌的单文件界面,挂在 /webui 下,根路径留给 API。 adminMux := http.NewServeMux() - registerAdminRoutes(adminMux, cc, pool, keys, cfg, usage, ring) + registerAdminRoutes(adminMux, cc, pool, keys, cfg, usage, ring, quotas) if cfg.WebUIEnabled() { mux.Handle("/admin/", adminAuth(cfg, nil)(adminMux)) // Command Code 页面回传凭据的公开端点(靠 state 校验,非管理密码)。 @@ -215,6 +216,11 @@ func runServer(cc *CCClient, cfg *Config, usage *UsageTracker, ring *logRing) er shutdownSignal, stopSignals := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stopSignals() + // 额度采集:启动后立即刷新一次,之后每 5 分钟一次;服务退出时停止。 + quotaCtx, stopQuotas := context.WithCancel(shutdownSignal) + defer stopQuotas() + go quotas.Run(quotaCtx) + idleConnsClosed := make(chan struct{}) go func() { <-shutdownSignal.Done() diff --git a/internal/app/server_test.go b/internal/app/server_test.go index 4fdb3cc..c64f218 100644 --- a/internal/app/server_test.go +++ b/internal/app/server_test.go @@ -166,6 +166,32 @@ func TestLoggingMiddlewareLogsMethodPathStatusAndDuration(t *testing.T) { } } +func TestLoggingMiddlewareLogsForwardedClientIP(t *testing.T) { + t.Setenv("NO_COLOR", "1") + oldWriter := log.Writer() + var buf bytes.Buffer + log.SetOutput(&buf) + defer log.SetOutput(oldWriter) + + handler := loggingMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNoContent) + })) + req := httptest.NewRequest(http.MethodGet, "/v1/models", nil) + req.RemoteAddr = "127.0.0.1:11434" + req.Header.Set("CF-Connecting-IP", "203.0.113.18") + rec := httptest.NewRecorder() + + handler.ServeHTTP(rec, req) + + line := buf.String() + if !strings.Contains(line, "203.0.113.18") { + t.Fatalf("log %q missing forwarded client IP", line) + } + if strings.Contains(line, "127.0.0.1") { + t.Fatalf("log %q contains proxy IP", line) + } +} + func TestLoggingMiddlewareSkipsHealth(t *testing.T) { oldWriter := log.Writer() var buf bytes.Buffer @@ -184,3 +210,62 @@ func TestLoggingMiddlewareSkipsHealth(t *testing.T) { t.Fatalf("health log = %q", buf.String()) } } + +func TestRequestClientIPUsesCloudflareHeaderThroughLoopbackProxy(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.RemoteAddr = "127.0.0.1:11434" + req.Header.Set("CF-Connecting-IP", "203.0.113.8") + req.Header.Set("X-Forwarded-For", "198.51.100.4, 203.0.113.8") + + if got := requestClientIP(req); got != "203.0.113.8" { + t.Fatalf("client IP = %q, want 203.0.113.8", got) + } +} + +func TestRequestClientIPFallsBackThroughTrustedHeaders(t *testing.T) { + tests := []struct { + name string + cf string + xff string + real string + want string + }{ + {name: "xff", xff: "198.51.100.7, 203.0.113.9", want: "198.51.100.7"}, + {name: "real ip", real: "2001:db8::7", want: "2001:db8::7"}, + {name: "invalid headers", cf: "not-an-ip", xff: "bad, also-bad", real: "bad", want: "127.0.0.1"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.RemoteAddr = "127.0.0.1:11434" + req.Header.Set("CF-Connecting-IP", tt.cf) + req.Header.Set("X-Forwarded-For", tt.xff) + req.Header.Set("X-Real-IP", tt.real) + if got := requestClientIP(req); got != tt.want { + t.Fatalf("client IP = %q, want %q", got, tt.want) + } + }) + } +} + +func TestRequestClientIPIgnoresHeadersFromDirectPeer(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.RemoteAddr = "198.51.100.20:443" + req.Header.Set("CF-Connecting-IP", "203.0.113.8") + req.Header.Set("X-Forwarded-For", "203.0.113.9") + req.Header.Set("X-Real-IP", "203.0.113.10") + + if got := requestClientIP(req); got != "198.51.100.20" { + t.Fatalf("client IP = %q, want 198.51.100.20", got) + } +} + +func TestRequestClientIPHandlesLoopbackIPv6(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.RemoteAddr = "[::1]:11434" + req.Header.Set("CF-Connecting-IP", "2001:db8::8") + + if got := requestClientIP(req); got != "2001:db8::8" { + t.Fatalf("client IP = %q, want 2001:db8::8", got) + } +} diff --git a/internal/app/usage.go b/internal/app/usage.go index d5fdeef..0c0319e 100644 --- a/internal/app/usage.go +++ b/internal/app/usage.go @@ -27,6 +27,9 @@ type UsageTracker struct { accMu sync.Mutex accounts map[string]*UsageCounters clientKeys map[string]*UsageCounters + // quotas caches the latest fetched quota snapshot per account ID; the + // entries are replaced wholesale and never mutated in place. + quotas map[string]*QuotaSnapshot } type UsageCounters struct { @@ -157,11 +160,18 @@ func (u *UsageTracker) ensureMapsLocked() { if u.clientKeys == nil { u.clientKeys = make(map[string]*UsageCounters) } + if u.quotas == nil { + u.quotas = make(map[string]*QuotaSnapshot) + } } -func (u *UsageTracker) countersFor(m map[string]*UsageCounters, id string) UsageSnapshotEntry { +// countersFor returns the counters for id from the map selected by m. m is a +// pointer so the field is dereferenced only under accMu: reading u.accounts / +// u.clientKeys at the call site would race with ensureMapsLocked, which can +// initialize them from a background goroutine. +func (u *UsageTracker) countersFor(m *map[string]*UsageCounters, id string) UsageSnapshotEntry { u.accMu.Lock() - c := m[id] + c := (*m)[id] u.accMu.Unlock() if c == nil { return UsageSnapshotEntry{} @@ -171,19 +181,62 @@ func (u *UsageTracker) countersFor(m map[string]*UsageCounters, id string) Usage // AccountUsage returns a snapshot of one account's durable counters. func (u *UsageTracker) AccountUsage(id string) UsageSnapshotEntry { - return u.countersFor(u.accounts, id) + return u.countersFor(&u.accounts, id) } // ClientKeyUsage returns a snapshot of one client key's durable counters. func (u *UsageTracker) ClientKeyUsage(id string) UsageSnapshotEntry { - return u.countersFor(u.clientKeys, id) + return u.countersFor(&u.clientKeys, id) } -// DropAccount forgets a removed account's counters so they stop persisting. +// DropAccount forgets a removed account's counters and quota snapshot so they +// stop persisting. func (u *UsageTracker) DropAccount(id string) { u.accMu.Lock() defer u.accMu.Unlock() delete(u.accounts, id) + delete(u.quotas, id) +} + +// Quota returns the cached quota snapshot for an account, or nil. Stored +// snapshots are immutable; callers must not mutate the returned value. +func (u *UsageTracker) Quota(id string) *QuotaSnapshot { + u.accMu.Lock() + defer u.accMu.Unlock() + return u.quotas[id] +} + +// SetQuota stores an account's latest quota snapshot. +func (u *UsageTracker) SetQuota(id string, snap *QuotaSnapshot) { + if id == "" || snap == nil { + return + } + u.accMu.Lock() + defer u.accMu.Unlock() + u.ensureMapsLocked() + u.quotas[id] = snap +} + +// DropQuota forgets an account's quota snapshot, used when its key changes +// because the snapshot belongs to the old credential. +func (u *UsageTracker) DropQuota(id string) { + u.accMu.Lock() + defer u.accMu.Unlock() + delete(u.quotas, id) +} + +// quotaSnapshot copies the quota map for persistence. +func (u *UsageTracker) quotaSnapshot() map[string]*QuotaSnapshot { + u.accMu.Lock() + defer u.accMu.Unlock() + if len(u.quotas) == 0 { + return nil + } + out := make(map[string]*QuotaSnapshot, len(u.quotas)) + for id, q := range u.quotas { + out[id] = q + } + return out } // MoveAccount migrates counters when an account's key (and therefore ID) @@ -263,6 +316,14 @@ type UsageSnapshotEntry struct { CacheWriteTokens int64 `json:"cache_write_tokens"` } +// persistedUsage is the on-disk shape of usage.json: the public usage +// snapshot plus per-account quota snapshots, which stay out of UsageSnapshot +// so they never reach the unauthenticated /usage endpoint. +type persistedUsage struct { + UsageSnapshot + Quotas map[string]*QuotaSnapshot `json:"quotas,omitempty"` +} + // TotalTokens returns prompt + completion (not counting cache separately) func (s UsageSnapshot) TotalTokens() int64 { return s.PromptTokens + s.CompletionTokens @@ -279,7 +340,7 @@ func loadUsage() *UsageTracker { if err != nil { return u } - var snap UsageSnapshot + var snap persistedUsage if json.Unmarshal(data, &snap) != nil { return u } @@ -301,6 +362,9 @@ func loadUsage() *UsageTracker { c.restore(entry) u.clientKeys[id] = c } + for id, q := range snap.Quotas { + u.quotas[id] = q + } u.accMu.Unlock() return u } @@ -309,7 +373,7 @@ func (u *UsageTracker) save() error { u.saveMu.Lock() defer u.saveMu.Unlock() - snap := u.Snapshot() + snap := persistedUsage{UsageSnapshot: u.Snapshot(), Quotas: u.quotaSnapshot()} data, err := json.MarshalIndent(snap, "", " ") if err != nil { return err diff --git a/internal/web/index.html b/internal/web/index.html index 03b7ae0..9e3c47d 100644 --- a/internal/web/index.html +++ b/internal/web/index.html @@ -208,6 +208,25 @@ .faint { color: var(--text-faint); } .restart-note { color: var(--amber); font-size: 12.5px; margin-top: 8px; display: none; } .err-line { color: var(--red); font-size: 12px; font-family: var(--mono); word-break: break-all; } + + /* quota */ + .quota-bars { display: flex; flex-direction: column; gap: 7px; max-width: 640px; } + .qbar { display: grid; grid-template-columns: 104px 1fr 240px; gap: 10px; align-items: center; font-size: 12px; } + .qbar .qlabel { color: var(--text-dim); white-space: nowrap; } + .qbar .track { background: #0d1117; border: 1px solid var(--border); border-radius: 6px; height: 9px; overflow: hidden; } + .qbar .fill { display: block; height: 100%; } + .qbar .meta { color: var(--text-faint); font-family: var(--mono); font-size: 11.5px; text-align: right; white-space: nowrap; } + .lvl-1 { background: var(--green); } + .lvl-2 { background: var(--amber); } + .lvl-3 { background: #f97316; } + .lvl-4 { background: var(--red); } + .quota-meta { display: flex; flex-wrap: wrap; gap: 4px 16px; margin-top: 8px; font-size: 12.5px; color: var(--text-dim); } + .quota-meta b { color: var(--text); font-weight: 600; font-family: var(--mono); } + .quota-err { color: var(--red); font-size: 12px; font-family: var(--mono); margin-top: 6px; word-break: break-all; } + + /* each account's main row and its quota/usage detail read as one block */ + tbody.acc-group tr > td { border-bottom: none; } + tbody.acc-group + tbody.acc-group tr:first-child > td { border-top: 1px solid var(--border); }
@@ -274,10 +293,13 @@多账号按轮询方式轮流使用;某个账号 429 限流时自动冷却并在期间跳过,401/403/5xx 自动切换到下一个账号。OAuth 需要浏览器能访问服务器的 127.0.0.1:5959-5968 回调端口。
+多账号按轮询方式轮流使用;某个账号 429 限流时自动冷却并在期间跳过,401/403/5xx 自动切换到下一个账号。OAuth 需要浏览器能访问服务器的 127.0.0.1:5959-5968 回调端口。额度每 5 分钟自动刷新,也可在账号列表中手动刷新。