xui.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package controller
  2. import (
  3. "net/http"
  4. "github.com/mhsanaei/3x-ui/v3/web/entity"
  5. "github.com/mhsanaei/3x-ui/v3/web/middleware"
  6. "github.com/mhsanaei/3x-ui/v3/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. g.GET("/api-docs", a.apiDocs)
  32. // SPA pages built by Vite don't have a server-rendered <meta name="csrf-token">,
  33. // so they fetch the session token via this endpoint at startup and replay it
  34. // on subsequent unsafe requests through axios.
  35. g.GET("/csrf-token", a.csrfToken)
  36. a.settingController = NewSettingController(g)
  37. a.xraySettingController = NewXraySettingController(g)
  38. }
  39. // All four panel pages now serve the Vue 3 builds from web/dist/
  40. // instead of rendering the legacy Go templates. Each handler is a
  41. // thin wrapper around serveDistPage so the basePath injection +
  42. // no-cache headers stay centralised.
  43. // index renders the main panel index page.
  44. func (a *XUIController) index(c *gin.Context) {
  45. serveDistPage(c, "index.html")
  46. }
  47. // inbounds renders the inbounds management page.
  48. func (a *XUIController) inbounds(c *gin.Context) {
  49. serveDistPage(c, "inbounds.html")
  50. }
  51. // nodes renders the multi-panel nodes management page.
  52. func (a *XUIController) nodes(c *gin.Context) {
  53. serveDistPage(c, "nodes.html")
  54. }
  55. // settings renders the settings management page.
  56. func (a *XUIController) settings(c *gin.Context) {
  57. serveDistPage(c, "settings.html")
  58. }
  59. // xraySettings renders the Xray settings page.
  60. func (a *XUIController) xraySettings(c *gin.Context) {
  61. serveDistPage(c, "xray.html")
  62. }
  63. // apiDocs renders the in-panel API documentation page.
  64. func (a *XUIController) apiDocs(c *gin.Context) {
  65. serveDistPage(c, "api-docs.html")
  66. }
  67. // csrfToken returns the session CSRF token to authenticated SPA clients.
  68. // The endpoint is GET (a safe method) so it bypasses CSRFMiddleware itself,
  69. // but checkLogin still gates the response — anonymous callers get 401/redirect.
  70. func (a *XUIController) csrfToken(c *gin.Context) {
  71. token, err := session.EnsureCSRFToken(c)
  72. if err != nil {
  73. c.JSON(http.StatusInternalServerError, entity.Msg{Success: false, Msg: err.Error()})
  74. return
  75. }
  76. c.JSON(http.StatusOK, entity.Msg{Success: true, Obj: token})
  77. }