misc.go 8.0 KB

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