2
0

misc.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package controller
  2. import (
  3. "slices"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "one-api/common"
  8. "one-api/constant"
  9. "one-api/middleware"
  10. "one-api/middleware/jsrt"
  11. "one-api/model"
  12. "one-api/setting"
  13. "one-api/setting/console_setting"
  14. "one-api/setting/operation_setting"
  15. "one-api/setting/system_setting"
  16. "strings"
  17. "github.com/gin-gonic/gin"
  18. )
  19. func TestStatus(c *gin.Context) {
  20. err := model.PingDB()
  21. if err != nil {
  22. c.JSON(http.StatusServiceUnavailable, gin.H{
  23. "success": false,
  24. "message": "数据库连接失败",
  25. })
  26. return
  27. }
  28. // 获取HTTP统计信息
  29. httpStats := middleware.GetStats()
  30. c.JSON(http.StatusOK, gin.H{
  31. "success": true,
  32. "message": "Server is running",
  33. "http_stats": httpStats,
  34. })
  35. }
  36. func GetStatus(c *gin.Context) {
  37. cs := console_setting.GetConsoleSetting()
  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. "default_use_auto_group": setting.DefaultUseAutoGroup,
  74. "pay_methods": setting.PayMethods,
  75. // 面板启用开关
  76. "api_info_enabled": cs.ApiInfoEnabled,
  77. "uptime_kuma_enabled": cs.UptimeKumaEnabled,
  78. "announcements_enabled": cs.AnnouncementsEnabled,
  79. "faq_enabled": cs.FAQEnabled,
  80. "oidc_enabled": system_setting.GetOIDCSettings().Enabled,
  81. "oidc_client_id": system_setting.GetOIDCSettings().ClientId,
  82. "oidc_authorization_endpoint": system_setting.GetOIDCSettings().AuthorizationEndpoint,
  83. "setup": constant.Setup,
  84. }
  85. // 根据启用状态注入可选内容
  86. if cs.ApiInfoEnabled {
  87. data["api_info"] = console_setting.GetApiInfo()
  88. }
  89. if cs.AnnouncementsEnabled {
  90. data["announcements"] = console_setting.GetAnnouncements()
  91. }
  92. if cs.FAQEnabled {
  93. data["faq"] = console_setting.GetFAQ()
  94. }
  95. c.JSON(http.StatusOK, gin.H{
  96. "success": true,
  97. "message": "",
  98. "data": data,
  99. })
  100. }
  101. func GetNotice(c *gin.Context) {
  102. common.OptionMapRWMutex.RLock()
  103. defer common.OptionMapRWMutex.RUnlock()
  104. c.JSON(http.StatusOK, gin.H{
  105. "success": true,
  106. "message": "",
  107. "data": common.OptionMap["Notice"],
  108. })
  109. }
  110. func GetAbout(c *gin.Context) {
  111. common.OptionMapRWMutex.RLock()
  112. defer common.OptionMapRWMutex.RUnlock()
  113. c.JSON(http.StatusOK, gin.H{
  114. "success": true,
  115. "message": "",
  116. "data": common.OptionMap["About"],
  117. })
  118. }
  119. func GetMidjourney(c *gin.Context) {
  120. common.OptionMapRWMutex.RLock()
  121. defer common.OptionMapRWMutex.RUnlock()
  122. c.JSON(http.StatusOK, gin.H{
  123. "success": true,
  124. "message": "",
  125. "data": common.OptionMap["Midjourney"],
  126. })
  127. }
  128. func GetHomePageContent(c *gin.Context) {
  129. common.OptionMapRWMutex.RLock()
  130. defer common.OptionMapRWMutex.RUnlock()
  131. c.JSON(http.StatusOK, gin.H{
  132. "success": true,
  133. "message": "",
  134. "data": common.OptionMap["HomePageContent"],
  135. })
  136. }
  137. func SendEmailVerification(c *gin.Context) {
  138. email := c.Query("email")
  139. if err := common.Validate.Var(email, "required,email"); err != nil {
  140. c.JSON(http.StatusOK, gin.H{
  141. "success": false,
  142. "message": "无效的参数",
  143. })
  144. return
  145. }
  146. parts := strings.Split(email, "@")
  147. if len(parts) != 2 {
  148. c.JSON(http.StatusOK, gin.H{
  149. "success": false,
  150. "message": "无效的邮箱地址",
  151. })
  152. return
  153. }
  154. localPart := parts[0]
  155. domainPart := parts[1]
  156. if common.EmailDomainRestrictionEnabled {
  157. allowed := slices.Contains(common.EmailDomainWhitelist, domainPart)
  158. if !allowed {
  159. c.JSON(http.StatusOK, gin.H{
  160. "success": false,
  161. "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.",
  162. })
  163. return
  164. }
  165. }
  166. if common.EmailAliasRestrictionEnabled {
  167. containsSpecialSymbols := strings.Contains(localPart, "+") || strings.Contains(localPart, ".")
  168. if containsSpecialSymbols {
  169. c.JSON(http.StatusOK, gin.H{
  170. "success": false,
  171. "message": "管理员已启用邮箱地址别名限制,您的邮箱地址由于包含特殊符号而被拒绝。",
  172. })
  173. return
  174. }
  175. }
  176. if model.IsEmailAlreadyTaken(email) {
  177. c.JSON(http.StatusOK, gin.H{
  178. "success": false,
  179. "message": "邮箱地址已被占用",
  180. })
  181. return
  182. }
  183. code := common.GenerateVerificationCode(6)
  184. common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
  185. subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
  186. content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
  187. "<p>您的验证码为: <strong>%s</strong></p>"+
  188. "<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, common.VerificationValidMinutes)
  189. err := common.SendEmail(subject, email, content)
  190. if err != nil {
  191. c.JSON(http.StatusOK, gin.H{
  192. "success": false,
  193. "message": err.Error(),
  194. })
  195. return
  196. }
  197. c.JSON(http.StatusOK, gin.H{
  198. "success": true,
  199. "message": "",
  200. })
  201. }
  202. func SendPasswordResetEmail(c *gin.Context) {
  203. email := c.Query("email")
  204. if err := common.Validate.Var(email, "required,email"); err != nil {
  205. c.JSON(http.StatusOK, gin.H{
  206. "success": false,
  207. "message": "无效的参数",
  208. })
  209. return
  210. }
  211. if !model.IsEmailAlreadyTaken(email) {
  212. c.JSON(http.StatusOK, gin.H{
  213. "success": false,
  214. "message": "该邮箱地址未注册",
  215. })
  216. return
  217. }
  218. code := common.GenerateVerificationCode(0)
  219. common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
  220. link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", setting.ServerAddress, email, code)
  221. subject := fmt.Sprintf("%s密码重置", common.SystemName)
  222. content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
  223. "<p>点击 <a href='%s'>此处</a> 进行密码重置。</p>"+
  224. "<p>如果链接无法点击,请尝试点击下面的链接或将其复制到浏览器中打开:<br> %s </p>"+
  225. "<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, link, common.VerificationValidMinutes)
  226. err := common.SendEmail(subject, email, content)
  227. if err != nil {
  228. c.JSON(http.StatusOK, gin.H{
  229. "success": false,
  230. "message": err.Error(),
  231. })
  232. return
  233. }
  234. c.JSON(http.StatusOK, gin.H{
  235. "success": true,
  236. "message": "",
  237. })
  238. }
  239. type PasswordResetRequest struct {
  240. Email string `json:"email"`
  241. Token string `json:"token"`
  242. }
  243. func ResetPassword(c *gin.Context) {
  244. var req PasswordResetRequest
  245. err := json.NewDecoder(c.Request.Body).Decode(&req)
  246. if req.Email == "" || req.Token == "" {
  247. c.JSON(http.StatusOK, gin.H{
  248. "success": false,
  249. "message": "无效的参数",
  250. })
  251. return
  252. }
  253. if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
  254. c.JSON(http.StatusOK, gin.H{
  255. "success": false,
  256. "message": "重置链接非法或已过期",
  257. })
  258. return
  259. }
  260. password := common.GenerateVerificationCode(12)
  261. err = model.ResetUserPasswordByEmail(req.Email, password)
  262. if err != nil {
  263. c.JSON(http.StatusOK, gin.H{
  264. "success": false,
  265. "message": err.Error(),
  266. })
  267. return
  268. }
  269. common.DeleteKey(req.Email, common.PasswordResetPurpose)
  270. c.JSON(http.StatusOK, gin.H{
  271. "success": true,
  272. "message": "",
  273. "data": password,
  274. })
  275. }
  276. func ReloadJSScripts(c *gin.Context) {
  277. jsrt.ReloadJSScripts()
  278. c.JSON(http.StatusOK, gin.H{
  279. "success": true,
  280. "message": "JavaScript 脚本已重新加载",
  281. })
  282. }