fix(traffic): make a client reset reach every counter enforcing its quota
Invariant: a client traffic reset zeroes every counter that enforces the
client's quota - the master's, each hosting node's, and the local MTProto
sidecar's. A node cuts a client on its own local counters, and the master
adopts that verdict when both judged the same limits (#4917).
Node counters: ResetClientTraffic tried the node once and dropped a
failure ("nothing replays a reset"); BulkResetTraffic,
ResetAllClientTraffics and ClientService.ResetAllTraffics never told the
node at all. The node kept its pre-reset usage, switched the client off
again on its next tick, and the master latched that - a client shown at
zero usage stayed disabled.
Every reset path now queues a node_pending_resets row per hosting node in
its own transaction. It is delivered right after commit (per-client
endpoint up to the push threshold, bulkResetTraffic above) and replayed by
the node sync ahead of its snapshot, and dropped only once the node
accepted it. While one is owed, the merge takes only that client's usage
from the node, not its enable or limits. Deliveries to a node are
serialized so a reset is not sent twice.
Sidecar quota: BulkResetTraffic, ClientService.ResetAllTraffics and
auto-renew zeroed the panel counters but not mtg's own quota counter, so
the sidecar kept refusing a renewed or reset MTProto client. They now
reset it too, scoped to the affected clients.
2026-09-27 17:56:53 -07:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"slices"
|
|
|
|
|
"testing"
|
2026-10-02 15:59:11 -07:00
|
|
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
fix(traffic): make a client reset reach every counter enforcing its quota
Invariant: a client traffic reset zeroes every counter that enforces the
client's quota - the master's, each hosting node's, and the local MTProto
sidecar's. A node cuts a client on its own local counters, and the master
adopts that verdict when both judged the same limits (#4917).
Node counters: ResetClientTraffic tried the node once and dropped a
failure ("nothing replays a reset"); BulkResetTraffic,
ResetAllClientTraffics and ClientService.ResetAllTraffics never told the
node at all. The node kept its pre-reset usage, switched the client off
again on its next tick, and the master latched that - a client shown at
zero usage stayed disabled.
Every reset path now queues a node_pending_resets row per hosting node in
its own transaction. It is delivered right after commit (per-client
endpoint up to the push threshold, bulkResetTraffic above) and replayed by
the node sync ahead of its snapshot, and dropped only once the node
accepted it. While one is owed, the merge takes only that client's usage
from the node, not its enable or limits. Deliveries to a node are
serialized so a reset is not sent twice.
Sidecar quota: BulkResetTraffic, ClientService.ResetAllTraffics and
auto-renew zeroed the panel counters but not mtg's own quota counter, so
the sidecar kept refusing a renewed or reset MTProto client. They now
reset it too, scoped to the affected clients.
2026-09-27 17:56:53 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// The master replays a node's reset backlog through the node's bulk endpoint.
|
|
|
|
|
func TestRemoteResetClientTrafficsPostsEmailsToBulkEndpoint(t *testing.T) {
|
|
|
|
|
var path string
|
|
|
|
|
var body struct {
|
|
|
|
|
Emails []string `json:"emails"`
|
|
|
|
|
}
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
path = r.URL.Path
|
|
|
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
_, _ = w.Write([]byte(`{"success":true}`))
|
|
|
|
|
}))
|
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
|
|
|
|
|
r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
|
|
|
|
|
if err := r.ResetClientTraffics(context.Background(), []string{"a@x", "b@x"}); err != nil {
|
|
|
|
|
t.Fatalf("ResetClientTraffics: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if path != "/panel/api/clients/bulkResetTraffic" || !slices.Equal(body.Emails, []string{"a@x", "b@x"}) {
|
|
|
|
|
t.Fatalf("node got %s %v, want /panel/api/clients/bulkResetTraffic [a@x b@x]", path, body.Emails)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-02 15:59:11 -07:00
|
|
|
|
|
|
|
|
// A central inbound id need not match the node's id, so the reset must target
|
|
|
|
|
// the node-side id resolved from the tag, never ib.Id.
|
|
|
|
|
func TestRemoteResetInboundTrafficUsesNodeInboundID(t *testing.T) {
|
|
|
|
|
var method, path string
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
method, path = req.Method, req.URL.Path
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
_, _ = w.Write([]byte(`{"success":true,"msg":"ok"}`))
|
|
|
|
|
}))
|
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
|
|
|
|
|
r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
|
|
|
|
|
r.cacheSet("n1-in-443", 7)
|
|
|
|
|
ib := &model.Inbound{Id: 42, Tag: "n1-in-443"}
|
|
|
|
|
if err := r.ResetInboundTraffic(context.Background(), ib); err != nil {
|
|
|
|
|
t.Fatalf("ResetInboundTraffic: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if method != http.MethodPost || path != "/panel/api/inbounds/7/resetTraffic" {
|
|
|
|
|
t.Fatalf("node got %s %s, want POST /panel/api/inbounds/7/resetTraffic", method, path)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// An unresolvable tag must fail before posting, so a reset never lands on an
|
|
|
|
|
// unrelated node inbound that happens to share the central id.
|
|
|
|
|
func TestRemoteResetInboundTrafficUnknownTagErrors(t *testing.T) {
|
|
|
|
|
var resetPosted bool
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
if req.URL.Path == "/panel/api/inbounds/list" {
|
|
|
|
|
_, _ = w.Write([]byte(`{"success":true,"msg":"ok","obj":[]}`))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resetPosted = true
|
|
|
|
|
_, _ = w.Write([]byte(`{"success":true,"msg":"ok"}`))
|
|
|
|
|
}))
|
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
|
|
|
|
|
r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
|
|
|
|
|
ib := &model.Inbound{Id: 42, Tag: "n1-in-443"}
|
|
|
|
|
if err := r.ResetInboundTraffic(context.Background(), ib); err == nil {
|
|
|
|
|
t.Fatal("ResetInboundTraffic error = nil, want unknown-tag error")
|
|
|
|
|
}
|
|
|
|
|
if resetPosted {
|
|
|
|
|
t.Fatal("reset request posted to node despite an unresolved tag")
|
|
|
|
|
}
|
|
|
|
|
}
|