From a53d2b927fdc4f543589750d761428f591cabaf7 Mon Sep 17 00:00:00 2001 From: Masoud Hidden Date: Fri, 5 May 2023 04:34:39 +0330 Subject: [PATCH] fix ResetClientExpiryTimeByEmail --- web/service/inbound.go | 69 +++++++++++++++++++++++++++++++++++++----- web/service/tgbot.go | 24 ++++++--------- 2 files changed, 71 insertions(+), 22 deletions(-) diff --git a/web/service/inbound.go b/web/service/inbound.go index 95c3d745..5d80c816 100644 --- a/web/service/inbound.go +++ b/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}) - - err := result.Error - +func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) error { + inbound, err := s.GetClientInboundByEmail(clientEmail) if err != nil { return err } - return nil + if inbound == nil { + return common.NewError("Inbound Not Found For Email:", clientEmail) + } + + 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 + } + 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 { diff --git a/web/service/tgbot.go b/web/service/tgbot.go index ef5d2abd..eca082b5 100644 --- a/web/service/tgbot.go +++ b/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 }