mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-18 19:15:03 +03:00
fix(xray): restart when a diff strands a client's live session (#6550)
* fix(xray): restart when a diff strands a client's live session Disabling or deleting a client took it out of the generated config and the hot path applied that with AlterInbound/RemoveUser, which only drops the credential (vless, vmess, trojan and shadowsocks all keep the established session running) -- so the panel showed a disabled client whose connection kept passing traffic, and the core offers no API to close one session. A diff that removes a user without re-adding the same email under the same tag is that case: honour the operator's restart-on-client-disable setting and let the caller replace the process, which is already how an auto-disabled client loses its session. An edit re-adds the email and keeps the hot path. * chore(i18n): cover manual disable and delete in the restart-setting description The setting now also decides what happens when a client is disabled or deleted by hand, so the description cannot keep naming only the automatic path. All 13 locales updated in the same commit to keep the wording consistent. * fix(xray): reach the guard from the manual switch and from every protocol Round-1 findings on this PR. The guard sat in tryHotApply, but a manual disable or delete applies through runtime.Runtime and finishes with needRestart false, so none of the three RestartXray schedulers fired and the predicate was never reached: the session in #6533 kept flowing. The apply layer now asks for the restart the setting promises when the client actually leaves the config, on the single-client update and delete paths and on bulk disable, and only for local inbounds so a node row cannot make the master restart its own core. The predicate itself could not fire for shadowsocks or hysteria either, because RemovedUsers is only produced for the protocols diffInboundUsers will diff. The diff now also compares settings.clients of an inbound present in both configs, which is the one shape every account list shares, so those protocols reach the guard through the inbound instead of through nothing. TestManualClientDisableHonoursRestartSetting fails without the apply-layer fix ("needRestart = false, want true" with the setting on) and TestHotDiffDropsUsersOnProtocolsItCannotDiff fails without the diff fix -- both watched red. The two three-line comments this PR added are back inside the cap. * docs(i18n): stop scoping restartXrayOnClientDisable to auto-disable The setting now covers a client disabled or deleted by hand as well, so its title no longer says "Auto" in all 13 locales, and the docs callouts in en, ru, zh and fa describe the same behaviour instead of the auto-only one.
This commit is contained in:
@@ -1812,6 +1812,9 @@ func (s *ClientService) bulkSetEnableInboundClients(inboundSvc *InboundService,
|
||||
if err1 != nil && !strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", ch.email)) {
|
||||
logger.Debug("Error in removing client on", rt.Name(), ":", err1)
|
||||
res.needRestart = true
|
||||
} else if err1 == nil && droppedClientNeedsRestart() {
|
||||
// A removed credential does not end the session it was serving.
|
||||
res.needRestart = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"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"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
||||
)
|
||||
|
||||
// The manual switch applies through the runtime, and the core's API removal
|
||||
// drops the credential only: whether the live session ends is what the setting
|
||||
// asks for, exactly as on the auto-disable path #6533 reports from.
|
||||
func TestManualClientDisableHonoursRestartSetting(t *testing.T) {
|
||||
const email = "manual-disable@example.com"
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
setting bool
|
||||
want bool
|
||||
}{
|
||||
{"setting on", true, true},
|
||||
{"setting off", false, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
setupConflictDB(t)
|
||||
setRestartOnClientDisable(t, tc.setting)
|
||||
|
||||
mgr := runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }})
|
||||
mgr.SetLocalRuntimeOverride(&fakeNodeRuntime{})
|
||||
runtime.SetManager(mgr)
|
||||
t.Cleanup(func() { runtime.SetManager(nil) })
|
||||
|
||||
seedInboundConflict(t, "manual-disable", "0.0.0.0", 50055, model.VLESS, `{"network":"tcp"}`,
|
||||
`{"clients":[{"email":"`+email+`","id":"5f2eb9d6-3a2f-4a55-9812-6ea1e2f7a333","enable":true}]}`)
|
||||
inbound := loadInboundByTag(t, "manual-disable")
|
||||
|
||||
inboundSvc := InboundService{}
|
||||
clientSvc := ClientService{}
|
||||
clients, err := inboundSvc.GetClients(inbound)
|
||||
if err != nil {
|
||||
t.Fatalf("GetClients: %v", err)
|
||||
}
|
||||
if err := clientSvc.SyncInbound(nil, inbound.Id, clients); err != nil {
|
||||
t.Fatalf("SyncInbound: %v", err)
|
||||
}
|
||||
if err := database.GetDB().Create(&xray.ClientTraffic{InboundId: inbound.Id, Email: email, Enable: true}).Error; err != nil {
|
||||
t.Fatalf("seed traffic: %v", err)
|
||||
}
|
||||
|
||||
changed, needRestart, err := clientSvc.SetClientEnableByEmail(&inboundSvc, email, false)
|
||||
if err != nil {
|
||||
t.Fatalf("SetClientEnableByEmail: %v", err)
|
||||
}
|
||||
if !changed {
|
||||
t.Fatal("the disable must be recorded")
|
||||
}
|
||||
if needRestart != tc.want {
|
||||
t.Fatalf("needRestart = %v, want %v", needRestart, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,16 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// droppedClientNeedsRestart is what restartXrayOnClientDisable asks for: the core
|
||||
// API drops the credential only, so a live session needs the process replaced.
|
||||
func droppedClientNeedsRestart() bool {
|
||||
on, err := (&SettingService{}).GetRestartXrayOnClientDisable()
|
||||
if err != nil {
|
||||
logger.Warning("get RestartXrayOnClientDisable failed:", err)
|
||||
}
|
||||
return on
|
||||
}
|
||||
|
||||
func sameClientConfigExceptUpdatedAt(a, b map[string]any) bool {
|
||||
aa := maps.Clone(a)
|
||||
bb := maps.Clone(b)
|
||||
@@ -1011,6 +1021,11 @@ func (s *ClientService) UpdateInboundClient(inboundSvc *InboundService, data *mo
|
||||
err1 := rt.RemoveUser(context.Background(), oldInbound, oldEmail)
|
||||
if err1 == nil {
|
||||
logger.Debug("Old client deleted on", rt.Name(), ":", oldEmail)
|
||||
// The API removal is enough only while the client is re-added; a
|
||||
// dropped one ends its session only through a restart.
|
||||
if !clients[0].Enable && droppedClientNeedsRestart() {
|
||||
needRestart = true
|
||||
}
|
||||
} else if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", oldEmail)) {
|
||||
logger.Debug("User is already deleted. Nothing to do more...")
|
||||
} else {
|
||||
@@ -1199,7 +1214,7 @@ func (s *ClientService) DelInboundClientByEmail(inboundSvc *InboundService, inbo
|
||||
needRestart = true
|
||||
} else if err1 := rt.RemoveUser(context.Background(), oldInbound, email); err1 == nil {
|
||||
logger.Debug("Client deleted on", rt.Name(), ":", email)
|
||||
needRestart = false
|
||||
needRestart = droppedClientNeedsRestart()
|
||||
} else if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", email)) {
|
||||
logger.Debug("User is already deleted. Nothing to do more...")
|
||||
} else {
|
||||
|
||||
@@ -1428,6 +1428,20 @@ func (s *XrayService) RestartXray(isForce bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// restartToDropClients reports whether a diff that strands clients must be
|
||||
// applied by restarting instead of through the API.
|
||||
func (s *XrayService) restartToDropClients(diff *xray.HotDiff) bool {
|
||||
if diff == nil || !diff.DropsUsers() {
|
||||
return false
|
||||
}
|
||||
restart, err := s.settingService.GetRestartXrayOnClientDisable()
|
||||
if err != nil {
|
||||
logger.Warning("get RestartXrayOnClientDisable failed:", err)
|
||||
return false
|
||||
}
|
||||
return restart
|
||||
}
|
||||
|
||||
// tryHotApply attempts to reconcile the running Xray instance with newCfg
|
||||
// through the core gRPC API (HandlerService for inbounds/outbounds,
|
||||
// RoutingService for rules/balancers). It returns true when the running
|
||||
@@ -1445,6 +1459,12 @@ func (s *XrayService) tryHotApply(process *xray.Process, newCfg *xray.Config) bo
|
||||
process.SetConfig(newCfg)
|
||||
return true
|
||||
}
|
||||
// The core's RemoveUser drops the credential only, so a disabled or deleted
|
||||
// client needs the restart this setting asks for.
|
||||
if s.restartToDropClients(diff) {
|
||||
logger.Info("hot apply: clients left the config, restarting to drop their live sessions")
|
||||
return false
|
||||
}
|
||||
|
||||
apiPort := process.GetAPIPort()
|
||||
if apiPort <= 0 {
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
||||
)
|
||||
|
||||
func setRestartOnClientDisable(t *testing.T, value bool) {
|
||||
t.Helper()
|
||||
if err := (&SettingService{}).SetRestartXrayOnClientDisable(value); err != nil {
|
||||
t.Fatalf("SetRestartXrayOnClientDisable(%v): %v", value, err)
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveUser drops the credential only, so a dropped client needs the restart the
|
||||
// setting asks for; an edit re-adds the email and needs none.
|
||||
func TestRestartToDropClients(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
diff *xray.HotDiff
|
||||
setSetting *bool
|
||||
wantRestart bool
|
||||
}{
|
||||
{
|
||||
// The default is what makes this reachable for a plain install.
|
||||
"disabled client, setting untouched",
|
||||
&xray.HotDiff{RemovedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}}},
|
||||
nil, true,
|
||||
},
|
||||
{
|
||||
"disabled client, setting on",
|
||||
&xray.HotDiff{RemovedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}}},
|
||||
new(true), true,
|
||||
},
|
||||
{
|
||||
"disabled client, setting off",
|
||||
&xray.HotDiff{RemovedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}}},
|
||||
new(false), false,
|
||||
},
|
||||
{
|
||||
"edited client",
|
||||
&xray.HotDiff{
|
||||
RemovedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}},
|
||||
AddedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}},
|
||||
},
|
||||
new(true), false,
|
||||
},
|
||||
{
|
||||
"unrelated change",
|
||||
&xray.HotDiff{AddedInbounds: [][]byte{[]byte(`{}`)}},
|
||||
new(true), false,
|
||||
},
|
||||
{
|
||||
"nil diff",
|
||||
nil,
|
||||
new(true), false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
setupConflictDB(t)
|
||||
if tc.setSetting != nil {
|
||||
setRestartOnClientDisable(t, *tc.setSetting)
|
||||
}
|
||||
got := (&XrayService{}).restartToDropClients(tc.diff)
|
||||
if got != tc.wantRestart {
|
||||
t.Fatalf("restartToDropClients = %v, want %v", got, tc.wantRestart)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user