diff options
| author | MHSanaei <ho3ein.sanaei@gmail.com> | 2023-04-25 14:08:35 +0300 |
|---|---|---|
| committer | MHSanaei <ho3ein.sanaei@gmail.com> | 2023-04-25 14:08:35 +0300 |
| commit | cc3ff61ae215bdaec0c1ae0c3ee232156015c6b4 (patch) | |
| tree | 0c4e9f1544b1cd50e03a22fa9a9b94838f90e511 /web/service | |
| parent | 045717010a74fa609670caf495458d8ffc99f98f (diff) | |
update by client id
Co-Authored-By: Alireza Ahmadi <alireza7@gmail.com>
Diffstat (limited to 'web/service')
| -rw-r--r-- | web/service/inbound.go | 48 | ||||
| -rw-r--r-- | web/service/tgbot.go | 40 |
2 files changed, 54 insertions, 34 deletions
diff --git a/web/service/inbound.go b/web/service/inbound.go index 5d6acb92..0245ffa7 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -367,7 +367,7 @@ func (s *InboundService) DelInboundClient(inboundId int, clientId string) error return db.Save(oldInbound).Error } -func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) error { +func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId string) error { clients, err := s.getClients(data) if err != nil { return err @@ -391,7 +391,23 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err return err } - if len(clients[0].Email) > 0 && clients[0].Email != oldClients[index].Email { + oldEmail := "" + clientIndex := 0 + for index, oldClient := range oldClients { + oldClientId := "" + if oldInbound.Protocol == "trojan" { + oldClientId = oldClient.Password + } else { + oldClientId = oldClient.ID + } + if clientId == oldClientId { + oldEmail = oldClient.Email + clientIndex = index + break + } + } + + if len(clients[0].Email) > 0 && clients[0].Email != oldEmail { existEmail, err := s.checkEmailsExistForClients(clients) if err != nil { return err @@ -406,10 +422,8 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err if err != nil { return err } - settingsClients := oldSettings["clients"].([]interface{}) - settingsClients[index] = inerfaceClients[0] - + settingsClients[clientIndex] = inerfaceClients[0] oldSettings["clients"] = settingsClients newSettings, err := json.MarshalIndent(oldSettings, "", " ") @@ -421,12 +435,12 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err db := database.GetDB() if len(clients[0].Email) > 0 { - if len(oldClients[index].Email) > 0 { - err = s.UpdateClientStat(oldClients[index].Email, &clients[0]) + if len(oldEmail) > 0 { + err = s.UpdateClientStat(oldEmail, &clients[0]) if err != nil { return err } - err = s.UpdateClientIPs(db, oldClients[index].Email, clients[0].Email) + err = s.UpdateClientIPs(db, oldEmail, clients[0].Email) if err != nil { return err } @@ -434,11 +448,11 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, index int) err s.AddClientStat(data.Id, &clients[0]) } } else { - err = s.DelClientStat(db, oldClients[index].Email) + err = s.DelClientStat(db, oldEmail) if err != nil { return err } - err = s.DelClientIPs(db, oldClients[index].Email) + err = s.DelClientIPs(db, oldEmail) if err != nil { return err } @@ -667,8 +681,15 @@ func (s *InboundService) ResetClientTraffic(id int, clientEmail string) error { func (s *InboundService) ResetAllClientTraffics(id int) error { db := database.GetDB() + whereText := "inbound_id " + if id == -1 { + whereText += " > ?" + } else { + whereText += " = ?" + } + result := db.Model(xray.ClientTraffic{}). - Where("inbound_id = ?", id). + Where(whereText, id). Updates(map[string]interface{}{"enable": true, "up": 0, "down": 0}) err := result.Error @@ -724,7 +745,7 @@ func (s *InboundService) GetClientTrafficTgBot(tguname string) ([]*xray.ClientTr return traffics, err } -func (s *InboundService) GetClientTrafficByEmail(email string) (traffic []*xray.ClientTraffic, err error) { +func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.ClientTraffic, err error) { db := database.GetDB() var traffics []*xray.ClientTraffic @@ -735,7 +756,7 @@ func (s *InboundService) GetClientTrafficByEmail(email string) (traffic []*xray. return nil, err } } - return traffics, err + return traffics[0], err } func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) { @@ -809,6 +830,7 @@ func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error) } return inbounds, nil } + func (s *InboundService) MigrationRequirements() { db := database.GetDB() var inbounds []*model.Inbound diff --git a/web/service/tgbot.go b/web/service/tgbot.go index b40e0e29..4703c0ca 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -404,38 +404,36 @@ func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) { } func (t *Tgbot) searchClient(chatId int64, email string) { - traffics, err := t.inboundService.GetClientTrafficByEmail(email) + traffic, err := t.inboundService.GetClientTrafficByEmail(email) if err != nil { logger.Warning(err) msg := "❌ Something went wrong!" t.SendMsgToTgbot(chatId, msg) return } - if len(traffics) == 0 { + if traffic == nil { msg := "No result!" t.SendMsgToTgbot(chatId, msg) return } - for _, traffic := range traffics { - expiryTime := "" - if traffic.ExpiryTime == 0 { - expiryTime = "♾Unlimited" - } else if traffic.ExpiryTime < 0 { - expiryTime = fmt.Sprintf("%d days", traffic.ExpiryTime/-86400000) - } 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) + expiryTime := "" + if traffic.ExpiryTime == 0 { + expiryTime = "♾Unlimited" + } else if traffic.ExpiryTime < 0 { + expiryTime = fmt.Sprintf("%d days", traffic.ExpiryTime/-86400000) + } 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) searchInbound(chatId int64, remark string) { |
