From 16e3107d23b4bc536d509c2dd832d10366fb895e Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Mon, 24 Apr 2023 15:07:11 +0330 Subject: [PATCH] Better client delete + api Co-Authored-By: Alireza Ahmadi --- README.md | 2 +- web/controller/api.go | 2 +- web/controller/inbound.go | 11 ++++----- web/html/xui/inbounds.html | 16 +++--------- web/service/inbound.go | 50 +++++++++++++++++++++++++++++--------- 5 files changed, 50 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index f410713a..97383f27 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ Reference syntax: | `POST` | `"/clientIps/:email"` | Client Ip address | | `POST` | `"/clearClientIps/:email"` | Clear Client Ip address | | `POST` | `"/addClient/"` | Add Client to inbound | -| `POST` | `"/delClient/:email"` | Delete Client | +| `POST` | `"/:id/delClient/:clientId"` | Delete Client by UID/Password as clientId | | `POST` | `"/updateClient/:index"` | Update Client | | `POST` | `"/:id/resetClientTraffic/:email"` | Reset Client's Traffic | | `POST` | `"/resetAllTraffics"` | Reset traffics of all inbounds | diff --git a/web/controller/api.go b/web/controller/api.go index c8ad2a67..b8da9243 100644 --- a/web/controller/api.go +++ b/web/controller/api.go @@ -26,7 +26,7 @@ func (a *APIController) initRouter(g *gin.RouterGroup) { g.POST("/clientIps/:email", a.getClientIps) g.POST("/clearClientIps/:email", a.clearClientIps) 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 f32cb766..ec37dcc7 100644 --- a/web/controller/inbound.go +++ b/web/controller/inbound.go @@ -34,7 +34,7 @@ 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("/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) @@ -155,7 +155,7 @@ func (a *InboundController) clearClientIps(c *gin.Context) { err := a.inboundService.ClearClientIps(email) if err != nil { - jsonMsg(c, "修改", err) + jsonMsg(c, "Revise", err) return } jsonMsg(c, "Log Cleared", nil) @@ -180,15 +180,14 @@ 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 } + clientId := c.Param("clientId") - err = a.inboundService.DelInboundClient(inbound, email) + 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 5bfcaccd..6f13cd86 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -70,7 +70,7 @@ {{ i18n "pages.inbounds.export" }} {{ i18n "pages.inbounds.resetAllTraffic" }} - + row.id === dbInboundId); - 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(), - }; + 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/delClient/' + client.email, data), + onOk: () => this.submit(`/xui/inbound/${dbInboundId}/delClient/${clientId}`), }); }, getClients(protocol, clientSettings) { diff --git a/web/service/inbound.go b/web/service/inbound.go index 3f736470..5d6acb92 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -314,28 +314,56 @@ 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, clientId string) 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 + email := "" + client_key := "id" + if oldInbound.Protocol == "trojan" { + client_key = "password" + } + + 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 + } + + oldInbound.Settings = string(newSettings) + + db := database.GetDB() + err = s.DelClientStat(db, email) + if err != nil { + logger.Error("Delete stats Data Error") + return err + } err = s.DelClientIPs(db, email) if err != nil { logger.Error("Error in delete client IPs") return err } - return db.Save(oldInbound).Error }