misc.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "strings"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func GetStatus(c *gin.Context) {
  12. c.JSON(http.StatusOK, gin.H{
  13. "success": true,
  14. "message": "",
  15. "data": gin.H{
  16. "version": common.Version,
  17. "start_time": common.StartTime,
  18. "email_verification": common.EmailVerificationEnabled,
  19. "github_oauth": common.GitHubOAuthEnabled,
  20. "github_client_id": common.GitHubClientId,
  21. "system_name": common.SystemName,
  22. "logo": common.Logo,
  23. "footer_html": common.Footer,
  24. "wechat_qrcode": common.WeChatAccountQRCodeImageURL,
  25. "wechat_login": common.WeChatAuthEnabled,
  26. "server_address": common.ServerAddress,
  27. "turnstile_check": common.TurnstileCheckEnabled,
  28. "turnstile_site_key": common.TurnstileSiteKey,
  29. "top_up_link": common.TopUpLink,
  30. "chat_link": common.ChatLink,
  31. "quota_per_unit": common.QuotaPerUnit,
  32. "display_in_currency": common.DisplayInCurrencyEnabled,
  33. },
  34. })
  35. return
  36. }
  37. func GetNotice(c *gin.Context) {
  38. common.OptionMapRWMutex.RLock()
  39. defer common.OptionMapRWMutex.RUnlock()
  40. c.JSON(http.StatusOK, gin.H{
  41. "success": true,
  42. "message": "",
  43. "data": common.OptionMap["Notice"],
  44. })
  45. return
  46. }
  47. func GetAbout(c *gin.Context) {
  48. common.OptionMapRWMutex.RLock()
  49. defer common.OptionMapRWMutex.RUnlock()
  50. c.JSON(http.StatusOK, gin.H{
  51. "success": true,
  52. "message": "",
  53. "data": common.OptionMap["About"],
  54. })
  55. return
  56. }
  57. func GetHomePageContent(c *gin.Context) {
  58. common.OptionMapRWMutex.RLock()
  59. defer common.OptionMapRWMutex.RUnlock()
  60. c.JSON(http.StatusOK, gin.H{
  61. "success": true,
  62. "message": "",
  63. "data": common.OptionMap["HomePageContent"],
  64. })
  65. return
  66. }
  67. func SendEmailVerification(c *gin.Context) {
  68. email := c.Query("email")
  69. if err := common.Validate.Var(email, "required,email"); err != nil {
  70. c.JSON(http.StatusOK, gin.H{
  71. "success": false,
  72. "message": "无效的参数",
  73. })
  74. return
  75. }
  76. if common.EmailDomainRestrictionEnabled {
  77. allowed := false
  78. for _, domain := range common.EmailDomainWhitelist {
  79. if strings.HasSuffix(email, "@"+domain) {
  80. allowed = true
  81. break
  82. }
  83. }
  84. if !allowed {
  85. c.JSON(http.StatusOK, gin.H{
  86. "success": false,
  87. "message": "管理员启用了邮箱域名白名单,您的邮箱地址的域名不在白名单中",
  88. })
  89. return
  90. }
  91. }
  92. if model.IsEmailAlreadyTaken(email) {
  93. c.JSON(http.StatusOK, gin.H{
  94. "success": false,
  95. "message": "邮箱地址已被占用",
  96. })
  97. return
  98. }
  99. code := common.GenerateVerificationCode(6)
  100. common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
  101. subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
  102. content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
  103. "<p>您的验证码为: <strong>%s</strong></p>"+
  104. "<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, common.VerificationValidMinutes)
  105. err := common.SendEmail(subject, email, content)
  106. if err != nil {
  107. c.JSON(http.StatusOK, gin.H{
  108. "success": false,
  109. "message": err.Error(),
  110. })
  111. return
  112. }
  113. c.JSON(http.StatusOK, gin.H{
  114. "success": true,
  115. "message": "",
  116. })
  117. return
  118. }
  119. func SendPasswordResetEmail(c *gin.Context) {
  120. email := c.Query("email")
  121. if err := common.Validate.Var(email, "required,email"); err != nil {
  122. c.JSON(http.StatusOK, gin.H{
  123. "success": false,
  124. "message": "无效的参数",
  125. })
  126. return
  127. }
  128. if !model.IsEmailAlreadyTaken(email) {
  129. c.JSON(http.StatusOK, gin.H{
  130. "success": false,
  131. "message": "该邮箱地址未注册",
  132. })
  133. return
  134. }
  135. code := common.GenerateVerificationCode(0)
  136. common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
  137. link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", common.ServerAddress, email, code)
  138. subject := fmt.Sprintf("%s密码重置", common.SystemName)
  139. content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
  140. "<p>点击 <a href='%s'>此处</a> 进行密码重置。</p>"+
  141. "<p>如果链接无法点击,请尝试点击下面的链接或将其复制到浏览器中打开:<br> %s </p>"+
  142. "<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, link, common.VerificationValidMinutes)
  143. err := common.SendEmail(subject, email, content)
  144. if err != nil {
  145. c.JSON(http.StatusOK, gin.H{
  146. "success": false,
  147. "message": err.Error(),
  148. })
  149. return
  150. }
  151. c.JSON(http.StatusOK, gin.H{
  152. "success": true,
  153. "message": "",
  154. })
  155. return
  156. }
  157. type PasswordResetRequest struct {
  158. Email string `json:"email"`
  159. Token string `json:"token"`
  160. }
  161. func ResetPassword(c *gin.Context) {
  162. var req PasswordResetRequest
  163. err := json.NewDecoder(c.Request.Body).Decode(&req)
  164. if req.Email == "" || req.Token == "" {
  165. c.JSON(http.StatusOK, gin.H{
  166. "success": false,
  167. "message": "无效的参数",
  168. })
  169. return
  170. }
  171. if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
  172. c.JSON(http.StatusOK, gin.H{
  173. "success": false,
  174. "message": "重置链接非法或已过期",
  175. })
  176. return
  177. }
  178. password := common.GenerateVerificationCode(12)
  179. err = model.ResetUserPasswordByEmail(req.Email, password)
  180. if err != nil {
  181. c.JSON(http.StatusOK, gin.H{
  182. "success": false,
  183. "message": err.Error(),
  184. })
  185. return
  186. }
  187. common.DeleteKey(req.Email, common.PasswordResetPurpose)
  188. c.JSON(http.StatusOK, gin.H{
  189. "success": true,
  190. "message": "",
  191. "data": password,
  192. })
  193. return
  194. }