misc.go 8.5 KB

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