3x-ui/web/controller/base.go

77 lines
2.4 KiB
Go
Raw Normal View History

2025-09-20 07:35:50 +00:00
// Package controller provides HTTP request handlers and controllers for the 3x-ui web management panel.
// It handles routing, authentication, and API endpoints for managing Xray inbounds, settings, and more.
2023-02-09 19:18:06 +00:00
package controller
import (
"net/http"
"strings"
2026-05-10 00:13:42 +00:00
"github.com/mhsanaei/3x-ui/v3/logger"
"github.com/mhsanaei/3x-ui/v3/util/crypto"
2026-05-10 00:13:42 +00:00
"github.com/mhsanaei/3x-ui/v3/web/locale"
"github.com/mhsanaei/3x-ui/v3/web/session"
"github.com/gin-gonic/gin"
2023-02-09 19:18:06 +00:00
)
2025-09-20 07:35:50 +00:00
// BaseController provides common functionality for all controllers, including authentication checks.
type BaseController struct{}
2023-02-09 19:18:06 +00:00
2025-09-20 07:35:50 +00:00
// checkLogin is a middleware that verifies user authentication and handles unauthorized access.
2023-02-09 19:18:06 +00:00
func (a *BaseController) checkLogin(c *gin.Context) {
user := session.GetLoginUser(c)
if user == nil {
2023-02-09 19:18:06 +00:00
if isAjax(c) {
pureJsonMsg(c, http.StatusUnauthorized, false, I18nWeb(c, "pages.login.loginAgain"))
2023-02-09 19:18:06 +00:00
} else {
c.Header("Cache-Control", "no-store")
2023-02-09 19:18:06 +00:00
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
}
c.Abort()
return
}
if isDefaultAdminCredential(user.Username, user.Password) && !credentialChangeRouteAllowed(c) {
if isAjax(c) {
pureJsonMsg(c, http.StatusForbidden, false, "Change the default admin credentials before continuing.")
} else {
c.Header("Cache-Control", "no-store")
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path")+"panel/settings")
}
c.Abort()
2023-02-09 19:18:06 +00:00
} else {
c.Next()
}
}
func isDefaultAdminCredential(username string, hashedPassword string) bool {
return username == "admin" && crypto.CheckPasswordHash(hashedPassword, "admin")
}
func credentialChangeRouteAllowed(c *gin.Context) bool {
basePath := c.GetString("base_path")
path := c.Request.URL.Path
allowedPrefixes := []string{
basePath + "panel/settings",
basePath + "panel/setting/",
basePath + "panel/csrf-token",
}
for _, prefix := range allowedPrefixes {
if strings.HasPrefix(path, prefix) {
return true
}
}
return false
}
2025-09-20 07:35:50 +00:00
// I18nWeb retrieves an internationalized message for the web interface based on the current locale.
2023-05-20 22:59:27 +00:00
func I18nWeb(c *gin.Context, name string, params ...string) string {
2023-05-20 15:19:39 +00:00
anyfunc, funcExists := c.Get("I18n")
if !funcExists {
logger.Warning("I18n function not exists in gin context!")
return ""
}
i18nFunc, _ := anyfunc.(func(i18nType locale.I18nType, key string, keyParams ...string) string)
msg := i18nFunc(locale.Web, name, params...)
return msg
2023-02-09 19:18:06 +00:00
}