feat(cli): name the inbounds a settings reset leaves on the panel's ports

x-ui setting -reset puts the panel back on its default ports, 2053 and
2096 for the subscription server. An inbound already listening on one of
them then shares a socket with the panel, and Xray cannot start. The
other writers of those ports refuse such a change, but a reset is the
way back into a panel whose port was forgotten, so it must not be
refused.

After the reset the command now lists every enabled local inbound that
collides with a panel socket and says that Xray will not start until one
of the two moves. Disabled inbounds are left out: they bind nothing, and
enabling one is refused by the port check.

Printing the lines from main.go is wiring no test reaches; running
x-ui setting -reset on a database with inbounds on 2053 and 2096 prints
one warning for each.
This commit is contained in:
Sanaei
2026-11-29 15:08:07 +03:30
parent 8849518f50
commit 2cfeec3b99
3 changed files with 72 additions and 0 deletions
+26
View File
@@ -126,6 +126,32 @@ func checkPanelListenersFree(db *gorm.DB, next []panelListener) error {
return nil
}
// PanelListenerCollisions describes each enabled local inbound that binds a socket the
// panel holds under the stored settings; Xray cannot start while one exists.
func (s *SettingService) PanelListenerCollisions() ([]string, error) {
listeners, err := livePanelListeners()
if err != nil {
return nil, err
}
ports := make([]int, 0, len(listeners))
for _, l := range listeners {
ports = append(ports, l.port)
}
var rows []*model.Inbound
if err := database.GetDB().Model(model.Inbound{}).
Where("node_id IS NULL AND enable = ? AND port IN ?", true, ports).
Order("id").Find(&rows).Error; err != nil {
return nil, err
}
var collisions []string
for _, row := range rows {
if l := panelListenerClaiming(listeners, row); l != nil {
collisions = append(collisions, "inbound "+inboundLabel(row.Remark, row.Tag, row.Id)+" cannot start: "+l.conflict().String())
}
}
return collisions, nil
}
func allSettingListeners(all *entity.AllSetting) ([]panelListener, error) {
values := map[string]string{
"webListen": all.WebListen,
@@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/config"
@@ -204,6 +205,39 @@ func TestPanelListenerCannotMoveOntoAnInbound(t *testing.T) {
})
}
// A reset is the way back into a panel whose port was forgotten, so it is never
// refused; it has to say which inbounds the default ports now collide with.
func TestResetSettingsReportsInboundsOnTheDefaultPorts(t *testing.T) {
setupConflictDB(t)
seedPanelSettings(t, map[string]string{"webPort": "8443", "subPort": "9443"})
seedInboundConflict(t, "on-sub-port", "", 2096, model.VLESS, rawTCPStream, vlessNoUsers)
seedInboundConflict(t, "elsewhere", "", 44321, model.VLESS, rawTCPStream, vlessNoUsers)
disabled := &model.Inbound{Tag: "disabled-on-web-port", Enable: true, Port: 2053, Protocol: model.VLESS, StreamSettings: rawTCPStream, Settings: vlessNoUsers}
if err := database.GetDB().Create(disabled).Error; err != nil {
t.Fatal(err)
}
if err := database.GetDB().Model(disabled).Update("enable", false).Error; err != nil {
t.Fatal(err)
}
s := &SettingService{}
before, err := s.PanelListenerCollisions()
if err != nil || len(before) != 0 {
t.Fatalf("collisions before the reset = %q, error=%v, want none: the panel is on 8443 and 9443", before, err)
}
if err := s.ResetSettings(); err != nil {
t.Fatalf("ResetSettings: %v", err)
}
after, err := s.PanelListenerCollisions()
if err != nil {
t.Fatalf("PanelListenerCollisions: %v", err)
}
want := []string{"inbound 'on-sub-port' (#1) cannot start: port 2096 (tcp) already used by this panel's own subscription listener on *"}
if !slices.Equal(after, want) {
t.Fatalf("collisions after the reset = %q, want %q", after, want)
}
}
// restoreBackupBytes builds a real panel database in its own folder and returns
// the file an operator would download from Back Up.
func restoreBackupBytes(t *testing.T, settings map[string]string, inbounds ...*model.Inbound) []byte {
+12
View File
@@ -243,6 +243,18 @@ func resetSetting() error {
} else {
fmt.Println("Settings successfully reset.")
}
// The reset is never refused, so a default port an inbound already holds is reported.
collisions, err := settingService.PanelListenerCollisions()
if err != nil {
fmt.Println("Could not check the inbounds against the default panel ports:", err)
return nil
}
for _, collision := range collisions {
fmt.Println("Warning:", collision)
}
if len(collisions) > 0 {
fmt.Println("Xray will not start until that inbound or the panel uses another port.")
}
return nil
}