misc.go 7.6 KB

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