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

121 lines
3.8 KiB
Go
Raw Normal View History

2023-12-04 19:20:46 +01:00
package controller
import (
2025-09-19 10:05:43 +02:00
"github.com/mhsanaei/3x-ui/v2/web/service"
2023-12-04 19:20:46 +01:00
"github.com/gin-gonic/gin"
)
2025-09-20 09:35:50 +02:00
// XraySettingController handles Xray configuration and settings operations.
2023-12-04 19:20:46 +01:00
type XraySettingController struct {
XraySettingService service.XraySettingService
SettingService service.SettingService
2023-12-05 18:13:36 +01:00
InboundService service.InboundService
OutboundService service.OutboundService
2023-12-10 12:57:39 +01:00
XrayService service.XrayService
2024-07-15 00:21:54 +02:00
WarpService service.WarpService
2023-12-04 19:20:46 +01:00
}
2025-09-20 09:35:50 +02:00
// NewXraySettingController creates a new XraySettingController and initializes its routes.
2023-12-04 19:20:46 +01:00
func NewXraySettingController(g *gin.RouterGroup) *XraySettingController {
a := &XraySettingController{}
a.initRouter(g)
return a
}
2025-09-20 09:35:50 +02:00
// initRouter sets up the routes for Xray settings management.
2023-12-04 19:20:46 +01:00
func (a *XraySettingController) initRouter(g *gin.RouterGroup) {
g = g.Group("/xray")
2025-09-17 01:08:59 +02:00
g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
g.GET("/getOutboundsTraffic", a.getOutboundsTraffic)
g.GET("/getXrayResult", a.getXrayResult)
2023-12-04 19:20:46 +01:00
g.POST("/", a.getXraySetting)
2024-01-11 09:57:21 +03:30
g.POST("/warp/:action", a.warp)
2025-09-17 01:08:59 +02:00
g.POST("/update", a.updateSetting)
2024-02-07 11:25:31 +03:30
g.POST("/resetOutboundsTraffic", a.resetOutboundsTraffic)
2023-12-04 19:20:46 +01:00
}
2025-09-20 09:35:50 +02:00
// getXraySetting retrieves the Xray configuration template and inbound tags.
2023-12-04 19:20:46 +01:00
func (a *XraySettingController) getXraySetting(c *gin.Context) {
xraySetting, err := a.SettingService.GetXrayConfigTemplate()
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
2023-12-05 18:13:36 +01:00
inboundTags, err := a.InboundService.GetInboundTags()
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
xrayResponse := "{ \"xraySetting\": " + xraySetting + ", \"inboundTags\": " + inboundTags + " }"
jsonObj(c, xrayResponse, nil)
2023-12-04 19:20:46 +01:00
}
2025-09-20 09:35:50 +02:00
// updateSetting updates the Xray configuration settings.
2023-12-04 19:20:46 +01:00
func (a *XraySettingController) updateSetting(c *gin.Context) {
xraySetting := c.PostForm("xraySetting")
err := a.XraySettingService.SaveXraySetting(xraySetting)
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
}
2025-09-20 09:35:50 +02:00
// getDefaultXrayConfig retrieves the default Xray configuration.
2023-12-04 19:20:46 +01:00
func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) {
defaultJsonConfig, err := a.SettingService.GetDefaultXrayConfig()
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
jsonObj(c, defaultJsonConfig, nil)
}
2023-12-10 12:57:39 +01:00
2025-09-20 09:35:50 +02:00
// getXrayResult retrieves the current Xray service result.
2023-12-10 12:57:39 +01:00
func (a *XraySettingController) getXrayResult(c *gin.Context) {
jsonObj(c, a.XrayService.GetXrayResult(), nil)
}
2024-01-11 09:57:21 +03:30
2025-09-20 09:35:50 +02:00
// warp handles Warp-related operations based on the action parameter.
2024-01-11 09:57:21 +03:30
func (a *XraySettingController) warp(c *gin.Context) {
action := c.Param("action")
var resp string
var err error
switch action {
case "data":
2024-07-15 00:21:54 +02:00
resp, err = a.WarpService.GetWarpData()
case "del":
err = a.WarpService.DelWarpData()
2024-01-11 09:57:21 +03:30
case "config":
2024-07-15 00:21:54 +02:00
resp, err = a.WarpService.GetWarpConfig()
2024-01-11 09:57:21 +03:30
case "reg":
skey := c.PostForm("privateKey")
pkey := c.PostForm("publicKey")
2024-07-15 00:21:54 +02:00
resp, err = a.WarpService.RegWarp(skey, pkey)
2024-01-11 09:57:21 +03:30
case "license":
license := c.PostForm("license")
2024-07-15 00:21:54 +02:00
resp, err = a.WarpService.SetWarpLicense(license)
2024-01-11 09:57:21 +03:30
}
jsonObj(c, resp, err)
}
2025-09-20 09:35:50 +02:00
// getOutboundsTraffic retrieves the traffic statistics for outbounds.
func (a *XraySettingController) getOutboundsTraffic(c *gin.Context) {
outboundsTraffic, err := a.OutboundService.GetOutboundsTraffic()
if err != nil {
2025-05-09 10:46:29 +07:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getOutboundTrafficError"), err)
return
}
jsonObj(c, outboundsTraffic, nil)
}
2024-02-07 11:25:31 +03:30
2025-09-20 09:35:50 +02:00
// resetOutboundsTraffic resets the traffic statistics for the specified outbound tag.
2024-02-07 11:25:31 +03:30
func (a *XraySettingController) resetOutboundsTraffic(c *gin.Context) {
tag := c.PostForm("tag")
err := a.OutboundService.ResetOutboundTraffic(tag)
if err != nil {
2025-05-09 10:46:29 +07:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.resetOutboundTrafficError"), err)
2024-02-07 11:25:31 +03:30
return
}
jsonObj(c, "", nil)
}