misc.go 8.1 KB

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