fix(traffic): preserve client usage on scheduled inbound resets (#7136)

A client attached to several inbounds shares one usage counter. When any one of those inbounds reached its scheduled reset, the job zeroed the client's whole usage, across inbounds and nodes, although the client's own cycle resets on another day (#7135).

Add a per-inbound "Keep client usage" switch, off by default: with it on, the scheduled reset clears only the inbound's own counters. The setting is pushed to nodes and read back from their snapshots. A node that does not advertise the capability is refused when the switch is saved, not later at push time, so an older node never ends up with a config it cannot apply.
This commit is contained in:
dkgks
2026-11-24 21:02:13 +08:00
committed by GitHub
parent ccdda6f728
commit 5d76c0a1c6
41 changed files with 504 additions and 29 deletions
+11
View File
@@ -114,6 +114,14 @@ func migrateInboundExcludeFromSubColumn() error {
return migrator.AddColumn(&model.Inbound{}, "ExcludeFromSub")
}
func migrateInboundPreserveClientTrafficColumn(tx *gorm.DB) error {
migrator := tx.Migrator()
if !migrator.HasTable(&model.Inbound{}) || migrator.HasColumn(&model.Inbound{}, "preserve_client_traffic") {
return nil
}
return migrator.AddColumn(&model.Inbound{}, "PreserveClientTraffic")
}
func migrateClientKeepAliveRangeColumn() error {
migrator := db.Migrator()
if !migrator.HasTable(&model.ClientRecord{}) || migrator.HasColumn(&model.ClientRecord{}, "wg_keep_alive_range") {
@@ -135,6 +143,9 @@ func initModels() error {
if err := migrateInboundExcludeFromSubColumn(); err != nil {
return err
}
if err := migrateInboundPreserveClientTrafficColumn(db); err != nil {
return err
}
models := allModels()
for _, mdl := range models {
if IsPostgres() && postgresModelSettled(mdl) {
@@ -0,0 +1,47 @@
package database
import (
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
func TestMigrateInboundPreserveClientTrafficColumn(t *testing.T) {
initMigrateDB(t)
tx := GetDB().Begin()
if tx.Error != nil {
t.Fatal(tx.Error)
}
t.Cleanup(func() { tx.Rollback() })
// Recreate the previous schema in this transaction on SQLite or PostgreSQL.
if err := tx.Exec("ALTER TABLE inbounds DROP COLUMN preserve_client_traffic").Error; err != nil {
t.Fatal(err)
}
const tag = "legacy-preserve-client-traffic"
if err := tx.Exec("INSERT INTO inbounds (tag, up, down, traffic_reset, traffic_reset_day) VALUES (?, 30, 50, 'monthly', 20)", tag).Error; err != nil {
t.Fatal(err)
}
if err := migrateInboundPreserveClientTrafficColumn(tx); err != nil {
t.Fatal(err)
}
var stored model.Inbound
if err := tx.Where("tag = ?", tag).First(&stored).Error; err != nil {
t.Fatal(err)
}
if stored.PreserveClientTraffic == nil || *stored.PreserveClientTraffic {
t.Fatal("legacy inbound must have a non-null, false preserve_client_traffic")
}
if stored.Up != 30 || stored.Down != 50 || stored.TrafficReset != "monthly" || stored.TrafficResetDay != 20 {
t.Fatalf("legacy inbound after migration: up=%d down=%d schedule=%s day=%d", stored.Up, stored.Down, stored.TrafficReset, stored.TrafficResetDay)
}
if err := tx.Model(&stored).Update("preserve_client_traffic", true).Error; err != nil {
t.Fatal(err)
}
if err := migrateInboundPreserveClientTrafficColumn(tx); err != nil {
t.Fatalf("repeat migration: %v", err)
}
if err := tx.First(&stored, stored.Id).Error; err != nil || !stored.PreservesClientTraffic() {
t.Fatalf("repeat migration lost the opt-in: preserve=%t, err=%v", stored.PreservesClientTraffic(), err)
}
}
+13
View File
@@ -66,6 +66,9 @@ type Inbound struct {
LastTrafficResetTime int64 `json:"lastTrafficResetTime" form:"lastTrafficResetTime" gorm:"default:0"` // Last traffic reset timestamp
ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id" json:"clientStats" form:"clientStats"` // Client traffic statistics
// Older snapshots omit this field.
PreserveClientTraffic *bool `json:"preserveClientTraffic,omitempty" form:"preserveClientTraffic" gorm:"not null;default:false"` // Keep client usage during scheduled inbound resets
// Xray configuration fields
Listen string `json:"listen" form:"listen"`
Port int `json:"port" form:"port" validate:"gte=0,lte=65535" example:"443"`
@@ -189,6 +192,16 @@ type ApiToken struct {
ExpiresAt int64 `json:"expiresAt" gorm:"not null;default:0"`
}
// PreservesClientTraffic reports whether a scheduled inbound reset keeps client usage.
func (i Inbound) PreservesClientTraffic() bool {
return i.PreserveClientTraffic != nil && *i.PreserveClientTraffic
}
// HasTrafficResetSchedule reports whether the inbound has periodic resets enabled.
func (i *Inbound) HasTrafficResetSchedule() bool {
return i.TrafficReset != "" && i.TrafficReset != "never"
}
// MarshalJSON emits settings, streamSettings, and sniffing as nested JSON
// objects rather than escaped strings, so API consumers don't need to JSON.parse
// a string inside a string. Empty fields render as null; fields whose stored