misc.go 8.4 KB

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