api.go 3.4 KB

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