Files
trihuy-russian/web/controller/index.go
T

155 lines
4.9 KiB
Go
Raw Normal View History

2023-02-09 22:48:06 +03:30
package controller
import (
"net/http"
2024-07-14 23:37:43 +02:00
"text/template"
2023-02-09 22:48:06 +03:30
"time"
2024-03-11 01:01:24 +03:30
2025-09-19 10:05:43 +02:00
"github.com/mhsanaei/3x-ui/v2/logger"
"github.com/mhsanaei/3x-ui/v2/web/middleware"
2025-09-19 10:05:43 +02:00
"github.com/mhsanaei/3x-ui/v2/web/service"
"github.com/mhsanaei/3x-ui/v2/web/session"
2023-02-09 22:48:06 +03:30
"github.com/gin-gonic/gin"
)
2025-09-20 09:35:50 +02:00
// LoginForm represents the login request structure.
2023-02-09 22:48:06 +03:30
type LoginForm struct {
2025-09-18 22:06:01 +02:00
Username string `json:"username" form:"username"`
Password string `json:"password" form:"password"`
TwoFactorCode string `json:"twoFactorCode" form:"twoFactorCode"`
2023-02-09 22:48:06 +03:30
}
2025-09-20 09:35:50 +02:00
// IndexController handles the main index and login-related routes.
2023-02-09 22:48:06 +03:30
type IndexController struct {
BaseController
2023-04-21 19:00:14 +03:30
settingService service.SettingService
userService service.UserService
tgbot service.Tgbot
2023-02-09 22:48:06 +03:30
}
2025-09-20 09:35:50 +02:00
// NewIndexController creates a new IndexController and initializes its routes.
2023-02-09 22:48:06 +03:30
func NewIndexController(g *gin.RouterGroup) *IndexController {
a := &IndexController{}
a.initRouter(g)
return a
}
2025-09-20 09:35:50 +02:00
// initRouter sets up the routes for index, login, logout, and two-factor authentication.
2023-02-09 22:48:06 +03:30
func (a *IndexController) initRouter(g *gin.RouterGroup) {
g.GET("/", a.index)
g.GET("/logout", a.logout)
2025-09-24 11:47:14 +02:00
g.POST("/login", middleware.CSRFMiddleware(), a.login)
g.POST("/getTwoFactorEnable", middleware.CSRFMiddleware(), a.getTwoFactorEnable)
2023-02-09 22:48:06 +03:30
}
2025-09-20 09:35:50 +02:00
// index handles the root route, redirecting logged-in users to the panel or showing the login page.
2023-02-09 22:48:06 +03:30
func (a *IndexController) index(c *gin.Context) {
if session.IsLogin(c) {
2023-05-12 22:36:05 +04:30
c.Redirect(http.StatusTemporaryRedirect, "panel/")
2023-02-09 22:48:06 +03:30
return
}
html(c, "login.html", "pages.login.title", nil)
}
2025-09-20 09:35:50 +02:00
// login handles user authentication and session creation.
2023-02-09 22:48:06 +03:30
func (a *IndexController) login(c *gin.Context) {
var form LoginForm
2024-12-16 14:24:59 +01:00
if err := c.ShouldBind(&form); err != nil {
2024-03-11 01:01:24 +03:30
pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.invalidFormData"))
2023-02-09 22:48:06 +03:30
return
}
if form.Username == "" {
2024-03-11 01:01:24 +03:30
pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.emptyUsername"))
2023-02-09 22:48:06 +03:30
return
}
if form.Password == "" {
2024-03-11 01:01:24 +03:30
pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.emptyPassword"))
2023-02-09 22:48:06 +03:30
return
}
2023-05-20 19:39:01 +04:30
remoteIP := getRemoteIp(c)
2024-07-14 23:37:43 +02:00
safeUser := template.HTMLEscapeString(form.Username)
timeStr := time.Now().Format("2006-01-02 15:04:05")
if blockedUntil, ok := defaultLoginLimiter.allow(remoteIP, form.Username); !ok {
reason := "too many failed attempts"
logger.Warningf("failed login: username=%q, IP=%q, reason=%q, blocked_until=%s", safeUser, remoteIP, reason, blockedUntil.Format(time.RFC3339))
a.tgbot.UserLoginNotify(service.LoginAttempt{
Username: safeUser,
IP: remoteIP,
Time: timeStr,
Status: service.LoginFail,
Reason: reason,
})
pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.wrongUsernameOrPassword"))
return
}
2026-03-17 22:30:05 +01:00
user, checkErr := a.userService.CheckUser(form.Username, form.Password, form.TwoFactorCode)
2026-03-17 22:30:05 +01:00
if user == nil {
reason := loginFailureReason(checkErr)
if blockedUntil, blocked := defaultLoginLimiter.registerFailure(remoteIP, form.Username); blocked {
logger.Warningf("failed login: username=%q, IP=%q, reason=%q, blocked_until=%s", safeUser, remoteIP, reason, blockedUntil.Format(time.RFC3339))
} else {
logger.Warningf("failed login: username=%q, IP=%q, reason=%q", safeUser, remoteIP, reason)
}
a.tgbot.UserLoginNotify(service.LoginAttempt{
Username: safeUser,
IP: remoteIP,
Time: timeStr,
Status: service.LoginFail,
Reason: reason,
})
2024-03-11 01:01:24 +03:30
pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.wrongUsernameOrPassword"))
2023-02-09 22:48:06 +03:30
return
2023-04-26 02:09:56 +03:30
}
2023-04-25 15:00:21 +03:30
defaultLoginLimiter.registerSuccess(remoteIP, form.Username)
logger.Infof("%s logged in successfully, Ip Address: %s\n", safeUser, remoteIP)
a.tgbot.UserLoginNotify(service.LoginAttempt{
Username: safeUser,
IP: remoteIP,
Time: timeStr,
Status: service.LoginSuccess,
})
2024-12-16 14:24:59 +01:00
if err := session.SetLoginUser(c, user); err != nil {
logger.Warning("Unable to save session:", err)
2024-12-16 14:24:59 +01:00
return
2023-02-09 22:48:06 +03:30
}
2024-12-16 14:24:59 +01:00
logger.Infof("%s logged in successfully", safeUser)
jsonMsg(c, I18nWeb(c, "pages.login.toasts.successLogin"), nil)
2023-02-09 22:48:06 +03:30
}
func loginFailureReason(err error) string {
if err != nil && err.Error() == "invalid 2fa code" {
return "invalid 2FA code"
}
return "invalid credentials"
}
2025-09-20 09:35:50 +02:00
// logout handles user logout by clearing the session and redirecting to the login page.
2023-02-09 22:48:06 +03:30
func (a *IndexController) logout(c *gin.Context) {
user := session.GetLoginUser(c)
if user != nil {
2024-07-08 23:08:00 +02:00
logger.Infof("%s logged out successfully", user.Username)
2023-02-09 22:48:06 +03:30
}
if err := session.ClearSession(c); err != nil {
logger.Warning("Unable to clear session on logout:", err)
2024-12-16 14:24:59 +01:00
}
2023-02-09 22:48:06 +03:30
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
}
2023-04-21 19:00:14 +03:30
2025-09-20 09:35:50 +02:00
// getTwoFactorEnable retrieves the current status of two-factor authentication.
2025-05-08 21:20:58 +07:00
func (a *IndexController) getTwoFactorEnable(c *gin.Context) {
status, err := a.settingService.GetTwoFactorEnable()
2023-04-21 19:00:14 +03:30
if err == nil {
jsonObj(c, status, nil)
}
}