Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
fix traffic bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hossinasaadi committed Nov 20, 2022
1 parent 53aa8fd commit 5509f94
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions web/service/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ func (s *InboundService) AddClientTraffic(traffics []*xray.ClientTraffic) (err e
return nil
}
db := database.GetDB()
dbInbound := db.Model(model.Inbound{})

db = db.Model(xray.ClientTraffic{})
tx := db.Begin()
defer func() {
Expand All @@ -187,13 +189,27 @@ func (s *InboundService) AddClientTraffic(traffics []*xray.ClientTraffic) (err e
tx.Commit()
}
}()
txInbound := dbInbound.Begin()
defer func() {
if err != nil {
txInbound.Rollback()
} else {
txInbound.Commit()
}
}()

for _, traffic := range traffics {
inbound := &model.Inbound{}

err := db.Model(model.Inbound{}).Where("settings like ?", "%" + traffic.Email + "%").First(inbound).Error
err := txInbound.Where("settings like ?", "%" + traffic.Email + "%").First(inbound).Error
traffic.InboundId = inbound.Id
if err != nil {
logger.Warning("AddClientTraffic find model ", err, traffic.Email)
if err == gorm.ErrRecordNotFound {
// delete removed client record
clientErr := s.DelClientStat(tx, traffic.Email)
logger.Warning(err, traffic.Email,clientErr)

}
continue
}
// get settings clients
Expand Down Expand Up @@ -274,6 +290,9 @@ func (s *InboundService) UpdateClientStat(inboundId int, inboundSettings string)
}
return nil
}
func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
return tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error
}

func (s *InboundService) GetInboundClientIps(clientEmail string) (string, error) {
db := database.GetDB()
Expand Down

0 comments on commit 5509f94

Please sign in to comment.