From aab672976ebbdc7086133f0078e4b5fe96c0a6cb Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Mon, 24 Apr 2023 20:30:01 +0200 Subject: [PATCH] update by client id #218 --- web/controller/api.go | 2 +- web/controller/inbound.go | 12 ++++-------- web/html/xui/client_modal.html | 10 ++++++---- web/html/xui/inbounds.html | 8 ++++---- web/service/inbound.go | 30 ++++++++++++++++++++++-------- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/web/controller/api.go b/web/controller/api.go index f756990b..333c96e5 100644 --- a/web/controller/api.go +++ b/web/controller/api.go @@ -25,7 +25,7 @@ func (a *APIController) initRouter(g *gin.RouterGroup) { g.POST("/update/:id", a.updateInbound) g.POST("/addClient/", a.addInboundClient) g.POST("/:id/delClient/:clientId", a.delInboundClient) - g.POST("/updateClient/:index", a.updateInboundClient) + g.POST("/updateClient/:clientId", a.updateInboundClient) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) g.POST("/resetAllTraffics", a.resetAllTraffics) g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics) diff --git a/web/controller/inbound.go b/web/controller/inbound.go index 2f66b247..5bb7cc12 100644 --- a/web/controller/inbound.go +++ b/web/controller/inbound.go @@ -33,7 +33,7 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) { g.POST("/update/:id", a.updateInbound) g.POST("/addClient", a.addInboundClient) g.POST("/:id/delClient/:clientId", a.delInboundClient) - g.POST("/updateClient/:index", a.updateInboundClient) + g.POST("/updateClient/:clientId", a.updateInboundClient) g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic) g.POST("/resetAllTraffics", a.resetAllTraffics) g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics) @@ -176,20 +176,16 @@ func (a *InboundController) delInboundClient(c *gin.Context) { } func (a *InboundController) updateInboundClient(c *gin.Context) { - index, err := strconv.Atoi(c.Param("index")) - if err != nil { - jsonMsg(c, I18n(c, "pages.inbounds.revise"), err) - return - } + clientId := c.Param("clientId") inbound := &model.Inbound{} - err = c.ShouldBind(inbound) + err := c.ShouldBind(inbound) if err != nil { jsonMsg(c, I18n(c, "pages.inbounds.revise"), err) return } - err = a.inboundService.UpdateInboundClient(inbound, index) + err = a.inboundService.UpdateInboundClient(inbound, clientId) if err != nil { jsonMsg(c, "something worng!", err) return diff --git a/web/html/xui/client_modal.html b/web/html/xui/client_modal.html index 510c8f10..c989ebb3 100644 --- a/web/html/xui/client_modal.html +++ b/web/html/xui/client_modal.html @@ -17,12 +17,13 @@ inbound: new Inbound(), clients: [], clientStats: [], + oldClientId: "", index: null, isExpired: false, delayedStart: false, ok() { if(clientModal.isEdit){ - ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id, clientModal.index); + ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id, clientModal.oldClientId); } else { ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id); } @@ -38,12 +39,13 @@ this.index = index === null ? this.clients.length : index; this.isExpired = isEdit ? this.inbound.isExpiry(this.index) : false; this.delayedStart = false; - if (!isEdit){ - this.addClient(this.inbound.protocol, this.clients); - } else { + if (isEdit){ if (this.clients[index].expiryTime < 0){ this.delayedStart = true; } + this.oldClientId = this.dbInbound.protocol == "trojan" ? this.clients[index].password : this.clients[index].id; + } else { + this.addClient(this.inbound.protocol, this.clients); } this.clientStats = this.dbInbound.clientStats.find(row => row.email === this.clients[this.index].email); this.confirm = confirm; diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html index 0f18285a..9bc1935b 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -558,9 +558,9 @@ okText: '{{ i18n "pages.client.submitEdit"}}', dbInbound: dbInbound, index: index, - confirm: async (client, dbInboundId, index) => { + confirm: async (client, dbInboundId, clientId) => { clientModal.loading(); - await this.updateClient(client, dbInboundId, index); + await this.updateClient(client, dbInboundId, clientId); clientModal.close(); }, isEdit: true @@ -577,12 +577,12 @@ }; await this.submit(`/xui/inbound/addClient`, data); }, - async updateClient(client, dbInboundId, index) { + async updateClient(client, dbInboundId, clientId) { const data = { id: dbInboundId, settings: '{"clients": [' + client.toString() +']}', }; - await this.submit(`/xui/inbound/updateClient/${index}`, data); + await this.submit(`/xui/inbound/updateClient/${clientId}`, data); }, resetTraffic(dbInboundId) { dbInbound = this.dbInbounds.find(row => row.id === dbInboundId); diff --git a/web/service/inbound.go b/web/service/inbound.go index b0bba9e0..293f768e 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -347,7 +347,7 @@ func (s *InboundService) DelInboundClient(inboundId int, clientId string) error return db.Save(oldInbound).Error } -func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) error { +func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId string) error { clients, err := s.getClients(data) if err != nil { return err @@ -371,7 +371,23 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err return err } - if len(clients[0].Email) > 0 && clients[0].Email != oldClients[index].Email { + oldEmail := "" + clientIndex := 0 + for index, oldClient := range oldClients { + oldClientId := "" + if oldInbound.Protocol == "trojan" { + oldClientId = oldClient.Password + } else { + oldClientId = oldClient.ID + } + if clientId == oldClientId { + oldEmail = oldClient.Email + clientIndex = index + break + } + } + + if len(clients[0].Email) > 0 && clients[0].Email != oldEmail { existEmail, err := s.checkEmailsExistForClients(clients) if err != nil { return err @@ -386,10 +402,8 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err if err != nil { return err } - settingsClients := oldSettings["clients"].([]interface{}) - settingsClients[index] = inerfaceClients[0] - + settingsClients[clientIndex] = inerfaceClients[0] oldSettings["clients"] = settingsClients newSettings, err := json.MarshalIndent(oldSettings, "", " ") @@ -401,8 +415,8 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err db := database.GetDB() if len(clients[0].Email) > 0 { - if len(oldClients[index].Email) > 0 { - err = s.UpdateClientStat(oldClients[index].Email, &clients[0]) + if len(oldEmail) > 0 { + err = s.UpdateClientStat(oldEmail, &clients[0]) if err != nil { return err } @@ -410,7 +424,7 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err s.AddClientStat(data.Id, &clients[0]) } } else { - err = s.DelClientStat(db, oldClients[index].Email) + err = s.DelClientStat(db, oldEmail) if err != nil { return err }