1
0

xui.go 3.0 KB

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