From bc3003be5486fc9fb4de8286f4d417caa47f145d Mon Sep 17 00:00:00 2001 From: Masoud Hidden Date: Fri, 5 May 2023 16:02:16 +0330 Subject: [PATCH 1/5] Change some InlineKeyboard data length --- web/service/tgbot.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/web/service/tgbot.go b/web/service/tgbot.go index eca082b5..2fc0a778 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -167,11 +167,11 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo tgbotapi.NewInlineKeyboardButtonData("❌ Cancel Reset", "admin_cancel "+email), ), tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("✅ Confirm Reset Traffic?", "reset_traffic_confirm "+email), + tgbotapi.NewInlineKeyboardButtonData("✅ Confirm Reset Traffic?", "reset_traffic_c "+email), ), ) t.editMessageCallbackTgBot(callbackQuery.From.ID, callbackQuery.Message.MessageID, inlineKeyboard) - case "reset_traffic_confirm": + case "reset_traffic_c": err := t.inboundService.ResetClientTrafficByEmail(email) if err == nil { t.xrayService.SetToNeedRestart() @@ -180,33 +180,33 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo } else { t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.") } - case "reset_expire_days": + case "reset_exp": var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("❌ Cancel Reset", "admin_cancel "+email), ), tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("♾ Unlimited", "reset_expire_days_confirm "+email+" 0"), + tgbotapi.NewInlineKeyboardButtonData("♾ Unlimited", "reset_exp_c "+email+" 0"), ), tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("1 Month", "reset_expire_days_confirm "+email+" 30"), - tgbotapi.NewInlineKeyboardButtonData("2 Months", "reset_expire_days_confirm "+email+" 60"), + tgbotapi.NewInlineKeyboardButtonData("1 Month", "reset_exp_c "+email+" 30"), + tgbotapi.NewInlineKeyboardButtonData("2 Months", "reset_exp_c "+email+" 60"), ), tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("3 Months", "reset_expire_days_confirm "+email+" 90"), - tgbotapi.NewInlineKeyboardButtonData("6 Months", "reset_expire_days_confirm "+email+" 180"), + tgbotapi.NewInlineKeyboardButtonData("3 Months", "reset_exp_c "+email+" 90"), + tgbotapi.NewInlineKeyboardButtonData("6 Months", "reset_exp_c "+email+" 180"), ), tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("9 Months", "reset_expire_days_confirm "+email+" 270"), - tgbotapi.NewInlineKeyboardButtonData("12 Months", "reset_expire_days_confirm "+email+" 360"), + tgbotapi.NewInlineKeyboardButtonData("9 Months", "reset_exp_c "+email+" 270"), + tgbotapi.NewInlineKeyboardButtonData("12 Months", "reset_exp_c "+email+" 360"), ), tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("10 Days", "reset_expire_days_confirm "+email+" 10"), - tgbotapi.NewInlineKeyboardButtonData("20 Days", "reset_expire_days_confirm "+email+" 20"), + tgbotapi.NewInlineKeyboardButtonData("10 Days", "reset_exp_c "+email+" 10"), + tgbotapi.NewInlineKeyboardButtonData("20 Days", "reset_exp_c "+email+" 20"), ), ) t.editMessageCallbackTgBot(callbackQuery.From.ID, callbackQuery.Message.MessageID, inlineKeyboard) - case "reset_expire_days_confirm": + case "reset_exp_c": if len(dataArray) == 3 { days, err := strconv.Atoi(dataArray[2]) if err == nil { @@ -526,7 +526,7 @@ func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) { tgbotapi.NewInlineKeyboardButtonData("📈 Reset Traffic", "reset_traffic "+email), ), tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("📅 Reset Expire Days", "reset_expire_days "+email), + tgbotapi.NewInlineKeyboardButtonData("📅 Reset Expire Days", "reset_exp "+email), ), ) if len(messageID) > 0 { From 5856160c30fb3f525b584fb25a35ec78c8fb0488 Mon Sep 17 00:00:00 2001 From: Masoud Hidden Date: Fri, 5 May 2023 18:20:56 +0330 Subject: [PATCH 2/5] Added some new buttons to bot and ability to use userId in tgId --- web/service/inbound.go | 125 +++++++++++++++++++++++++++++++++--- web/service/tgbot.go | 140 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 247 insertions(+), 18 deletions(-) diff --git a/web/service/inbound.go b/web/service/inbound.go index 5d80c816..2cd42b8e 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -664,26 +664,137 @@ func (s *InboundService) DelClientIPs(tx *gorm.DB, email string) error { return tx.Where("client_email = ?", email).Delete(model.InboundClientIps{}).Error } -func (s *InboundService) GetClientInboundByEmail(email string) (inbound *model.Inbound, err error) { +func (s *InboundService) GetClientInboundByEmail(email string) (traffic *xray.ClientTraffic, 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 + return nil, nil, err } if len(traffics) > 0 { - return s.GetInbound(traffics[0].InboundId) + inbound, err = s.GetInbound(traffics[0].InboundId) + return traffics[0], inbound, err } - return nil, nil + return nil, nil, nil } -func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) error { - inbound, err := s.GetClientInboundByEmail(clientEmail) +func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (*xray.ClientTraffic, error) { + traffic, inbound, err := s.GetClientInboundByEmail(clientEmail) + if err != nil { + return nil, err + } + if inbound == nil || traffic == nil { + return nil, common.NewError("Inbound Not Found For Email:", clientEmail) + } + + oldClients, err := s.getClients(inbound) + if err != nil { + return nil, 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 nil, common.NewError("Client Not Found For Email:", clientEmail) + } + + traffic.Enable = !traffic.Enable + + var settings map[string]interface{} + err = json.Unmarshal([]byte(inbound.Settings), &settings) + if err != nil { + return nil, 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["enable"] = traffic.Enable + newClients = append(newClients, interface{}(c)) + } + } + settings["clients"] = newClients + modifiedSettings, err := json.MarshalIndent(settings, "", " ") + if err != nil { + return nil, err + } + inbound.Settings = string(modifiedSettings) + return traffic, s.UpdateInboundClient(inbound, clientId) +} + +func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) error { + traffic, inbound, err := s.GetClientInboundByEmail(clientEmail) if err != nil { return err } - if inbound == nil { + if inbound == nil || traffic == 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["limitIp"] = count + 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) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) error { + traffic, inbound, err := s.GetClientInboundByEmail(clientEmail) + if err != nil { + return err + } + if inbound == nil || traffic == nil { return common.NewError("Inbound Not Found For Email:", clientEmail) } diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 2fc0a778..8ff25dc0 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -155,16 +155,22 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo if len(dataArray) >= 2 && len(dataArray[1]) > 0 { email := dataArray[1] switch dataArray[0] { - case "refresh_client": - t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Refreshed successfully.", email)) + case "client_refresh": + t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Client Refreshed successfully.", email)) t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID) - case "admin_cancel": + case "client_cancel": t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("❌ %s : Operation canceled.", email)) t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID) + case "ips_refresh": + t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : IPs Refreshed successfully.", email)) + t.searchClientIps(callbackQuery.From.ID, email, callbackQuery.Message.MessageID) + case "ips_cancel": + t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("❌ %s : Operation canceled.", email)) + t.searchClientIps(callbackQuery.From.ID, email, callbackQuery.Message.MessageID) case "reset_traffic": var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("❌ Cancel Reset", "admin_cancel "+email), + tgbotapi.NewInlineKeyboardButtonData("❌ Cancel Reset", "client_cancel "+email), ), tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("✅ Confirm Reset Traffic?", "reset_traffic_c "+email), @@ -183,7 +189,7 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo case "reset_exp": var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("❌ Cancel Reset", "admin_cancel "+email), + tgbotapi.NewInlineKeyboardButtonData("❌ Cancel Reset", "client_cancel "+email), ), tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("♾ Unlimited", "reset_exp_c "+email+" 0"), @@ -225,6 +231,83 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo } t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.") t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID) + case "ip_limit": + var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup( + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("❌ Cancel IP Limit", "client_cancel "+email), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("♾ Unlimited", "ip_limit_c "+email+" 0"), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("1", "ip_limit_c "+email+" 1"), + tgbotapi.NewInlineKeyboardButtonData("2", "ip_limit_c "+email+" 2"), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("3", "ip_limit_c "+email+" 3"), + tgbotapi.NewInlineKeyboardButtonData("4", "ip_limit_c "+email+" 4"), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("5", "ip_limit_c "+email+" 5"), + tgbotapi.NewInlineKeyboardButtonData("6", "ip_limit_c "+email+" 6"), + tgbotapi.NewInlineKeyboardButtonData("7", "ip_limit_c "+email+" 7"), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("8", "ip_limit_c "+email+" 8"), + tgbotapi.NewInlineKeyboardButtonData("9", "ip_limit_c "+email+" 9"), + tgbotapi.NewInlineKeyboardButtonData("10", "ip_limit_c "+email+" 10"), + ), + ) + t.editMessageCallbackTgBot(callbackQuery.From.ID, callbackQuery.Message.MessageID, inlineKeyboard) + case "ip_limit_c": + if len(dataArray) == 3 { + count, err := strconv.Atoi(dataArray[2]) + if err == nil { + err := t.inboundService.ResetClientIpLimitByEmail(email, count) + if err == nil { + t.xrayService.SetToNeedRestart() + t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : IP limit %d saved successfully.", email, count)) + t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID) + return + } + } + } + t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.") + t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID) + case "clear_ips": + var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup( + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("❌ Cancel", "ips_cancel "+email), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("✅ Confirm Clear IPs?", "clear_ips_c "+email), + ), + ) + t.editMessageCallbackTgBot(callbackQuery.From.ID, callbackQuery.Message.MessageID, inlineKeyboard) + case "clear_ips_c": + err := t.inboundService.ClearClientIps(email) + if err == nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : IPs cleared successfully.", email)) + t.searchClientIps(callbackQuery.From.ID, email, callbackQuery.Message.MessageID) + } else { + t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.") + } + case "ip_log": + t.sendCallbackAnswerTgBot(callbackQuery.ID, "✅ %s : Get IP Log.") + t.searchClientIps(callbackQuery.From.ID, email) + case "toggle_enable": + trrafic, err := t.inboundService.ToggleClientEnableByEmail(email) + if err == nil { + t.xrayService.SetToNeedRestart() + if trrafic.Enable { + t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Enabled successfully.", email)) + } else { + t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Disabled successfully.", email)) + } + t.searchClient(callbackQuery.From.ID, email, callbackQuery.Message.MessageID) + } else { + t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.") + } } return } @@ -247,7 +330,7 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo case "get_backup": t.sendBackup(callbackQuery.From.ID) case "client_traffic": - t.getClientUsage(callbackQuery.From.ID, callbackQuery.From.UserName) + t.getClientUsage(callbackQuery.From.ID, callbackQuery.From.UserName, "#"+strconv.FormatInt(callbackQuery.From.ID, 10)) case "client_commands": t.SendMsgToTgbot(callbackQuery.From.ID, "To search for statistics, just use folowing command:\r\n \r\n/usage [UID|Password]\r\n \r\nUse UID for vmess/vless and Password for Trojan.") case "commands": @@ -447,13 +530,17 @@ func (t *Tgbot) getInboundUsages() string { return info } -func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) { - if len(tgUserName) == 0 { +func (t *Tgbot) getClientUsage(chatId int64, tgUserName string, tgUserID string) { + if len(tgUserName) == 0 && len(tgUserID) == 0 { msg := "Your configuration is not found!\nYou should configure your telegram username and ask Admin to add it to your configuration." t.SendMsgToTgbot(chatId, msg) return } - traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserName) + userIdentifier := tgUserName + if len(userIdentifier) == 0 { + userIdentifier = tgUserID + } + traffics, err := t.inboundService.GetClientTrafficTgBot(userIdentifier) if err != nil { logger.Warning(err) msg := "❌ Something went wrong!" @@ -461,7 +548,10 @@ func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) { return } if len(traffics) == 0 { - msg := "Your configuration is not found!\nPlease ask your Admin to use your telegram username in your configuration(s).\n\nYour username: @" + tgUserName + "" + if len(tgUserName) > 0 { + userIdentifier = "@" + tgUserName + } + msg := "Your configuration is not found!\nPlease ask your Admin to use your telegram username in your configuration(s).\n\nYour username: " + userIdentifier + "" t.SendMsgToTgbot(chatId, msg) return } @@ -488,6 +578,27 @@ func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) { t.SendAnswer(chatId, "Please choose:", false) } +func (t *Tgbot) searchClientIps(chatId int64, email string, messageID ...int) { + ips, err := t.inboundService.GetInboundClientIps(email) + if err != nil || len(ips) == 0 { + ips = "No IP Record" + } + output := fmt.Sprintf("📧 Email: %s\r\n🔢 IPs: \r\n%s\r\n", email, ips) + var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup( + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("🔄 Refresh", "ips_refresh "+email), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("❌ Clear IPs", "clear_ips "+email), + ), + ) + if len(messageID) > 0 { + t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard) + } else { + t.SendMsgToTgbot(chatId, output, inlineKeyboard) + } +} + func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) { traffic, err := t.inboundService.GetClientTrafficByEmail(email) if err != nil { @@ -520,7 +631,7 @@ func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) { total, expiryTime) var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("🔄 Refresh", "refresh_client "+email), + tgbotapi.NewInlineKeyboardButtonData("🔄 Refresh", "client_refresh "+email), ), tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("📈 Reset Traffic", "reset_traffic "+email), @@ -528,6 +639,13 @@ func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) { tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("📅 Reset Expire Days", "reset_exp "+email), ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("🔢 IP Log", "ip_log "+email), + tgbotapi.NewInlineKeyboardButtonData("🔢 IP Limit", "ip_limit "+email), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("💡 Enable / Disable", "toggle_enable "+email), + ), ) if len(messageID) > 0 { t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard) From d349bffcd6ba547ed9e4cdf5b199c5100a99437f Mon Sep 17 00:00:00 2001 From: Masoud Hidden Date: Fri, 5 May 2023 19:50:40 +0330 Subject: [PATCH 3/5] Fix bot client enable button --- web/service/inbound.go | 34 +++++++++++++++++----------------- web/service/tgbot.go | 8 ++++---- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/web/service/inbound.go b/web/service/inbound.go index 2cd42b8e..dd633c4b 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -679,21 +679,22 @@ func (s *InboundService) GetClientInboundByEmail(email string) (traffic *xray.Cl return nil, nil, nil } -func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (*xray.ClientTraffic, error) { - traffic, inbound, err := s.GetClientInboundByEmail(clientEmail) +func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (bool, error) { + _, inbound, err := s.GetClientInboundByEmail(clientEmail) if err != nil { - return nil, err + return false, err } - if inbound == nil || traffic == nil { - return nil, common.NewError("Inbound Not Found For Email:", clientEmail) + if inbound == nil { + return false, common.NewError("Inbound Not Found For Email:", clientEmail) } oldClients, err := s.getClients(inbound) if err != nil { - return nil, err + return false, err } clientId := "" + clientOldEnabled := false for _, oldClient := range oldClients { if oldClient.Email == clientEmail { @@ -702,45 +703,44 @@ func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (*xray.Cl } else { clientId = oldClient.ID } + clientOldEnabled = oldClient.Enable break } } if len(clientId) == 0 { - return nil, common.NewError("Client Not Found For Email:", clientEmail) + return false, common.NewError("Client Not Found For Email:", clientEmail) } - traffic.Enable = !traffic.Enable - var settings map[string]interface{} err = json.Unmarshal([]byte(inbound.Settings), &settings) if err != nil { - return nil, err + return false, 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["enable"] = traffic.Enable + c["enable"] = !clientOldEnabled newClients = append(newClients, interface{}(c)) } } settings["clients"] = newClients modifiedSettings, err := json.MarshalIndent(settings, "", " ") if err != nil { - return nil, err + return false, err } inbound.Settings = string(modifiedSettings) - return traffic, s.UpdateInboundClient(inbound, clientId) + return !clientOldEnabled, s.UpdateInboundClient(inbound, clientId) } func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) error { - traffic, inbound, err := s.GetClientInboundByEmail(clientEmail) + _, inbound, err := s.GetClientInboundByEmail(clientEmail) if err != nil { return err } - if inbound == nil || traffic == nil { + if inbound == nil { return common.NewError("Inbound Not Found For Email:", clientEmail) } @@ -790,11 +790,11 @@ func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int } func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) error { - traffic, inbound, err := s.GetClientInboundByEmail(clientEmail) + _, inbound, err := s.GetClientInboundByEmail(clientEmail) if err != nil { return err } - if inbound == nil || traffic == nil { + if inbound == nil { return common.NewError("Inbound Not Found For Email:", clientEmail) } diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 8ff25dc0..0e5c4abb 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -296,10 +296,10 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo t.sendCallbackAnswerTgBot(callbackQuery.ID, "✅ %s : Get IP Log.") t.searchClientIps(callbackQuery.From.ID, email) case "toggle_enable": - trrafic, err := t.inboundService.ToggleClientEnableByEmail(email) + enabled, err := t.inboundService.ToggleClientEnableByEmail(email) if err == nil { t.xrayService.SetToNeedRestart() - if trrafic.Enable { + if enabled { t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Enabled successfully.", email)) } else { t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Disabled successfully.", email)) @@ -583,7 +583,7 @@ func (t *Tgbot) searchClientIps(chatId int64, email string, messageID ...int) { if err != nil || len(ips) == 0 { ips = "No IP Record" } - output := fmt.Sprintf("📧 Email: %s\r\n🔢 IPs: \r\n%s\r\n", email, ips) + output := fmt.Sprintf("📧 Email: %s\r\n🔢 IPs: \r\n\r\n%s\r\n", email, ips) var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("🔄 Refresh", "ips_refresh "+email), @@ -644,7 +644,7 @@ func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) { tgbotapi.NewInlineKeyboardButtonData("🔢 IP Limit", "ip_limit "+email), ), tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("💡 Enable / Disable", "toggle_enable "+email), + tgbotapi.NewInlineKeyboardButtonData("🔘 Enable / Disable", "toggle_enable "+email), ), ) if len(messageID) > 0 { From 8d11f83ac7ea837104a112fa99ee0ed99c3bf4c4 Mon Sep 17 00:00:00 2001 From: Masoud Hidden Date: Fri, 5 May 2023 20:07:47 +0330 Subject: [PATCH 4/5] Fix get client ips --- web/service/tgbot.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 0e5c4abb..090f282c 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -293,7 +293,7 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.") } case "ip_log": - t.sendCallbackAnswerTgBot(callbackQuery.ID, "✅ %s : Get IP Log.") + t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Get IP Log.", email)) t.searchClientIps(callbackQuery.From.ID, email) case "toggle_enable": enabled, err := t.inboundService.ToggleClientEnableByEmail(email) @@ -583,7 +583,7 @@ func (t *Tgbot) searchClientIps(chatId int64, email string, messageID ...int) { if err != nil || len(ips) == 0 { ips = "No IP Record" } - output := fmt.Sprintf("📧 Email: %s\r\n🔢 IPs: \r\n\r\n%s\r\n", email, ips) + output := fmt.Sprintf("📧 Email: %s\r\n🔢 IPs: \r\n%s\r\n", email, ips) var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("🔄 Refresh", "ips_refresh "+email), From 7f8f0b0f2d8dd1935455d6abf4e511c4d4d93ba7 Mon Sep 17 00:00:00 2001 From: Masoud Hidden Date: Sat, 6 May 2023 02:36:46 +0330 Subject: [PATCH 5/5] Fix ability to use userId in tgId --- web/service/tgbot.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 090f282c..5b12a229 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -330,7 +330,7 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo case "get_backup": t.sendBackup(callbackQuery.From.ID) case "client_traffic": - t.getClientUsage(callbackQuery.From.ID, callbackQuery.From.UserName, "#"+strconv.FormatInt(callbackQuery.From.ID, 10)) + t.getClientUsage(callbackQuery.From.ID, callbackQuery.From.UserName, strconv.FormatInt(callbackQuery.From.ID, 10)) case "client_commands": t.SendMsgToTgbot(callbackQuery.From.ID, "To search for statistics, just use folowing command:\r\n \r\n/usage [UID|Password]\r\n \r\nUse UID for vmess/vless and Password for Trojan.") case "commands": @@ -531,16 +531,7 @@ func (t *Tgbot) getInboundUsages() string { } func (t *Tgbot) getClientUsage(chatId int64, tgUserName string, tgUserID string) { - if len(tgUserName) == 0 && len(tgUserID) == 0 { - msg := "Your configuration is not found!\nYou should configure your telegram username and ask Admin to add it to your configuration." - t.SendMsgToTgbot(chatId, msg) - return - } - userIdentifier := tgUserName - if len(userIdentifier) == 0 { - userIdentifier = tgUserID - } - traffics, err := t.inboundService.GetClientTrafficTgBot(userIdentifier) + traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID) if err != nil { logger.Warning(err) msg := "❌ Something went wrong!" @@ -548,10 +539,21 @@ func (t *Tgbot) getClientUsage(chatId int64, tgUserName string, tgUserID string) return } if len(traffics) == 0 { - if len(tgUserName) > 0 { - userIdentifier = "@" + tgUserName + if len(tgUserName) == 0 { + msg := "Your configuration is not found!\nPlease ask your Admin to use your telegram user id in your configuration(s).\n\nYour user id: " + tgUserID + "" + t.SendMsgToTgbot(chatId, msg) + return } - msg := "Your configuration is not found!\nPlease ask your Admin to use your telegram username in your configuration(s).\n\nYour username: " + userIdentifier + "" + traffics, err = t.inboundService.GetClientTrafficTgBot(tgUserName) + } + if err != nil { + logger.Warning(err) + msg := "❌ Something went wrong!" + t.SendMsgToTgbot(chatId, msg) + return + } + if len(traffics) == 0 { + msg := "Your configuration is not found!\nPlease ask your Admin to use your telegram username or user id in your configuration(s).\n\nYour username: @" + tgUserName + "\n\nYour user id: " + tgUserID + "" t.SendMsgToTgbot(chatId, msg) return }