misc.go 7.5 KB

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