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:
115
backend/ent/schema/sora_account.go
Normal file
115
backend/ent/schema/sora_account.go
Normal file
@@ -0,0 +1,115 @@
|
||||
// Package schema 定义 Ent ORM 的数据库 schema。
|
||||
// 每个文件对应一个数据库实体(表),定义其字段、边(关联)和索引。
|
||||
package schema
|
||||
|
||||
import (
|
||||
"github.com/Wei-Shaw/sub2api/ent/schema/mixins"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
)
|
||||
|
||||
// SoraAccount 定义 Sora 账号扩展表。
|
||||
type SoraAccount struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Annotations 返回 schema 的注解配置。
|
||||
func (SoraAccount) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Annotation{Table: "sora_accounts"},
|
||||
}
|
||||
}
|
||||
|
||||
// Mixin 返回该 schema 使用的混入组件。
|
||||
func (SoraAccount) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
mixins.TimeMixin{},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields 定义 SoraAccount 的字段。
|
||||
func (SoraAccount) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int64("account_id").
|
||||
Comment("关联 accounts 表的 ID"),
|
||||
field.String("access_token").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("session_token").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("refresh_token").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("client_id").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("email").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("username").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("remark").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "text"}),
|
||||
field.Int("use_count").
|
||||
Default(0),
|
||||
field.String("plan_type").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("plan_title").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Time("subscription_end").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.Bool("sora_supported").
|
||||
Default(false),
|
||||
field.String("sora_invite_code").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Int("sora_redeemed_count").
|
||||
Default(0),
|
||||
field.Int("sora_remaining_count").
|
||||
Default(0),
|
||||
field.Int("sora_total_count").
|
||||
Default(0),
|
||||
field.Time("sora_cooldown_until").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.Time("cooled_until").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.Bool("image_enabled").
|
||||
Default(true),
|
||||
field.Bool("video_enabled").
|
||||
Default(true),
|
||||
field.Int("image_concurrency").
|
||||
Default(-1),
|
||||
field.Int("video_concurrency").
|
||||
Default(-1),
|
||||
field.Bool("is_expired").
|
||||
Default(false),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes 定义索引。
|
||||
func (SoraAccount) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("account_id").Unique(),
|
||||
index.Fields("plan_type"),
|
||||
index.Fields("sora_supported"),
|
||||
index.Fields("image_enabled"),
|
||||
index.Fields("video_enabled"),
|
||||
}
|
||||
}
|
||||
60
backend/ent/schema/sora_cache_file.go
Normal file
60
backend/ent/schema/sora_cache_file.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Package schema 定义 Ent ORM 的数据库 schema。
|
||||
// 每个文件对应一个数据库实体(表),定义其字段、边(关联)和索引。
|
||||
package schema
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
)
|
||||
|
||||
// SoraCacheFile 定义 Sora 缓存文件表。
|
||||
type SoraCacheFile struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Annotations 返回 schema 的注解配置。
|
||||
func (SoraCacheFile) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Annotation{Table: "sora_cache_files"},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields 定义 SoraCacheFile 的字段。
|
||||
func (SoraCacheFile) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("task_id").
|
||||
MaxLen(120).
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Int64("account_id"),
|
||||
field.Int64("user_id"),
|
||||
field.String("media_type").
|
||||
MaxLen(32),
|
||||
field.String("original_url").
|
||||
SchemaType(map[string]string{dialect.Postgres: "text"}),
|
||||
field.String("cache_path").
|
||||
SchemaType(map[string]string{dialect.Postgres: "text"}),
|
||||
field.String("cache_url").
|
||||
SchemaType(map[string]string{dialect.Postgres: "text"}),
|
||||
field.Int64("size_bytes").
|
||||
Default(0),
|
||||
field.Time("created_at").
|
||||
Default(time.Now).
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes 定义索引。
|
||||
func (SoraCacheFile) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("account_id"),
|
||||
index.Fields("user_id"),
|
||||
index.Fields("media_type"),
|
||||
}
|
||||
}
|
||||
70
backend/ent/schema/sora_task.go
Normal file
70
backend/ent/schema/sora_task.go
Normal file
@@ -0,0 +1,70 @@
|
||||
// Package schema 定义 Ent ORM 的数据库 schema。
|
||||
// 每个文件对应一个数据库实体(表),定义其字段、边(关联)和索引。
|
||||
package schema
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
)
|
||||
|
||||
// SoraTask 定义 Sora 任务记录表。
|
||||
type SoraTask struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Annotations 返回 schema 的注解配置。
|
||||
func (SoraTask) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Annotation{Table: "sora_tasks"},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields 定义 SoraTask 的字段。
|
||||
func (SoraTask) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("task_id").
|
||||
MaxLen(120).
|
||||
Unique(),
|
||||
field.Int64("account_id"),
|
||||
field.String("model").
|
||||
MaxLen(120),
|
||||
field.String("prompt").
|
||||
SchemaType(map[string]string{dialect.Postgres: "text"}),
|
||||
field.String("status").
|
||||
MaxLen(32).
|
||||
Default("processing"),
|
||||
field.Float("progress").
|
||||
Default(0),
|
||||
field.String("result_urls").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "text"}),
|
||||
field.String("error_message").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "text"}),
|
||||
field.Int("retry_count").
|
||||
Default(0),
|
||||
field.Time("created_at").
|
||||
Default(time.Now).
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.Time("completed_at").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes 定义索引。
|
||||
func (SoraTask) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("account_id"),
|
||||
index.Fields("status"),
|
||||
}
|
||||
}
|
||||
71
backend/ent/schema/sora_usage_stat.go
Normal file
71
backend/ent/schema/sora_usage_stat.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// Package schema 定义 Ent ORM 的数据库 schema。
|
||||
// 每个文件对应一个数据库实体(表),定义其字段、边(关联)和索引。
|
||||
package schema
|
||||
|
||||
import (
|
||||
"github.com/Wei-Shaw/sub2api/ent/schema/mixins"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
)
|
||||
|
||||
// SoraUsageStat 定义 Sora 调用统计表。
|
||||
type SoraUsageStat struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Annotations 返回 schema 的注解配置。
|
||||
func (SoraUsageStat) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Annotation{Table: "sora_usage_stats"},
|
||||
}
|
||||
}
|
||||
|
||||
// Mixin 返回该 schema 使用的混入组件。
|
||||
func (SoraUsageStat) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
mixins.TimeMixin{},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields 定义 SoraUsageStat 的字段。
|
||||
func (SoraUsageStat) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int64("account_id").
|
||||
Comment("关联 accounts 表的 ID"),
|
||||
field.Int("image_count").
|
||||
Default(0),
|
||||
field.Int("video_count").
|
||||
Default(0),
|
||||
field.Int("error_count").
|
||||
Default(0),
|
||||
field.Time("last_error_at").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.Int("today_image_count").
|
||||
Default(0),
|
||||
field.Int("today_video_count").
|
||||
Default(0),
|
||||
field.Int("today_error_count").
|
||||
Default(0),
|
||||
field.Time("today_date").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "date"}),
|
||||
field.Int("consecutive_error_count").
|
||||
Default(0),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes 定义索引。
|
||||
func (SoraUsageStat) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("account_id").Unique(),
|
||||
index.Fields("today_date"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user