yangjianbo
53e1c8b268
perf(日志): 降噪优化,将常规成功日志降级为 Debug 级别
...
- GIN Logger 中间件跳过 /health 和 /setup/status 的请求日志
- UsageCleanup 空闲轮询(no_task)日志降级为 slog.Debug
- Scheduler 常规 rebuild ok 日志降级为 slog.Debug
- DashboardAggregation 常规聚合完成日志降级为 slog.Debug
- TokenRefresh 无刷新活动时周期日志降级为 slog.Debug
生产环境(Info 级别)下自动静默,debug 模式下仍可见。
错误、警告类日志保持原有级别不变。
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-07 23:29:24 +08:00
yangjianbo
00caf0bcd8
test: 为代码审核修复添加详细单元测试(7个测试文件,50+测试用例)
...
新增测试文件:
- cors_test.go: CORS 条件化头部测试(12个测试,覆盖白名单/黑名单/通配符/凭证/多源/Vary)
- gateway_helper_backoff_test.go: nextBackoff 退避测试(6个测试+基准,验证指数增长/边界/抖动/收敛)
- billing_cache_jitter_test.go: jitteredTTL 抖动测试(5个测试+基准,验证范围/上界/方差/均值)
- subscription_calculate_progress_test.go: calculateProgress 纯函数测试(9个测试,覆盖日/周/月限额/超限截断/过期)
- openai_gateway_handler_test.go: SSE JSON 转义测试(7个子用例,验证双引号/反斜杠/换行符安全)
更新测试文件:
- response_transformer_test.go: 增强 generateRandomID 测试(7个测试,含并发/字符集/降级计数器)
- security_headers_test.go: 适配 GenerateNonce 新签名
- api_key_auth_test.go: 适配 NewSubscriptionService 新参数
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-07 22:14:07 +08:00
yangjianbo
9634494ba9
fix: 修复代码审核发现的10个问题(P0安全+P1数据一致性+P2性能优化)
...
P0: OpenAI SSE 错误消息 JSON 注入 — 使用 json.Marshal 替代 fmt.Sprintf
P1: subscription 续期包裹 Ent 事务确保原子性
P1: CSP nonce 生成处理 crypto/rand 错误,失败降级为 unsafe-inline
P1: singleflight 透传数据库真实错误,不再吞没为 not found
P1: GetUserSubscriptionsWithProgress 提取 calculateProgress 消除 N+1
P2: billing_cache/gateway_helper 迁移到 math/rand/v2 消除全局锁争用
P2: generateRandomID 降级分支增加原子计数器防碰撞
P2: CORS 非白名单 origin 不再设置 Allow-Headers/Methods/Max-Age
P2: Turnstile 验证移除 VerifyCode 空值跳过条件防绕过
P2: Redis Cluster Lua 脚本空 KEYS 添加兼容性警告注释
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-07 22:13:45 +08:00
yangjianbo
fd43be8d0b
merge: 合并 main 分支到 test,解决 config 和 modelWhitelist 冲突
...
- config.go: 保留 Sora 配置,合入 SubscriptionCache 配置
- useModelWhitelist.ts: 同时保留 soraModels 和 antigravityModels
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-07 20:18:07 +08:00
yangjianbo
a14dfb769a
Merge branch 'dev-release'
2026-02-07 19:58:00 +08:00
yangjianbo
2588fa6a8f
fix(audit): 第二批审计修复 — P0 生产 Bug、安全加固、性能优化、缓存一致性、代码质量
...
基于 backend-code-audit 审计报告,修复剩余 P0/P1/P2 共 34 项问题:
P0 生产 Bug:
- 修复 time.Since(time.Now()) 计时逻辑错误 (P0-03)
- generateRandomID 改用 crypto/rand 替代固定索引 (P0-04)
- IncrementQuotaUsed 重写为 Ent 原子操作消除 TOCTOU 竞态 (P0-05)
安全加固:
- gateway/openai handler 错误响应替换为泛化消息,防止内部信息泄露 (P1-14)
- usage_log_repo dateFormat 参数改用白名单映射,防止 SQL 注入 (P1-16)
- 默认配置安全加固:sslmode=prefer、response_headers=true、mode=release (P1-18/19, P2-15)
性能优化:
- gateway handler 循环内 defer 替换为显式 releaseWait 闭包 (P1-02)
- group_repo/promo_code_repo Count 前 Clone 查询避免状态污染 (P1-03)
- usage_log_repo 四个查询添加 LIMIT 10000 防止 OOM (P1-07)
- GetBatchUsageStats 添加时间范围参数,默认最近 30 天 (P1-10)
- ip.go CIDR 预编译为包级变量 (P1-11)
- BatchUpdateCredentials 重构为先验证后更新 (P1-13)
缓存一致性:
- billing_cache 添加 jitteredTTL 防止缓存雪崩 (P2-10)
- DeductUserBalance/UpdateSubscriptionUsage 错误传播修复 (P2-12)
- UserService.UpdateBalance 成功后异步失效 billingCache (P2-13)
代码质量:
- search 截断改为按 rune 处理,支持多字节字符 (P2-01)
- TLS Handshake 改为 HandshakeContext 支持 context 取消 (P2-07)
- CORS 预检添加 Access-Control-Max-Age: 86400 (P2-16)
测试覆盖:
- 新增 user_service_test.go(UpdateBalance 缓存失效 6 个用例)
- 新增 batch_update_credentials_test.go(fail-fast + 类型验证 7 个用例)
- 新增 response_transformer_test.go、ip_test.go、usage_log_repo_unit_test.go、search_truncate_test.go
- 集成测试:IncrementQuotaUsed 并发测试、billing_cache 错误传播测试
- config_test.go 补充 server.mode/sslmode 默认值断言
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-07 19:46:42 +08:00
yangjianbo
65c0d8b51f
fix(middleware): 管理员JWT增加TokenVersion校验
...
管理员改密后旧JWT会被拒绝,并补充单元测试覆盖。
2026-02-07 16:34:57 +08:00
yangjianbo
0e514ed80b
perf(middleware): 优化订阅模式认证中间件,5次串行调用降至2步同步+1步异步
...
- 为 GetActiveSubscription 添加 ristretto L1 缓存 + singleflight 防击穿
- 合并 ValidateSubscription + CheckUsageLimits 为纯内存 ValidateAndCheckLimits
- 窗口维护操作(激活/重置)异步化,不再阻塞首字节
- 缓存返回浅拷贝,避免并发 data race 和缓存污染
- 所有管理操作(分配/续期/撤销/扩展/窗口重置)同步失效 L1 缓存
- 新增 SubscriptionCacheConfig 可配置 L1 缓存大小/TTL/抖动
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-07 14:43:12 +08:00
erio
5e98445b22
feat(antigravity): comprehensive enhancements - model mapping, rate limiting, scheduling & ops
...
Key changes:
- Upgrade model mapping: Opus 4.5 → Opus 4.6-thinking with precise matching
- Unified rate limiting: scope-level → model-level with Redis snapshot sync
- Load-balanced scheduling by call count with smart retry mechanism
- Force cache billing support
- Model identity injection in prompts with leak prevention
- Thinking mode auto-handling (max_tokens/budget_tokens fix)
- Frontend: whitelist mode toggle, model mapping validation, status indicators
- Gemini session fallback with Redis Trie O(L) matching
- Ops: enhanced concurrency monitoring, account availability, retry logic
- Migration scripts: 049-051 for model mapping unification
2026-02-07 12:31:10 +08:00
LLLLLLiulei
7319122e92
merge upstream/main
2026-02-06 11:33:45 +08:00
yangjianbo
d8e405511e
Merge branch 'main' of https://github.com/mt21625457/aicodex2api
2026-02-06 06:56:23 +08:00
shaw
39e05a2dad
feat: 新增全局错误透传规则功能
...
支持管理员配置上游错误如何返回给客户端:
- 新增 ErrorPassthroughRule 数据模型和 Ent Schema
- 实现规则的 CRUD API(/admin/error-passthrough-rules)
- 支持按错误码、关键词匹配,支持 any/all 匹配模式
- 支持按平台过滤(anthropic/openai/gemini/antigravity)
- 支持透传或自定义响应状态码和错误消息
- 实现两级缓存(Redis + 本地内存)和多实例同步
- 集成到 gateway_handler 的错误处理流程
- 新增前端管理界面组件
- 新增单元测试覆盖核心匹配逻辑
优化:
- 移除 refreshLocalCache 中的冗余排序(数据库已排序)
- 后端 Validate() 增加匹配条件非空校验
2026-02-05 21:52:54 +08:00
LLLLLLiulei
0b45d48e85
perf: batch fetch proxies for account export
2026-02-05 18:40:49 +08:00
LLLLLLiulei
ce9a247a9d
feat: add proxy import flow
2026-02-05 18:23:49 +08:00
LLLLLLiulei
b4bd46d067
feat: add data import/export bundle
2026-02-05 17:46:08 +08:00
shaw
2b192f7dca
feat: 支持用户专属分组倍率配置
2026-02-05 16:05:42 +08:00
Wesley Liddick
dabed96af4
Merge pull request #486 from s-Joshua-s/feat/usage-filter-by-apikey
...
feat(gateway): filter /v1/usage stats by API Key instead of UserID
2026-02-05 13:37:31 +08:00
Lemon
7498035d24
Merge branch 'main' into mod
2026-02-05 12:49:43 +08:00
Lemon
39a0359dd5
feat: enhance HTTP/2 Cleartext (h2c) configuration options
2026-02-05 12:48:05 +08:00
shaw
49a3c43741
feat(auth): 实现 Refresh Token 机制
...
- 新增 Access Token + Refresh Token 双令牌认证
- 支持 Token 自动刷新和轮转
- 添加登出和撤销所有会话接口
- 前端实现无感刷新和主动刷新定时器
2026-02-05 12:42:54 +08:00
JIA-ss
fa3ea5ee4d
feat(gateway): filter /v1/usage stats by API Key instead of UserID
...
Previously the /v1/usage endpoint aggregated usage stats (today/total
tokens, cost, RPM/TPM) across all API Keys belonging to the user.
This made it impossible to distinguish usage from different API Keys
(e.g. balance vs subscription keys).
Now the usage stats are filtered by the current request's API Key ID,
so each key only sees its own usage data. The balance/remaining fields
are unaffected and still reflect the user-level wallet balance.
Changes:
- Add GetAPIKeyDashboardStats to repository interface and implementation
- Add getPerformanceStatsByAPIKey helper (also fixes TPM to include
cache_creation_tokens and cache_read_tokens)
- Add GetAPIKeyDashboardStats to UsageService
- Update Usage handler to call GetAPIKeyDashboardStats(apiKey.ID)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com >
2026-02-05 11:45:54 +08:00
Lemon
97a5c1ac1d
feat: add support for HTTP/2 Cleartext (h2c) connections
2026-02-04 21:40:25 +08:00
yangjianbo
de7ff902de
Merge branch 'main' into test
2026-02-04 20:35:09 +08:00
程序猿MT
dd96ada3c6
Merge branch 'Wei-Shaw:main' into main
2026-02-04 18:56:47 +08:00
柴叁
9985c4a344
fix(gemini): 优化 Gemini 接口认证兼容性,支持 Authorization: Bearer
...
调整 API key 提取优先级,让 /v1beta 接口同时支持 x-goog-api-key 和
Authorization: Bearer 两种认证方式,解决 OpenClaw 等使用 Bearer 认证
的客户端无法直接访问 Gemini 接口的问题。
2026-02-04 16:26:36 +08:00
yangjianbo
377bffe281
Merge branch 'main' into test
2026-02-03 22:48:04 +08:00
Wesley Liddick
4cce21b125
Merge branch 'main' into main
2026-02-03 21:43:41 +08:00
Wesley Liddick
c0c9c984d1
Merge pull request #471 from bayma888/feature/api-key-quota-expiration
...
feat(api-key): 添加API密钥独立配额和过期时间功能
2026-02-03 21:11:17 +08:00
yangjianbo
31fe017888
Merge branch 'main' of https://github.com/mt21625457/aicodex2api
2026-02-03 21:00:11 +08:00
bayma888
0afc5d0b1a
fix(test): update API contract tests for new quota fields
...
Add quota, quota_used, expires_at fields to expected JSON responses
in POST /api/v1/keys and GET /api/v1/keys test cases.
2026-02-03 20:54:25 +08:00
bayma888
be7bc658fc
fix(test): add IncrementQuotaUsed to all APIKeyRepository test stubs
...
- Add missing IncrementQuotaUsed method to stubApiKeyRepo in api_contract_test.go
- Fix gofmt formatting issues in api_key_service.go, dto/types.go, api_key_handler.go
2026-02-03 20:49:58 +08:00
Wesley Liddick
c89bbf5130
Merge pull request #458 from bayma888/feature/admin-user-balance-history
...
feat(admin): 管理员可查看每个用户充值和并发变动记录、点击余额可直接查看、优化弹框UI
2026-02-03 20:37:30 +08:00
bayma888
e59e3a9f00
fix(test): add IncrementQuotaUsed to all APIKeyRepository test stubs
...
Add the missing IncrementQuotaUsed method to:
- fakeAPIKeyRepo (api_key_auth_google_test.go)
- stubApiKeyRepo (api_key_auth_test.go)
- apiKeyRepoStub (api_key_service_delete_test.go)
- authRepoStub (api_key_service_cache_test.go)
2026-02-03 20:00:43 +08:00
bayma888
6146be1474
feat(api-key): add independent quota and expiration support
...
This feature allows API Keys to have their own quota limits and expiration
times, independent of the user's balance.
Backend:
- Add quota, quota_used, expires_at fields to api_key schema
- Implement IsExpired() and IsQuotaExhausted() checks in middleware
- Add ResetQuota and ClearExpiration API endpoints
- Integrate quota billing in gateway handlers (OpenAI, Anthropic, Gemini)
- Include quota/expiration fields in auth cache for performance
- Expiration check returns 403, quota exhausted returns 429
Frontend:
- Add quota and expiration inputs to key create/edit dialog
- Add quick-select buttons for expiration (+7, +30, +90 days)
- Add reset quota confirmation dialog
- Add expires_at column to keys list
- Add i18n translations for new features (en/zh)
Migration:
- Add 045_add_api_key_quota.sql for new columns
2026-02-03 19:49:31 +08:00
bayma888
730d2a9ad2
fix(test): add missing stub methods to stubRedeemCodeRepo in api_contract_test
...
Add ListByUserPaginated and SumPositiveBalanceByUser methods
2026-02-03 19:36:17 +08:00
song
7cb5444dbb
fix: update tests for group fallback
2026-02-03 16:48:52 +08:00
song
2220fd18ca
merge upstream main
2026-02-03 15:36:17 +08:00
shuike
0ed4a404e4
fix(test): api_contract_test添加 invitation_code_enabled 字段
2026-02-03 13:38:44 +08:00
shuike
6c86501d11
feat: 增加邀请码注册功能
2026-02-03 13:38:44 +08:00
bayma888
606e29d390
feat(admin): add user balance/concurrency history modal
...
- Add new API endpoint GET /admin/users/:id/balance-history with pagination and type filter
- Add SumPositiveBalanceByUser for calculating total recharged amount
- Create UserBalanceHistoryModal component with:
- User info header (email, username, created_at, current balance, notes, total recharged)
- Type filter dropdown (all/balance/admin_balance/concurrency/admin_concurrency/subscription)
- Quick deposit/withdraw buttons
- Paginated history list with icons and colored values
- Add instant tooltip on balance column for better UX
- Add z-index prop to BaseDialog for modal stacking control
- Update i18n translations (zh/en)
2026-02-03 00:16:10 +08:00
song
0170d19fa7
merge upstream main
2026-02-02 22:13:50 +08:00
liuxiongfeng
ce1d2904c7
test: 为测试 stub 添加缺失的 GroupRepository 接口方法
...
新增 BindAccountsToGroup 和 GetAccountIDsByGroupIDs 方法的 stub 实现,
确保测试文件中的 mock 类型满足 GroupRepository 接口要求。
2026-02-02 22:06:37 +08:00
yangjianbo
399dd78b2a
feat(Sora): 直连生成并移除sora2api依赖
...
实现直连 Sora 客户端、媒体落地与清理策略\n更新网关与前端配置以支持 Sora 平台\n补齐单元测试与契约测试,新增 curl 测试脚本\n\n测试: go test ./... -tags=unit
2026-02-01 21:37:10 +08:00
yangjianbo
618a614cbf
feat(Sora): 完成Sora网关接入与媒体能力
...
新增 Sora 网关路由、账号调度与同步服务\n补充媒体代理与签名 URL、模型列表动态拉取\n完善计费配置、前端支持与相关测试
2026-01-31 20:22:22 +08:00
ducky
b7f69844e1
feat(announcements): add admin/user announcement system
...
Implements announcements end-to-end (admin CRUD + read status, user list + mark read) with OR-of-AND targeting. Also breaks the ent<->service import cycle by moving schema-facing constants/targeting into a new domain package.
2026-01-30 16:45:04 +08:00
yangjianbo
99dc3b59bc
feat(账号): 添加 Sora 账号双表同步与创建
...
- 新增 sora_accounts 表与 accounts.extra GIN 索引\n- OpenAI OAuth 支持同时创建 Sora 账号并同步配置\n- Token 刷新同步关联 Sora 账号凭证与扩展表\n- 增加 Sora 账号连通性测试与前端开关文案
2026-01-30 14:08:04 +08:00
ducky
04a509d45e
feat(purchase): 增加购买订阅 iframe 页面与配置
...
- 新增 /purchase 页面(iframe + 新窗口兜底)
- 管理员系统设置可配置开关与URL
- 非 simple mode 才在侧边栏展示入口
2026-01-28 13:54:32 +08:00
shaw
1245f07a2d
feat(auth): 实现 TOTP 双因素认证功能
...
新增功能:
- 支持 Google Authenticator 等应用进行 TOTP 二次验证
- 用户可在个人设置中启用/禁用 2FA
- 登录时支持 TOTP 验证流程
- 管理后台可全局开关 TOTP 功能
安全增强:
- TOTP 密钥使用 AES-256-GCM 加密存储
- 添加 TOTP_ENCRYPTION_KEY 配置项,必须手动配置才能启用功能
- 防止服务重启导致加密密钥变更使用户无法登录
- 验证失败次数限制,防止暴力破解
配置说明:
- Docker 部署:在 .env 中设置 TOTP_ENCRYPTION_KEY
- 非 Docker 部署:在 config.yaml 中设置 totp.encryption_key
- 生成密钥命令:openssl rand -hex 32
2026-01-26 09:19:53 +08:00
shaw
9cc8352593
feat(auth): 密码重置邮件队列化与限流优化
...
- 邮件发送改为异步队列处理,避免并发导致发送失败
- 新增 Email 维度限流(30秒冷却期),防止邮件轰炸
- Token 验证使用常量时间比较,防止时序攻击
- 重构代码消除冗余,提取公共验证逻辑
2026-01-24 22:55:28 +08:00
shaw
43a1031e38
fix(test): 修复订阅相关测试失败问题
...
1. 使用未来日期(2099年)作为测试订阅的过期时间,避免
normalizeSubscriptionStatus 将测试数据标记为过期
2. 修复 List 方法调用参数不足的问题(新增 sortBy/sortOrder 参数)
2026-01-24 21:10:02 +08:00