1
0

xui.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package controller
  2. import (
  3. "net/http"
  4. "github.com/mhsanaei/3x-ui/v2/web/entity"
  5. "github.com/mhsanaei/3x-ui/v2/web/middleware"
  6. "github.com/mhsanaei/3x-ui/v2/web/session"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // XUIController is the main controller for the X-UI panel, managing sub-controllers.
  10. type XUIController struct {
  11. BaseController
  12. settingController *SettingController
  13. xraySettingController *XraySettingController
  14. }
  15. // NewXUIController creates a new XUIController and initializes its routes.
  16. func NewXUIController(g *gin.RouterGroup) *XUIController {
  17. a := &XUIController{}
  18. a.initRouter(g)
  19. return a
  20. }
  21. // initRouter sets up the main panel routes and initializes sub-controllers.
  22. func (a *XUIController) initRouter(g *gin.RouterGroup) {
  23. g = g.Group("/panel")
  24. g.Use(a.checkLogin)
  25. g.Use(middleware.CSRFMiddleware())
  26. g.GET("/", a.index)
  27. g.GET("/inbounds", a.inbounds)
  28. g.GET("/nodes", a.nodes)
  29. g.GET("/settings", a.settings)
  30. g.GET("/xray", a.xraySettings)
  31. // SPA pages built by Vite don't have a server-rendered <meta name="csrf-token">,
  32. // so they fetch the session token via this endpoint at startup and replay it
  33. // on subsequent unsafe requests through axios.
  34. g.GET("/csrf-token", a.csrfToken)
  35. a.settingController = NewSettingController(g)
  36. a.xraySettingController = NewXraySettingController(g)
  37. }
  38. // All four panel pages now serve the Vue 3 builds from web/dist/
  39. // instead of rendering the legacy Go templates. Each handler is a
  40. // thin wrapper around serveDistPage so the basePath injection +
  41. // no-cache headers stay centralised.
  42. // index renders the main panel index page.
  43. func (a *XUIController) index(c *gin.Context) {
  44. serveDistPage(c, "index.html")
  45. }
  46. // inbounds renders the inbounds management page.
  47. func (a *XUIController) inbounds(c *gin.Context) {
  48. serveDistPage(c, "inbounds.html")
  49. }
  50. // nodes renders the multi-panel nodes management page.
  51. func (a *XUIController) nodes(c *gin.Context) {
  52. serveDistPage(c, "nodes.html")
  53. }
  54. // settings renders the settings management page.
  55. func (a *XUIController) settings(c *gin.Context) {
  56. serveDistPage(c, "settings.html")
  57. }
  58. // xraySettings renders the Xray settings page.
  59. func (a *XUIController) xraySettings(c *gin.Context) {
  60. serveDistPage(c, "xray.html")
  61. }
  62. // csrfToken returns the session CSRF token to authenticated SPA clients.
  63. // The endpoint is GET (a safe method) so it bypasses CSRFMiddleware itself,
  64. // but checkLogin still gates the response — anonymous callers get 401/redirect.
  65. func (a *XUIController) csrfToken(c *gin.Context) {
  66. token, err := session.EnsureCSRFToken(c)
  67. if err != nil {
  68. c.JSON(http.StatusInternalServerError, entity.Msg{Success: false, Msg: err.Error()})
  69. return
  70. }
  71. c.JSON(http.StatusOK, entity.Msg{Success: true, Obj: token})
  72. }