mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-28 21:44:50 +03:00
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
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user