misc.go 7.8 KB

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