From b4b7ec565e513d597eda91cff56a4526005a8ebc Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Sat, 22 Apr 2023 11:21:34 +0200 Subject: [PATCH] simplify API: del client #218 --- web/controller/inbound.go | 13 ++++++++----- web/html/xui/inbounds.html | 7 +------ web/service/inbound.go | 35 +++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/web/controller/inbound.go b/web/controller/inbound.go index 82b227fe..93bdbc14 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("/delClient/:email", a.delInboundClient) + g.POST("/:id/delClient/:index", a.delInboundClient) g.POST("/updateClient/:index", a.updateInboundClient) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) g.POST("/resetAllTraffics", a.resetAllTraffics) @@ -157,15 +157,18 @@ func (a *InboundController) addInboundClient(c *gin.Context) { } func (a *InboundController) delInboundClient(c *gin.Context) { - email := c.Param("email") - inbound := &model.Inbound{} - err := c.ShouldBind(inbound) + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + 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 } - err = a.inboundService.DelInboundClient(inbound, email) + err = a.inboundService.DelInboundClient(id, index) if err != nil { jsonMsg(c, "something worng!", err) return diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html index 90876777..a3ddf3d6 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -616,18 +616,13 @@ inbound = newDbInbound.toInbound(); clients = this.getClients(dbInbound.protocol, inbound.settings); index = this.findIndexOfClient(clients, client); - clients.splice(index, 1); - const data = { - id: dbInboundId, - settings: inbound.settings.toString(), - }; 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/delClient/' + client.email, data), + onOk: () => this.submit(`/xui/inbound/${dbInboundId}/delClient/${index}`), }); }, getClients(protocol, clientSettings) { diff --git a/web/service/inbound.go b/web/service/inbound.go index 2c9fea12..687adc40 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -300,22 +300,37 @@ func (s *InboundService) AddInboundClient(data *model.Inbound) error { return db.Save(oldInbound).Error } -func (s *InboundService) DelInboundClient(inbound *model.Inbound, email string) error { - db := database.GetDB() - err := s.DelClientStat(db, email) - if err != nil { - logger.Error("Delete stats Data Error") - return err - } - - oldInbound, err := s.GetInbound(inbound.Id) +func (s *InboundService) DelInboundClient(inboundId int, index int) error { + oldInbound, err := s.GetInbound(inboundId) if err != nil { logger.Error("Load Old Data Error") return err } + var settings map[string]interface{} + err = json.Unmarshal([]byte(oldInbound.Settings), &settings) + if err != nil { + return err + } - oldInbound.Settings = inbound.Settings + inerfaceClients := settings["clients"].([]interface{}) + client := inerfaceClients[index].(map[string]interface{}) + email := client["email"].(string) + inerfaceClients = append(inerfaceClients[:index], inerfaceClients[index+1:]...) + settings["clients"] = inerfaceClients + newSettings, err := json.MarshalIndent(settings, "", " ") + if err != nil { + return err + } + + oldInbound.Settings = string(newSettings) + + db := database.GetDB() + err = s.DelClientStat(db, email) + if err != nil { + logger.Error("Delete stats Data Error") + return err + } return db.Save(oldInbound).Error }