Forráskód Böngészése

update by client id

Co-Authored-By: Alireza Ahmadi <[email protected]>
MHSanaei 1 éve
szülő
commit
cc3ff61ae2

+ 1 - 1
web/controller/api.go

@@ -27,7 +27,7 @@ func (a *APIController) initRouter(g *gin.RouterGroup) {
 	g.POST("/clearClientIps/:email", a.clearClientIps)
 	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)

+ 4 - 8
web/controller/inbound.go

@@ -35,7 +35,7 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) {
 	g.POST("/clearClientIps/:email", a.clearClientIps)
 	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)
@@ -199,20 +199,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

+ 6 - 4
web/html/xui/client_modal.html

@@ -17,13 +17,14 @@
         inbound: new Inbound(),
         clients: [],
         clientStats: [],
+        oldClientId: "",
         index: null,
         clientIps: 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);
             }
@@ -39,12 +40,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;

+ 4 - 4
web/html/xui/inbounds.html

@@ -561,9 +561,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
@@ -580,12 +580,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);

+ 35 - 13
web/service/inbound.go

@@ -367,7 +367,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
@@ -391,7 +391,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
@@ -406,10 +422,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, "", "  ")
@@ -421,12 +435,12 @@ 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
 			}
-			err = s.UpdateClientIPs(db, oldClients[index].Email, clients[0].Email)
+			err = s.UpdateClientIPs(db, oldEmail, clients[0].Email)
 			if err != nil {
 				return err
 			}
@@ -434,11 +448,11 @@ 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
 		}
-		err = s.DelClientIPs(db, oldClients[index].Email)
+		err = s.DelClientIPs(db, oldEmail)
 		if err != nil {
 			return err
 		}
@@ -667,8 +681,15 @@ func (s *InboundService) ResetClientTraffic(id int, clientEmail string) error {
 func (s *InboundService) ResetAllClientTraffics(id int) error {
 	db := database.GetDB()
 
+	whereText := "inbound_id "
+	if id == -1 {
+		whereText += " > ?"
+	} else {
+		whereText += " = ?"
+	}
+
 	result := db.Model(xray.ClientTraffic{}).
-		Where("inbound_id = ?", id).
+		Where(whereText, id).
 		Updates(map[string]interface{}{"enable": true, "up": 0, "down": 0})
 
 	err := result.Error
@@ -724,7 +745,7 @@ func (s *InboundService) GetClientTrafficTgBot(tguname string) ([]*xray.ClientTr
 	return traffics, err
 }
 
-func (s *InboundService) GetClientTrafficByEmail(email string) (traffic []*xray.ClientTraffic, err error) {
+func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.ClientTraffic, err error) {
 	db := database.GetDB()
 	var traffics []*xray.ClientTraffic
 
@@ -735,7 +756,7 @@ func (s *InboundService) GetClientTrafficByEmail(email string) (traffic []*xray.
 			return nil, err
 		}
 	}
-	return traffics, err
+	return traffics[0], err
 }
 
 func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) {
@@ -809,6 +830,7 @@ func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error)
 	}
 	return inbounds, nil
 }
+
 func (s *InboundService) MigrationRequirements() {
 	db := database.GetDB()
 	var inbounds []*model.Inbound

+ 19 - 21
web/service/tgbot.go

@@ -404,38 +404,36 @@ func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) {
 }
 
 func (t *Tgbot) searchClient(chatId int64, email string) {
-	traffics, err := t.inboundService.GetClientTrafficByEmail(email)
+	traffic, err := t.inboundService.GetClientTrafficByEmail(email)
 	if err != nil {
 		logger.Warning(err)
 		msg := "❌ Something went wrong!"
 		t.SendMsgToTgbot(chatId, msg)
 		return
 	}
-	if len(traffics) == 0 {
+	if traffic == nil {
 		msg := "No result!"
 		t.SendMsgToTgbot(chatId, msg)
 		return
 	}
-	for _, traffic := range traffics {
-		expiryTime := ""
-		if traffic.ExpiryTime == 0 {
-			expiryTime = "♾Unlimited"
-		} else if traffic.ExpiryTime < 0 {
-			expiryTime = fmt.Sprintf("%d days", traffic.ExpiryTime/-86400000)
-		} else {
-			expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
-		}
-		total := ""
-		if traffic.Total == 0 {
-			total = "♾Unlimited"
-		} else {
-			total = common.FormatTraffic((traffic.Total))
-		}
-		output := fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n",
-			traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
-			total, expiryTime)
-		t.SendMsgToTgbot(chatId, output)
+	expiryTime := ""
+	if traffic.ExpiryTime == 0 {
+		expiryTime = "♾Unlimited"
+	} else if traffic.ExpiryTime < 0 {
+		expiryTime = fmt.Sprintf("%d days", traffic.ExpiryTime/-86400000)
+	} else {
+		expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
 	}
+	total := ""
+	if traffic.Total == 0 {
+		total = "♾Unlimited"
+	} else {
+		total = common.FormatTraffic((traffic.Total))
+	}
+	output := fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n",
+		traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
+		total, expiryTime)
+	t.SendMsgToTgbot(chatId, output)
 }
 
 func (t *Tgbot) searchInbound(chatId int64, remark string) {