feat(sora): 新增 Sora 平台支持并修复高危安全和性能问题
新增功能: - 新增 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>
This commit is contained in:
113
backend/internal/service/sora_repository.go
Normal file
113
backend/internal/service/sora_repository.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
||||
)
|
||||
|
||||
// SoraAccount 表示 Sora 账号扩展信息。
|
||||
type SoraAccount struct {
|
||||
AccountID int64
|
||||
AccessToken string
|
||||
SessionToken string
|
||||
RefreshToken string
|
||||
ClientID string
|
||||
Email string
|
||||
Username string
|
||||
Remark string
|
||||
UseCount int
|
||||
PlanType string
|
||||
PlanTitle string
|
||||
SubscriptionEnd *time.Time
|
||||
SoraSupported bool
|
||||
SoraInviteCode string
|
||||
SoraRedeemedCount int
|
||||
SoraRemainingCount int
|
||||
SoraTotalCount int
|
||||
SoraCooldownUntil *time.Time
|
||||
CooledUntil *time.Time
|
||||
ImageEnabled bool
|
||||
VideoEnabled bool
|
||||
ImageConcurrency int
|
||||
VideoConcurrency int
|
||||
IsExpired bool
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// SoraUsageStat 表示 Sora 调用统计。
|
||||
type SoraUsageStat struct {
|
||||
AccountID int64
|
||||
ImageCount int
|
||||
VideoCount int
|
||||
ErrorCount int
|
||||
LastErrorAt *time.Time
|
||||
TodayImageCount int
|
||||
TodayVideoCount int
|
||||
TodayErrorCount int
|
||||
TodayDate *time.Time
|
||||
ConsecutiveErrorCount int
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// SoraTask 表示 Sora 任务记录。
|
||||
type SoraTask struct {
|
||||
TaskID string
|
||||
AccountID int64
|
||||
Model string
|
||||
Prompt string
|
||||
Status string
|
||||
Progress float64
|
||||
ResultURLs string
|
||||
ErrorMessage string
|
||||
RetryCount int
|
||||
CreatedAt time.Time
|
||||
CompletedAt *time.Time
|
||||
}
|
||||
|
||||
// SoraCacheFile 表示 Sora 缓存文件记录。
|
||||
type SoraCacheFile struct {
|
||||
ID int64
|
||||
TaskID string
|
||||
AccountID int64
|
||||
UserID int64
|
||||
MediaType string
|
||||
OriginalURL string
|
||||
CachePath string
|
||||
CacheURL string
|
||||
SizeBytes int64
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// SoraAccountRepository 定义 Sora 账号仓储接口。
|
||||
type SoraAccountRepository interface {
|
||||
GetByAccountID(ctx context.Context, accountID int64) (*SoraAccount, error)
|
||||
GetByAccountIDs(ctx context.Context, accountIDs []int64) (map[int64]*SoraAccount, error)
|
||||
Upsert(ctx context.Context, accountID int64, updates map[string]any) error
|
||||
}
|
||||
|
||||
// SoraUsageStatRepository 定义 Sora 调用统计仓储接口。
|
||||
type SoraUsageStatRepository interface {
|
||||
RecordSuccess(ctx context.Context, accountID int64, isVideo bool) error
|
||||
RecordError(ctx context.Context, accountID int64) (int, error)
|
||||
ResetConsecutiveErrors(ctx context.Context, accountID int64) error
|
||||
GetByAccountID(ctx context.Context, accountID int64) (*SoraUsageStat, error)
|
||||
GetByAccountIDs(ctx context.Context, accountIDs []int64) (map[int64]*SoraUsageStat, error)
|
||||
List(ctx context.Context, params pagination.PaginationParams) ([]*SoraUsageStat, *pagination.PaginationResult, error)
|
||||
}
|
||||
|
||||
// SoraTaskRepository 定义 Sora 任务仓储接口。
|
||||
type SoraTaskRepository interface {
|
||||
Create(ctx context.Context, task *SoraTask) error
|
||||
UpdateStatus(ctx context.Context, taskID string, status string, progress float64, resultURLs string, errorMessage string, completedAt *time.Time) error
|
||||
}
|
||||
|
||||
// SoraCacheFileRepository 定义 Sora 缓存文件仓储接口。
|
||||
type SoraCacheFileRepository interface {
|
||||
Create(ctx context.Context, file *SoraCacheFile) error
|
||||
ListOldest(ctx context.Context, limit int) ([]*SoraCacheFile, error)
|
||||
DeleteByIDs(ctx context.Context, ids []int64) error
|
||||
}
|
||||
Reference in New Issue
Block a user