base.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Package controller provides HTTP request handlers and controllers for the 3x-ui web management panel.
  2. // It handles routing, authentication, and API endpoints for managing Xray inbounds, settings, and more.
  3. package controller
  4. import (
  5. "net/http"
  6. "github.com/mhsanaei/3x-ui/v2/logger"
  7. "github.com/mhsanaei/3x-ui/v2/web/locale"
  8. "github.com/mhsanaei/3x-ui/v2/web/session"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // BaseController provides common functionality for all controllers, including authentication checks.
  12. type BaseController struct{}
  13. // checkLogin is a middleware that verifies user authentication and handles unauthorized access.
  14. func (a *BaseController) checkLogin(c *gin.Context) {
  15. if !session.IsLogin(c) {
  16. if isAjax(c) {
  17. pureJsonMsg(c, http.StatusUnauthorized, false, I18nWeb(c, "pages.login.loginAgain"))
  18. } else {
  19. c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
  20. }
  21. c.Abort()
  22. } else {
  23. c.Next()
  24. }
  25. }
  26. // I18nWeb retrieves an internationalized message for the web interface based on the current locale.
  27. func I18nWeb(c *gin.Context, name string, params ...string) string {
  28. anyfunc, funcExists := c.Get("I18n")
  29. if !funcExists {
  30. logger.Warning("I18n function not exists in gin context!")
  31. return ""
  32. }
  33. i18nFunc, _ := anyfunc.(func(i18nType locale.I18nType, key string, keyParams ...string) string)
  34. msg := i18nFunc(locale.Web, name, params...)
  35. return msg
  36. }