1
0

api.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package controller
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/mhsanaei/3x-ui/v3/web/middleware"
  6. "github.com/mhsanaei/3x-ui/v3/web/service"
  7. "github.com/mhsanaei/3x-ui/v3/web/session"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // APIController handles the main API routes for the 3x-ui panel, including inbounds and server management.
  11. type APIController struct {
  12. BaseController
  13. inboundController *InboundController
  14. serverController *ServerController
  15. nodeController *NodeController
  16. settingController *SettingController
  17. xraySettingController *XraySettingController
  18. settingService service.SettingService
  19. userService service.UserService
  20. apiTokenService service.ApiTokenService
  21. Tgbot service.Tgbot
  22. }
  23. // NewAPIController creates a new APIController instance and initializes its routes.
  24. func NewAPIController(g *gin.RouterGroup, customGeo *service.CustomGeoService) *APIController {
  25. a := &APIController{}
  26. a.initRouter(g, customGeo)
  27. return a
  28. }
  29. func (a *APIController) checkAPIAuth(c *gin.Context) {
  30. auth := c.GetHeader("Authorization")
  31. if after, ok := strings.CutPrefix(auth, "Bearer "); ok {
  32. tok := after
  33. if a.apiTokenService.Match(tok) {
  34. if u, err := a.userService.GetFirstUser(); err == nil {
  35. session.SetAPIAuthUser(c, u)
  36. }
  37. c.Set("api_authed", true)
  38. c.Next()
  39. return
  40. }
  41. }
  42. if !session.IsLogin(c) {
  43. if c.GetHeader("X-Requested-With") == "XMLHttpRequest" {
  44. c.AbortWithStatus(http.StatusUnauthorized)
  45. } else {
  46. c.AbortWithStatus(http.StatusNotFound)
  47. }
  48. return
  49. }
  50. c.Next()
  51. }
  52. // initRouter sets up the API routes for inbounds, server, and other endpoints.
  53. func (a *APIController) initRouter(g *gin.RouterGroup, customGeo *service.CustomGeoService) {
  54. // Main API group
  55. api := g.Group("/panel/api")
  56. api.Use(a.checkAPIAuth)
  57. api.Use(middleware.CSRFMiddleware())
  58. // Inbounds API
  59. inbounds := api.Group("/inbounds")
  60. a.inboundController = NewInboundController(inbounds)
  61. clients := api.Group("/clients")
  62. NewClientController(clients)
  63. NewGroupController(clients)
  64. // Server API
  65. server := api.Group("/server")
  66. a.serverController = NewServerController(server)
  67. // Nodes API — multi-panel management
  68. nodes := api.Group("/nodes")
  69. a.nodeController = NewNodeController(nodes)
  70. NewCustomGeoController(api.Group("/custom-geo"), customGeo)
  71. // Settings + Xray config management live under the API surface too, so the
  72. // same API token drives them. Paths are /panel/api/setting/* and
  73. // /panel/api/xray/*.
  74. a.settingController = NewSettingController(api)
  75. a.xraySettingController = NewXraySettingController(api)
  76. // Extra routes
  77. api.POST("/backuptotgbot", a.BackuptoTgbot)
  78. }
  79. // BackuptoTgbot sends a backup of the panel data to Telegram bot admins.
  80. func (a *APIController) BackuptoTgbot(c *gin.Context) {
  81. a.Tgbot.SendBackupToAdmins()
  82. }