mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-02 15:35:22 +03:00
33cfa52770
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
64 lines
2.3 KiB
Go
64 lines
2.3 KiB
Go
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)
|
|
}
|
|
}
|