diff --git a/web/service/inbound.go b/web/service/inbound.go index 08965899..57d3933c 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -58,7 +58,8 @@ func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound,erro return inbound, common.NewError("端口已存在:", inbound.Port) } db := database.GetDB() - + s.UpdateClientStat(inbound.Id,inbound.Settings) + return inbound, db.Save(inbound).Error } @@ -136,6 +137,7 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound, oldInbound.Sniffing = inbound.Sniffing oldInbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port) + s.UpdateClientStat(inbound.Id,inbound.Settings) db := database.GetDB() return inbound, db.Save(oldInbound).Error } @@ -236,6 +238,36 @@ func (s *InboundService) DisableInvalidClients() (int64, error) { count := result.RowsAffected return count, err } +func (s *InboundService) UpdateClientStat(inboundId int, inboundSettings string) (error) { + db := database.GetDB() + + // get settings clients + settings := map[string][]model.Client{} + json.Unmarshal([]byte(inboundSettings), &settings) + clients := settings["clients"] + for _, client := range clients { + result := db.Model(xray.ClientTraffic{}). + Where("inbound_id = ? and email = ?", inboundId, client.Email). + Updates(map[string]interface{}{"enable": true, "total": client.TotalGB, "expiry_time": client.ExpiryTime}) + if result.RowsAffected == 0 { + clientTraffic := xray.ClientTraffic{} + clientTraffic.InboundId = inboundId + clientTraffic.Email = client.Email + clientTraffic.Total = client.TotalGB + clientTraffic.ExpiryTime = client.ExpiryTime + clientTraffic.Enable = true + clientTraffic.Up = 0 + clientTraffic.Down = 0 + db.Create(&clientTraffic) + } + err := result.Error + if err != nil { + return err + } + + } + return nil +} func (s *InboundService) GetInboundClientIps(clientEmail string) (string, error) { db := database.GetDB() diff --git a/web/service/xray.go b/web/service/xray.go index e664d064..259e8975 100644 --- a/web/service/xray.go +++ b/web/service/xray.go @@ -4,8 +4,6 @@ import ( "encoding/json" "errors" "sync" - "time" - "x-ui/logger" "x-ui/xray" "x-ui/database/model" @@ -69,6 +67,8 @@ func (s *XrayService) GetXrayConfig() (*xray.Config, error) { return nil, err } + s.inboundService.DisableInvalidClients() + inbounds, err := s.inboundService.GetAllInbounds() if err != nil { return nil, err @@ -85,17 +85,15 @@ func (s *XrayService) GetXrayConfig() (*xray.Config, error) { // check users active or not - now := time.Now().Unix() * 1000 clientStats := inbound.ClientStats for _, clientTraffic := range clientStats { for index, client := range clients { if client.Email == clientTraffic.Email { - totalUsage := clientTraffic.Up + clientTraffic.Down - if totalUsage > client.TotalGB || (client.ExpiryTime > 0 && client.ExpiryTime <= now){ + if ! clientTraffic.Enable { clients = RemoveIndex(clients,index) - logger.Debug("Remove Inbound User",client.Email) + logger.Info("Remove Inbound User",client.Email) }