2026-06-20 01:35:46 -07:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-08-14 11:11:19 -07:00
|
|
|
"errors"
|
2026-06-20 01:35:46 -07:00
|
|
|
"fmt"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2026-08-14 11:11:19 -07:00
|
|
|
"gorm.io/gorm"
|
style: adopt golangci-lint v2 and resolve all findings
Add .golangci.yml (v2): the standard linters plus bodyclose, errorlint, noctx, misspell, rowserrcheck, sqlclosecheck, unconvert, usestdlibvars, with gofumpt + goimports formatters. Enable the std-error-handling exclusion preset for idiomatic Close/Remove/Setenv ignores; scope-exclude SA1019 (parser.ParseDir in tools/openapigen) and ST1005 (intentional capitalized user-facing error copy that tests assert verbatim). No inline nolint directives were introduced.
Resolve all 217 findings behavior-preserving: gofumpt/goimports formatting, explicit blank assignment on intentionally ignored errors, errors.Is/errors.As and %w wrapping, context-aware stdlib calls (CommandContext/QueryContext/NewRequestWithContext/Dialer), staticcheck simplifications, removed redundant conversions, http.StatusOK and http.MethodGet, inlined the go:fix intPtr helper, and deferred sql rows Close. Add a golangci CI job mirroring the existing Go jobs.
2026-06-27 06:42:22 -07:00
|
|
|
|
2026-06-20 01:35:46 -07:00
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// fakeNodeRuntime is a runtime.Runtime stub that counts the per-client dispatch
|
|
|
|
|
// calls so a test can assert a bulk op does NOT stream one RPC per client.
|
|
|
|
|
type fakeNodeRuntime struct {
|
2026-07-08 13:12:28 -07:00
|
|
|
addInbound atomic.Int32
|
|
|
|
|
delInbound atomic.Int32
|
|
|
|
|
addClient atomic.Int32
|
|
|
|
|
deleteClient atomic.Int32
|
|
|
|
|
deleteUser atomic.Int32
|
|
|
|
|
updateInbound atomic.Int32
|
2026-08-15 12:48:24 -07:00
|
|
|
updateSubSort atomic.Int32
|
2026-07-08 13:12:28 -07:00
|
|
|
updateUser atomic.Int32
|
2026-06-20 01:35:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeNodeRuntime) Name() string { return "fake-node" }
|
|
|
|
|
|
2026-07-08 13:12:28 -07:00
|
|
|
func (f *fakeNodeRuntime) AddInbound(context.Context, *model.Inbound) error {
|
|
|
|
|
f.addInbound.Add(1)
|
2026-06-20 01:35:46 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-07-08 13:12:28 -07:00
|
|
|
|
|
|
|
|
func (f *fakeNodeRuntime) DelInbound(context.Context, *model.Inbound) error {
|
|
|
|
|
f.delInbound.Add(1)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeNodeRuntime) UpdateInbound(context.Context, *model.Inbound, *model.Inbound) error {
|
|
|
|
|
f.updateInbound.Add(1)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 12:48:24 -07:00
|
|
|
func (f *fakeNodeRuntime) SetInboundSubSortIndex(context.Context, *model.Inbound, int) error {
|
|
|
|
|
f.updateSubSort.Add(1)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-20 01:35:46 -07:00
|
|
|
func (f *fakeNodeRuntime) AddUser(context.Context, *model.Inbound, map[string]any) error { return nil }
|
2026-07-08 13:12:28 -07:00
|
|
|
|
|
|
|
|
func (f *fakeNodeRuntime) RemoveUser(context.Context, *model.Inbound, string) error { return nil }
|
|
|
|
|
|
2026-06-20 01:35:46 -07:00
|
|
|
func (f *fakeNodeRuntime) UpdateUser(context.Context, *model.Inbound, string, model.Client) error {
|
|
|
|
|
f.updateUser.Add(1)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
style: adopt golangci-lint v2 and resolve all findings
Add .golangci.yml (v2): the standard linters plus bodyclose, errorlint, noctx, misspell, rowserrcheck, sqlclosecheck, unconvert, usestdlibvars, with gofumpt + goimports formatters. Enable the std-error-handling exclusion preset for idiomatic Close/Remove/Setenv ignores; scope-exclude SA1019 (parser.ParseDir in tools/openapigen) and ST1005 (intentional capitalized user-facing error copy that tests assert verbatim). No inline nolint directives were introduced.
Resolve all 217 findings behavior-preserving: gofumpt/goimports formatting, explicit blank assignment on intentionally ignored errors, errors.Is/errors.As and %w wrapping, context-aware stdlib calls (CommandContext/QueryContext/NewRequestWithContext/Dialer), staticcheck simplifications, removed redundant conversions, http.StatusOK and http.MethodGet, inlined the go:fix intPtr helper, and deferred sql rows Close. Add a golangci CI job mirroring the existing Go jobs.
2026-06-27 06:42:22 -07:00
|
|
|
|
2026-06-20 01:35:46 -07:00
|
|
|
func (f *fakeNodeRuntime) DeleteUser(context.Context, *model.Inbound, string) error {
|
|
|
|
|
f.deleteUser.Add(1)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
style: adopt golangci-lint v2 and resolve all findings
Add .golangci.yml (v2): the standard linters plus bodyclose, errorlint, noctx, misspell, rowserrcheck, sqlclosecheck, unconvert, usestdlibvars, with gofumpt + goimports formatters. Enable the std-error-handling exclusion preset for idiomatic Close/Remove/Setenv ignores; scope-exclude SA1019 (parser.ParseDir in tools/openapigen) and ST1005 (intentional capitalized user-facing error copy that tests assert verbatim). No inline nolint directives were introduced.
Resolve all 217 findings behavior-preserving: gofumpt/goimports formatting, explicit blank assignment on intentionally ignored errors, errors.Is/errors.As and %w wrapping, context-aware stdlib calls (CommandContext/QueryContext/NewRequestWithContext/Dialer), staticcheck simplifications, removed redundant conversions, http.StatusOK and http.MethodGet, inlined the go:fix intPtr helper, and deferred sql rows Close. Add a golangci CI job mirroring the existing Go jobs.
2026-06-27 06:42:22 -07:00
|
|
|
|
2026-07-05 11:28:26 -07:00
|
|
|
func (f *fakeNodeRuntime) DeleteClient(context.Context, string) error {
|
|
|
|
|
f.deleteClient.Add(1)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-20 01:35:46 -07:00
|
|
|
func (f *fakeNodeRuntime) AddClient(context.Context, *model.Inbound, model.Client) error {
|
|
|
|
|
f.addClient.Add(1)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (f *fakeNodeRuntime) RestartXray(context.Context) error { return nil }
|
|
|
|
|
func (f *fakeNodeRuntime) ResetClientTraffic(context.Context, *model.Inbound, string) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (f *fakeNodeRuntime) ResetInboundTraffic(context.Context, *model.Inbound) error { return nil }
|
|
|
|
|
func (f *fakeNodeRuntime) ResetAllTraffics(context.Context) error { return nil }
|
|
|
|
|
|
2026-09-03 16:01:20 -07:00
|
|
|
// startSerializedWriter runs the single traffic-writer goroutine for the test, so
|
|
|
|
|
// concurrent service writes take the serialized path production uses.
|
|
|
|
|
func startSerializedWriter(t *testing.T) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
resetTrafficWriterForTest(t)
|
|
|
|
|
StartTrafficWriter()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// useTestRuntimeManager swaps in a fresh runtime.Manager for the test and puts
|
|
|
|
|
// the previous one back afterwards, so overrides can't leak between tests.
|
|
|
|
|
func useTestRuntimeManager(t *testing.T) *runtime.Manager {
|
2026-06-20 01:35:46 -07:00
|
|
|
t.Helper()
|
|
|
|
|
prev := runtime.GetManager()
|
|
|
|
|
mgr := runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }, SetNeedRestart: func() {}})
|
|
|
|
|
runtime.SetManager(mgr)
|
|
|
|
|
t.Cleanup(func() { runtime.SetManager(prev) })
|
2026-09-03 16:01:20 -07:00
|
|
|
return mgr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// panicNodeRuntime panics on the per-client push, standing in for a bug in the
|
|
|
|
|
// apply path that would otherwise unwind straight out of a fanout goroutine.
|
|
|
|
|
type panicNodeRuntime struct{ fakeNodeRuntime }
|
|
|
|
|
|
|
|
|
|
func (p *panicNodeRuntime) AddClient(context.Context, *model.Inbound, model.Client) error {
|
|
|
|
|
panic("boom from node runtime")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setupNodeRuntime wires an online node + a fake runtime override so a test can
|
|
|
|
|
// drive the service node-dispatch path without a network node.
|
|
|
|
|
func setupNodeRuntime(t *testing.T) (int, *fakeNodeRuntime) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
mgr := useTestRuntimeManager(t)
|
2026-06-20 01:35:46 -07:00
|
|
|
|
2026-07-08 13:12:28 -07:00
|
|
|
node := &model.Node{Name: "n1-" + t.Name(), Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true, Status: "online"}
|
2026-06-20 01:35:46 -07:00
|
|
|
if err := database.GetDB().Create(node).Error; err != nil {
|
|
|
|
|
t.Fatalf("create node: %v", err)
|
|
|
|
|
}
|
2026-07-08 13:12:28 -07:00
|
|
|
t.Cleanup(func() {
|
|
|
|
|
_ = database.GetDB().Where("id = ?", node.Id).Delete(&model.Node{}).Error
|
|
|
|
|
})
|
2026-06-20 01:35:46 -07:00
|
|
|
fake := &fakeNodeRuntime{}
|
|
|
|
|
mgr.SetRuntimeOverride(node.Id, fake)
|
|
|
|
|
return node.Id, fake
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nodeInbound(t *testing.T, nodeID, port int, clients []model.Client) *model.Inbound {
|
|
|
|
|
t.Helper()
|
|
|
|
|
if clients == nil {
|
|
|
|
|
clients = []model.Client{}
|
|
|
|
|
}
|
|
|
|
|
ib := &model.Inbound{
|
|
|
|
|
UserId: 1, NodeID: &nodeID, Tag: fmt.Sprintf("in-%d", port), Enable: true,
|
|
|
|
|
Port: port, Protocol: model.VLESS, Settings: clientsSettings(t, clients),
|
|
|
|
|
}
|
|
|
|
|
if err := database.GetDB().Create(ib).Error; err != nil {
|
|
|
|
|
t.Fatalf("create node inbound: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if err := (&ClientService{}).SyncInbound(nil, ib.Id, clients); err != nil {
|
|
|
|
|
t.Fatalf("seed SyncInbound: %v", err)
|
|
|
|
|
}
|
|
|
|
|
return ib
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func makeNodeClients(n int) []model.Client {
|
|
|
|
|
out := make([]model.Client, n)
|
|
|
|
|
for i := range n {
|
|
|
|
|
out[i] = model.Client{ID: uuid.NewString(), Email: fmt.Sprintf("nu-%05d@x", i), Enable: true}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestNodeBulk_LargeAddFoldsToDirty: adding more than the threshold of clients to
|
|
|
|
|
// an online node inbound must NOT stream one AddClient RPC per client; it marks
|
|
|
|
|
// the node dirty so a single reconcile push converges it instead.
|
|
|
|
|
func TestNodeBulk_LargeAddFoldsToDirty(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
ib := nodeInbound(t, nodeID, 30001, nil)
|
|
|
|
|
|
|
|
|
|
svc := &ClientService{}
|
|
|
|
|
inboundSvc := &InboundService{}
|
|
|
|
|
|
|
|
|
|
add := makeNodeClients(nodeBulkPushThreshold + 10)
|
|
|
|
|
if _, err := svc.AddInboundClient(inboundSvc, &model.Inbound{Id: ib.Id, Protocol: model.VLESS, Settings: clientsSettings(t, add)}); err != nil {
|
|
|
|
|
t.Fatalf("AddInboundClient: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if got := fake.addClient.Load(); got != 0 {
|
|
|
|
|
t.Fatalf("large add streamed %d AddClient RPCs, want 0 (should fold to dirty)", got)
|
|
|
|
|
}
|
|
|
|
|
if _, _, dirty, _, err := (&NodeService{}).NodeSyncState(nodeID); err != nil {
|
|
|
|
|
t.Fatalf("NodeSyncState: %v", err)
|
|
|
|
|
} else if !dirty {
|
|
|
|
|
t.Fatal("large add must mark the node dirty")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestNodeBulk_SmallAddPushesLive: a small add stays on the live per-client path.
|
|
|
|
|
func TestNodeBulk_SmallAddPushesLive(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
ib := nodeInbound(t, nodeID, 30002, nil)
|
|
|
|
|
|
|
|
|
|
svc := &ClientService{}
|
|
|
|
|
inboundSvc := &InboundService{}
|
|
|
|
|
|
|
|
|
|
const small = 3
|
|
|
|
|
add := makeNodeClients(small)
|
|
|
|
|
if _, err := svc.AddInboundClient(inboundSvc, &model.Inbound{Id: ib.Id, Protocol: model.VLESS, Settings: clientsSettings(t, add)}); err != nil {
|
|
|
|
|
t.Fatalf("AddInboundClient: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if got := fake.addClient.Load(); got != int32(small) {
|
|
|
|
|
t.Fatalf("small add streamed %d AddClient RPCs, want %d", got, small)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 11:11:19 -07:00
|
|
|
func TestNodeBulkAdjustDoesNotPushBeforeFailedCommit(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
client := model.Client{
|
|
|
|
|
ID: uuid.NewString(),
|
|
|
|
|
Email: "txfail-adjust@x",
|
|
|
|
|
Enable: true,
|
|
|
|
|
ExpiryTime: 1_900_000_000_000,
|
|
|
|
|
}
|
|
|
|
|
nodeInbound(t, nodeID, 30022, []model.Client{client})
|
|
|
|
|
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
const callbackName = "bulk-adjust:fail-inbound-update"
|
|
|
|
|
if err := db.Callback().Update().After("gorm:update").Register(callbackName, func(tx *gorm.DB) {
|
|
|
|
|
if tx.Statement != nil && tx.Statement.Table == "inbounds" {
|
|
|
|
|
tx.AddError(errors.New("injected bulk-adjust transaction failure"))
|
|
|
|
|
}
|
|
|
|
|
}); err != nil {
|
|
|
|
|
t.Fatalf("register callback: %v", err)
|
|
|
|
|
}
|
|
|
|
|
t.Cleanup(func() { _ = db.Callback().Update().Remove(callbackName) })
|
|
|
|
|
|
feat(clients): support setting HWID limit and MTProto ad-tag in bulk adjust (#6399)
* feat(clients): support setting HWID limit and MTProto ad-tag in bulk adjust
Add HWID device limit and Telegram MTProto sponsor channel (ad-tag)
support to the bulk client adjustment flow in both the panel API
and frontend ClientBulkAdjustModal.
Co-Authored-By: Claude Code <noreply@anthropic.com>
* fix(clients): gate adTag to MTProto inbounds and avoid inbound rewrite for limitHwid
Co-Authored-By: Claude Code <noreply@anthropic.com>
* fix(clients): stamp updated_at only on the clients a bulk adjust changed
The updated_at write was gated on hasInboundChanges, which accumulates
over the whole inbound instead of describing the client in hand. Once any
client in the settings array changed, every client after it was re-stamped
as well, so whether an untouched client kept its own updated_at depended on
its position in the array. That field feeds node-snapshot conflict
resolution, where a spurious bump lets a stale snapshot value win over the
stored record.
Track the change per client and fold it into the inbound-level flag where
the stamp is written, so the early return still skips a save whose settings
JSON would be unchanged.
Also condenses the BulkAdjust doc comment back to the two-line maximum.
* docs(api): regenerate the bulkAdjust reference for limitHwid and adTag
frontend/public/openapi.json was copied to docs/public/, but pnpm gen:api
was never re-run, so the API reference page's heading, anchor id and search
index still described bulkAdjust without limitHwid or adTag. docs-ci.yml
fires only on docs/**, and that path had been touched, so nothing flagged
the stale MDX.
The externalLinks hunks are the generator rewrapping lines main had left
stale, not a content change.
* fix(i18n): stop enumerating fields in the bulk-adjust empty-form message
bulkAdjustNothing listed the fields the form accepts, so it went stale
every time one was added: only en-US ever gained "flow", leaving the other
twelve locales describing days and traffic alone, and limitHwid and adTag
would have repeated that. Say that one field is required instead of naming
which, so the message cannot drift again.
2026-09-10 07:24:48 -07:00
|
|
|
result, _, err := (&ClientService{}).BulkAdjust(&InboundService{}, []string{client.Email}, 1, 0, "", nil, "")
|
2026-08-14 11:11:19 -07:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("BulkAdjust: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if result.Adjusted != 0 || len(result.Skipped) != 1 {
|
|
|
|
|
t.Fatalf("BulkAdjust result = %+v, want one skipped client after injected failure", result)
|
|
|
|
|
}
|
|
|
|
|
if got := fake.updateUser.Load(); got != 0 {
|
|
|
|
|
t.Fatalf("failed transaction pushed %d UpdateUser call(s) to the node, want 0", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNodeBulkDeleteDoesNotPushBeforeFailedCommit(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
client := model.Client{ID: uuid.NewString(), Email: "txfail-delete@x", Enable: true}
|
|
|
|
|
nodeInbound(t, nodeID, 30023, []model.Client{client})
|
|
|
|
|
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
const callbackName = "bulk-delete:fail-inbound-update"
|
|
|
|
|
if err := db.Callback().Update().After("gorm:update").Register(callbackName, func(tx *gorm.DB) {
|
|
|
|
|
if tx.Statement != nil && tx.Statement.Table == "inbounds" {
|
|
|
|
|
tx.AddError(errors.New("injected bulk-delete transaction failure"))
|
|
|
|
|
}
|
|
|
|
|
}); err != nil {
|
|
|
|
|
t.Fatalf("register callback: %v", err)
|
|
|
|
|
}
|
|
|
|
|
t.Cleanup(func() { _ = db.Callback().Update().Remove(callbackName) })
|
|
|
|
|
|
|
|
|
|
result, _, err := (&ClientService{}).BulkDelete(&InboundService{}, []string{client.Email}, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("BulkDelete: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if result.Deleted != 0 || len(result.Skipped) != 1 {
|
|
|
|
|
t.Fatalf("BulkDelete result = %+v, want one skipped client after injected failure", result)
|
|
|
|
|
}
|
|
|
|
|
if got := fake.deleteClient.Load() + fake.deleteUser.Load(); got != 0 {
|
|
|
|
|
t.Fatalf("failed transaction pushed %d delete call(s) to the node, want 0", got)
|
|
|
|
|
}
|
2026-09-02 11:25:40 -07:00
|
|
|
if isClientEmailTombstoned(client.Email) {
|
|
|
|
|
t.Fatal("failed bulk delete left a live tombstone")
|
|
|
|
|
}
|
2026-08-14 11:11:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNodeBulkSmallDeleteRemovesWholeRemoteClient(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
client := model.Client{ID: uuid.NewString(), Email: "full-delete@x", Enable: true}
|
|
|
|
|
nodeInbound(t, nodeID, 30024, []model.Client{client})
|
2026-09-02 11:25:40 -07:00
|
|
|
var record model.ClientRecord
|
|
|
|
|
if err := database.GetDB().Where("email = ?", client.Email).First(&record).Error; err != nil {
|
|
|
|
|
t.Fatalf("load client record: %v", err)
|
|
|
|
|
}
|
2026-08-14 11:11:19 -07:00
|
|
|
|
|
|
|
|
result, _, err := (&ClientService{}).BulkDelete(&InboundService{}, []string{client.Email}, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("BulkDelete: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if result.Deleted != 1 || len(result.Skipped) != 0 {
|
|
|
|
|
t.Fatalf("BulkDelete result = %+v, want one deleted client", result)
|
|
|
|
|
}
|
|
|
|
|
if got := fake.deleteClient.Load(); got != 1 {
|
|
|
|
|
t.Fatalf("remote DeleteClient calls = %d, want 1", got)
|
|
|
|
|
}
|
|
|
|
|
if got := fake.deleteUser.Load(); got != 0 {
|
|
|
|
|
t.Fatalf("remote DeleteUser detach calls = %d, want 0 for full deletion", got)
|
|
|
|
|
}
|
2026-09-02 11:25:40 -07:00
|
|
|
var records, links int64
|
|
|
|
|
if err := database.GetDB().Model(&model.ClientRecord{}).Where("email = ?", client.Email).Count(&records).Error; err != nil {
|
|
|
|
|
t.Fatalf("count client records: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if err := database.GetDB().Model(&model.ClientInbound{}).Where("client_id = ?", record.Id).Count(&links).Error; err != nil {
|
|
|
|
|
t.Fatalf("count client links: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if records != 0 || links != 0 {
|
|
|
|
|
t.Fatalf("bulk delete left records=%d links=%d, want 0/0", records, links)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNodeBulkDeleteTreatsMissingSettingsClientAsAlreadyDeleted(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
client := model.Client{ID: uuid.NewString(), Email: "drifted-delete@x", Enable: true}
|
|
|
|
|
ib := nodeInbound(t, nodeID, 30025, []model.Client{client})
|
|
|
|
|
|
|
|
|
|
// Simulate a stale normalized link after the client has already disappeared
|
|
|
|
|
// from the inbound settings JSON.
|
|
|
|
|
if err := database.GetDB().Model(&model.Inbound{}).Where("id = ?", ib.Id).
|
|
|
|
|
Update("settings", clientsSettings(t, nil)).Error; err != nil {
|
|
|
|
|
t.Fatalf("drift inbound settings: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, _, err := (&ClientService{}).BulkDelete(&InboundService{}, []string{client.Email}, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("BulkDelete: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if result.Deleted != 1 || len(result.Skipped) != 0 {
|
|
|
|
|
t.Fatalf("BulkDelete result = %+v, want one deleted client", result)
|
|
|
|
|
}
|
|
|
|
|
if got := fake.deleteClient.Load(); got != 1 {
|
|
|
|
|
t.Fatalf("remote DeleteClient calls = %d, want 1", got)
|
|
|
|
|
}
|
|
|
|
|
var records, links int64
|
|
|
|
|
if err := database.GetDB().Model(&model.ClientRecord{}).Where("email = ?", client.Email).Count(&records).Error; err != nil {
|
|
|
|
|
t.Fatalf("count client records: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if err := database.GetDB().Model(&model.ClientInbound{}).Where("inbound_id = ?", ib.Id).Count(&links).Error; err != nil {
|
|
|
|
|
t.Fatalf("count client links: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if records != 0 || links != 0 {
|
|
|
|
|
t.Fatalf("bulk delete left records=%d links=%d, want 0/0", records, links)
|
|
|
|
|
}
|
|
|
|
|
if !isClientEmailTombstoned(client.Email) {
|
|
|
|
|
t.Fatal("successful bulk delete withdrew the client tombstone")
|
|
|
|
|
}
|
|
|
|
|
t.Cleanup(func() { withdrawClientTombstones(client.Email) })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNodeBulkDeleteCompletesAcrossPresentAndMissingSettings(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
client := model.Client{ID: uuid.NewString(), Email: "mixed-delete@x", Enable: true}
|
|
|
|
|
drifted := nodeInbound(t, nodeID, 30026, []model.Client{client})
|
|
|
|
|
nodeInbound(t, nodeID, 30027, []model.Client{client})
|
|
|
|
|
|
|
|
|
|
if err := database.GetDB().Model(&model.Inbound{}).Where("id = ?", drifted.Id).
|
|
|
|
|
Update("settings", clientsSettings(t, nil)).Error; err != nil {
|
|
|
|
|
t.Fatalf("drift inbound settings: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, _, err := (&ClientService{}).BulkDelete(&InboundService{}, []string{client.Email}, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("BulkDelete: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if result.Deleted != 1 || len(result.Skipped) != 0 {
|
|
|
|
|
t.Fatalf("BulkDelete result = %+v, want one deleted client", result)
|
|
|
|
|
}
|
|
|
|
|
if got := fake.deleteClient.Load(); got != 2 {
|
|
|
|
|
t.Fatalf("remote DeleteClient calls = %d, want one per node inbound", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNodeBulkDeleteMalformedSettingsWithdrawsTombstone(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
client := model.Client{ID: uuid.NewString(), Email: "malformed-delete@x", Enable: true}
|
|
|
|
|
ib := nodeInbound(t, nodeID, 30028, []model.Client{client})
|
|
|
|
|
if err := database.GetDB().Model(&model.Inbound{}).Where("id = ?", ib.Id).
|
|
|
|
|
Update("settings", `{"clients":[`).Error; err != nil {
|
|
|
|
|
t.Fatalf("break inbound settings: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, _, err := (&ClientService{}).BulkDelete(&InboundService{}, []string{client.Email}, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("BulkDelete: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if result.Deleted != 0 || len(result.Skipped) != 1 {
|
|
|
|
|
t.Fatalf("BulkDelete result = %+v, want one skipped client", result)
|
|
|
|
|
}
|
|
|
|
|
if got := fake.deleteClient.Load() + fake.deleteUser.Load(); got != 0 {
|
|
|
|
|
t.Fatalf("malformed settings pushed %d delete call(s) to the node, want 0", got)
|
|
|
|
|
}
|
|
|
|
|
if isClientEmailTombstoned(client.Email) {
|
|
|
|
|
t.Fatal("failed bulk delete left a live tombstone")
|
|
|
|
|
}
|
2026-08-14 11:11:19 -07:00
|
|
|
}
|
|
|
|
|
|
fix(node): stop client edits from tearing down node inbounds and harden reconcile fingerprints
A client save on the master always stamped a fresh updated_at, marked
the node dirty, and let the 5s sync push a full inbounds/update to the
node, where applying it removes and re-adds the Xray handler - killing
live traffic on every edit, including no-op saves (open the editor,
click Save). Nodes stayed online with Xray running while forwarding
nothing until a manual Xray restart.
- No-op client saves preserve the client's updated_at and return before
any DB write, runtime RPC, or node dirty mark when the effective
settings did not change.
- Successful per-client add/update/delete pushes advance the node's
reconcile-skip fingerprint only when the recorded fingerprint proves
the node held the exact pre-edit payload and every push in the edit
succeeded (Remote.AdvancePushedInbound). Anything unproven keeps the
stale fingerprint so the dirty reconcile still sends the full inbound.
Unconditional stamping would certify folded bulk changes (threshold,
flow change, offline edit) or partially failed batches as delivered:
a folded 41->6 bulk delete followed by one live edit left the node
permanently serving all 41 clients in end-to-end testing, with the
snapshot adoption then resurrecting the deleted clients on the master.
- DeleteUser treats only an envelope-level not-found as already deleted;
an HTTP 404 from an old node build without the detach endpoint
surfaces as an error instead of certifying an undelivered delete.
cacheDel drops the fingerprint alongside the id cache so DelInbound
and tag renames leave no stale skip entry.
- Adopting the node's own settings serialization into the master row now
also stamps the fingerprint (RecordAdoptedInbound). Without it the
serialization round-trip invalidated the fingerprint one sync tick
after every push, so each edit degraded back to a full teardown push.
- UpdateInboundClient applies the Shadowsocks method normalization
before the no-op comparison (real method changes bump updated_at, SS
no-op edits are detected) and syncs the generated subId into the
pushed client so the node cannot mint a different one.
Verified with a two-panel docker deployment: no-op saves produce zero
node requests, real edits send one lightweight clients/update RPC with
zero full inbound updates and zero handler teardowns, and folded bulk
deletes still converge.
Based on PR #5778 by @rqzbeh.
Closes #5764
Closes #5771
2026-07-04 17:06:58 -07:00
|
|
|
func TestNodeUpdateInboundClientNoopSkipsRuntimeAndDirty(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
client := model.Client{
|
|
|
|
|
ID: uuid.NewString(),
|
|
|
|
|
Email: "noop@x",
|
|
|
|
|
SubID: "sub-noop",
|
|
|
|
|
Enable: true,
|
|
|
|
|
CreatedAt: 111,
|
|
|
|
|
UpdatedAt: 222,
|
|
|
|
|
}
|
|
|
|
|
ib := nodeInbound(t, nodeID, 30020, []model.Client{client})
|
|
|
|
|
|
|
|
|
|
svc := &ClientService{}
|
|
|
|
|
inboundSvc := &InboundService{}
|
|
|
|
|
if _, err := svc.UpdateInboundClient(inboundSvc, &model.Inbound{
|
|
|
|
|
Id: ib.Id,
|
|
|
|
|
Protocol: model.VLESS,
|
|
|
|
|
Settings: clientsSettings(t, []model.Client{client}),
|
|
|
|
|
}, client.Email); err != nil {
|
|
|
|
|
t.Fatalf("UpdateInboundClient: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if got := fake.updateUser.Load(); got != 0 {
|
|
|
|
|
t.Fatalf("no-op update streamed %d UpdateUser RPCs, want 0", got)
|
|
|
|
|
}
|
|
|
|
|
if _, _, dirty, _, err := (&NodeService{}).NodeSyncState(nodeID); err != nil {
|
|
|
|
|
t.Fatalf("NodeSyncState: %v", err)
|
|
|
|
|
} else if dirty {
|
|
|
|
|
t.Fatal("no-op update must not mark the node dirty")
|
|
|
|
|
}
|
|
|
|
|
reloaded, err := inboundSvc.GetInbound(ib.Id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("GetInbound: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if reloaded.Settings != ib.Settings {
|
|
|
|
|
t.Fatal("no-op update rewrote inbound settings")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNodeUpdateInboundClientLivePushKeepsDirtyBackup(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
client := model.Client{
|
|
|
|
|
ID: uuid.NewString(),
|
|
|
|
|
Email: "edit@x",
|
|
|
|
|
SubID: "sub-edit",
|
|
|
|
|
Enable: true,
|
|
|
|
|
CreatedAt: 111,
|
|
|
|
|
UpdatedAt: 222,
|
|
|
|
|
}
|
|
|
|
|
ib := nodeInbound(t, nodeID, 30021, []model.Client{client})
|
|
|
|
|
|
|
|
|
|
edited := client
|
|
|
|
|
edited.Comment = "changed"
|
|
|
|
|
svc := &ClientService{}
|
|
|
|
|
inboundSvc := &InboundService{}
|
|
|
|
|
if _, err := svc.UpdateInboundClient(inboundSvc, &model.Inbound{
|
|
|
|
|
Id: ib.Id,
|
|
|
|
|
Protocol: model.VLESS,
|
|
|
|
|
Settings: clientsSettings(t, []model.Client{edited}),
|
|
|
|
|
}, client.Email); err != nil {
|
|
|
|
|
t.Fatalf("UpdateInboundClient: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if got := fake.updateUser.Load(); got != 1 {
|
|
|
|
|
t.Fatalf("edit streamed %d UpdateUser RPCs, want 1", got)
|
|
|
|
|
}
|
|
|
|
|
if _, _, dirty, _, err := (&NodeService{}).NodeSyncState(nodeID); err != nil {
|
|
|
|
|
t.Fatalf("NodeSyncState: %v", err)
|
|
|
|
|
} else if !dirty {
|
|
|
|
|
t.Fatal("successful live update should keep node dirty as reconcile backup")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-20 01:35:46 -07:00
|
|
|
// TestNodeBulk_LargeDeleteFoldsToDirty: deleting more than the threshold from an
|
|
|
|
|
// online node inbound must fold into a reconcile rather than per-client deletes.
|
|
|
|
|
func TestNodeBulk_LargeDeleteFoldsToDirty(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
|
|
|
|
|
seed := makeNodeClients(nodeBulkPushThreshold + 10)
|
|
|
|
|
nodeInbound(t, nodeID, 30003, seed)
|
|
|
|
|
|
|
|
|
|
svc := &ClientService{}
|
|
|
|
|
inboundSvc := &InboundService{}
|
|
|
|
|
emails := make([]string, len(seed))
|
|
|
|
|
for i := range seed {
|
|
|
|
|
emails[i] = seed[i].Email
|
|
|
|
|
}
|
|
|
|
|
if _, _, err := svc.BulkDelete(inboundSvc, emails, false); err != nil {
|
|
|
|
|
t.Fatalf("BulkDelete: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-02 11:25:40 -07:00
|
|
|
if got := fake.deleteClient.Load() + fake.deleteUser.Load(); got != 0 {
|
|
|
|
|
t.Fatalf("large delete streamed %d delete RPCs, want 0 (should fold to dirty)", got)
|
2026-06-20 01:35:46 -07:00
|
|
|
}
|
|
|
|
|
if _, _, dirty, _, err := (&NodeService{}).NodeSyncState(nodeID); err != nil {
|
|
|
|
|
t.Fatalf("NodeSyncState: %v", err)
|
|
|
|
|
} else if !dirty {
|
|
|
|
|
t.Fatal("large delete must mark the node dirty")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-08 13:12:28 -07:00
|
|
|
|
|
|
|
|
func TestDelInbound_NodeSelectedModeDeletesRemoteImmediately(t *testing.T) {
|
|
|
|
|
setupBulkDB(t)
|
|
|
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
|
|
|
if err := database.GetDB().Model(&model.Node{}).Where("id = ?", nodeID).
|
|
|
|
|
Updates(map[string]any{
|
|
|
|
|
"inbound_sync_mode": "selected",
|
|
|
|
|
"inbound_tags": []string{"other-tag"},
|
|
|
|
|
}).Error; err != nil {
|
|
|
|
|
t.Fatalf("set selected mode: %v", err)
|
|
|
|
|
}
|
|
|
|
|
ib := nodeInbound(t, nodeID, 30004, makeNodeClients(1))
|
|
|
|
|
|
|
|
|
|
needRestart, err := (&InboundService{}).DelInbound(ib.Id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("DelInbound: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if needRestart {
|
|
|
|
|
t.Fatal("node-owned delete should not request local restart")
|
|
|
|
|
}
|
|
|
|
|
if got := fake.delInbound.Load(); got != 1 {
|
|
|
|
|
t.Fatalf("node-owned delete streamed %d DelInbound RPCs, want 1", got)
|
|
|
|
|
}
|
|
|
|
|
var count int64
|
|
|
|
|
if err := database.GetDB().Model(&model.Inbound{}).Where("id = ?", ib.Id).Count(&count).Error; err != nil {
|
|
|
|
|
t.Fatalf("count inbound: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if count != 0 {
|
|
|
|
|
t.Fatalf("deleted inbound row count = %d, want 0", count)
|
|
|
|
|
}
|
|
|
|
|
if _, _, dirty, _, err := (&NodeService{}).NodeSyncState(nodeID); err != nil {
|
|
|
|
|
t.Fatalf("NodeSyncState: %v", err)
|
|
|
|
|
} else if !dirty {
|
|
|
|
|
t.Fatal("node-owned delete should still mark the node dirty as reconcile backup")
|
|
|
|
|
}
|
|
|
|
|
}
|