misc.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/constant"
  8. "one-api/model"
  9. "one-api/setting"
  10. "one-api/setting/operation_setting"
  11. "one-api/setting/system_setting"
  12. "strings"
  13. "github.com/gin-gonic/gin"
  14. )
  15. func TestStatus(c *gin.Context) {
  16. err := model.PingDB()
  17. if err != nil {
  18. c.JSON(http.StatusServiceUnavailable, gin.H{
  19. "success": false,
  20. "message": "数据库连接失败",
  21. })
  22. return
  23. }
  24. c.JSON(http.StatusOK, gin.H{
  25. "success": true,
  26. "message": "Server is running",
  27. })
  28. return
  29. }
  30. func GetStatus(c *gin.Context) {
  31. c.JSON(http.StatusOK, gin.H{
  32. "success": true,
  33. "message": "",
  34. "data": gin.H{
  35. "version": common.Version,
  36. "start_time": common.StartTime,
  37. "email_verification": common.EmailVerificationEnabled,
  38. "github_oauth": common.GitHubOAuthEnabled,
  39. "github_client_id": common.GitHubClientId,
  40. "linuxdo_oauth": common.LinuxDOOAuthEnabled,
  41. "linuxdo_client_id": common.LinuxDOClientId,
  42. "telegram_oauth": common.TelegramOAuthEnabled,
  43. "telegram_bot_name": common.TelegramBotName,
  44. "system_name": common.SystemName,
  45. "logo": common.Logo,
  46. "footer_html": common.Footer,
  47. "wechat_qrcode": common.WeChatAccountQRCodeImageURL,
  48. "wechat_login": common.WeChatAuthEnabled,
  49. "server_address": setting.ServerAddress,
  50. "price": setting.Price,
  51. "min_topup": setting.MinTopUp,
  52. "turnstile_check": common.TurnstileCheckEnabled,
  53. "turnstile_site_key": common.TurnstileSiteKey,
  54. "top_up_link": common.TopUpLink,
  55. "docs_link": operation_setting.GetGeneralSetting().DocsLink,
  56. "quota_per_unit": common.QuotaPerUnit,
  57. "display_in_currency": common.DisplayInCurrencyEnabled,
  58. "enable_batch_update": common.BatchUpdateEnabled,
  59. "enable_drawing": common.DrawingEnabled,
  60. "enable_task": common.TaskEnabled,
  61. "enable_data_export": common.DataExportEnabled,
  62. "data_export_default_time": common.DataExportDefaultTime,
  63. "default_collapse_sidebar": common.DefaultCollapseSidebar,
  64. "enable_online_topup": setting.PayAddress != "" && setting.EpayId != "" && setting.EpayKey != "",
  65. "mj_notify_enabled": setting.MjNotifyEnabled,
  66. "chats": setting.Chats,
  67. "demo_site_enabled": operation_setting.DemoSiteEnabled,
  68. "self_use_mode_enabled": operation_setting.SelfUseModeEnabled,
  69. "oidc_enabled": system_setting.GetOIDCSettings().Enabled,
  70. "oidc_client_id": system_setting.GetOIDCSettings().ClientId,
  71. "oidc_authorization_endpoint": system_setting.GetOIDCSettings().AuthorizationEndpoint,
  72. "setup": constant.Setup,
  73. "api_info": setting.GetApiInfo(),
  74. },
  75. })
  76. return
  77. }
  78. func GetNotice(c *gin.Context) {
  79. common.OptionMapRWMutex.RLock()
  80. defer common.OptionMapRWMutex.RUnlock()
  81. c.JSON(http.StatusOK, gin.H{
  82. "success": true,
  83. "message": "",
  84. "data": common.OptionMap["Notice"],
  85. })
  86. return
  87. }
  88. func GetAbout(c *gin.Context) {
  89. common.OptionMapRWMutex.RLock()
  90. defer common.OptionMapRWMutex.RUnlock()
  91. c.JSON(http.StatusOK, gin.H{
  92. "success": true,
  93. "message": "",
  94. "data": common.OptionMap["About"],
  95. })
  96. return
  97. }
  98. func GetMidjourney(c *gin.Context) {
  99. common.OptionMapRWMutex.RLock()
  100. defer common.OptionMapRWMutex.RUnlock()
  101. c.JSON(http.StatusOK, gin.H{
  102. "success": true,
  103. "message": "",
  104. "data": common.OptionMap["Midjourney"],
  105. })
  106. return
  107. }
  108. func GetHomePageContent(c *gin.Context) {
  109. common.OptionMapRWMutex.RLock()
  110. defer common.OptionMapRWMutex.RUnlock()
  111. c.JSON(http.StatusOK, gin.H{
  112. "success": true,
  113. "message": "",
  114. "data": common.OptionMap["HomePageContent"],
  115. })
  116. return
  117. }
  118. func SendEmailVerification(c *gin.Context) {
  119. email := c.Query("email")
  120. if err := common.Validate.Var(email, "required,email"); err != nil {
  121. c.JSON(http.StatusOK, gin.H{
  122. "success": false,
  123. "message": "无效的参数",
  124. })
  125. return
  126. }
  127. parts := strings.Split(email, "@")
  128. if len(parts) != 2 {
  129. c.JSON(http.StatusOK, gin.H{
  130. "success": false,
  131. "message": "无效的邮箱地址",
  132. })
  133. return
  134. }
  135. localPart := parts[0]
  136. domainPart := parts[1]
  137. if common.EmailDomainRestrictionEnabled {
  138. allowed := false
  139. for _, domain := range common.EmailDomainWhitelist {
  140. if domainPart == domain {
  141. allowed = true
  142. break
  143. }
  144. }
  145. if !allowed {
  146. c.JSON(http.StatusOK, gin.H{
  147. "success": false,
  148. "message": "The administrator has enabled the email domain name whitelist, and your email address is not allowed due to special symbols or it's not in the whitelist.",
  149. })
  150. return
  151. }
  152. }
  153. if common.EmailAliasRestrictionEnabled {
  154. containsSpecialSymbols := strings.Contains(localPart, "+") || strings.Contains(localPart, ".")
  155. if containsSpecialSymbols {
  156. c.JSON(http.StatusOK, gin.H{
  157. "success": false,
  158. "message": "管理员已启用邮箱地址别名限制,您的邮箱地址由于包含特殊符号而被拒绝。",
  159. })
  160. return
  161. }
  162. }
  163. if model.IsEmailAlreadyTaken(email) {
  164. c.JSON(http.StatusOK, gin.H{
  165. "success": false,
  166. "message": "邮箱地址已被占用",
  167. })
  168. return
  169. }
  170. code := common.GenerateVerificationCode(6)
  171. common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
  172. subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
  173. content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
  174. "<p>您的验证码为: <strong>%s</strong></p>"+
  175. "<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, common.VerificationValidMinutes)
  176. err := common.SendEmail(subject, email, content)
  177. if err != nil {
  178. c.JSON(http.StatusOK, gin.H{
  179. "success": false,
  180. "message": err.Error(),
  181. })
  182. return
  183. }
  184. c.JSON(http.StatusOK, gin.H{
  185. "success": true,
  186. "message": "",
  187. })
  188. return
  189. }
  190. func SendPasswordResetEmail(c *gin.Context) {
  191. email := c.Query("email")
  192. if err := common.Validate.Var(email, "required,email"); err != nil {
  193. c.JSON(http.StatusOK, gin.H{
  194. "success": false,
  195. "message": "无效的参数",
  196. })
  197. return
  198. }
  199. if !model.IsEmailAlreadyTaken(email) {
  200. c.JSON(http.StatusOK, gin.H{
  201. "success": false,
  202. "message": "该邮箱地址未注册",
  203. })
  204. return
  205. }
  206. code := common.GenerateVerificationCode(0)
  207. common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
  208. link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", setting.ServerAddress, email, code)
  209. subject := fmt.Sprintf("%s密码重置", common.SystemName)
  210. content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
  211. "<p>点击 <a href='%s'>此处</a> 进行密码重置。</p>"+
  212. "<p>如果链接无法点击,请尝试点击下面的链接或将其复制到浏览器中打开:<br> %s </p>"+
  213. "<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, link, common.VerificationValidMinutes)
  214. err := common.SendEmail(subject, email, content)
  215. if err != nil {
  216. c.JSON(http.StatusOK, gin.H{
  217. "success": false,
  218. "message": err.Error(),
  219. })
  220. return
  221. }
  222. c.JSON(http.StatusOK, gin.H{
  223. "success": true,
  224. "message": "",
  225. })
  226. return
  227. }
  228. type PasswordResetRequest struct {
  229. Email string `json:"email"`
  230. Token string `json:"token"`
  231. }
  232. func ResetPassword(c *gin.Context) {
  233. var req PasswordResetRequest
  234. err := json.NewDecoder(c.Request.Body).Decode(&req)
  235. if req.Email == "" || req.Token == "" {
  236. c.JSON(http.StatusOK, gin.H{
  237. "success": false,
  238. "message": "无效的参数",
  239. })
  240. return
  241. }
  242. if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
  243. c.JSON(http.StatusOK, gin.H{
  244. "success": false,
  245. "message": "重置链接非法或已过期",
  246. })
  247. return
  248. }
  249. password := common.GenerateVerificationCode(12)
  250. err = model.ResetUserPasswordByEmail(req.Email, password)
  251. if err != nil {
  252. c.JSON(http.StatusOK, gin.H{
  253. "success": false,
  254. "message": err.Error(),
  255. })
  256. return
  257. }
  258. common.DeleteKey(req.Email, common.PasswordResetPurpose)
  259. c.JSON(http.StatusOK, gin.H{
  260. "success": true,
  261. "message": "",
  262. "data": password,
  263. })
  264. return
  265. }