From afd369993a304e5b612ea4dfcd49d8bfc58b7cb7 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Wed, 1 Mar 2023 13:57:48 +0100 Subject: [PATCH] Fix Bug in deleting the client alireza0/x-ui#20 --- web/controller/inbound.go | 14 ++++++-------- web/html/xui/inbounds.html | 19 +++++++++++++++++-- web/service/inbound.go | 30 ++++++++++++------------------ 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/web/controller/inbound.go b/web/controller/inbound.go index 355bfd31..d15c47dd 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/:inboundId/:index", a.delInboundClient) + g.POST("/delClient/:email", a.delInboundClient) g.POST("/updateClient/:index", a.updateInboundClient) g.POST("/resetClientTraffic/:email", a.resetClientTraffic) @@ -146,19 +146,17 @@ func (a *InboundController) addInboundClient(c *gin.Context) { } func (a *InboundController) delInboundClient(c *gin.Context) { - inboundId, err := strconv.Atoi(c.Param("inboundId")) + email := c.Param("email") + inbound := &model.Inbound{} + err := c.ShouldBind(inbound) 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 - } + logger.Error("email: " + email + " ID: " + strconv.Itoa(inbound.Id) + " Settings: " + inbound.Settings) - err = a.inboundService.DelInboundClient(inboundId, index) + err = a.inboundService.DelInboundClient(inbound, email) if err != nil { jsonMsg(c, "something worng!", err) return diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html index 298d5d52..3413a248 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -423,16 +423,31 @@ }, delClient(dbInboundId,client) { dbInbound = this.dbInbounds.find(row => row.id === dbInboundId); - clients = this.getInboundClients(dbInbound); + newDbInbound = new DBInbound(dbInbound); + 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"}}', okText: '{{ i18n "delete"}}', cancelText: '{{ i18n "cancel"}}', - onOk: () => this.submit('/xui/inbound/delClient/' + dbInboundId + '/' + index), + onOk: () => this.submit('/xui/inbound/delClient/' + client.email, data), }); }, + getClients(protocol, clientSettings) { + switch(protocol){ + case Protocols.VMESS: return clientSettings.vmesses; + case Protocols.VLESS: return clientSettings.vlesses; + case Protocols.TROJAN: return clientSettings.trojans; + default: return null; + } + }, showQrcode(dbInbound, clientIndex) { const link = dbInbound.genLink(clientIndex); qrModal.show('{{ i18n "qrCode"}}', link, dbInbound); diff --git a/web/service/inbound.go b/web/service/inbound.go index 38254d36..b0ccddd5 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -258,28 +258,22 @@ func (s *InboundService) AddInboundClient(inbound *model.Inbound) error { return db.Save(oldInbound).Error } -func (s *InboundService) DelInboundClient(inboundId int, index int) error { - oldInbound, err := s.GetInbound(inboundId) - if err != nil { - return err - } - settings := map[string][]model.Client{} - json.Unmarshal([]byte(oldInbound.Settings), &settings) - clients := settings["clients"] - email := clients[index].Email - settings["clients"] = append(clients[:index], clients[index+1:]...) - - newSetting, err := json.Marshal(settings) - if err != nil { - return err - } - oldInbound.Settings = string(newSetting) - +func (s *InboundService) DelInboundClient(inbound *model.Inbound, email string) error { db := database.GetDB() - err = s.DelClientStat(db, email) + err := s.DelClientStat(db, email) if err != nil { + logger.Error("Delete stats Data Error") return err } + + oldInbound, err := s.GetInbound(inbound.Id) + if err != nil { + logger.Error("Load Old Data Error") + return err + } + + oldInbound.Settings = inbound.Settings + return db.Save(oldInbound).Error }