diff options
| author | Vadim Iskuchekov <egregors@pm.me> | 2025-09-16 10:24:32 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-16 10:24:32 +0300 |
| commit | 9623e875113adad1fcbae7370e043db040462f2a (patch) | |
| tree | 925a7b445bfb7bd41b4bdec665d5c75353dafcfa /web/service | |
| parent | bc0518391ef06d3d6b9f826085b73d1f1e35c913 (diff) | |
feat: Simple periodic traffic reset (for Inbounds) – daily | weekly | monthly (#3407)
* Add periodic traffic reset feature model and ui with localization support
* Remove periodic traffic reset fields from client
* fix: add periodicTrafficReset field to inbound data structure
* feat: implement periodic traffic reset job and integrate with cron scheduler
* feat: enhance periodic traffic reset functionality with scheduling and inbound filtering
* refactor: rename periodicTrafficReset to trafficReset and add lastTrafficResetTime field
* feat: add periodic client traffic reset job and schedule tasks
* Update web/job/periodic_traffic_reset_job.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update web/job/periodic_client_traffic_reset_job.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update web/service/inbound.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* refactor: rename periodicTrafficReset to trafficReset and add lastTrafficResetTime
* feat: add last traffic reset time display and update logic in inbound service
* fix: correct log message for completed periodic traffic reset
* refactor: update traffic reset fields in Inbound model and remove unused client traffic reset job
* refactor: remove unused traffic reset logic and clean up client model fields
* cleanup comments
* fix
Diffstat (limited to 'web/service')
| -rw-r--r-- | web/service/inbound.go | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/web/service/inbound.go b/web/service/inbound.go index 2646b1e7..9d2e09dd 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -41,6 +41,16 @@ func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) { return inbounds, nil } +func (s *InboundService) GetInboundsByTrafficReset(period string) ([]*model.Inbound, error) { + db := database.GetDB() + var inbounds []*model.Inbound + err := db.Model(model.Inbound{}).Where("traffic_reset = ?", period).Find(&inbounds).Error + if err != nil && err != gorm.ErrRecordNotFound { + return nil, err + } + return inbounds, nil +} + func (s *InboundService) checkPortExist(listen string, port int, ignoreId int) (bool, error) { db := database.GetDB() if listen == "" || listen == "0.0.0.0" || listen == "::" || listen == "::0" { @@ -409,6 +419,7 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound, oldInbound.Remark = inbound.Remark oldInbound.Enable = inbound.Enable oldInbound.ExpiryTime = inbound.ExpiryTime + oldInbound.TrafficReset = inbound.TrafficReset oldInbound.Listen = inbound.Listen oldInbound.Port = inbound.Port oldInbound.Protocol = inbound.Protocol @@ -698,6 +709,7 @@ func (s *InboundService) DelInboundClient(inboundId int, clientId string) (bool, } func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId string) (bool, error) { + // TODO: check if TrafficReset field is updating clients, err := s.GetClients(data) if err != nil { return false, err @@ -1684,6 +1696,7 @@ func (s *InboundService) ResetClientTrafficLimitByEmail(clientEmail string, tota func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error { db := database.GetDB() + // Reset traffic stats in ClientTraffic table result := db.Model(xray.ClientTraffic{}). Where("email = ?", clientEmail). Updates(map[string]any{"enable": true, "up": 0, "down": 0}) @@ -1692,6 +1705,7 @@ func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error { if err != nil { return err } + return nil } @@ -1759,20 +1773,39 @@ func (s *InboundService) ResetClientTraffic(id int, clientEmail string) (bool, e func (s *InboundService) ResetAllClientTraffics(id int) error { db := database.GetDB() + now := time.Now().Unix() * 1000 - whereText := "inbound_id " - if id == -1 { - whereText += " > ?" - } else { - whereText += " = ?" - } + return db.Transaction(func(tx *gorm.DB) error { + whereText := "inbound_id " + if id == -1 { + whereText += " > ?" + } else { + whereText += " = ?" + } - result := db.Model(xray.ClientTraffic{}). - Where(whereText, id). - Updates(map[string]any{"enable": true, "up": 0, "down": 0}) + // Reset client traffics + result := tx.Model(xray.ClientTraffic{}). + Where(whereText, id). + Updates(map[string]any{"enable": true, "up": 0, "down": 0}) - err := result.Error - return err + if result.Error != nil { + return result.Error + } + + // Update lastTrafficResetTime for the inbound(s) + inboundWhereText := "id " + if id == -1 { + inboundWhereText += " > ?" + } else { + inboundWhereText += " = ?" + } + + result = tx.Model(model.Inbound{}). + Where(inboundWhereText, id). + Update("last_traffic_reset_time", now) + + return result.Error + }) } func (s *InboundService) ResetAllTraffics() error { |
