index.go 2.9 KB

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