From 5856160c30fb3f525b584fb25a35ec78c8fb0488 Mon Sep 17 00:00:00 2001 From: Masoud Hidden Date: Fri, 5 May 2023 18:20:56 +0330 Subject: Added some new buttons to bot and ability to use userId in tgId --- web/service/inbound.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 117 insertions(+), 6 deletions(-) (limited to 'web/service/inbound.go') 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) 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 || 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 { - inbound, err := s.GetClientInboundByEmail(clientEmail) + 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) } -- cgit v1.2.3 From d349bffcd6ba547ed9e4cdf5b199c5100a99437f Mon Sep 17 00:00:00 2001 From: Masoud Hidden Date: Fri, 5 May 2023 19:50:40 +0330 Subject: Fix bot client enable button --- web/service/inbound.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'web/service/inbound.go') 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) } -- cgit v1.2.3