option.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package controller
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "one-api/setting"
  8. "one-api/setting/console_setting"
  9. "one-api/setting/ratio_setting"
  10. "one-api/setting/system_setting"
  11. "strings"
  12. "github.com/gin-gonic/gin"
  13. )
  14. func GetOptions(c *gin.Context) {
  15. var options []*model.Option
  16. common.OptionMapRWMutex.Lock()
  17. for k, v := range common.OptionMap {
  18. if strings.HasSuffix(k, "Token") || strings.HasSuffix(k, "Secret") || strings.HasSuffix(k, "Key") {
  19. continue
  20. }
  21. options = append(options, &model.Option{
  22. Key: k,
  23. Value: common.Interface2String(v),
  24. })
  25. }
  26. common.OptionMapRWMutex.Unlock()
  27. c.JSON(http.StatusOK, gin.H{
  28. "success": true,
  29. "message": "",
  30. "data": options,
  31. })
  32. return
  33. }
  34. func UpdateOption(c *gin.Context) {
  35. var option model.Option
  36. err := json.NewDecoder(c.Request.Body).Decode(&option)
  37. if err != nil {
  38. c.JSON(http.StatusBadRequest, gin.H{
  39. "success": false,
  40. "message": "无效的参数",
  41. })
  42. return
  43. }
  44. switch option.Key {
  45. case "GitHubOAuthEnabled":
  46. if option.Value == "true" && common.GitHubClientId == "" {
  47. c.JSON(http.StatusOK, gin.H{
  48. "success": false,
  49. "message": "无法启用 GitHub OAuth,请先填入 GitHub Client Id 以及 GitHub Client Secret!",
  50. })
  51. return
  52. }
  53. case "oidc.enabled":
  54. if option.Value == "true" && system_setting.GetOIDCSettings().ClientId == "" {
  55. c.JSON(http.StatusOK, gin.H{
  56. "success": false,
  57. "message": "无法启用 OIDC 登录,请先填入 OIDC Client Id 以及 OIDC Client Secret!",
  58. })
  59. return
  60. }
  61. case "LinuxDOOAuthEnabled":
  62. if option.Value == "true" && common.LinuxDOClientId == "" {
  63. c.JSON(http.StatusOK, gin.H{
  64. "success": false,
  65. "message": "无法启用 LinuxDO OAuth,请先填入 LinuxDO Client Id 以及 LinuxDO Client Secret!",
  66. })
  67. return
  68. }
  69. case "EmailDomainRestrictionEnabled":
  70. if option.Value == "true" && len(common.EmailDomainWhitelist) == 0 {
  71. c.JSON(http.StatusOK, gin.H{
  72. "success": false,
  73. "message": "无法启用邮箱域名限制,请先填入限制的邮箱域名!",
  74. })
  75. return
  76. }
  77. case "WeChatAuthEnabled":
  78. if option.Value == "true" && common.WeChatServerAddress == "" {
  79. c.JSON(http.StatusOK, gin.H{
  80. "success": false,
  81. "message": "无法启用微信登录,请先填入微信登录相关配置信息!",
  82. })
  83. return
  84. }
  85. case "TurnstileCheckEnabled":
  86. if option.Value == "true" && common.TurnstileSiteKey == "" {
  87. c.JSON(http.StatusOK, gin.H{
  88. "success": false,
  89. "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
  90. })
  91. return
  92. }
  93. case "TelegramOAuthEnabled":
  94. if option.Value == "true" && common.TelegramBotToken == "" {
  95. c.JSON(http.StatusOK, gin.H{
  96. "success": false,
  97. "message": "无法启用 Telegram OAuth,请先填入 Telegram Bot Token!",
  98. })
  99. return
  100. }
  101. case "GroupRatio":
  102. err = ratio_setting.CheckGroupRatio(option.Value)
  103. if err != nil {
  104. c.JSON(http.StatusOK, gin.H{
  105. "success": false,
  106. "message": err.Error(),
  107. })
  108. return
  109. }
  110. case "ModelRequestRateLimitGroup":
  111. err = setting.CheckModelRequestRateLimitGroup(option.Value)
  112. if err != nil {
  113. c.JSON(http.StatusOK, gin.H{
  114. "success": false,
  115. "message": err.Error(),
  116. })
  117. return
  118. }
  119. case "console_setting.api_info":
  120. err = console_setting.ValidateConsoleSettings(option.Value, "ApiInfo")
  121. if err != nil {
  122. c.JSON(http.StatusOK, gin.H{
  123. "success": false,
  124. "message": err.Error(),
  125. })
  126. return
  127. }
  128. case "console_setting.announcements":
  129. err = console_setting.ValidateConsoleSettings(option.Value, "Announcements")
  130. if err != nil {
  131. c.JSON(http.StatusOK, gin.H{
  132. "success": false,
  133. "message": err.Error(),
  134. })
  135. return
  136. }
  137. case "console_setting.faq":
  138. err = console_setting.ValidateConsoleSettings(option.Value, "FAQ")
  139. if err != nil {
  140. c.JSON(http.StatusOK, gin.H{
  141. "success": false,
  142. "message": err.Error(),
  143. })
  144. return
  145. }
  146. case "console_setting.uptime_kuma_groups":
  147. err = console_setting.ValidateConsoleSettings(option.Value, "UptimeKumaGroups")
  148. if err != nil {
  149. c.JSON(http.StatusOK, gin.H{
  150. "success": false,
  151. "message": err.Error(),
  152. })
  153. return
  154. }
  155. }
  156. err = model.UpdateOption(option.Key, option.Value)
  157. if err != nil {
  158. common.ApiError(c, err)
  159. return
  160. }
  161. c.JSON(http.StatusOK, gin.H{
  162. "success": true,
  163. "message": "",
  164. })
  165. return
  166. }