From 7a229b27f553e9fa4e43c8ff1d22dbc0d8ac4ee0 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Mon, 24 Apr 2023 11:10:59 +0200 Subject: [PATCH] Better client delete + api #218 --- web/controller/api.go | 2 +- web/controller/inbound.go | 10 +++------- web/html/xui/inbounds.html | 7 ++----- web/service/inbound.go | 25 +++++++++++++++++++------ 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/web/controller/api.go b/web/controller/api.go index 652fe640..f756990b 100644 --- a/web/controller/api.go +++ b/web/controller/api.go @@ -24,7 +24,7 @@ func (a *APIController) initRouter(g *gin.RouterGroup) { g.POST("/del/:id", a.delInbound) g.POST("/update/:id", a.updateInbound) g.POST("/addClient/", a.addInboundClient) - g.POST("/delClient/:email", a.delInboundClient) + g.POST("/:id/delClient/:clientId", a.delInboundClient) g.POST("/updateClient/:index", a.updateInboundClient) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) g.POST("/resetAllTraffics", a.resetAllTraffics) diff --git a/web/controller/inbound.go b/web/controller/inbound.go index 93bdbc14..2f66b247 100644 --- a/web/controller/inbound.go +++ b/web/controller/inbound.go @@ -32,7 +32,7 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) { g.POST("/del/:id", a.delInbound) g.POST("/update/:id", a.updateInbound) g.POST("/addClient", a.addInboundClient) - g.POST("/:id/delClient/:index", a.delInboundClient) + g.POST("/:id/delClient/:clientId", a.delInboundClient) g.POST("/updateClient/:index", a.updateInboundClient) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) g.POST("/resetAllTraffics", a.resetAllTraffics) @@ -162,13 +162,9 @@ func (a *InboundController) delInboundClient(c *gin.Context) { jsonMsg(c, I18n(c, "pages.inbounds.revise"), err) return } - index, err := strconv.Atoi(c.Param("index")) - if err != nil { - jsonMsg(c, I18n(c, "pages.inbounds.revise"), err) - return - } + clientId := c.Param("clientId") - err = a.inboundService.DelInboundClient(id, index) + err = a.inboundService.DelInboundClient(id, clientId) if err != nil { jsonMsg(c, "something worng!", err) return diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html index a3ddf3d6..0f18285a 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -612,17 +612,14 @@ }, delClient(dbInboundId,client) { dbInbound = this.dbInbounds.find(row => row.id === dbInboundId); - newDbInbound = new DBInbound(dbInbound); - inbound = newDbInbound.toInbound(); - clients = this.getClients(dbInbound.protocol, inbound.settings); - index = this.findIndexOfClient(clients, client); + clientId = dbInbound.protocol == "trojan" ? client.password : client.id; this.$confirm({ title: '{{ i18n "pages.inbounds.deleteInbound"}}', content: '{{ i18n "pages.inbounds.deleteInboundContent"}}', class: siderDrawer.isDarkTheme ? darkClass : '', okText: '{{ i18n "delete"}}', cancelText: '{{ i18n "cancel"}}', - onOk: () => this.submit(`/xui/inbound/${dbInboundId}/delClient/${index}`), + onOk: () => this.submit(`/xui/inbound/${dbInboundId}/delClient/${clientId}`), }); }, getClients(protocol, clientSettings) { diff --git a/web/service/inbound.go b/web/service/inbound.go index 918745c5..a8fba2b4 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -300,7 +300,7 @@ func (s *InboundService) AddInboundClient(data *model.Inbound) error { return db.Save(oldInbound).Error } -func (s *InboundService) DelInboundClient(inboundId int, index int) error { +func (s *InboundService) DelInboundClient(inboundId int, clientId string) error { oldInbound, err := s.GetInbound(inboundId) if err != nil { logger.Error("Load Old Data Error") @@ -312,12 +312,25 @@ func (s *InboundService) DelInboundClient(inboundId int, index int) error { return err } - inerfaceClients := settings["clients"].([]interface{}) - client := inerfaceClients[index].(map[string]interface{}) - email := client["email"].(string) - inerfaceClients = append(inerfaceClients[:index], inerfaceClients[index+1:]...) + email := "" + client_key := "id" + if oldInbound.Protocol == "trojan" { + client_key = "password" + } - settings["clients"] = inerfaceClients + inerfaceClients := settings["clients"].([]interface{}) + var newClients []interface{} + for _, client := range inerfaceClients { + c := client.(map[string]interface{}) + c_id := c[client_key].(string) + if c_id == clientId { + email = c["email"].(string) + } else { + newClients = append(newClients, client) + } + } + + settings["clients"] = newClients newSettings, err := json.MarshalIndent(settings, "", " ") if err != nil { return err