mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-02 23:45:11 +03:00
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:
@@ -93,10 +93,8 @@ type Remote struct {
|
||||
// seen, config pushes to this node are zstd-compressed. Old nodes never set
|
||||
// it, so they keep receiving plain bodies (mixed-version safe).
|
||||
supportsZstd bool
|
||||
// capsSeen tells a node that advertised no AmneziaWG kernel support apart
|
||||
// from one this Remote has not heard from yet.
|
||||
capsSeen bool
|
||||
supportsAWGKernel bool
|
||||
// nodeCaps is nil until the first response, even if that response advertises no capabilities.
|
||||
nodeCaps []string
|
||||
|
||||
// Per-node client honoring the TLS verify mode, built once and reused; a
|
||||
// node config change drops the cached Remote so the next one rebuilds it.
|
||||
@@ -140,8 +138,7 @@ func (r *Remote) recordCaps(h http.Header) {
|
||||
caps := strings.Split(h.Get(wirecodec.CapsHeader), ",")
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.capsSeen = true
|
||||
r.supportsAWGKernel = slices.Contains(caps, wirecodec.CapAmneziaWGKernel)
|
||||
r.nodeCaps = caps
|
||||
if slices.Contains(caps, wirecodec.CapZstd) {
|
||||
r.supportsZstd = true
|
||||
}
|
||||
@@ -157,23 +154,61 @@ func (r *Remote) requireAmneziaWGKernel(ctx context.Context, ib *model.Inbound)
|
||||
if err := json.Unmarshal([]byte(ib.Settings), &settings); err != nil || settings.Server == nil || !settings.Server.KernelMode {
|
||||
return nil
|
||||
}
|
||||
r.mu.RLock()
|
||||
seen := r.capsSeen
|
||||
r.mu.RUnlock()
|
||||
if !seen {
|
||||
if err := r.refreshRemoteIDs(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
supported, err := r.nodeSupports(ctx, wirecodec.CapAmneziaWGKernel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.mu.RLock()
|
||||
supported := r.supportsAWGKernel
|
||||
r.mu.RUnlock()
|
||||
if !supported {
|
||||
return fmt.Errorf("node %s does not support AmneziaWG kernel mode; upgrade its panel or switch the inbound to the embedded datapath", r.node.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Remote) nodeSupports(ctx context.Context, capability string) (bool, error) {
|
||||
r.mu.RLock()
|
||||
caps := r.nodeCaps
|
||||
r.mu.RUnlock()
|
||||
if caps == nil {
|
||||
if err := r.refreshRemoteIDs(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
r.mu.RLock()
|
||||
caps = r.nodeCaps
|
||||
r.mu.RUnlock()
|
||||
}
|
||||
return slices.Contains(caps, capability), nil
|
||||
}
|
||||
|
||||
func (r *Remote) checkInboundCapabilities(ctx context.Context, ib *model.Inbound) error {
|
||||
if err := r.requireAmneziaWGKernel(ctx, ib); err != nil {
|
||||
return err
|
||||
}
|
||||
if !ib.PreservesClientTraffic() || !ib.HasTrafficResetSchedule() {
|
||||
return nil
|
||||
}
|
||||
return r.requirePreserveClientTraffic(ctx)
|
||||
}
|
||||
|
||||
// CheckPreserveClientTraffic probes the current node build before the master
|
||||
// commits an opt-in. An upgrade or rollback can make previously learned caps stale.
|
||||
func (r *Remote) CheckPreserveClientTraffic(ctx context.Context) error {
|
||||
if _, err := r.do(ctx, http.MethodGet, "panel/api/server/status", nil); err != nil {
|
||||
return fmt.Errorf("cannot verify support for keeping client usage on node %s: %w", r.node.Name, err)
|
||||
}
|
||||
return r.requirePreserveClientTraffic(ctx)
|
||||
}
|
||||
|
||||
func (r *Remote) requirePreserveClientTraffic(ctx context.Context) error {
|
||||
supported, err := r.nodeSupports(ctx, wirecodec.CapPreserveClientTraffic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !supported {
|
||||
return fmt.Errorf("node %s does not support inbound-only scheduled traffic resets; upgrade its panel before enabling this option", r.node.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// httpClient lazily builds and caches the per-node client honoring the TLS
|
||||
// verify mode, so Remote ops don't fall back to system CA on skip/pin (#5264).
|
||||
func (r *Remote) httpClient() (*http.Client, error) {
|
||||
@@ -469,7 +504,7 @@ func (r *Remote) refreshRemoteIDs(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (r *Remote) AddInbound(ctx context.Context, ib *model.Inbound) error {
|
||||
if err := r.requireAmneziaWGKernel(ctx, ib); err != nil {
|
||||
if err := r.checkInboundCapabilities(ctx, ib); err != nil {
|
||||
return err
|
||||
}
|
||||
payload := wireInbound(ib, r.node.Id)
|
||||
@@ -508,7 +543,7 @@ func (r *Remote) UpdateInbound(ctx context.Context, oldIb, newIb *model.Inbound)
|
||||
if err != nil {
|
||||
return r.AddInbound(ctx, newIb)
|
||||
}
|
||||
if err := r.requireAmneziaWGKernel(ctx, newIb); err != nil {
|
||||
if err := r.checkInboundCapabilities(ctx, newIb); err != nil {
|
||||
return err
|
||||
}
|
||||
payload := wireInbound(newIb, r.node.Id)
|
||||
@@ -928,6 +963,7 @@ func wireInbound(ib *model.Inbound, remoteNodeID int) url.Values {
|
||||
if ib.TrafficResetDay > 0 {
|
||||
v.Set("trafficResetDay", strconv.Itoa(ib.TrafficResetDay))
|
||||
}
|
||||
v.Set("preserveClientTraffic", strconv.FormatBool(ib.PreservesClientTraffic()))
|
||||
return v
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/wirecodec"
|
||||
)
|
||||
|
||||
@@ -91,6 +92,34 @@ func TestRemoteSendsPlainWhenNoCap(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteRequiresSupportForInboundOnlyScheduledReset(t *testing.T) {
|
||||
for _, caps := range []string{wirecodec.CapZstd, wirecodec.NodeCaps} {
|
||||
t.Run(caps, func(t *testing.T) {
|
||||
var pushed bool
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(wirecodec.CapsHeader, caps)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.Method == http.MethodPost {
|
||||
pushed = true
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"success":true,"obj":[]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
|
||||
ib := &model.Inbound{TrafficReset: "monthly", PreserveClientTraffic: new(true)}
|
||||
err := r.AddInbound(context.Background(), ib)
|
||||
if caps == wirecodec.NodeCaps {
|
||||
if err != nil || !pushed {
|
||||
t.Fatalf("capable node: pushed = %v, error = %v", pushed, err)
|
||||
}
|
||||
} else if pushed || err == nil || !strings.Contains(err.Error(), "does not support inbound-only scheduled traffic resets") {
|
||||
t.Fatalf("older node: pushed = %v, error = %v", pushed, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRemoteRefusesKernelAmneziaWGOnNodeWithoutCap: an older node drops the
|
||||
// kernelMode flag and silently runs the inbound on the embedded datapath.
|
||||
func TestRemoteRefusesKernelAmneziaWGOnNodeWithoutCap(t *testing.T) {
|
||||
|
||||
@@ -303,18 +303,24 @@ func TestIsNonEmptySlice(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWireInboundTrafficReset(t *testing.T) {
|
||||
with := wireInbound(&model.Inbound{TrafficReset: "monthly", TrafficResetDay: 15}, 0)
|
||||
with := wireInbound(&model.Inbound{TrafficReset: "monthly", TrafficResetDay: 15, PreserveClientTraffic: new(true)}, 0)
|
||||
if got := with.Get("trafficReset"); got != "monthly" {
|
||||
t.Fatalf("trafficReset = %q, want monthly", got)
|
||||
}
|
||||
if got := with.Get("trafficResetDay"); got != "15" {
|
||||
t.Fatalf("trafficResetDay = %q, want 15", got)
|
||||
}
|
||||
if got := with.Get("preserveClientTraffic"); got != "true" {
|
||||
t.Fatalf("preserveClientTraffic = %q, want true", got)
|
||||
}
|
||||
// Empty TrafficReset must be omitted entirely, not sent as an empty field.
|
||||
without := wireInbound(&model.Inbound{}, 0)
|
||||
if without.Has("trafficReset") {
|
||||
t.Fatalf("trafficReset must be omitted when empty, got %q", without.Get("trafficReset"))
|
||||
}
|
||||
if got := without.Get("preserveClientTraffic"); got != "false" {
|
||||
t.Fatalf("preserveClientTraffic = %q, want false", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWireInboundDefaultsShareAddressStrategy(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user