Fix bot client enable button

This commit is contained in:
Masoud Hidden 2023-05-05 19:50:40 +03:30
parent 5856160c30
commit d349bffcd6
2 changed files with 21 additions and 21 deletions

View File

@ -679,21 +679,22 @@ func (s *InboundService) GetClientInboundByEmail(email string) (traffic *xray.Cl
return nil, nil, nil return nil, nil, nil
} }
func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (*xray.ClientTraffic, error) { func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (bool, error) {
traffic, inbound, err := s.GetClientInboundByEmail(clientEmail) _, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil { if err != nil {
return nil, err return false, err
} }
if inbound == nil || traffic == nil { if inbound == nil {
return nil, common.NewError("Inbound Not Found For Email:", clientEmail) return false, common.NewError("Inbound Not Found For Email:", clientEmail)
} }
oldClients, err := s.getClients(inbound) oldClients, err := s.getClients(inbound)
if err != nil { if err != nil {
return nil, err return false, err
} }
clientId := "" clientId := ""
clientOldEnabled := false
for _, oldClient := range oldClients { for _, oldClient := range oldClients {
if oldClient.Email == clientEmail { if oldClient.Email == clientEmail {
@ -702,45 +703,44 @@ func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (*xray.Cl
} else { } else {
clientId = oldClient.ID clientId = oldClient.ID
} }
clientOldEnabled = oldClient.Enable
break break
} }
} }
if len(clientId) == 0 { 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{} var settings map[string]interface{}
err = json.Unmarshal([]byte(inbound.Settings), &settings) err = json.Unmarshal([]byte(inbound.Settings), &settings)
if err != nil { if err != nil {
return nil, err return false, err
} }
clients := settings["clients"].([]interface{}) clients := settings["clients"].([]interface{})
var newClients []interface{} var newClients []interface{}
for client_index := range clients { for client_index := range clients {
c := clients[client_index].(map[string]interface{}) c := clients[client_index].(map[string]interface{})
if c["email"] == clientEmail { if c["email"] == clientEmail {
c["enable"] = traffic.Enable c["enable"] = !clientOldEnabled
newClients = append(newClients, interface{}(c)) newClients = append(newClients, interface{}(c))
} }
} }
settings["clients"] = newClients settings["clients"] = newClients
modifiedSettings, err := json.MarshalIndent(settings, "", " ") modifiedSettings, err := json.MarshalIndent(settings, "", " ")
if err != nil { if err != nil {
return nil, err return false, err
} }
inbound.Settings = string(modifiedSettings) 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 { func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) error {
traffic, inbound, err := s.GetClientInboundByEmail(clientEmail) _, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil { if err != nil {
return err return err
} }
if inbound == nil || traffic == nil { if inbound == nil {
return common.NewError("Inbound Not Found For Email:", clientEmail) 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 { func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) error {
traffic, inbound, err := s.GetClientInboundByEmail(clientEmail) _, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil { if err != nil {
return err return err
} }
if inbound == nil || traffic == nil { if inbound == nil {
return common.NewError("Inbound Not Found For Email:", clientEmail) return common.NewError("Inbound Not Found For Email:", clientEmail)
} }

View File

@ -296,10 +296,10 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo
t.sendCallbackAnswerTgBot(callbackQuery.ID, "✅ %s : Get IP Log.") t.sendCallbackAnswerTgBot(callbackQuery.ID, "✅ %s : Get IP Log.")
t.searchClientIps(callbackQuery.From.ID, email) t.searchClientIps(callbackQuery.From.ID, email)
case "toggle_enable": case "toggle_enable":
trrafic, err := t.inboundService.ToggleClientEnableByEmail(email) enabled, err := t.inboundService.ToggleClientEnableByEmail(email)
if err == nil { if err == nil {
t.xrayService.SetToNeedRestart() t.xrayService.SetToNeedRestart()
if trrafic.Enable { if enabled {
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Enabled successfully.", email)) t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Enabled successfully.", email))
} else { } else {
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Disabled successfully.", email)) 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 { if err != nil || len(ips) == 0 {
ips = "No IP Record" 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( var inlineKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("🔄 Refresh", "ips_refresh "+email), 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.NewInlineKeyboardButtonData("🔢 IP Limit", "ip_limit "+email),
), ),
tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("💡 Enable / Disable", "toggle_enable "+email), tgbotapi.NewInlineKeyboardButtonData("🔘 Enable / Disable", "toggle_enable "+email),
), ),
) )
if len(messageID) > 0 { if len(messageID) > 0 {