misc.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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_data_export": common.DataExportEnabled,
  57. "data_export_default_time": common.DataExportDefaultTime,
  58. "default_collapse_sidebar": common.DefaultCollapseSidebar,
  59. "enable_online_topup": constant.PayAddress != "" && constant.EpayId != "" && constant.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. parts := strings.Split(email, "@")
  115. if len(parts) != 2 {
  116. c.JSON(http.StatusOK, gin.H{
  117. "success": false,
  118. "message": "无效的邮箱地址",
  119. })
  120. return
  121. }
  122. localPart := parts[0]
  123. domainPart := parts[1]
  124. if common.EmailDomainRestrictionEnabled {
  125. allowed := false
  126. for _, domain := range common.EmailDomainWhitelist {
  127. if domainPart == domain {
  128. allowed = true
  129. break
  130. }
  131. }
  132. if !allowed {
  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 common.EmailAliasRestrictionEnabled {
  141. containsSpecialSymbols := strings.Contains(localPart, "+") || strings.Contains(localPart, ".")
  142. if containsSpecialSymbols {
  143. c.JSON(http.StatusOK, gin.H{
  144. "success": false,
  145. "message": "管理员已启用邮箱地址别名限制,您的邮箱地址由于包含特殊符号而被拒绝。",
  146. })
  147. return
  148. }
  149. }
  150. if model.IsEmailAlreadyTaken(email) {
  151. c.JSON(http.StatusOK, gin.H{
  152. "success": false,
  153. "message": "邮箱地址已被占用",
  154. })
  155. return
  156. }
  157. code := common.GenerateVerificationCode(6)
  158. common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
  159. subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
  160. content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
  161. "<p>您的验证码为: <strong>%s</strong></p>"+
  162. "<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, common.VerificationValidMinutes)
  163. err := common.SendEmail(subject, email, content)
  164. if err != nil {
  165. c.JSON(http.StatusOK, gin.H{
  166. "success": false,
  167. "message": err.Error(),
  168. })
  169. return
  170. }
  171. c.JSON(http.StatusOK, gin.H{
  172. "success": true,
  173. "message": "",
  174. })
  175. return
  176. }
  177. func SendPasswordResetEmail(c *gin.Context) {
  178. email := c.Query("email")
  179. if err := common.Validate.Var(email, "required,email"); err != nil {
  180. c.JSON(http.StatusOK, gin.H{
  181. "success": false,
  182. "message": "无效的参数",
  183. })
  184. return
  185. }
  186. if !model.IsEmailAlreadyTaken(email) {
  187. c.JSON(http.StatusOK, gin.H{
  188. "success": false,
  189. "message": "该邮箱地址未注册",
  190. })
  191. return
  192. }
  193. code := common.GenerateVerificationCode(0)
  194. common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
  195. link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", constant.ServerAddress, email, code)
  196. subject := fmt.Sprintf("%s密码重置", common.SystemName)
  197. content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
  198. "<p>点击 <a href='%s'>此处</a> 进行密码重置。</p>"+
  199. "<p>如果链接无法点击,请尝试点击下面的链接或将其复制到浏览器中打开:<br> %s </p>"+
  200. "<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, link, common.VerificationValidMinutes)
  201. err := common.SendEmail(subject, email, content)
  202. if err != nil {
  203. c.JSON(http.StatusOK, gin.H{
  204. "success": false,
  205. "message": err.Error(),
  206. })
  207. return
  208. }
  209. c.JSON(http.StatusOK, gin.H{
  210. "success": true,
  211. "message": "",
  212. })
  213. return
  214. }
  215. type PasswordResetRequest struct {
  216. Email string `json:"email"`
  217. Token string `json:"token"`
  218. }
  219. func ResetPassword(c *gin.Context) {
  220. var req PasswordResetRequest
  221. err := json.NewDecoder(c.Request.Body).Decode(&req)
  222. if req.Email == "" || req.Token == "" {
  223. c.JSON(http.StatusOK, gin.H{
  224. "success": false,
  225. "message": "无效的参数",
  226. })
  227. return
  228. }
  229. if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
  230. c.JSON(http.StatusOK, gin.H{
  231. "success": false,
  232. "message": "重置链接非法或已过期",
  233. })
  234. return
  235. }
  236. password := common.GenerateVerificationCode(12)
  237. err = model.ResetUserPasswordByEmail(req.Email, password)
  238. if err != nil {
  239. c.JSON(http.StatusOK, gin.H{
  240. "success": false,
  241. "message": err.Error(),
  242. })
  243. return
  244. }
  245. common.DeleteKey(req.Email, common.PasswordResetPurpose)
  246. c.JSON(http.StatusOK, gin.H{
  247. "success": true,
  248. "message": "",
  249. "data": password,
  250. })
  251. return
  252. }