Browse Source

fix ResetClientExpiryTimeByEmail

Masoud Hidden 1 year ago
parent
commit
a53d2b927f
2 changed files with 69 additions and 20 deletions
  1. 59 6
      web/service/inbound.go
  2. 10 14
      web/service/tgbot.go

+ 59 - 6
web/service/inbound.go

@@ -664,19 +664,72 @@ func (s *InboundService) DelClientIPs(tx *gorm.DB, email string) error {
 	return tx.Where("client_email = ?", email).Delete(model.InboundClientIps{}).Error
 }
 
-func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) error {
+func (s *InboundService) GetClientInboundByEmail(email string) (inbound *model.Inbound, err error) {
 	db := database.GetDB()
+	var traffics []*xray.ClientTraffic
+	err = db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error
+	if err != nil {
+		logger.Warning(err)
+		return nil, err
+	}
+	if len(traffics) > 0 {
+		return s.GetInbound(traffics[0].InboundId)
+	}
+	return nil, nil
+}
 
-	result := db.Model(xray.ClientTraffic{}).
-		Where("email = ?", clientEmail).
-		Updates(map[string]interface{}{"enable": true, "expiry_time": expiry_time})
+func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) error {
+	inbound, err := s.GetClientInboundByEmail(clientEmail)
+	if err != nil {
+		return err
+	}
+	if inbound == nil {
+		return common.NewError("Inbound Not Found For Email:", clientEmail)
+	}
 
-	err := result.Error
+	oldClients, err := s.getClients(inbound)
+	if err != nil {
+		return err
+	}
+
+	clientId := ""
+
+	for _, oldClient := range oldClients {
+		if oldClient.Email == clientEmail {
+			if inbound.Protocol == "trojan" {
+				clientId = oldClient.Password
+			} else {
+				clientId = oldClient.ID
+			}
+			break
+		}
+	}
+
+	if len(clientId) == 0 {
+		return common.NewError("Client Not Found For Email:", clientEmail)
+	}
 
+	var settings map[string]interface{}
+	err = json.Unmarshal([]byte(inbound.Settings), &settings)
 	if err != nil {
 		return err
 	}
-	return nil
+	clients := settings["clients"].([]interface{})
+	var newClients []interface{}
+	for client_index := range clients {
+		c := clients[client_index].(map[string]interface{})
+		if c["email"] == clientEmail {
+			c["expiryTime"] = expiry_time
+			newClients = append(newClients, interface{}(c))
+		}
+	}
+	settings["clients"] = newClients
+	modifiedSettings, err := json.MarshalIndent(settings, "", "  ")
+	if err != nil {
+		return err
+	}
+	inbound.Settings = string(modifiedSettings)
+	return s.UpdateInboundClient(inbound, clientId)
 }
 
 func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {

+ 10 - 14
web/service/tgbot.go

@@ -172,8 +172,8 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo
 				)
 				t.editMessageCallbackTgBot(callbackQuery.From.ID, callbackQuery.Message.MessageID, inlineKeyboard)
 			case "reset_traffic_confirm":
-				resetError := t.inboundService.ResetClientTrafficByEmail(email)
-				if resetError == nil {
+				err := t.inboundService.ResetClientTrafficByEmail(email)
+				if err == nil {
 					t.xrayService.SetToNeedRestart()
 					t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Traffic reset successfully.", email))
 					t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID)
@@ -207,28 +207,24 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo
 				)
 				t.editMessageCallbackTgBot(callbackQuery.From.ID, callbackQuery.Message.MessageID, inlineKeyboard)
 			case "reset_expire_days_confirm":
-				err := len(dataArray) < 3
-				if !err {
-					days, err2 := strconv.Atoi(dataArray[2])
-					if err2 == nil {
+				if len(dataArray) == 3 {
+					days, err := strconv.Atoi(dataArray[2])
+					if err == nil {
 						var date int64 = 0
 						if days > 0 {
 							date = int64(-(days * 24 * 60 * 60000))
 						}
-						resetError := t.inboundService.ResetClientExpiryTimeByEmail(email, date)
-						if resetError == nil {
+						err := t.inboundService.ResetClientExpiryTimeByEmail(email, date)
+						if err == nil {
 							t.xrayService.SetToNeedRestart()
 							t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Expire days reset successfully.", email))
 							t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID)
+							return
 						}
-					} else {
-						err = true
 					}
 				}
-				if err {
-					t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.")
-					t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID)
-				}
+				t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.")
+				t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID)
 			}
 			return
 		}