api.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package controller
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/mhsanaei/3x-ui/v3/internal/web/middleware"
  6. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  7. "github.com/mhsanaei/3x-ui/v3/internal/web/service/panel"
  8. "github.com/mhsanaei/3x-ui/v3/internal/web/service/tgbot"
  9. "github.com/mhsanaei/3x-ui/v3/internal/web/session"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // APIController handles the main API routes for the 3x-ui panel, including inbounds and server management.
  13. type APIController struct {
  14. BaseController
  15. inboundController *InboundController
  16. serverController *ServerController
  17. nodeController *NodeController
  18. hostController *HostController
  19. settingController *SettingController
  20. xraySettingController *XraySettingController
  21. settingService service.SettingService
  22. userService panel.UserService
  23. apiTokenService panel.ApiTokenService
  24. Tgbot tgbot.Tgbot
  25. }
  26. // NewAPIController creates a new APIController instance and initializes its routes.
  27. func NewAPIController(g *gin.RouterGroup) *APIController {
  28. a := &APIController{}
  29. a.initRouter(g)
  30. return a
  31. }
  32. func (a *APIController) checkAPIAuth(c *gin.Context) {
  33. // A verified client certificate (a completed mTLS handshake) authenticates
  34. // the caller, equivalent to a valid bearer token. api_authed must be set so
  35. // the CSRF middleware lets cert-authed mutations through.
  36. if c.Request.TLS != nil && len(c.Request.TLS.VerifiedChains) > 0 {
  37. if u, err := a.userService.GetFirstUser(); err == nil {
  38. session.SetAPIAuthUser(c, u)
  39. }
  40. c.Set("api_authed", true)
  41. c.Next()
  42. return
  43. }
  44. auth := c.GetHeader("Authorization")
  45. if after, ok := strings.CutPrefix(auth, "Bearer "); ok {
  46. tok := after
  47. if a.apiTokenService.Match(tok) {
  48. if u, err := a.userService.GetFirstUser(); err == nil {
  49. session.SetAPIAuthUser(c, u)
  50. }
  51. c.Set("api_authed", true)
  52. c.Next()
  53. return
  54. }
  55. }
  56. if !session.IsLogin(c) {
  57. if c.GetHeader("X-Requested-With") == "XMLHttpRequest" {
  58. c.AbortWithStatus(http.StatusUnauthorized)
  59. } else {
  60. c.AbortWithStatus(http.StatusNotFound)
  61. }
  62. return
  63. }
  64. c.Next()
  65. }
  66. // initRouter sets up the API routes for inbounds, server, and other endpoints.
  67. func (a *APIController) initRouter(g *gin.RouterGroup) {
  68. // Main API group
  69. api := g.Group("/panel/api")
  70. api.Use(a.checkAPIAuth)
  71. // Decode + verify the node config envelope (zstd + X-Config-Sha256) and
  72. // advertise support, before CSRF/handlers read the body.
  73. api.Use(middleware.ConfigEnvelopeMiddleware())
  74. api.Use(middleware.CSRFMiddleware())
  75. // Inbounds API
  76. inbounds := api.Group("/inbounds")
  77. a.inboundController = NewInboundController(inbounds)
  78. clients := api.Group("/clients")
  79. NewClientController(clients)
  80. NewGroupController(clients)
  81. // Server API
  82. server := api.Group("/server")
  83. a.serverController = NewServerController(server)
  84. // Nodes API — multi-panel management
  85. nodes := api.Group("/nodes")
  86. a.nodeController = NewNodeController(nodes)
  87. // Hosts API — per-inbound override endpoints for subscription links
  88. hosts := api.Group("/hosts")
  89. a.hostController = NewHostController(hosts)
  90. // Settings + Xray config management live under the API surface too, so the
  91. // same API token drives them. Paths are /panel/api/setting/* and
  92. // /panel/api/xray/*.
  93. a.settingController = NewSettingController(api)
  94. a.xraySettingController = NewXraySettingController(api)
  95. // Extra routes
  96. api.POST("/backuptotgbot", a.BackuptoTgbot)
  97. }
  98. // BackuptoTgbot sends a backup of the panel data to Telegram bot admins.
  99. func (a *APIController) BackuptoTgbot(c *gin.Context) {
  100. a.Tgbot.SendBackupToAdmins()
  101. }