diff options
Diffstat (limited to 'web/controller/inbound.go')
| -rw-r--r-- | web/controller/inbound.go | 154 |
1 files changed, 153 insertions, 1 deletions
diff --git a/web/controller/inbound.go b/web/controller/inbound.go index c22ce192..a8003484 100644 --- a/web/controller/inbound.go +++ b/web/controller/inbound.go @@ -1,10 +1,10 @@ package controller import ( + "errors" "encoding/json" "fmt" "strconv" - "x-ui/database/model" "x-ui/web/service" "x-ui/web/session" @@ -33,9 +33,13 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) { g.POST("/clientIps/:email", a.getClientIps) g.POST("/clearClientIps/:email", a.clearClientIps) g.POST("/addClient", a.addInboundClient) + g.POST("/addGroupClient", a.addGroupInboundClient) g.POST("/:id/delClient/:clientId", a.delInboundClient) + g.POST("/delGroupClients", a.delGroupClients) g.POST("/updateClient/:clientId", a.updateInboundClient) + g.POST("/updateClients", a.updateGroupInboundClient) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) + g.POST("/resetGroupClientTraffic", a.resetGroupClientTraffic) g.POST("/resetAllTraffics", a.resetAllTraffics) g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics) g.POST("/delDepletedClients/:id", a.delDepletedClients) @@ -190,6 +194,34 @@ func (a *InboundController) addInboundClient(c *gin.Context) { } } +func (a *InboundController) addGroupInboundClient(c *gin.Context) { + var requestData []model.Inbound + + err := c.ShouldBindJSON(&requestData) + + if err != nil { + jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err) + return + } + + needRestart := true + + for _, data := range requestData { + + needRestart, err = a.inboundService.AddInboundClient(&data) + if err != nil { + jsonMsg(c, "Something went wrong!", err) + return + } + } + + jsonMsg(c, "Client(s) added", nil) + if err == nil && needRestart { + a.xrayService.SetToNeedRestart() + } + +} + func (a *InboundController) delInboundClient(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { @@ -211,6 +243,38 @@ func (a *InboundController) delInboundClient(c *gin.Context) { } } +func (a *InboundController) delGroupClients(c *gin.Context) { + var requestData []struct { + InboundID int `json:"inboundId"` + ClientID string `json:"clientId"` + } + + if err := c.ShouldBindJSON(&requestData); err != nil { + jsonMsg(c, "Invalid request data", err) + return + } + + needRestart := false + + for _, req := range requestData { + needRestartTmp, err := a.inboundService.DelInboundClient(req.InboundID, req.ClientID) + if err != nil { + jsonMsg(c, "Failed to delete client", err) + return + } + + if needRestartTmp { + needRestart = true + } + } + + jsonMsg(c, "Clients deleted successfully", nil) + + if needRestart { + a.xrayService.SetToNeedRestart() + } +} + func (a *InboundController) updateInboundClient(c *gin.Context) { clientId := c.Param("clientId") @@ -234,6 +298,56 @@ func (a *InboundController) updateInboundClient(c *gin.Context) { } } +func (a *InboundController) updateGroupInboundClient(c *gin.Context) { + var requestData []map[string]interface{} + + if err := c.ShouldBindJSON(&requestData); err != nil { + jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err) + return + } + + needRestart := false + + for _, item := range requestData { + + inboundMap, ok := item["inbound"].(map[string]interface{}) + if !ok { + jsonMsg(c, "Something went wrong!", errors.New("Failed to convert 'inbound' to map")) + return + } + + clientId, ok := item["clientId"].(string) + if !ok { + jsonMsg(c, "Something went wrong!", errors.New("Failed to convert 'clientId' to string")) + return + } + + inboundJSON, err := json.Marshal(inboundMap) + if err != nil { + jsonMsg(c, "Something went wrong!", err) + return + } + + var inboundModel model.Inbound + if err := json.Unmarshal(inboundJSON, &inboundModel); err != nil { + jsonMsg(c, "Something went wrong!", err) + return + } + + if restart, err := a.inboundService.UpdateInboundClient(&inboundModel, clientId); err != nil { + jsonMsg(c, "Something went wrong!", err) + return + } else { + needRestart = needRestart || restart + } + } + + jsonMsg(c, "Client updated", nil) + if needRestart { + a.xrayService.SetToNeedRestart() + } +} + func (a *InboundController) resetClientTraffic(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { @@ -253,6 +367,44 @@ func (a *InboundController) resetClientTraffic(c *gin.Context) { } } +func (a *InboundController) resetGroupClientTraffic(c *gin.Context) { + var requestData []struct { + InboundID int `json:"inboundId"` // Map JSON "inboundId" to struct field "InboundID" + Email string `json:"email"` // Map JSON "email" to struct field "Email" + } + + // Parse JSON body directly using ShouldBindJSON + if err := c.ShouldBindJSON(&requestData); err != nil { + jsonMsg(c, "Invalid request data", err) + return + } + + needRestart := false + + // Process each request data + for _, req := range requestData { + needRestartTmp, err := a.inboundService.ResetClientTraffic(req.InboundID, req.Email) + if err != nil { + jsonMsg(c, "Failed to reset client traffic", err) + return + } + + // If any request requires a restart, set needRestart to true + if needRestartTmp { + needRestart = true + } + } + + // Send response back to the client + jsonMsg(c, "Traffic reset for all clients", nil) + + // Restart the service if required + if needRestart { + a.xrayService.SetToNeedRestart() + } +} + + func (a *InboundController) resetAllTraffics(c *gin.Context) { err := a.inboundService.ResetAllTraffics() if err != nil { |
