From 33cfa52770a6cfb92e6ba91325b5fbcf64b523e8 Mon Sep 17 00:00:00 2001 From: Hamed Date: Sat, 28 Nov 2026 17:29:47 +0330 Subject: [PATCH] fix(nodes): propagate inbound lookup failures (#7250) A master pushing an inbound delete or update to a node treated every failed lookup of the node's inbound list as "the inbound is not there". An auth error, a 503, an application error or a malformed list made a delete count as done while nothing was deleted, and made an update fall back to adding the inbound a second time. Only a list that loaded and lacks the tag now counts as absent: deleting an inbound that is really gone still succeeds and an edit still re-creates it. Every other failure is returned and no add, update or delete is sent to the node. Refs #7237 --- internal/web/node_contract_test.go | 12 ++++ internal/web/runtime/remote.go | 15 +++-- .../web/runtime/remote_inbound_lookup_test.go | 63 +++++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 internal/web/runtime/remote_inbound_lookup_test.go diff --git a/internal/web/node_contract_test.go b/internal/web/node_contract_test.go index 6ad27ce9f..bcc21d332 100644 --- a/internal/web/node_contract_test.go +++ b/internal/web/node_contract_test.go @@ -369,6 +369,18 @@ func TestMasterNodeContract(t *testing.T) { t.Fatal("node still has the inbound") } }}, + {"DelInbound is idempotent after confirmed absence", []string{"DelInbound"}, func() error { + return master.DelInbound(ctx, masterInbound("already missing", false)) + }, nil}, + {"UpdateInbound reseeds an inbound confirmed missing", []string{"UpdateInbound"}, func() error { + inbound := masterInbound("reseeded", false) + return master.UpdateInbound(ctx, inbound, inbound) + }, func(t *testing.T) { + inbound, ok := nodeRow(t, contractTag) + if !ok || inbound.Remark != "reseeded" { + t.Fatalf("reseeded inbound = %+v", inbound) + } + }}, } covered := map[string]bool{} for _, c := range cells { diff --git a/internal/web/runtime/remote.go b/internal/web/runtime/remote.go index 9c72b5081..07b16d5ed 100644 --- a/internal/web/runtime/remote.go +++ b/internal/web/runtime/remote.go @@ -52,6 +52,8 @@ const errBodyDiagBytes = 8 << 10 // 8 KiB // errRemoteResponseTooLarge is returned when a node response exceeds the cap. var errRemoteResponseTooLarge = errors.New("remote response exceeds size limit") +var errRemoteInboundNotFound = errors.New("remote inbound not found") + // readCappedBody reads all of r but rejects bodies larger than limit, returning // errRemoteResponseTooLarge. It reads at most limit+1 bytes so a body of exactly // limit is accepted and the first oversize byte is detected without buffering @@ -424,7 +426,7 @@ func (r *Remote) resolveRemoteID(ctx context.Context, tag string) (int, error) { if id, ok := r.cacheGetTag(tag); ok { return id, nil } - return 0, fmt.Errorf("remote inbound with tag %q not found on node %s", tag, r.node.Name) + return 0, fmt.Errorf("%w: tag %q on node %s", errRemoteInboundNotFound, tag, r.node.Name) } // nodeInboundTagPrefix is the central-panel alias for an inbound on nodeID. @@ -572,10 +574,12 @@ func (r *Remote) AddInbound(ctx context.Context, ib *model.Inbound) error { func (r *Remote) DelInbound(ctx context.Context, ib *model.Inbound) error { id, err := r.resolveRemoteID(ctx, ib.Tag) - if err != nil { - logger.Warning("remote DelInbound: tag", ib.Tag, "not found on", r.node.Name) + if errors.Is(err, errRemoteInboundNotFound) { return nil } + if err != nil { + return err + } if _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/del/"+strconv.Itoa(id), nil); err != nil { return err } @@ -585,9 +589,12 @@ func (r *Remote) DelInbound(ctx context.Context, ib *model.Inbound) error { func (r *Remote) UpdateInbound(ctx context.Context, oldIb, newIb *model.Inbound) error { id, err := r.resolveRemoteID(ctx, oldIb.Tag) - if err != nil { + if errors.Is(err, errRemoteInboundNotFound) { return r.AddInbound(ctx, newIb) } + if err != nil { + return err + } if err := r.checkInboundCapabilities(ctx, newIb); err != nil { return err } diff --git a/internal/web/runtime/remote_inbound_lookup_test.go b/internal/web/runtime/remote_inbound_lookup_test.go new file mode 100644 index 000000000..a0504029b --- /dev/null +++ b/internal/web/runtime/remote_inbound_lookup_test.go @@ -0,0 +1,63 @@ +package runtime + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" + + "github.com/mhsanaei/3x-ui/v3/internal/database/model" +) + +func TestInboundLookupFailureDoesNotAcknowledgeMutation(t *testing.T) { + for _, tc := range []struct { + name string + status int + body string + want string + }{ + {"forbidden", http.StatusForbidden, "", "HTTP 403"}, + {"unavailable", http.StatusServiceUnavailable, "", "HTTP 503"}, + {"application error", http.StatusOK, `{"success":false,"msg":"node database unavailable"}`, "node database unavailable"}, + {"invalid catalog", http.StatusOK, `{"success":true,"obj":{"id":4}}`, "decode inbound list"}, + } { + t.Run(tc.name, func(t *testing.T) { + var mutations atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + if req.Method != http.MethodGet || !strings.HasSuffix(req.URL.Path, "/panel/api/inbounds/list") { + mutations.Add(1) + _, _ = w.Write([]byte(`{"success":true}`)) + return + } + w.WriteHeader(tc.status) + _, _ = w.Write([]byte(tc.body)) + })) + t.Cleanup(srv.Close) + remote := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil) + inbound := &model.Inbound{Tag: "in-lookup", Protocol: model.VLESS, Port: 443, Settings: `{"clients":[]}`} + if err := remote.DelInbound(context.Background(), inbound); err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("DelInbound error = %v, want %q", err, tc.want) + } + if err := remote.UpdateInbound(context.Background(), inbound, inbound); err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("UpdateInbound error = %v, want %q", err, tc.want) + } + if got := mutations.Load(); got != 0 { + t.Errorf("mutation requests after failed lookup = %d, want 0", got) + } + }) + } +} + +func TestDelInboundPreservesCanceledContext(t *testing.T) { + srv, _ := newCountingNodeServer(t, `{"success":true}`) + remote := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + inbound := &model.Inbound{Tag: "in-canceled", Protocol: model.VLESS} + if err := remote.DelInbound(ctx, inbound); !errors.Is(err, context.Canceled) { + t.Errorf("DelInbound error = %v, want canceled context", err) + } +}