option.go 6.0 KB

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