api.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. auth := c.GetHeader("Authorization")
  33. if after, ok := strings.CutPrefix(auth, "Bearer "); ok {
  34. tok := after
  35. if a.apiTokenService.Match(tok) {
  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. }
  44. if !session.IsLogin(c) {
  45. if c.GetHeader("X-Requested-With") == "XMLHttpRequest" {
  46. c.AbortWithStatus(http.StatusUnauthorized)
  47. } else {
  48. c.AbortWithStatus(http.StatusNotFound)
  49. }
  50. return
  51. }
  52. c.Next()
  53. }
  54. // initRouter sets up the API routes for inbounds, server, and other endpoints.
  55. func (a *APIController) initRouter(g *gin.RouterGroup) {
  56. // Main API group
  57. api := g.Group("/panel/api")
  58. api.Use(a.checkAPIAuth)
  59. api.Use(middleware.CSRFMiddleware())
  60. // Inbounds API
  61. inbounds := api.Group("/inbounds")
  62. a.inboundController = NewInboundController(inbounds)
  63. clients := api.Group("/clients")
  64. NewClientController(clients)
  65. NewGroupController(clients)
  66. // Server API
  67. server := api.Group("/server")
  68. a.serverController = NewServerController(server)
  69. // Nodes API — multi-panel management
  70. nodes := api.Group("/nodes")
  71. a.nodeController = NewNodeController(nodes)
  72. // Settings + Xray config management live under the API surface too, so the
  73. // same API token drives them. Paths are /panel/api/setting/* and
  74. // /panel/api/xray/*.
  75. a.settingController = NewSettingController(api)
  76. a.xraySettingController = NewXraySettingController(api)
  77. // Extra routes
  78. api.POST("/backuptotgbot", a.BackuptoTgbot)
  79. }
  80. // BackuptoTgbot sends a backup of the panel data to Telegram bot admins.
  81. func (a *APIController) BackuptoTgbot(c *gin.Context) {
  82. a.Tgbot.SendBackupToAdmins()
  83. }