index.go 3.8 KB

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