Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'web/service')
-rw-r--r--web/service/inbound.go55
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 {