index.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package controller
  2. import (
  3. "net/http"
  4. "text/template"
  5. "time"
  6. "x-ui/logger"
  7. "x-ui/web/service"
  8. "x-ui/web/session"
  9. "github.com/gin-contrib/sessions"
  10. "github.com/gin-gonic/gin"
  11. )
  12. type LoginForm struct {
  13. Username string `json:"username" form:"username"`
  14. Password string `json:"password" form:"password"`
  15. LoginSecret string `json:"loginSecret" form:"loginSecret"`
  16. }
  17. type IndexController struct {
  18. BaseController
  19. settingService service.SettingService
  20. userService service.UserService
  21. tgbot service.Tgbot
  22. }
  23. func NewIndexController(g *gin.RouterGroup) *IndexController {
  24. a := &IndexController{}
  25. a.initRouter(g)
  26. return a
  27. }
  28. func (a *IndexController) initRouter(g *gin.RouterGroup) {
  29. g.GET("/", a.index)
  30. g.POST("/login", a.login)
  31. g.GET("/logout", a.logout)
  32. g.POST("/getSecretStatus", a.getSecretStatus)
  33. }
  34. func (a *IndexController) index(c *gin.Context) {
  35. if session.IsLogin(c) {
  36. c.Redirect(http.StatusTemporaryRedirect, "panel/")
  37. return
  38. }
  39. html(c, "login.html", "pages.login.title", nil)
  40. }
  41. func (a *IndexController) login(c *gin.Context) {
  42. var form LoginForm
  43. if err := c.ShouldBind(&form); err != nil {
  44. pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.invalidFormData"))
  45. return
  46. }
  47. if form.Username == "" {
  48. pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.emptyUsername"))
  49. return
  50. }
  51. if form.Password == "" {
  52. pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.emptyPassword"))
  53. return
  54. }
  55. user := a.userService.CheckUser(form.Username, form.Password, form.LoginSecret)
  56. timeStr := time.Now().Format("2006-01-02 15:04:05")
  57. safeUser := template.HTMLEscapeString(form.Username)
  58. safePass := template.HTMLEscapeString(form.Password)
  59. safeSecret := template.HTMLEscapeString(form.LoginSecret)
  60. if user == nil {
  61. logger.Warningf("wrong username: \"%s\", password: \"%s\", secret: \"%s\", IP: \"%s\"", safeUser, safePass, safeSecret, getRemoteIp(c))
  62. a.tgbot.UserLoginNotify(safeUser, safePass, getRemoteIp(c), timeStr, 0)
  63. pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.wrongUsernameOrPassword"))
  64. return
  65. }
  66. logger.Infof("%s logged in successfully, Ip Address: %s\n", safeUser, getRemoteIp(c))
  67. a.tgbot.UserLoginNotify(safeUser, ``, getRemoteIp(c), timeStr, 1)
  68. sessionMaxAge, err := a.settingService.GetSessionMaxAge()
  69. if err != nil {
  70. logger.Warning("Unable to get session's max age from DB")
  71. }
  72. session.SetMaxAge(c, sessionMaxAge*60)
  73. session.SetLoginUser(c, user)
  74. if err := sessions.Default(c).Save(); err != nil {
  75. logger.Warning("Unable to save session: ", err)
  76. return
  77. }
  78. logger.Infof("%s logged in successfully", safeUser)
  79. jsonMsg(c, I18nWeb(c, "pages.login.toasts.successLogin"), nil)
  80. }
  81. func (a *IndexController) logout(c *gin.Context) {
  82. user := session.GetLoginUser(c)
  83. if user != nil {
  84. logger.Infof("%s logged out successfully", user.Username)
  85. }
  86. session.ClearSession(c)
  87. if err := sessions.Default(c).Save(); err != nil {
  88. logger.Warning("Unable to save session after clearing:", err)
  89. }
  90. c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
  91. }
  92. func (a *IndexController) getSecretStatus(c *gin.Context) {
  93. status, err := a.settingService.GetSecretStatus()
  94. if err == nil {
  95. jsonObj(c, status, nil)
  96. }
  97. }