1
0

index.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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-gonic/gin"
  10. )
  11. type LoginForm struct {
  12. Username string `json:"username" form:"username"`
  13. Password string `json:"password" form:"password"`
  14. LoginSecret string `json:"loginSecret" form:"loginSecret"`
  15. }
  16. type IndexController struct {
  17. BaseController
  18. settingService service.SettingService
  19. userService service.UserService
  20. tgbot service.Tgbot
  21. }
  22. func NewIndexController(g *gin.RouterGroup) *IndexController {
  23. a := &IndexController{}
  24. a.initRouter(g)
  25. return a
  26. }
  27. func (a *IndexController) initRouter(g *gin.RouterGroup) {
  28. g.GET("/", a.index)
  29. g.POST("/login", a.login)
  30. g.GET("/logout", a.logout)
  31. g.POST("/getSecretStatus", a.getSecretStatus)
  32. }
  33. func (a *IndexController) index(c *gin.Context) {
  34. if session.IsLogin(c) {
  35. c.Redirect(http.StatusTemporaryRedirect, "panel/")
  36. return
  37. }
  38. html(c, "login.html", "pages.login.title", nil)
  39. }
  40. func (a *IndexController) login(c *gin.Context) {
  41. var form LoginForm
  42. err := c.ShouldBind(&form)
  43. if 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 or password or secret: \"%s\" \"%s\" \"%s\"", safeUser, safePass, safeSecret)
  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. } else {
  66. logger.Infof("%s logged in successfully, Ip Address: %s\n", safeUser, getRemoteIp(c))
  67. a.tgbot.UserLoginNotify(safeUser, ``, getRemoteIp(c), timeStr, 1)
  68. }
  69. sessionMaxAge, err := a.settingService.GetSessionMaxAge()
  70. if err != nil {
  71. logger.Warning("Unable to get session's max age from DB")
  72. }
  73. if sessionMaxAge <= 0 {
  74. sessionMaxAge = 60
  75. }
  76. err = session.SetMaxAge(c, sessionMaxAge*60)
  77. if err != nil {
  78. logger.Warning("Unable to set session's max age")
  79. }
  80. err = session.SetLoginUser(c, user)
  81. logger.Infof("%s logged in successfully", user.Username)
  82. jsonMsg(c, I18nWeb(c, "pages.login.toasts.successLogin"), err)
  83. }
  84. func (a *IndexController) logout(c *gin.Context) {
  85. user := session.GetLoginUser(c)
  86. if user != nil {
  87. logger.Infof("%s logged out successfully", user.Username)
  88. }
  89. session.ClearSession(c)
  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. }