1
0

base.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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.Header("Cache-Control", "no-store")
  20. c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
  21. }
  22. c.Abort()
  23. } else {
  24. c.Next()
  25. }
  26. }
  27. // I18nWeb retrieves an internationalized message for the web interface based on the current locale.
  28. func I18nWeb(c *gin.Context, name string, params ...string) string {
  29. anyfunc, funcExists := c.Get("I18n")
  30. if !funcExists {
  31. logger.Warning("I18n function not exists in gin context!")
  32. return ""
  33. }
  34. i18nFunc, _ := anyfunc.(func(i18nType locale.I18nType, key string, keyParams ...string) string)
  35. msg := i18nFunc(locale.Web, name, params...)
  36. return msg
  37. }