mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-27 04:54:45 +03:00
fix(inbounds): keep the running handler when its replacement cannot be added
A save that has to rebuild an inbound's handler removed the running one through the Xray API and then added the new one. xray-core's API server reads requests of at most 4 MiB, which a VLESS inbound passes at about 46,000 clients, so the add was refused after the removal had gone through. The inbound then stayed down until the periodic restart picked the change up: 12 s on a 100,000-client inbound in a lab run, up to the restart interval in general. The add request is now built and measured before anything is sent, and the save asks the local runtime for that check before it removes the old handler. When the replacement cannot go through the API the old handler keeps serving and the save asks for the restart it needed anyway, with a warning that names the size. In the same lab run the inbound's port was closed only for the restart itself. Refs #7207
This commit is contained in:
@@ -118,6 +118,20 @@ func (l *Local) AddInbound(_ context.Context, ib *model.Inbound) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// CheckAddInbound reports why AddInbound could not hand ib to the core, without
|
||||
// touching the core: a replace asks it before removing the handler that still serves.
|
||||
func (l *Local) CheckAddInbound(ib *model.Inbound) error {
|
||||
switch ib.Protocol {
|
||||
case model.MTProto, model.AmneziaWG, model.TUIC:
|
||||
return nil
|
||||
}
|
||||
body, err := json.MarshalIndent(ib.GenXrayInboundConfig(), "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return xray.CheckAddInbound(body)
|
||||
}
|
||||
|
||||
func (l *Local) DelInbound(_ context.Context, ib *model.Inbound) error {
|
||||
if ib.Protocol == model.MTProto {
|
||||
mtproto.GetManager().Remove(ib.Id)
|
||||
|
||||
@@ -2312,6 +2312,15 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound,
|
||||
if runningInbound != nil && applyInboundUserOps(rt, runningInbound, runtimeInbound) {
|
||||
return
|
||||
}
|
||||
// Removed first, a handler whose replacement the core's API cannot
|
||||
// take would leave the inbound down until the restart.
|
||||
if checker, ok := rt.(interface{ CheckAddInbound(*model.Inbound) error }); ok && runtimeInbound != nil {
|
||||
if err2 := checker.CheckAddInbound(runtimeInbound); err2 != nil {
|
||||
logger.Warning("Inbound", oldInbound.Tag, "cannot be replaced on", rt.Name(), "without a restart:", err2)
|
||||
needRestart = true
|
||||
return
|
||||
}
|
||||
}
|
||||
if err2 := rt.DelInbound(context.Background(), &oldSnapshot); err2 == nil {
|
||||
logger.Debug("Old inbound deleted on", rt.Name(), ":", tag)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// oversizedVlessSettings holds enough clients that the core's 4 MiB API limit
|
||||
// refuses the inbound as one request, as it does past about 46,000 users.
|
||||
func oversizedVlessSettings() string {
|
||||
var b strings.Builder
|
||||
b.WriteString(`{"decryption":"none","clients":[`)
|
||||
for i := range 50000 {
|
||||
if i > 0 {
|
||||
b.WriteByte(',')
|
||||
}
|
||||
fmt.Fprintf(&b, `{"id":"%08d-0000-4000-8000-000000000000","email":"user-%05d@oversized","enable":true,"created_at":1,"updated_at":1}`, i, i)
|
||||
}
|
||||
b.WriteString(`]}`)
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// runOversizedInbound starts a core already serving the inbound, stores its row
|
||||
// and makes that core the panel's local runtime.
|
||||
func runOversizedInbound(t *testing.T) (*model.Inbound, func() any) {
|
||||
t.Helper()
|
||||
setupBulkDB(t)
|
||||
ib := &model.Inbound{
|
||||
Tag: "oversized", Listen: "127.0.0.1", Port: freeLoopbackPort(t), Protocol: model.VLESS, Enable: true,
|
||||
Settings: oversizedVlessSettings(),
|
||||
StreamSettings: `{"network":"tcp","security":"none"}`,
|
||||
Sniffing: `{"enabled":false}`,
|
||||
}
|
||||
if err := database.GetDB().Create(ib).Error; err != nil {
|
||||
t.Fatalf("create inbound: %v", err)
|
||||
}
|
||||
instance, _, local := localRuntimeOnCore(t, withInbound(isolationConfig(t), ib))
|
||||
mgr := runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }})
|
||||
mgr.SetLocalRuntimeOverride(local)
|
||||
runtime.SetManager(mgr)
|
||||
t.Cleanup(func() { runtime.SetManager(nil) })
|
||||
return ib, func() any { return coreInboundHandler(t, instance, ib.Tag) }
|
||||
}
|
||||
|
||||
// A save that rebuilds the handler cannot send a 50,000-client inbound through
|
||||
// the core's API; the handler it serves from must stay up until the restart.
|
||||
func TestInboundSaveKeepsAHandlerItCannotReplaceLive(t *testing.T) {
|
||||
ib, handler := runOversizedInbound(t)
|
||||
serving := handler()
|
||||
if serving == nil {
|
||||
t.Fatal("the core did not start with the inbound")
|
||||
}
|
||||
|
||||
form := *ib
|
||||
form.Settings = `{"decryption":"none"}`
|
||||
form.Sniffing = `{"enabled":true,"destOverride":["http","tls"]}`
|
||||
_, needRestart, err := (&InboundService{}).UpdateInbound(&form)
|
||||
if err != nil {
|
||||
t.Fatalf("UpdateInbound: %v", err)
|
||||
}
|
||||
if !needRestart {
|
||||
t.Fatal("needRestart = false: nothing would apply the new sniffing")
|
||||
}
|
||||
if handler() != serving {
|
||||
t.Fatal("the save removed the running handler and could not add its replacement: the inbound is down until the next restart")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user