misc.go 7.7 KB

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