option.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "one-api/common"
  6. "one-api/setting"
  7. "one-api/setting/config"
  8. "one-api/setting/operation_setting"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type Option struct {
  14. Key string `json:"key" gorm:"primaryKey"`
  15. Value string `json:"value"`
  16. }
  17. func AllOption() ([]*Option, error) {
  18. var options []*Option
  19. var err error
  20. err = DB.Find(&options).Error
  21. return options, err
  22. }
  23. func InitOptionMap() {
  24. common.OptionMapRWMutex.Lock()
  25. common.OptionMap = make(map[string]string)
  26. // 添加原有的系统配置
  27. common.OptionMap["FileUploadPermission"] = strconv.Itoa(common.FileUploadPermission)
  28. common.OptionMap["FileDownloadPermission"] = strconv.Itoa(common.FileDownloadPermission)
  29. common.OptionMap["ImageUploadPermission"] = strconv.Itoa(common.ImageUploadPermission)
  30. common.OptionMap["ImageDownloadPermission"] = strconv.Itoa(common.ImageDownloadPermission)
  31. common.OptionMap["PasswordLoginEnabled"] = strconv.FormatBool(common.PasswordLoginEnabled)
  32. common.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(common.PasswordRegisterEnabled)
  33. common.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(common.EmailVerificationEnabled)
  34. common.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(common.GitHubOAuthEnabled)
  35. common.OptionMap["LinuxDOOAuthEnabled"] = strconv.FormatBool(common.LinuxDOOAuthEnabled)
  36. common.OptionMap["TelegramOAuthEnabled"] = strconv.FormatBool(common.TelegramOAuthEnabled)
  37. common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled)
  38. common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled)
  39. common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled)
  40. common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled)
  41. common.OptionMap["AutomaticEnableChannelEnabled"] = strconv.FormatBool(common.AutomaticEnableChannelEnabled)
  42. common.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(common.LogConsumeEnabled)
  43. common.OptionMap["DisplayInCurrencyEnabled"] = strconv.FormatBool(common.DisplayInCurrencyEnabled)
  44. common.OptionMap["DisplayTokenStatEnabled"] = strconv.FormatBool(common.DisplayTokenStatEnabled)
  45. common.OptionMap["DrawingEnabled"] = strconv.FormatBool(common.DrawingEnabled)
  46. common.OptionMap["TaskEnabled"] = strconv.FormatBool(common.TaskEnabled)
  47. common.OptionMap["DataExportEnabled"] = strconv.FormatBool(common.DataExportEnabled)
  48. common.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(common.ChannelDisableThreshold, 'f', -1, 64)
  49. common.OptionMap["EmailDomainRestrictionEnabled"] = strconv.FormatBool(common.EmailDomainRestrictionEnabled)
  50. common.OptionMap["EmailAliasRestrictionEnabled"] = strconv.FormatBool(common.EmailAliasRestrictionEnabled)
  51. common.OptionMap["EmailDomainWhitelist"] = strings.Join(common.EmailDomainWhitelist, ",")
  52. common.OptionMap["SMTPServer"] = ""
  53. common.OptionMap["SMTPFrom"] = ""
  54. common.OptionMap["SMTPPort"] = strconv.Itoa(common.SMTPPort)
  55. common.OptionMap["SMTPAccount"] = ""
  56. common.OptionMap["SMTPToken"] = ""
  57. common.OptionMap["SMTPSSLEnabled"] = strconv.FormatBool(common.SMTPSSLEnabled)
  58. common.OptionMap["Notice"] = ""
  59. common.OptionMap["About"] = ""
  60. common.OptionMap["HomePageContent"] = ""
  61. common.OptionMap["Footer"] = common.Footer
  62. common.OptionMap["SystemName"] = common.SystemName
  63. common.OptionMap["Logo"] = common.Logo
  64. common.OptionMap["ServerAddress"] = ""
  65. common.OptionMap["WorkerUrl"] = setting.WorkerUrl
  66. common.OptionMap["WorkerValidKey"] = setting.WorkerValidKey
  67. common.OptionMap["PayAddress"] = ""
  68. common.OptionMap["CustomCallbackAddress"] = ""
  69. common.OptionMap["EpayId"] = ""
  70. common.OptionMap["EpayKey"] = ""
  71. common.OptionMap["Price"] = strconv.FormatFloat(setting.Price, 'f', -1, 64)
  72. common.OptionMap["MinTopUp"] = strconv.Itoa(setting.MinTopUp)
  73. common.OptionMap["TopupGroupRatio"] = common.TopupGroupRatio2JSONString()
  74. common.OptionMap["Chats"] = setting.Chats2JsonString()
  75. common.OptionMap["GitHubClientId"] = ""
  76. common.OptionMap["GitHubClientSecret"] = ""
  77. common.OptionMap["TelegramBotToken"] = ""
  78. common.OptionMap["TelegramBotName"] = ""
  79. common.OptionMap["WeChatServerAddress"] = ""
  80. common.OptionMap["WeChatServerToken"] = ""
  81. common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
  82. common.OptionMap["TurnstileSiteKey"] = ""
  83. common.OptionMap["TurnstileSecretKey"] = ""
  84. common.OptionMap["QuotaForNewUser"] = strconv.Itoa(common.QuotaForNewUser)
  85. common.OptionMap["QuotaForInviter"] = strconv.Itoa(common.QuotaForInviter)
  86. common.OptionMap["QuotaForInvitee"] = strconv.Itoa(common.QuotaForInvitee)
  87. common.OptionMap["QuotaRemindThreshold"] = strconv.Itoa(common.QuotaRemindThreshold)
  88. common.OptionMap["PreConsumedQuota"] = strconv.Itoa(common.PreConsumedQuota)
  89. common.OptionMap["ModelRequestRateLimitCount"] = strconv.Itoa(setting.ModelRequestRateLimitCount)
  90. common.OptionMap["ModelRequestRateLimitDurationMinutes"] = strconv.Itoa(setting.ModelRequestRateLimitDurationMinutes)
  91. common.OptionMap["ModelRequestRateLimitSuccessCount"] = strconv.Itoa(setting.ModelRequestRateLimitSuccessCount)
  92. common.OptionMap["ModelRatio"] = operation_setting.ModelRatio2JSONString()
  93. common.OptionMap["ModelPrice"] = operation_setting.ModelPrice2JSONString()
  94. common.OptionMap["CacheRatio"] = operation_setting.CacheRatio2JSONString()
  95. common.OptionMap["GroupRatio"] = setting.GroupRatio2JSONString()
  96. common.OptionMap[setting.ModelRequestRateLimitGroupKey] = "{}" // 添加用户组速率限制默认值
  97. common.OptionMap["UserUsableGroups"] = setting.UserUsableGroups2JSONString()
  98. common.OptionMap["CompletionRatio"] = operation_setting.CompletionRatio2JSONString()
  99. common.OptionMap["TopUpLink"] = common.TopUpLink
  100. //common.OptionMap["ChatLink"] = common.ChatLink
  101. //common.OptionMap["ChatLink2"] = common.ChatLink2
  102. common.OptionMap["QuotaPerUnit"] = strconv.FormatFloat(common.QuotaPerUnit, 'f', -1, 64)
  103. common.OptionMap["RetryTimes"] = strconv.Itoa(common.RetryTimes)
  104. common.OptionMap["DataExportInterval"] = strconv.Itoa(common.DataExportInterval)
  105. common.OptionMap["DataExportDefaultTime"] = common.DataExportDefaultTime
  106. common.OptionMap["DefaultCollapseSidebar"] = strconv.FormatBool(common.DefaultCollapseSidebar)
  107. common.OptionMap["MjNotifyEnabled"] = strconv.FormatBool(setting.MjNotifyEnabled)
  108. common.OptionMap["MjAccountFilterEnabled"] = strconv.FormatBool(setting.MjAccountFilterEnabled)
  109. common.OptionMap["MjModeClearEnabled"] = strconv.FormatBool(setting.MjModeClearEnabled)
  110. common.OptionMap["MjForwardUrlEnabled"] = strconv.FormatBool(setting.MjForwardUrlEnabled)
  111. common.OptionMap["MjActionCheckSuccessEnabled"] = strconv.FormatBool(setting.MjActionCheckSuccessEnabled)
  112. common.OptionMap["CheckSensitiveEnabled"] = strconv.FormatBool(setting.CheckSensitiveEnabled)
  113. common.OptionMap["DemoSiteEnabled"] = strconv.FormatBool(operation_setting.DemoSiteEnabled)
  114. common.OptionMap["SelfUseModeEnabled"] = strconv.FormatBool(operation_setting.SelfUseModeEnabled)
  115. common.OptionMap["ModelRequestRateLimitEnabled"] = strconv.FormatBool(setting.ModelRequestRateLimitEnabled)
  116. common.OptionMap["CheckSensitiveOnPromptEnabled"] = strconv.FormatBool(setting.CheckSensitiveOnPromptEnabled)
  117. common.OptionMap["StopOnSensitiveEnabled"] = strconv.FormatBool(setting.StopOnSensitiveEnabled)
  118. common.OptionMap["SensitiveWords"] = setting.SensitiveWordsToString()
  119. common.OptionMap["StreamCacheQueueLength"] = strconv.Itoa(setting.StreamCacheQueueLength)
  120. common.OptionMap["AutomaticDisableKeywords"] = operation_setting.AutomaticDisableKeywordsToString()
  121. // 自动添加所有注册的模型配置
  122. modelConfigs := config.GlobalConfig.ExportAllConfigs()
  123. for k, v := range modelConfigs {
  124. common.OptionMap[k] = v
  125. }
  126. common.OptionMapRWMutex.Unlock()
  127. loadOptionsFromDatabase()
  128. }
  129. func loadOptionsFromDatabase() {
  130. options, _ := AllOption()
  131. for _, option := range options {
  132. err := updateOptionMap(option.Key, option.Value)
  133. if err != nil {
  134. common.SysError("failed to update option map: " + err.Error())
  135. }
  136. }
  137. }
  138. func SyncOptions(frequency int) {
  139. for {
  140. time.Sleep(time.Duration(frequency) * time.Second)
  141. common.SysLog("syncing options from database")
  142. loadOptionsFromDatabase()
  143. }
  144. }
  145. func UpdateOption(key string, value string) error {
  146. originalValue := value // 保存原始值以备后用
  147. // Validate and format specific keys before saving
  148. if key == setting.ModelRequestRateLimitGroupKey {
  149. var cfg map[string][2]int
  150. // Validate the JSON structure first using the original value
  151. err := json.Unmarshal([]byte(originalValue), &cfg)
  152. if err != nil {
  153. // 提供更具体的错误信息
  154. return fmt.Errorf("无效的 JSON 格式 for %s: %w", key, err)
  155. }
  156. // TODO: 可以添加更细致的结构验证,例如检查数组长度是否为2,值是否为非负数等。
  157. // if !isValidModelRequestRateLimitGroupConfig(cfg) {
  158. // return fmt.Errorf("无效的配置值 for %s", key)
  159. // }
  160. // If valid, format the JSON before saving
  161. formattedValueBytes, marshalErr := json.MarshalIndent(cfg, "", " ")
  162. if marshalErr != nil {
  163. // This should ideally not happen if validation passed, but handle defensively
  164. return fmt.Errorf("failed to marshal validated %s config: %w", key, marshalErr)
  165. }
  166. value = string(formattedValueBytes) // Use formatted JSON for saving and memory update
  167. }
  168. // Save to database
  169. option := Option{
  170. Key: key,
  171. }
  172. // https://gorm.io/docs/update.html#Save-All-Fields
  173. DB.FirstOrCreate(&option, Option{Key: key})
  174. option.Value = value
  175. // Save is a combination function.
  176. // If save value does not contain primary key, it will execute Create,
  177. // otherwise it will execute Update (with all fields).
  178. if err := DB.Save(&option).Error; err != nil {
  179. return fmt.Errorf("保存选项 %s 到数据库失败: %w", key, err) // 添加错误上下文
  180. }
  181. // Update OptionMap in memory using the potentially formatted value
  182. // updateOptionMap 会处理内存中 setting.ModelRequestRateLimitGroupConfig 的更新
  183. return updateOptionMap(key, value)
  184. }
  185. func updateOptionMap(key string, value string) (err error) {
  186. common.OptionMapRWMutex.Lock()
  187. defer common.OptionMapRWMutex.Unlock()
  188. common.OptionMap[key] = value
  189. // 检查是否是模型配置 - 使用更规范的方式处理
  190. if handleConfigUpdate(key, value) {
  191. return nil // 已由配置系统处理
  192. }
  193. // 处理传统配置项...
  194. if strings.HasSuffix(key, "Permission") {
  195. intValue, _ := strconv.Atoi(value)
  196. switch key {
  197. case "FileUploadPermission":
  198. common.FileUploadPermission = intValue
  199. case "FileDownloadPermission":
  200. common.FileDownloadPermission = intValue
  201. case "ImageUploadPermission":
  202. common.ImageUploadPermission = intValue
  203. case "ImageDownloadPermission":
  204. common.ImageDownloadPermission = intValue
  205. }
  206. }
  207. if strings.HasSuffix(key, "Enabled") || key == "DefaultCollapseSidebar" {
  208. boolValue := value == "true"
  209. switch key {
  210. case "PasswordRegisterEnabled":
  211. common.PasswordRegisterEnabled = boolValue
  212. case "PasswordLoginEnabled":
  213. common.PasswordLoginEnabled = boolValue
  214. case "EmailVerificationEnabled":
  215. common.EmailVerificationEnabled = boolValue
  216. case "GitHubOAuthEnabled":
  217. common.GitHubOAuthEnabled = boolValue
  218. case "LinuxDOOAuthEnabled":
  219. common.LinuxDOOAuthEnabled = boolValue
  220. case "WeChatAuthEnabled":
  221. common.WeChatAuthEnabled = boolValue
  222. case "TelegramOAuthEnabled":
  223. common.TelegramOAuthEnabled = boolValue
  224. case "TurnstileCheckEnabled":
  225. common.TurnstileCheckEnabled = boolValue
  226. case "RegisterEnabled":
  227. common.RegisterEnabled = boolValue
  228. case "EmailDomainRestrictionEnabled":
  229. common.EmailDomainRestrictionEnabled = boolValue
  230. case "EmailAliasRestrictionEnabled":
  231. common.EmailAliasRestrictionEnabled = boolValue
  232. case "AutomaticDisableChannelEnabled":
  233. common.AutomaticDisableChannelEnabled = boolValue
  234. case "AutomaticEnableChannelEnabled":
  235. common.AutomaticEnableChannelEnabled = boolValue
  236. case "LogConsumeEnabled":
  237. common.LogConsumeEnabled = boolValue
  238. case "DisplayInCurrencyEnabled":
  239. common.DisplayInCurrencyEnabled = boolValue
  240. case "DisplayTokenStatEnabled":
  241. common.DisplayTokenStatEnabled = boolValue
  242. case "DrawingEnabled":
  243. common.DrawingEnabled = boolValue
  244. case "TaskEnabled":
  245. common.TaskEnabled = boolValue
  246. case "DataExportEnabled":
  247. common.DataExportEnabled = boolValue
  248. case "DefaultCollapseSidebar":
  249. common.DefaultCollapseSidebar = boolValue
  250. case "MjNotifyEnabled":
  251. setting.MjNotifyEnabled = boolValue
  252. case "MjAccountFilterEnabled":
  253. setting.MjAccountFilterEnabled = boolValue
  254. case "MjModeClearEnabled":
  255. setting.MjModeClearEnabled = boolValue
  256. case "MjForwardUrlEnabled":
  257. setting.MjForwardUrlEnabled = boolValue
  258. case "MjActionCheckSuccessEnabled":
  259. setting.MjActionCheckSuccessEnabled = boolValue
  260. case "CheckSensitiveEnabled":
  261. setting.CheckSensitiveEnabled = boolValue
  262. case "DemoSiteEnabled":
  263. operation_setting.DemoSiteEnabled = boolValue
  264. case "SelfUseModeEnabled":
  265. operation_setting.SelfUseModeEnabled = boolValue
  266. case "CheckSensitiveOnPromptEnabled":
  267. setting.CheckSensitiveOnPromptEnabled = boolValue
  268. case "ModelRequestRateLimitEnabled":
  269. setting.ModelRequestRateLimitEnabled = boolValue
  270. case "StopOnSensitiveEnabled":
  271. setting.StopOnSensitiveEnabled = boolValue
  272. case "SMTPSSLEnabled":
  273. common.SMTPSSLEnabled = boolValue
  274. }
  275. }
  276. switch key {
  277. case "EmailDomainWhitelist":
  278. common.EmailDomainWhitelist = strings.Split(value, ",")
  279. case "SMTPServer":
  280. common.SMTPServer = value
  281. case "SMTPPort":
  282. intValue, _ := strconv.Atoi(value)
  283. common.SMTPPort = intValue
  284. case "SMTPAccount":
  285. common.SMTPAccount = value
  286. case "SMTPFrom":
  287. common.SMTPFrom = value
  288. case "SMTPToken":
  289. common.SMTPToken = value
  290. case "ServerAddress":
  291. setting.ServerAddress = value
  292. case "WorkerUrl":
  293. setting.WorkerUrl = value
  294. case "WorkerValidKey":
  295. setting.WorkerValidKey = value
  296. case "PayAddress":
  297. setting.PayAddress = value
  298. case "Chats":
  299. err = setting.UpdateChatsByJsonString(value)
  300. case "CustomCallbackAddress":
  301. setting.CustomCallbackAddress = value
  302. case "EpayId":
  303. setting.EpayId = value
  304. case "EpayKey":
  305. setting.EpayKey = value
  306. case "Price":
  307. setting.Price, _ = strconv.ParseFloat(value, 64)
  308. case "MinTopUp":
  309. setting.MinTopUp, _ = strconv.Atoi(value)
  310. case "TopupGroupRatio":
  311. err = common.UpdateTopupGroupRatioByJSONString(value)
  312. case "GitHubClientId":
  313. common.GitHubClientId = value
  314. case "GitHubClientSecret":
  315. common.GitHubClientSecret = value
  316. case "LinuxDOClientId":
  317. common.LinuxDOClientId = value
  318. case "LinuxDOClientSecret":
  319. common.LinuxDOClientSecret = value
  320. case "Footer":
  321. common.Footer = value
  322. case "SystemName":
  323. common.SystemName = value
  324. case "Logo":
  325. common.Logo = value
  326. case "WeChatServerAddress":
  327. common.WeChatServerAddress = value
  328. case "WeChatServerToken":
  329. common.WeChatServerToken = value
  330. case "WeChatAccountQRCodeImageURL":
  331. common.WeChatAccountQRCodeImageURL = value
  332. case "TelegramBotToken":
  333. common.TelegramBotToken = value
  334. case "TelegramBotName":
  335. common.TelegramBotName = value
  336. case "TurnstileSiteKey":
  337. common.TurnstileSiteKey = value
  338. case "TurnstileSecretKey":
  339. common.TurnstileSecretKey = value
  340. case "QuotaForNewUser":
  341. common.QuotaForNewUser, _ = strconv.Atoi(value)
  342. case "QuotaForInviter":
  343. common.QuotaForInviter, _ = strconv.Atoi(value)
  344. case "QuotaForInvitee":
  345. common.QuotaForInvitee, _ = strconv.Atoi(value)
  346. case "QuotaRemindThreshold":
  347. common.QuotaRemindThreshold, _ = strconv.Atoi(value)
  348. case "PreConsumedQuota":
  349. common.PreConsumedQuota, _ = strconv.Atoi(value)
  350. case "ModelRequestRateLimitCount":
  351. setting.ModelRequestRateLimitCount, _ = strconv.Atoi(value)
  352. case "ModelRequestRateLimitDurationMinutes":
  353. setting.ModelRequestRateLimitDurationMinutes, _ = strconv.Atoi(value)
  354. case "ModelRequestRateLimitSuccessCount":
  355. setting.ModelRequestRateLimitSuccessCount, _ = strconv.Atoi(value)
  356. case "RetryTimes":
  357. common.RetryTimes, _ = strconv.Atoi(value)
  358. case "DataExportInterval":
  359. common.DataExportInterval, _ = strconv.Atoi(value)
  360. case "DataExportDefaultTime":
  361. common.DataExportDefaultTime = value
  362. case "ModelRatio":
  363. err = operation_setting.UpdateModelRatioByJSONString(value)
  364. case "GroupRatio":
  365. err = setting.UpdateGroupRatioByJSONString(value)
  366. case "UserUsableGroups":
  367. err = setting.UpdateUserUsableGroupsByJSONString(value)
  368. case "CompletionRatio":
  369. err = operation_setting.UpdateCompletionRatioByJSONString(value)
  370. case "ModelPrice":
  371. err = operation_setting.UpdateModelPriceByJSONString(value)
  372. case "CacheRatio":
  373. err = operation_setting.UpdateCacheRatioByJSONString(value)
  374. case "TopUpLink":
  375. common.TopUpLink = value
  376. //case "ChatLink":
  377. // common.ChatLink = value
  378. //case "ChatLink2":
  379. // common.ChatLink2 = value
  380. case "ChannelDisableThreshold":
  381. common.ChannelDisableThreshold, _ = strconv.ParseFloat(value, 64)
  382. case "QuotaPerUnit":
  383. common.QuotaPerUnit, _ = strconv.ParseFloat(value, 64)
  384. case "SensitiveWords":
  385. setting.SensitiveWordsFromString(value)
  386. case "AutomaticDisableKeywords":
  387. operation_setting.AutomaticDisableKeywordsFromString(value)
  388. case "StreamCacheQueueLength":
  389. setting.StreamCacheQueueLength, _ = strconv.Atoi(value)
  390. case setting.ModelRequestRateLimitGroupKey:
  391. // Use the (potentially formatted) value passed from UpdateOption
  392. // to update the actual configuration in memory.
  393. // This is the single point where the memory state for this specific setting is updated.
  394. err = setting.UpdateModelRequestRateLimitGroupConfig(value)
  395. if err != nil {
  396. // 添加错误上下文
  397. err = fmt.Errorf("更新内存中的 %s 配置失败: %w", key, err)
  398. }
  399. }
  400. return err
  401. }
  402. // handleConfigUpdate 处理分层配置更新,返回是否已处理
  403. func handleConfigUpdate(key, value string) bool {
  404. parts := strings.SplitN(key, ".", 2)
  405. if len(parts) != 2 {
  406. return false // 不是分层配置
  407. }
  408. configName := parts[0]
  409. configKey := parts[1]
  410. // 获取配置对象
  411. cfg := config.GlobalConfig.Get(configName)
  412. if cfg == nil {
  413. return false // 未注册的配置
  414. }
  415. // 更新配置
  416. configMap := map[string]string{
  417. configKey: value,
  418. }
  419. config.UpdateConfigFromMap(cfg, configMap)
  420. return true // 已处理
  421. }