option.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/QuantumNous/new-api/common"
  8. "github.com/QuantumNous/new-api/model"
  9. "github.com/QuantumNous/new-api/setting"
  10. "github.com/QuantumNous/new-api/setting/console_setting"
  11. "github.com/QuantumNous/new-api/setting/ratio_setting"
  12. "github.com/QuantumNous/new-api/setting/system_setting"
  13. "github.com/gin-gonic/gin"
  14. )
  15. func GetOptions(c *gin.Context) {
  16. var options []*model.Option
  17. common.OptionMapRWMutex.Lock()
  18. for k, v := range common.OptionMap {
  19. if strings.HasSuffix(k, "Token") || strings.HasSuffix(k, "Secret") || strings.HasSuffix(k, "Key") {
  20. continue
  21. }
  22. options = append(options, &model.Option{
  23. Key: k,
  24. Value: common.Interface2String(v),
  25. })
  26. }
  27. common.OptionMapRWMutex.Unlock()
  28. c.JSON(http.StatusOK, gin.H{
  29. "success": true,
  30. "message": "",
  31. "data": options,
  32. })
  33. return
  34. }
  35. type OptionUpdateRequest struct {
  36. Key string `json:"key"`
  37. Value any `json:"value"`
  38. }
  39. func UpdateOption(c *gin.Context) {
  40. var option OptionUpdateRequest
  41. err := json.NewDecoder(c.Request.Body).Decode(&option)
  42. if err != nil {
  43. c.JSON(http.StatusBadRequest, gin.H{
  44. "success": false,
  45. "message": "无效的参数",
  46. })
  47. return
  48. }
  49. switch option.Value.(type) {
  50. case bool:
  51. option.Value = common.Interface2String(option.Value.(bool))
  52. case float64:
  53. option.Value = common.Interface2String(option.Value.(float64))
  54. case int:
  55. option.Value = common.Interface2String(option.Value.(int))
  56. default:
  57. option.Value = fmt.Sprintf("%v", option.Value)
  58. }
  59. switch option.Key {
  60. case "GitHubOAuthEnabled":
  61. if option.Value == "true" && common.GitHubClientId == "" {
  62. c.JSON(http.StatusOK, gin.H{
  63. "success": false,
  64. "message": "无法启用 GitHub OAuth,请先填入 GitHub Client Id 以及 GitHub Client Secret!",
  65. })
  66. return
  67. }
  68. case "discord.enabled":
  69. if option.Value == "true" && system_setting.GetDiscordSettings().ClientId == "" {
  70. c.JSON(http.StatusOK, gin.H{
  71. "success": false,
  72. "message": "无法启用 Discord OAuth,请先填入 Discord Client Id 以及 Discord Client Secret!",
  73. })
  74. return
  75. }
  76. case "oidc.enabled":
  77. if option.Value == "true" && system_setting.GetOIDCSettings().ClientId == "" {
  78. c.JSON(http.StatusOK, gin.H{
  79. "success": false,
  80. "message": "无法启用 OIDC 登录,请先填入 OIDC Client Id 以及 OIDC Client Secret!",
  81. })
  82. return
  83. }
  84. case "LinuxDOOAuthEnabled":
  85. if option.Value == "true" && common.LinuxDOClientId == "" {
  86. c.JSON(http.StatusOK, gin.H{
  87. "success": false,
  88. "message": "无法启用 LinuxDO OAuth,请先填入 LinuxDO Client Id 以及 LinuxDO Client Secret!",
  89. })
  90. return
  91. }
  92. case "EmailDomainRestrictionEnabled":
  93. if option.Value == "true" && len(common.EmailDomainWhitelist) == 0 {
  94. c.JSON(http.StatusOK, gin.H{
  95. "success": false,
  96. "message": "无法启用邮箱域名限制,请先填入限制的邮箱域名!",
  97. })
  98. return
  99. }
  100. case "WeChatAuthEnabled":
  101. if option.Value == "true" && common.WeChatServerAddress == "" {
  102. c.JSON(http.StatusOK, gin.H{
  103. "success": false,
  104. "message": "无法启用微信登录,请先填入微信登录相关配置信息!",
  105. })
  106. return
  107. }
  108. case "TurnstileCheckEnabled":
  109. if option.Value == "true" && common.TurnstileSiteKey == "" {
  110. c.JSON(http.StatusOK, gin.H{
  111. "success": false,
  112. "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
  113. })
  114. return
  115. }
  116. case "TelegramOAuthEnabled":
  117. if option.Value == "true" && common.TelegramBotToken == "" {
  118. c.JSON(http.StatusOK, gin.H{
  119. "success": false,
  120. "message": "无法启用 Telegram OAuth,请先填入 Telegram Bot Token!",
  121. })
  122. return
  123. }
  124. case "GroupRatio":
  125. err = ratio_setting.CheckGroupRatio(option.Value.(string))
  126. if err != nil {
  127. c.JSON(http.StatusOK, gin.H{
  128. "success": false,
  129. "message": err.Error(),
  130. })
  131. return
  132. }
  133. case "ImageRatio":
  134. err = ratio_setting.UpdateImageRatioByJSONString(option.Value.(string))
  135. if err != nil {
  136. c.JSON(http.StatusOK, gin.H{
  137. "success": false,
  138. "message": "图片倍率设置失败: " + err.Error(),
  139. })
  140. return
  141. }
  142. case "AudioRatio":
  143. err = ratio_setting.UpdateAudioRatioByJSONString(option.Value.(string))
  144. if err != nil {
  145. c.JSON(http.StatusOK, gin.H{
  146. "success": false,
  147. "message": "音频倍率设置失败: " + err.Error(),
  148. })
  149. return
  150. }
  151. case "AudioCompletionRatio":
  152. err = ratio_setting.UpdateAudioCompletionRatioByJSONString(option.Value.(string))
  153. if err != nil {
  154. c.JSON(http.StatusOK, gin.H{
  155. "success": false,
  156. "message": "音频补全倍率设置失败: " + err.Error(),
  157. })
  158. return
  159. }
  160. case "ModelRequestRateLimitGroup":
  161. err = setting.CheckModelRequestRateLimitGroup(option.Value.(string))
  162. if err != nil {
  163. c.JSON(http.StatusOK, gin.H{
  164. "success": false,
  165. "message": err.Error(),
  166. })
  167. return
  168. }
  169. case "console_setting.api_info":
  170. err = console_setting.ValidateConsoleSettings(option.Value.(string), "ApiInfo")
  171. if err != nil {
  172. c.JSON(http.StatusOK, gin.H{
  173. "success": false,
  174. "message": err.Error(),
  175. })
  176. return
  177. }
  178. case "console_setting.announcements":
  179. err = console_setting.ValidateConsoleSettings(option.Value.(string), "Announcements")
  180. if err != nil {
  181. c.JSON(http.StatusOK, gin.H{
  182. "success": false,
  183. "message": err.Error(),
  184. })
  185. return
  186. }
  187. case "console_setting.faq":
  188. err = console_setting.ValidateConsoleSettings(option.Value.(string), "FAQ")
  189. if err != nil {
  190. c.JSON(http.StatusOK, gin.H{
  191. "success": false,
  192. "message": err.Error(),
  193. })
  194. return
  195. }
  196. case "console_setting.uptime_kuma_groups":
  197. err = console_setting.ValidateConsoleSettings(option.Value.(string), "UptimeKumaGroups")
  198. if err != nil {
  199. c.JSON(http.StatusOK, gin.H{
  200. "success": false,
  201. "message": err.Error(),
  202. })
  203. return
  204. }
  205. }
  206. err = model.UpdateOption(option.Key, option.Value.(string))
  207. if err != nil {
  208. common.ApiError(c, err)
  209. return
  210. }
  211. c.JSON(http.StatusOK, gin.H{
  212. "success": true,
  213. "message": "",
  214. })
  215. return
  216. }