|
@@ -41,6 +41,16 @@ func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) {
|
|
return inbounds, nil
|
|
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) {
|
|
func (s *InboundService) checkPortExist(listen string, port int, ignoreId int) (bool, error) {
|
|
db := database.GetDB()
|
|
db := database.GetDB()
|
|
if listen == "" || listen == "0.0.0.0" || listen == "::" || listen == "::0" {
|
|
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.Remark = inbound.Remark
|
|
oldInbound.Enable = inbound.Enable
|
|
oldInbound.Enable = inbound.Enable
|
|
oldInbound.ExpiryTime = inbound.ExpiryTime
|
|
oldInbound.ExpiryTime = inbound.ExpiryTime
|
|
|
|
+ oldInbound.TrafficReset = inbound.TrafficReset
|
|
oldInbound.Listen = inbound.Listen
|
|
oldInbound.Listen = inbound.Listen
|
|
oldInbound.Port = inbound.Port
|
|
oldInbound.Port = inbound.Port
|
|
oldInbound.Protocol = inbound.Protocol
|
|
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) {
|
|
func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId string) (bool, error) {
|
|
|
|
+ // TODO: check if TrafficReset field is updating
|
|
clients, err := s.GetClients(data)
|
|
clients, err := s.GetClients(data)
|
|
if err != nil {
|
|
if err != nil {
|
|
return false, err
|
|
return false, err
|
|
@@ -1684,6 +1696,7 @@ func (s *InboundService) ResetClientTrafficLimitByEmail(clientEmail string, tota
|
|
func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
|
|
func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
|
|
db := database.GetDB()
|
|
db := database.GetDB()
|
|
|
|
|
|
|
|
+ // Reset traffic stats in ClientTraffic table
|
|
result := db.Model(xray.ClientTraffic{}).
|
|
result := db.Model(xray.ClientTraffic{}).
|
|
Where("email = ?", clientEmail).
|
|
Where("email = ?", clientEmail).
|
|
Updates(map[string]any{"enable": true, "up": 0, "down": 0})
|
|
Updates(map[string]any{"enable": true, "up": 0, "down": 0})
|
|
@@ -1692,6 +1705,7 @@ func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
+
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1759,20 +1773,39 @@ func (s *InboundService) ResetClientTraffic(id int, clientEmail string) (bool, e
|
|
|
|
|
|
func (s *InboundService) ResetAllClientTraffics(id int) error {
|
|
func (s *InboundService) ResetAllClientTraffics(id int) error {
|
|
db := database.GetDB()
|
|
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 {
|
|
func (s *InboundService) ResetAllTraffics() error {
|