mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 12:04:44 +03:00
142a1ab28e
The target scanner finds good REALITY targets, but nothing noticed when the one in use stopped working: every client of the inbound then failed until an admin edited it by hand. A REALITY inbound can now keep spare targets. The panel checks each watched target every 10 minutes with the scanner's own probe, and after three failed checks in a row an inbound with automatic replacement on moves to the first spare that passes both that probe and a real REALITY connection through a throwaway Xray server and client. Only target and serverNames change, saved through UpdateInbound like an admin's edit; nothing changes when no spare passes, and node inbounds are neither checked nor switched from the master. Off until spares are added and automatic replacement is turned on.
94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package controller
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// RealityWatchController sets the spare targets of REALITY inbounds and switches them.
|
|
type RealityWatchController struct {
|
|
realityWatchService service.RealityWatchService
|
|
}
|
|
|
|
func NewRealityWatchController(g *gin.RouterGroup) *RealityWatchController {
|
|
a := &RealityWatchController{}
|
|
g.GET("/list", a.list)
|
|
g.GET("/status/:id", a.status)
|
|
g.POST("/save/:id", a.save)
|
|
g.POST("/switch/:id", a.switchTarget)
|
|
g.POST("/test", a.test)
|
|
return a
|
|
}
|
|
|
|
type realityWatchTargetRequest struct {
|
|
Target string `json:"target"`
|
|
}
|
|
|
|
func (a *RealityWatchController) list(c *gin.Context) {
|
|
jsonObj(c, a.realityWatchService.List(), nil)
|
|
}
|
|
|
|
func (a *RealityWatchController) status(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.realityWatch.toasts.failed"), err)
|
|
return
|
|
}
|
|
jsonObj(c, a.realityWatchService.Status(id), nil)
|
|
}
|
|
|
|
func (a *RealityWatchController) save(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.realityWatch.toasts.failed"), err)
|
|
return
|
|
}
|
|
var entry service.RealityWatchEntry
|
|
if err := c.ShouldBindJSON(&entry); err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.realityWatch.toasts.failed"), err)
|
|
return
|
|
}
|
|
if err := a.realityWatchService.SaveConfig(id, entry); err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.realityWatch.toasts.failed"), err)
|
|
return
|
|
}
|
|
jsonObj(c, a.realityWatchService.Status(id), nil)
|
|
}
|
|
|
|
// switchTarget answers with the status so the editor gets the saved target and SNIs in one step;
|
|
// a form left on the old ones would put them back on its next Save.
|
|
func (a *RealityWatchController) switchTarget(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.realityWatch.toasts.switchFailed"), err)
|
|
return
|
|
}
|
|
var req realityWatchTargetRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.realityWatch.toasts.switchFailed"), err)
|
|
return
|
|
}
|
|
if _, err := a.realityWatchService.SwitchTo(c.Request.Context(), id, req.Target); err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.realityWatch.toasts.switchFailed"), err)
|
|
return
|
|
}
|
|
jsonObj(c, a.realityWatchService.Status(id), nil)
|
|
}
|
|
|
|
func (a *RealityWatchController) test(c *gin.Context) {
|
|
var req realityWatchTargetRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.realityWatch.toasts.failed"), err)
|
|
return
|
|
}
|
|
res, err := a.realityWatchService.TestTarget(c.Request.Context(), req.Target)
|
|
if err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.realityWatch.toasts.failed"), err)
|
|
return
|
|
}
|
|
jsonObj(c, res, nil)
|
|
}
|