Files
gemini-banlancer/internal/models/runtime.go
2025-11-23 22:42:58 +08:00

123 lines
14 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// internal\models\runtime.go
package models
// ========= 运行时配置 (非数据库模型并提供默认值) =========
type SystemSettings struct {
DefaultUpstreamURL string `json:"default_upstream_url" default:"https://generativelanguage.googleapis.com/v1beta" name:"全局默认上游URL" category:"请求设置" desc:"当密钥组未指定任何专属上游时将使用此URL作为最终的兜底。"`
RequestLogRetentionDays int `json:"request_log_retention_days" default:"7" name:"日志保留天数" category:"基础设置" desc:"请求日志在数据库中的保留天数。"`
RequestTimeoutSeconds int `json:"request_timeout_seconds" default:"600" name:"请求超时(秒)" category:"请求设置" desc:"转发请求的完整生命周期超时(秒)。"`
ConnectTimeoutSeconds int `json:"connect_timeout_seconds" default:"15" name:"连接超时(秒)" category:"请求设置" desc:"与上游服务建立新连接的超时时间(秒)。"`
MaxRetries int `json:"max_retries" default:"3" name:"最大重试次数" category:"请求设置" desc:"单个请求使用不同Key的最大重试次数。"`
BlacklistThreshold int `json:"blacklist_threshold" default:"3" name:"拉黑阈值" category:"密钥设置" desc:"一个Key连续失败多少次后进入冷却状态。"`
KeyCooldownMinutes int `json:"key_cooldown_minutes" default:"10" name:"密钥冷却时长(分钟)" category:"密钥设置" desc:"一个Key进入冷却状态后需要等待的时间单位为分钟。"`
LogFlushIntervalSeconds int `json:"log_flush_interval_seconds" default:"10" name:"日志刷新间隔(秒)" category:"日志设置" desc:"异步日志写入数据库的间隔时间(秒)。"`
MaxRequestBodySizeMB int `json:"max_request_body_size_mb" default:"10" name:"最大请求体大小 (MB)" category:"请求设置" desc:"允许代理接收的最大请求体大小单位为MB。超过此大小的请求将被拒绝。"`
PollingStrategy PollingStrategy `json:"polling_strategy" default:"random" name:"全局轮询策略" category:"调度设置" desc:"智能聚合模式下,从所有可用密钥中选择一个的默认策略。可选值: sequential(顺序), random(随机), weighted(加权)。"`
// HealthCheckIntervalSeconds is DEPRECATED. Use specific intervals below.
UpstreamCheckIntervalSeconds int `json:"upstream_check_interval_seconds" default:"300" name:"上游检查周期(秒)" category:"健康检查" desc:"对所有上游服务进行健康检查的周期。"`
ProxyCheckIntervalSeconds int `json:"proxy_check_interval_seconds" default:"600" name:"代理检查周期(秒)" category:"健康检查" desc:"对所有代理服务进行健康检查的周期。"`
EnableBaseKeyCheck bool `json:"enable_base_key_check" default:"true" name:"启用全局基础Key检查" category:"健康检查" desc:"是否启用全局的、长周期的Key身份状态检查。"`
KeyCheckTimeoutSeconds int `json:"key_check_timeout_seconds" default:"20" name:"Key检查超时(秒)" category:"健康检查" desc:"对单个API Key进行有效性验证时的网络超时时间全局与分组检查共用。"`
BaseKeyCheckIntervalMinutes int `json:"base_key_check_interval_minutes" default:"1440" name:"全局Key检查周期(分钟)" category:"健康检查" desc:"对所有ACTIVE状态的Key进行身份检查的周期建议设置较长时间例如1天(1440分钟)。"`
BaseKeyCheckConcurrency int `json:"base_key_check_concurrency" default:"5" name:"全局Key检查并发数" category:"健康检查" desc:"执行全局Key身份检查时的并发请求数量。"`
BaseKeyCheckEndpoint string `json:"base_key_check_endpoint" default:"https://generativelanguage.googleapis.com/v1beta/models" name:"全局Key检查端点" category:"健康检查" desc:"用于全局Key身份检查的目标URL。"`
BaseKeyCheckModel string `json:"base_key_check_model" default:"gemini-2.0-flash-lite" name:"默认Key检查模型" category:"健康检查" desc:"用于分组健康检查和手动密钥测试时的默认回退模型。"`
EnableUpstreamCheck bool `json:"enable_upstream_check" default:"true" name:"启用上游检查" category:"健康检查" desc:"是否启用对上游服务(Upstream)的健康检查。"`
UpstreamCheckTimeoutSeconds int `json:"upstream_check_timeout_seconds" default:"20" name:"上游检查超时(秒)" category:"健康检查" desc:"对单个上游服务进行健康检查时的网络超时时间。"`
EnableProxyCheck bool `json:"enable_proxy_check" default:"true" name:"启用代理检查" category:"健康检查" desc:"是否启用对代理(Proxy)的健康检查。"`
ProxyCheckTimeoutSeconds int `json:"proxy_check_timeout_seconds" default:"20" name:"代理检查超时(秒)" category:"健康检查" desc:"通过代理进行连通性测试时的网络超时时间。"`
ProxyCheckConcurrency int `json:"proxy_check_concurrency" default:"5" name:"代理测试并发数" category:"健康检查" desc:"后台手动批量测试代理时的默认并发请求数量。"`
UseProxyHash bool `json:"use_proxy_hash" default:"false" name:"是否开启固定代理策略" category:"API配置" desc:"开启后对于每一个API_KEY将根据算法从代理列表中选取同一个代理IP防止一个API_KEY同时被多个IP访问也同时防止了一个IP访问了过多的API_KEY。"`
AnalyticsFlushIntervalSeconds int `json:"analytics_flush_interval_seconds" default:"60" name:"分析数据落盘间隔(秒)" category:"高级设置" desc:"内存中的统计数据多久写入数据库一次。"`
// 安全设置
EnableIPBanning bool `json:"enable_ip_banning" default:"false" name:"启用IP封禁功能" category:"安全设置" desc:"当一个IP连续多次登录失败后是否自动将其封禁一段时间。"`
MaxLoginAttempts int `json:"max_login_attempts" default:"5" name:"最大登录失败次数" category:"安全设置" desc:"在一个IP被封禁前允许的连续登录失败次数。"`
IPBanDurationMinutes int `json:"ip_ban_duration_minutes" default:"15" name:"IP封禁时长(分钟)" category:"安全设置" desc:"IP被封禁的时长单位为分钟。"`
// BasePool 相关配置
// BasePoolTTLMinutes int `json:"base_pool_ttl_minutes" default:"30" name:"基础资源池最大生存时间(分钟)" category:"基础资源池" desc:"一个动态构建的基础资源池BasePool在Redis中的最大生存时间。到期后即使仍在活跃使用也会被强制重建。"`
// BasePoolTTIMinutes int `json:"base_pool_tti_minutes" default:"10" name:"基础资源池空闲超时(分钟)" category:"基础资源池" desc:"一个基础资源池BasePool在连续无请求后自动销毁的空闲等待时间。"`
//智能网关
LogTruncationLimit int `json:"log_truncation_limit" default:"8000" name:"日志截断长度" category:"日志设置" desc:"在日志中记录上游响应或错误时保留的最大字符数。0表示不截断。"`
EnableSmartGateway bool `json:"enable_smart_gateway" default:"false" name:"启用智能网关" category:"代理设置" desc:"开启后,系统将对流式请求进行智能中断续传、错误标准化等优化。关闭后,系统将作为一个纯净、无干扰的透明代理。"`
EnableStreamingRetry bool `json:"enable_streaming_retry" default:"true" name:"启用流式重试" category:"代理设置" desc:"当智能网关开启时,是否对流式请求进行智能中断续传。"`
MaxStreamingRetries int `json:"max_streaming_retries" default:"2" name:"最大流式重试次数" category:"代理设置" desc:"对单个流式会话,允许的最大连续重试次数。"`
StreamingRetryDelayMs int `json:"streaming_retry_delay_ms" default:"750" name:"流式重试延迟(毫秒)" category:"代理设置" desc:"流式会话重试之间的等待时间,单位为毫秒。"`
// 智能网关底层HTTP Transport配置
TransportMaxIdleConns int `json:"transport_max_idle_conns" default:"200" name:"最大空闲连接数(总)" category:"高级设置" desc:"HTTP客户端Transport的最大总空闲连接数。"`
TransportMaxIdleConnsPerHost int `json:"transport_max_idle_conns_per_host" default:"100" name:"最大空-闲连接数(单主机)" category:"高级设置" desc:"HTTP客户端Transport对单个主机的最大空闲连接数。"`
TransportIdleConnTimeoutSecs int `json:"transport_idle_conn_timeout_secs" default:"90" name:"空闲连接超时(秒)" category:"高级设置" desc:"HTTP客户端Transport中空闲连接被关闭前的等待时间。"`
TransportTLSHandshakeTimeout int `json:"transport_tls_handshake_timeout" default:"10" name:"TLS握手超时(秒)" category:"高级设置" desc:"TLS握手的超时时间。"`
// 智能续传的自定义Prompt
StreamingRetryPrompt string `json:"streaming_retry_prompt" default:"Continue exactly where you left off, providing the final answer without repeating the previous thinking steps." name:"智能续传提示词" category:"代理设置" desc:"在进行智能中断续传时,向模型发送的指令。"`
// 日志服务相关配置
LogLevel string `json:"log_level" default:"INFO" name:"日志级别" category:"日志配置"`
AutoDeleteErrorLogsEnabled bool `json:"auto_delete_error_logs_enabled" default:"false" name:"自动删除错误日志" category:"日志配置"`
AutoDeleteRequestLogsEnabled bool `json:"auto_delete_request_logs_enabled" default:"false" name:"自动删除请求日志" category:"日志配置"`
LogBufferCapacity int `json:"log_buffer_capacity" default:"1000" name:"日志缓冲区容量" category:"日志设置" desc:"内存中日志缓冲区的最大容量,超过则可能丢弃日志。"`
LogFlushBatchSize int `json:"log_flush_batch_size" default:"100" name:"日志刷新批次大小" category:"日志设置" desc:"每次向数据库批量写入日志的最大数量。"`
// --- API配置 ---
CustomHeaders map[string]string `json:"custom_headers" name:"自定义Headers" category:"API配置" ` // 默认为nil
// --- TTS 配置 (模块化预留) ---
TTSModel string `json:"tts_model" name:"TTS模型" category:"TTS配置"`
TTSVoiceName string `json:"tts_voice_name" name:"TTS语音名称" category:"TTS配置"`
TTSSpeed string `json:"tts_speed" name:"TTS语速" category:"TTS配置"`
// --- 图像生成配置 (模块化预留) ---
PaidKey string `json:"paid_key" name:"付费API密钥" category:"图像生成"`
CreateImageModel string `json:"create_image_model" name:"图像生成模型" category:"图像生成"`
UploadProvider string `json:"upload_provider" name:"上传提供商" category:"图像生成"`
SmmsSecretToken string `json:"smms_secret_token" name:"SM.MS密钥" category:"图像生成"`
PicgoAPIKey string `json:"picgo_api_key" name:"PicGo API密钥" category:"图像生成"`
CloudflareImgbedURL string `json:"cloudflare_imgbed_url" name:"Cloudflare图床URL" category:"图像生成"`
CloudflareImgbedAuthCode string `json:"cloudflare_imgbed_auth_code" name:"Cloudflare认证码" category:"图像生成"`
CloudflareImgbedUploadFolder string `json:"cloudflare_imgbed_upload_folder" name:"Cloudflare上传文件夹" category:"图像生成"`
// --- 流式输出配置 (模块化预留) ---
EnableStreamOptimizer bool `json:"enable_stream_optimizer" default:"false" name:"启用流式输出优化" category:"流式输出"`
StreamMinDelay int `json:"stream_min_delay" default:"16" name:"最小延迟(秒)" category:"流式输出"`
StreamMaxDelay int `json:"stream_max_delay" default:"24" name:"最大延迟(秒)" category:"流式输出"`
StreamShortTextThresh int `json:"stream_short_text_thresh" default:"10" name:"短文本阈值" category:"流式输出"`
StreamLongTextThresh int `json:"stream_long_text_thresh" default:"50" name:"长文本阈值" category:"流式输出"`
StreamChunkSize int `json:"stream_chunk_size" default:"5" name:"分块大小" category:"流式输出"`
EnableFakeStream bool `json:"enable_fake_stream" default:"false" name:"启用假流式输出" category:"流式输出"`
FakeStreamInterval int `json:"fake_stream_interval" default:"5" name:"假流式空数据发送间隔(秒)" category:"流式输出"`
// --- 定时任务配置 ---
Timezone string `json:"timezone" default:"Asia/Shanghai" name:"时区" category:"定时任务"`
// --- [短期冻结] 为了UI兼容性而保留的“幻影”字段 ---
AllowedTokens []string `json:"-"` // 不参与JSON序列化
Proxies []string `json:"-"` // 不参与JSON序列化
ModelSettings ModelSettings `json:"model_settings" name:"模型配置"`
}
// ModelSettings
type ModelSettings struct {
ImageModels []string `json:"image_models" name:"图像模型列表"`
SearchModels []string `json:"search_models" name:"搜索模型列表"`
FilteredModels []string `json:"filtered_models" name:"过滤模型列表"`
EnableCodeExecutor bool `json:"enable_code_executor" default:"false" name:"启用代码执行工具"`
EnableURLContext bool `json:"enable_url_context" default:"false" name:"启用网址上下文"`
URLContextModels []string `json:"url_context_models" name:"网址上下文模型列表"`
ShowSearchLink bool `json:"show_search_link" default:"false" name:"显示搜索链接"`
ShowThinking bool `json:"show_thinking" default:"false" name:"显示思考过程"`
ThinkingModels []string `json:"thinking_models" name:"思考模型列表"`
ThinkingBudgetMap map[string]int `json:"thinking_budget_map" name:"思考模型预算映射"`
SafetySettings []SafetySetting `json:"safety_settings" name:"安全设置"`
}