mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-10-27 02:24:40 +00:00
refactor: update traffic reset fields in Inbound model and remove unused client traffic reset job
This commit is contained in:
parent
ec7f28179c
commit
b00de0a59b
4 changed files with 3 additions and 75 deletions
|
|
@ -34,9 +34,9 @@ type Inbound struct {
|
|||
Total int64 `json:"total" form:"total"`
|
||||
AllTime int64 `json:"allTime" form:"allTime" gorm:"default:0"`
|
||||
Remark string `json:"remark" form:"remark"`
|
||||
Enable bool `json:"enable" form:"enable"`
|
||||
Enable bool `json:"enable" form:"enable" gorm:"index:idx_enable_traffic_reset,priority:1"`
|
||||
ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
|
||||
TrafficReset string `json:"trafficReset" form:"trafficReset" gorm:"default:never"`
|
||||
TrafficReset string `json:"trafficReset" form:"trafficReset" gorm:"default:never;index:idx_enable_traffic_reset,priority:2"`
|
||||
LastTrafficResetTime int64 `json:"lastTrafficResetTime" form:"lastTrafficResetTime" gorm:"default:0"`
|
||||
ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id" json:"clientStats" form:"clientStats"`
|
||||
|
||||
|
|
|
|||
|
|
@ -1,42 +0,0 @@
|
|||
package job
|
||||
|
||||
import (
|
||||
"x-ui/logger"
|
||||
"x-ui/web/service"
|
||||
)
|
||||
|
||||
type PeriodicClientTrafficResetJob struct {
|
||||
inboundService service.InboundService
|
||||
period Period
|
||||
}
|
||||
|
||||
func NewPeriodicClientTrafficResetJob(period Period) *PeriodicClientTrafficResetJob {
|
||||
return &PeriodicClientTrafficResetJob{
|
||||
period: period,
|
||||
}
|
||||
}
|
||||
|
||||
func (j *PeriodicClientTrafficResetJob) Run() {
|
||||
clients, err := j.inboundService.GetClientsByTrafficReset(string(j.period))
|
||||
logger.Infof("Running periodic client traffic reset job for period: %s", j.period)
|
||||
if err != nil {
|
||||
logger.Warning("Failed to get clients for traffic reset:", err)
|
||||
return
|
||||
}
|
||||
|
||||
resetCount := 0
|
||||
|
||||
for _, client := range clients {
|
||||
if err := j.inboundService.ResetClientTrafficByEmail(client.Email); err != nil {
|
||||
logger.Warning("Failed to reset traffic for client", client.Email, ":", err)
|
||||
continue
|
||||
}
|
||||
|
||||
resetCount++
|
||||
logger.Infof("Reset traffic for client %s", client.Email)
|
||||
}
|
||||
|
||||
if resetCount > 0 {
|
||||
logger.Infof("Periodic client traffic reset completed: %d clients reset", resetCount)
|
||||
}
|
||||
}
|
||||
|
|
@ -51,36 +51,6 @@ func (s *InboundService) GetInboundsByTrafficReset(period string) ([]*model.Inbo
|
|||
return inbounds, nil
|
||||
}
|
||||
|
||||
func (s *InboundService) GetClientsByTrafficReset(period string) ([]model.Client, error) {
|
||||
db := database.GetDB()
|
||||
var inbounds []*model.Inbound
|
||||
|
||||
// Get all inbounds first
|
||||
err := db.Model(model.Inbound{}).Find(&inbounds).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var clientsWithReset []model.Client
|
||||
|
||||
// Parse each inbound's settings to find clients with matching traffic reset period
|
||||
for _, inbound := range inbounds {
|
||||
clients, err := s.GetClients(inbound)
|
||||
if err != nil {
|
||||
logger.Warning("Failed to get clients for inbound", inbound.Id, ":", err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, client := range clients {
|
||||
if client.TrafficReset == period {
|
||||
clientsWithReset = append(clientsWithReset, client)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return clientsWithReset, 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" {
|
||||
|
|
|
|||
|
|
@ -280,7 +280,7 @@ func (s *Server) startTask() {
|
|||
// TODO: Client traffic reset jobs
|
||||
// logger.Info("Scheduling periodic client traffic reset jobs")
|
||||
// Run once a day, midnight
|
||||
// TODO: for testing, run every minute, change back to daily later
|
||||
// TODO: for testing, run every 10s, change back to daily later
|
||||
// s.cron.AddJob("@every 10s", job.NewPeriodicClientTrafficResetJob("daily"))
|
||||
// s.cron.AddJob("* * * * *", job.NewPeriodicClientTrafficResetJob("daily"))
|
||||
// Run once a week, midnight between Sat/Sun
|
||||
|
|
|
|||
Loading…
Reference in a new issue