misc.go 7.2 KB

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