Files
trihuy-russian/web/controller/api.go
T

59 lines
1.5 KiB
Go
Raw Normal View History

2023-02-09 22:48:06 +03:30
package controller
2023-05-19 00:31:05 +03:30
import (
"net/http"
2025-09-19 10:05:43 +02:00
"github.com/mhsanaei/3x-ui/v2/web/service"
"github.com/mhsanaei/3x-ui/v2/web/session"
2023-05-19 00:31:05 +03:30
"github.com/gin-gonic/gin"
)
2023-02-09 22:48:06 +03:30
2025-09-20 09:35:50 +02:00
// APIController handles the main API routes for the 3x-ui panel, including inbounds and server management.
2023-04-02 02:30:15 +04:30
type APIController struct {
2023-04-09 23:13:18 +03:30
BaseController
inboundController *InboundController
2025-09-09 01:22:43 +02:00
serverController *ServerController
2023-05-19 00:31:05 +03:30
Tgbot service.Tgbot
2023-02-09 22:48:06 +03:30
}
2025-09-20 09:35:50 +02:00
// NewAPIController creates a new APIController instance and initializes its routes.
2023-02-09 22:48:06 +03:30
func NewAPIController(g *gin.RouterGroup) *APIController {
2023-04-09 23:13:18 +03:30
a := &APIController{}
a.initRouter(g)
return a
2023-02-09 22:48:06 +03:30
}
// checkAPIAuth is a middleware that returns 404 for unauthenticated API requests
// to hide the existence of API endpoints from unauthorized users
func (a *APIController) checkAPIAuth(c *gin.Context) {
if !session.IsLogin(c) {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.Next()
}
2025-09-20 09:35:50 +02:00
// initRouter sets up the API routes for inbounds, server, and other endpoints.
2023-02-09 22:48:06 +03:30
func (a *APIController) initRouter(g *gin.RouterGroup) {
2025-09-09 01:22:43 +02:00
// Main API group
api := g.Group("/panel/api")
api.Use(a.checkAPIAuth)
2025-09-09 01:22:43 +02:00
// Inbounds API
inbounds := api.Group("/inbounds")
a.inboundController = NewInboundController(inbounds)
// Server API
server := api.Group("/server")
a.serverController = NewServerController(server)
// Extra routes
api.GET("/backuptotgbot", a.BackuptoTgbot)
2023-04-25 18:46:09 +03:30
}
2023-05-19 00:31:05 +03:30
2025-09-20 09:35:50 +02:00
// BackuptoTgbot sends a backup of the panel data to Telegram bot admins.
2025-09-09 01:22:43 +02:00
func (a *APIController) BackuptoTgbot(c *gin.Context) {
2023-05-21 03:29:27 +04:30
a.Tgbot.SendBackupToAdmins()
2023-05-19 00:31:05 +03:30
}