Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHo3ein <ho3ein.sanaei@gmail.com>2023-05-06 02:18:14 +0300
committerGitHub <noreply@github.com>2023-05-06 02:18:14 +0300
commit78638a97373bccc761f46cbbef0fda34a8dedd6c (patch)
tree8dffbad741ffaf05ff0e00962f4773065df52f0c /web/service/inbound.go
parent3ee3432d8f1685e95ff48e1560f6444053613655 (diff)
parent7f8f0b0f2d8dd1935455d6abf4e511c4d4d93ba7 (diff)
Merge pull request #346 from masoud-hidden/main
Some new buttons for bot and ability to use userId in tgId
Diffstat (limited to 'web/service/inbound.go')
-rw-r--r--web/service/inbound.go121
1 files changed, 116 insertions, 5 deletions
diff --git a/web/service/inbound.go b/web/service/inbound.go
index 5d80c816..dd633c4b 100644
--- a/web/service/inbound.go
+++ b/web/service/inbound.go
@@ -664,22 +664,133 @@ 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) (bool, error) {
+ _, inbound, err := s.GetClientInboundByEmail(clientEmail)
+ if err != nil {
+ return false, err
+ }
+ if inbound == nil {
+ return false, common.NewError("Inbound Not Found For Email:", clientEmail)
+ }
+
+ oldClients, err := s.getClients(inbound)
+ if err != nil {
+ return false, err
+ }
+
+ clientId := ""
+ clientOldEnabled := false
+
+ for _, oldClient := range oldClients {
+ if oldClient.Email == clientEmail {
+ if inbound.Protocol == "trojan" {
+ clientId = oldClient.Password
+ } else {
+ clientId = oldClient.ID
+ }
+ clientOldEnabled = oldClient.Enable
+ break
+ }
+ }
+
+ if len(clientId) == 0 {
+ return false, common.NewError("Client Not Found For Email:", clientEmail)
+ }
+
+ var settings map[string]interface{}
+ err = json.Unmarshal([]byte(inbound.Settings), &settings)
+ if err != nil {
+ 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"] = !clientOldEnabled
+ newClients = append(newClients, interface{}(c))
+ }
+ }
+ settings["clients"] = newClients
+ modifiedSettings, err := json.MarshalIndent(settings, "", " ")
+ if err != nil {
+ return false, err
+ }
+ inbound.Settings = string(modifiedSettings)
+ return !clientOldEnabled, s.UpdateInboundClient(inbound, clientId)
+}
+
+func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) error {
+ _, inbound, err := s.GetClientInboundByEmail(clientEmail)
+ if err != nil {
+ return err
+ }
+ 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["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)
+ _, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil {
return err
}