2023-02-09 11:18:06 -08:00
|
|
|
package controller
|
|
|
|
|
|
2023-05-18 14:01:05 -07:00
|
|
|
import (
|
2025-09-24 02:25:35 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
2026-05-07 14:36:11 -07:00
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/middleware"
|
2025-09-19 01:05:43 -07:00
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/service"
|
2025-09-24 02:25:35 -07:00
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/session"
|
2023-05-18 14:01:05 -07:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
2023-02-09 11:18:06 -08:00
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
// APIController handles the main API routes for the 3x-ui panel, including inbounds and server management.
|
2023-04-01 15:00:15 -07:00
|
|
|
type APIController struct {
|
2023-04-09 12:43:18 -07:00
|
|
|
BaseController
|
|
|
|
|
inboundController *InboundController
|
2025-09-08 16:22:43 -07:00
|
|
|
serverController *ServerController
|
2023-05-18 14:01:05 -07:00
|
|
|
Tgbot service.Tgbot
|
2023-02-09 11:18:06 -08:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
// NewAPIController creates a new APIController instance and initializes its routes.
|
2026-04-19 12:24:24 -07:00
|
|
|
func NewAPIController(g *gin.RouterGroup, customGeo *service.CustomGeoService) *APIController {
|
2023-04-09 12:43:18 -07:00
|
|
|
a := &APIController{}
|
2026-04-19 12:24:24 -07:00
|
|
|
a.initRouter(g, customGeo)
|
2023-04-09 12:43:18 -07:00
|
|
|
return a
|
2023-02-09 11:18:06 -08:00
|
|
|
}
|
|
|
|
|
|
2025-09-24 02:25:35 -07:00
|
|
|
// 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 00:35:50 -07:00
|
|
|
// initRouter sets up the API routes for inbounds, server, and other endpoints.
|
2026-04-19 12:24:24 -07:00
|
|
|
func (a *APIController) initRouter(g *gin.RouterGroup, customGeo *service.CustomGeoService) {
|
2025-09-08 16:22:43 -07:00
|
|
|
// Main API group
|
|
|
|
|
api := g.Group("/panel/api")
|
2025-09-24 02:25:35 -07:00
|
|
|
api.Use(a.checkAPIAuth)
|
2026-05-07 14:36:11 -07:00
|
|
|
api.Use(middleware.CSRFMiddleware())
|
2023-02-09 11:18:06 -08:00
|
|
|
|
2025-09-08 16:22:43 -07:00
|
|
|
// Inbounds API
|
|
|
|
|
inbounds := api.Group("/inbounds")
|
|
|
|
|
a.inboundController = NewInboundController(inbounds)
|
2023-05-13 14:11:18 -07:00
|
|
|
|
2025-09-08 16:22:43 -07:00
|
|
|
// Server API
|
|
|
|
|
server := api.Group("/server")
|
|
|
|
|
a.serverController = NewServerController(server)
|
2023-05-13 14:11:18 -07:00
|
|
|
|
2026-04-19 12:24:24 -07:00
|
|
|
NewCustomGeoController(api.Group("/custom-geo"), customGeo)
|
|
|
|
|
|
2025-09-08 16:22:43 -07:00
|
|
|
// Extra routes
|
|
|
|
|
api.GET("/backuptotgbot", a.BackuptoTgbot)
|
2023-04-25 08:16:09 -07:00
|
|
|
}
|
2023-05-18 14:01:05 -07:00
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
// BackuptoTgbot sends a backup of the panel data to Telegram bot admins.
|
2025-09-08 16:22:43 -07:00
|
|
|
func (a *APIController) BackuptoTgbot(c *gin.Context) {
|
2023-05-20 15:59:27 -07:00
|
|
|
a.Tgbot.SendBackupToAdmins()
|
2023-05-18 14:01:05 -07:00
|
|
|
}
|