misc.go 7.7 KB

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