From 585f43498cfcbe237a52460dcbe4f0bbfac6954c Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Thu, 16 Mar 2023 20:48:21 +0100 Subject: [PATCH] TGBot: Add usage search for user #67 --- web/service/inbound.go | 39 +++++++++++++++++++++++++++++++++++++++ web/service/tgbot.go | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/web/service/inbound.go b/web/service/inbound.go index a0d77638..ada831af 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -528,3 +528,42 @@ func (s *InboundService) GetClientTrafficByEmail(email string) (traffic []*xray. } return traffics, err } + +func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) { + db := database.GetDB() + inbound := &model.Inbound{} + traffic = &xray.ClientTraffic{} + + err = db.Model(model.Inbound{}).Where("settings like ?", "%\""+query+"\"%").First(inbound).Error + if err != nil { + if err == gorm.ErrRecordNotFound { + logger.Warning(err) + return nil, err + } + } + traffic.InboundId = inbound.Id + + // get settings clients + settings := map[string][]model.Client{} + json.Unmarshal([]byte(inbound.Settings), &settings) + clients := settings["clients"] + for _, client := range clients { + if client.ID == query && client.Email != "" { + traffic.Email = client.Email + break + } + if client.Password == query && client.Email != "" { + traffic.Email = client.Email + break + } + } + if traffic.Email == "" { + return nil, err + } + err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error + if err != nil { + logger.Warning(err) + return nil, err + } + return traffic, err +} diff --git a/web/service/tgbot.go b/web/service/tgbot.go index c08284a3..e7c9f97a 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -131,7 +131,7 @@ func (t *Tgbot) answerCommand(message *tgbotapi.Message, chatId int64, isAdmin b if isAdmin { t.searchClient(chatId, message.CommandArguments()) } else { - msg = "🚫 Insufficient privilege" + t.searchForClient(chatId, message.CommandArguments()) } default: msg = "❗ Unknown command" @@ -162,6 +162,8 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo t.sendBackup(callbackQuery.From.ID) case "client_traffic": t.getClientUsage(callbackQuery.From.ID, callbackQuery.From.UserName) + case "client_commands": + t.SendMsgToTgbot(callbackQuery.From.ID, "To search for statistics, just use folowing command:\r\n \r\n/usage [UID|Passowrd]\r\n \r\nUse UID for vmess and vless and Password for Trojan.") case "commands": t.SendMsgToTgbot(callbackQuery.From.ID, "To search for a client email, just use folowing command:\r\n \r\n/usage email") } @@ -193,6 +195,7 @@ func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) { var numericKeyboardClient = tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("Get Usage", "client_traffic"), + tgbotapi.NewInlineKeyboardButtonData("Commands", "client_commands"), ), ) msgConfig := tgbotapi.NewMessage(chatId, msg) @@ -420,6 +423,37 @@ func (t *Tgbot) searchClient(chatId int64, email string) { } } +func (t *Tgbot) searchForClient(chatId int64, query string) { + traffic, err := t.inboundService.SearchClientTraffic(query) + if err != nil { + logger.Warning(err) + msg := "❌ Something went wrong!" + t.SendMsgToTgbot(chatId, msg) + return + } + if traffic == nil { + msg := "No result!" + t.SendMsgToTgbot(chatId, msg) + return + } + expiryTime := "" + if traffic.ExpiryTime == 0 { + expiryTime = "♾Unlimited" + } 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) getExhausted() string { trDiff := int64(0) exDiff := int64(0)