misc.go 8.2 KB

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