misc.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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": constant.ServerAddress,
  45. "price": constant.Price,
  46. "min_topup": constant.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_task": common.TaskEnabled,
  57. "enable_data_export": common.DataExportEnabled,
  58. "data_export_default_time": common.DataExportDefaultTime,
  59. "default_collapse_sidebar": common.DefaultCollapseSidebar,
  60. "enable_online_topup": constant.PayAddress != "" && constant.EpayId != "" && constant.EpayKey != "",
  61. "mj_notify_enabled": constant.MjNotifyEnabled,
  62. "chats": constant.Chats,
  63. },
  64. })
  65. return
  66. }
  67. func GetNotice(c *gin.Context) {
  68. common.OptionMapRWMutex.RLock()
  69. defer common.OptionMapRWMutex.RUnlock()
  70. c.JSON(http.StatusOK, gin.H{
  71. "success": true,
  72. "message": "",
  73. "data": common.OptionMap["Notice"],
  74. })
  75. return
  76. }
  77. func GetAbout(c *gin.Context) {
  78. common.OptionMapRWMutex.RLock()
  79. defer common.OptionMapRWMutex.RUnlock()
  80. c.JSON(http.StatusOK, gin.H{
  81. "success": true,
  82. "message": "",
  83. "data": common.OptionMap["About"],
  84. })
  85. return
  86. }
  87. func GetMidjourney(c *gin.Context) {
  88. common.OptionMapRWMutex.RLock()
  89. defer common.OptionMapRWMutex.RUnlock()
  90. c.JSON(http.StatusOK, gin.H{
  91. "success": true,
  92. "message": "",
  93. "data": common.OptionMap["Midjourney"],
  94. })
  95. return
  96. }
  97. func GetHomePageContent(c *gin.Context) {
  98. common.OptionMapRWMutex.RLock()
  99. defer common.OptionMapRWMutex.RUnlock()
  100. c.JSON(http.StatusOK, gin.H{
  101. "success": true,
  102. "message": "",
  103. "data": common.OptionMap["HomePageContent"],
  104. })
  105. return
  106. }
  107. func SendEmailVerification(c *gin.Context) {
  108. email := c.Query("email")
  109. if err := common.Validate.Var(email, "required,email"); err != nil {
  110. c.JSON(http.StatusOK, gin.H{
  111. "success": false,
  112. "message": "无效的参数",
  113. })
  114. return
  115. }
  116. parts := strings.Split(email, "@")
  117. if len(parts) != 2 {
  118. c.JSON(http.StatusOK, gin.H{
  119. "success": false,
  120. "message": "无效的邮箱地址",
  121. })
  122. return
  123. }
  124. localPart := parts[0]
  125. domainPart := parts[1]
  126. if common.EmailDomainRestrictionEnabled {
  127. allowed := false
  128. for _, domain := range common.EmailDomainWhitelist {
  129. if domainPart == domain {
  130. allowed = true
  131. break
  132. }
  133. }
  134. if !allowed {
  135. c.JSON(http.StatusOK, gin.H{
  136. "success": false,
  137. "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.",
  138. })
  139. return
  140. }
  141. }
  142. if common.EmailAliasRestrictionEnabled {
  143. containsSpecialSymbols := strings.Contains(localPart, "+") || strings.Contains(localPart, ".")
  144. if containsSpecialSymbols {
  145. c.JSON(http.StatusOK, gin.H{
  146. "success": false,
  147. "message": "管理员已启用邮箱地址别名限制,您的邮箱地址由于包含特殊符号而被拒绝。",
  148. })
  149. return
  150. }
  151. }
  152. if model.IsEmailAlreadyTaken(email) {
  153. c.JSON(http.StatusOK, gin.H{
  154. "success": false,
  155. "message": "邮箱地址已被占用",
  156. })
  157. return
  158. }
  159. code := common.GenerateVerificationCode(6)
  160. common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
  161. subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
  162. content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
  163. "<p>您的验证码为: <strong>%s</strong></p>"+
  164. "<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, common.VerificationValidMinutes)
  165. err := common.SendEmail(subject, email, content)
  166. if err != nil {
  167. c.JSON(http.StatusOK, gin.H{
  168. "success": false,
  169. "message": err.Error(),
  170. })
  171. return
  172. }
  173. c.JSON(http.StatusOK, gin.H{
  174. "success": true,
  175. "message": "",
  176. })
  177. return
  178. }
  179. func SendPasswordResetEmail(c *gin.Context) {
  180. email := c.Query("email")
  181. if err := common.Validate.Var(email, "required,email"); err != nil {
  182. c.JSON(http.StatusOK, gin.H{
  183. "success": false,
  184. "message": "无效的参数",
  185. })
  186. return
  187. }
  188. if !model.IsEmailAlreadyTaken(email) {
  189. c.JSON(http.StatusOK, gin.H{
  190. "success": false,
  191. "message": "该邮箱地址未注册",
  192. })
  193. return
  194. }
  195. code := common.GenerateVerificationCode(0)
  196. common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
  197. link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", constant.ServerAddress, email, code)
  198. subject := fmt.Sprintf("%s密码重置", common.SystemName)
  199. content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
  200. "<p>点击 <a href='%s'>此处</a> 进行密码重置。</p>"+
  201. "<p>如果链接无法点击,请尝试点击下面的链接或将其复制到浏览器中打开:<br> %s </p>"+
  202. "<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, link, common.VerificationValidMinutes)
  203. err := common.SendEmail(subject, email, content)
  204. if err != nil {
  205. c.JSON(http.StatusOK, gin.H{
  206. "success": false,
  207. "message": err.Error(),
  208. })
  209. return
  210. }
  211. c.JSON(http.StatusOK, gin.H{
  212. "success": true,
  213. "message": "",
  214. })
  215. return
  216. }
  217. type PasswordResetRequest struct {
  218. Email string `json:"email"`
  219. Token string `json:"token"`
  220. }
  221. func ResetPassword(c *gin.Context) {
  222. var req PasswordResetRequest
  223. err := json.NewDecoder(c.Request.Body).Decode(&req)
  224. if req.Email == "" || req.Token == "" {
  225. c.JSON(http.StatusOK, gin.H{
  226. "success": false,
  227. "message": "无效的参数",
  228. })
  229. return
  230. }
  231. if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
  232. c.JSON(http.StatusOK, gin.H{
  233. "success": false,
  234. "message": "重置链接非法或已过期",
  235. })
  236. return
  237. }
  238. password := common.GenerateVerificationCode(12)
  239. err = model.ResetUserPasswordByEmail(req.Email, password)
  240. if err != nil {
  241. c.JSON(http.StatusOK, gin.H{
  242. "success": false,
  243. "message": err.Error(),
  244. })
  245. return
  246. }
  247. common.DeleteKey(req.Email, common.PasswordResetPurpose)
  248. c.JSON(http.StatusOK, gin.H{
  249. "success": true,
  250. "message": "",
  251. "data": password,
  252. })
  253. return
  254. }