新增功能: - 新增 Sora 账号管理和 OAuth 认证 - 新增 Sora 视频/图片生成 API 网关 - 新增 Sora 任务调度和缓存机制 - 新增 Sora 使用统计和计费支持 - 前端增加 Sora 平台配置界面 安全修复(代码审核): - [SEC-001] 限制媒体下载响应体大小(图片 20MB、视频 200MB),防止 DoS 攻击 - [SEC-002] 限制 SDK API 响应大小(1MB),防止内存耗尽 - [SEC-003] 修复 SSRF 风险,添加 URL 验证并强制使用代理配置 BUG 修复(代码审核): - [BUG-001] 修复 for 循环内 defer 累积导致的资源泄漏 - [BUG-002] 修复图片并发槽位获取失败时已持有锁未释放的永久泄漏 性能优化(代码审核): - [PERF-001] 添加 Sentinel Token 缓存(3 分钟有效期),减少 PoW 计算开销 技术细节: - 使用 io.LimitReader 限制所有外部输入的大小 - 添加 urlvalidator 验证防止 SSRF 攻击 - 使用 sync.Map 实现线程安全的包级缓存 - 优化并发槽位管理,添加 releaseAll 模式防止泄漏 影响范围: - 后端:新增 Sora 相关数据模型、服务、网关和管理接口 - 前端:新增 Sora 平台配置、账号管理和监控界面 - 配置:新增 Sora 相关配置项和环境变量 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
185 lines
7.6 KiB
Go
185 lines
7.6 KiB
Go
package service
|
||
|
||
// Status constants
|
||
const (
|
||
StatusActive = "active"
|
||
StatusDisabled = "disabled"
|
||
StatusError = "error"
|
||
StatusUnused = "unused"
|
||
StatusUsed = "used"
|
||
StatusExpired = "expired"
|
||
)
|
||
|
||
// Role constants
|
||
const (
|
||
RoleAdmin = "admin"
|
||
RoleUser = "user"
|
||
)
|
||
|
||
// Platform constants
|
||
const (
|
||
PlatformAnthropic = "anthropic"
|
||
PlatformOpenAI = "openai"
|
||
PlatformGemini = "gemini"
|
||
PlatformAntigravity = "antigravity"
|
||
PlatformSora = "sora"
|
||
)
|
||
|
||
// Account type constants
|
||
const (
|
||
AccountTypeOAuth = "oauth" // OAuth类型账号(full scope: profile + inference)
|
||
AccountTypeSetupToken = "setup-token" // Setup Token类型账号(inference only scope)
|
||
AccountTypeAPIKey = "apikey" // API Key类型账号
|
||
)
|
||
|
||
// Redeem type constants
|
||
const (
|
||
RedeemTypeBalance = "balance"
|
||
RedeemTypeConcurrency = "concurrency"
|
||
RedeemTypeSubscription = "subscription"
|
||
)
|
||
|
||
// PromoCode status constants
|
||
const (
|
||
PromoCodeStatusActive = "active"
|
||
PromoCodeStatusDisabled = "disabled"
|
||
)
|
||
|
||
// Admin adjustment type constants
|
||
const (
|
||
AdjustmentTypeAdminBalance = "admin_balance" // 管理员调整余额
|
||
AdjustmentTypeAdminConcurrency = "admin_concurrency" // 管理员调整并发数
|
||
)
|
||
|
||
// Group subscription type constants
|
||
const (
|
||
SubscriptionTypeStandard = "standard" // 标准计费模式(按余额扣费)
|
||
SubscriptionTypeSubscription = "subscription" // 订阅模式(按限额控制)
|
||
)
|
||
|
||
// Subscription status constants
|
||
const (
|
||
SubscriptionStatusActive = "active"
|
||
SubscriptionStatusExpired = "expired"
|
||
SubscriptionStatusSuspended = "suspended"
|
||
)
|
||
|
||
// LinuxDoConnectSyntheticEmailDomain 是 LinuxDo Connect 用户的合成邮箱后缀(RFC 保留域名)。
|
||
const LinuxDoConnectSyntheticEmailDomain = "@linuxdo-connect.invalid"
|
||
|
||
// Setting keys
|
||
const (
|
||
// 注册设置
|
||
SettingKeyRegistrationEnabled = "registration_enabled" // 是否开放注册
|
||
SettingKeyEmailVerifyEnabled = "email_verify_enabled" // 是否开启邮件验证
|
||
SettingKeyPromoCodeEnabled = "promo_code_enabled" // 是否启用优惠码功能
|
||
|
||
// 邮件服务设置
|
||
SettingKeySMTPHost = "smtp_host" // SMTP服务器地址
|
||
SettingKeySMTPPort = "smtp_port" // SMTP端口
|
||
SettingKeySMTPUsername = "smtp_username" // SMTP用户名
|
||
SettingKeySMTPPassword = "smtp_password" // SMTP密码(加密存储)
|
||
SettingKeySMTPFrom = "smtp_from" // 发件人地址
|
||
SettingKeySMTPFromName = "smtp_from_name" // 发件人名称
|
||
SettingKeySMTPUseTLS = "smtp_use_tls" // 是否使用TLS
|
||
|
||
// Cloudflare Turnstile 设置
|
||
SettingKeyTurnstileEnabled = "turnstile_enabled" // 是否启用 Turnstile 验证
|
||
SettingKeyTurnstileSiteKey = "turnstile_site_key" // Turnstile Site Key
|
||
SettingKeyTurnstileSecretKey = "turnstile_secret_key" // Turnstile Secret Key
|
||
|
||
// LinuxDo Connect OAuth 登录设置
|
||
SettingKeyLinuxDoConnectEnabled = "linuxdo_connect_enabled"
|
||
SettingKeyLinuxDoConnectClientID = "linuxdo_connect_client_id"
|
||
SettingKeyLinuxDoConnectClientSecret = "linuxdo_connect_client_secret"
|
||
SettingKeyLinuxDoConnectRedirectURL = "linuxdo_connect_redirect_url"
|
||
|
||
// OEM设置
|
||
SettingKeySiteName = "site_name" // 网站名称
|
||
SettingKeySiteLogo = "site_logo" // 网站Logo (base64)
|
||
SettingKeySiteSubtitle = "site_subtitle" // 网站副标题
|
||
SettingKeyAPIBaseURL = "api_base_url" // API端点地址(用于客户端配置和导入)
|
||
SettingKeyContactInfo = "contact_info" // 客服联系方式
|
||
SettingKeyDocURL = "doc_url" // 文档链接
|
||
SettingKeyHomeContent = "home_content" // 首页内容(支持 Markdown/HTML,或 URL 作为 iframe src)
|
||
SettingKeyHideCcsImportButton = "hide_ccs_import_button" // 是否隐藏 API Keys 页面的导入 CCS 按钮
|
||
|
||
// 默认配置
|
||
SettingKeyDefaultConcurrency = "default_concurrency" // 新用户默认并发量
|
||
SettingKeyDefaultBalance = "default_balance" // 新用户默认余额
|
||
|
||
// 管理员 API Key
|
||
SettingKeyAdminAPIKey = "admin_api_key" // 全局管理员 API Key(用于外部系统集成)
|
||
|
||
// Gemini 配额策略(JSON)
|
||
SettingKeyGeminiQuotaPolicy = "gemini_quota_policy"
|
||
|
||
// Model fallback settings
|
||
SettingKeyEnableModelFallback = "enable_model_fallback"
|
||
SettingKeyFallbackModelAnthropic = "fallback_model_anthropic"
|
||
SettingKeyFallbackModelOpenAI = "fallback_model_openai"
|
||
SettingKeyFallbackModelGemini = "fallback_model_gemini"
|
||
SettingKeyFallbackModelAntigravity = "fallback_model_antigravity"
|
||
|
||
// Request identity patch (Claude -> Gemini systemInstruction injection)
|
||
SettingKeyEnableIdentityPatch = "enable_identity_patch"
|
||
SettingKeyIdentityPatchPrompt = "identity_patch_prompt"
|
||
|
||
// =========================
|
||
// Sora Settings
|
||
// =========================
|
||
|
||
SettingKeySoraBaseURL = "sora_base_url"
|
||
SettingKeySoraTimeout = "sora_timeout"
|
||
SettingKeySoraMaxRetries = "sora_max_retries"
|
||
SettingKeySoraPollInterval = "sora_poll_interval"
|
||
SettingKeySoraCallLogicMode = "sora_call_logic_mode"
|
||
SettingKeySoraCacheEnabled = "sora_cache_enabled"
|
||
SettingKeySoraCacheBaseDir = "sora_cache_base_dir"
|
||
SettingKeySoraCacheVideoDir = "sora_cache_video_dir"
|
||
SettingKeySoraCacheMaxBytes = "sora_cache_max_bytes"
|
||
SettingKeySoraCacheAllowedHosts = "sora_cache_allowed_hosts"
|
||
SettingKeySoraCacheUserDirEnabled = "sora_cache_user_dir_enabled"
|
||
SettingKeySoraWatermarkFreeEnabled = "sora_watermark_free_enabled"
|
||
SettingKeySoraWatermarkFreeParseMethod = "sora_watermark_free_parse_method"
|
||
SettingKeySoraWatermarkFreeCustomParseURL = "sora_watermark_free_custom_parse_url"
|
||
SettingKeySoraWatermarkFreeCustomParseToken = "sora_watermark_free_custom_parse_token"
|
||
SettingKeySoraWatermarkFreeFallbackOnFailure = "sora_watermark_free_fallback_on_failure"
|
||
SettingKeySoraTokenRefreshEnabled = "sora_token_refresh_enabled"
|
||
|
||
// =========================
|
||
// Ops Monitoring (vNext)
|
||
// =========================
|
||
|
||
// SettingKeyOpsMonitoringEnabled is a DB-backed soft switch to enable/disable ops module at runtime.
|
||
SettingKeyOpsMonitoringEnabled = "ops_monitoring_enabled"
|
||
|
||
// SettingKeyOpsRealtimeMonitoringEnabled controls realtime features (e.g. WS/QPS push).
|
||
SettingKeyOpsRealtimeMonitoringEnabled = "ops_realtime_monitoring_enabled"
|
||
|
||
// SettingKeyOpsQueryModeDefault controls the default query mode for ops dashboard (auto/raw/preagg).
|
||
SettingKeyOpsQueryModeDefault = "ops_query_mode_default"
|
||
|
||
// SettingKeyOpsEmailNotificationConfig stores JSON config for ops email notifications.
|
||
SettingKeyOpsEmailNotificationConfig = "ops_email_notification_config"
|
||
|
||
// SettingKeyOpsAlertRuntimeSettings stores JSON config for ops alert evaluator runtime settings.
|
||
SettingKeyOpsAlertRuntimeSettings = "ops_alert_runtime_settings"
|
||
|
||
// SettingKeyOpsMetricsIntervalSeconds controls the ops metrics collector interval (>=60).
|
||
SettingKeyOpsMetricsIntervalSeconds = "ops_metrics_interval_seconds"
|
||
|
||
// SettingKeyOpsAdvancedSettings stores JSON config for ops advanced settings (data retention, aggregation).
|
||
SettingKeyOpsAdvancedSettings = "ops_advanced_settings"
|
||
|
||
// =========================
|
||
// Stream Timeout Handling
|
||
// =========================
|
||
|
||
// SettingKeyStreamTimeoutSettings stores JSON config for stream timeout handling.
|
||
SettingKeyStreamTimeoutSettings = "stream_timeout_settings"
|
||
)
|
||
|
||
// AdminAPIKeyPrefix is the prefix for admin API keys (distinct from user "sk-" keys).
|
||
const AdminAPIKeyPrefix = "admin-"
|