From 9623e875113adad1fcbae7370e043db040462f2a Mon Sep 17 00:00:00 2001 From: Vadim Iskuchekov Date: Tue, 16 Sep 2025 09:24:32 +0200 Subject: =?UTF-8?q?feat:=20Simple=20periodic=20traffic=20reset=20(for=20In?= =?UTF-8?q?bounds)=20=E2=80=93=20daily=20|=20weekly=20|=20monthly=20=20(#3?= =?UTF-8?q?407)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- web/service/inbound.go | 55 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 11 deletions(-) (limited to 'web/service') 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 { -- cgit v1.2.3