[PR #6549] [MERGED] fix(inbounds): check ports when an inbound is enabled, not only when it is saved #6347

Closed
opened 2026-11-02 04:08:07 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6549
Author: @BlindMaster24
Created: 9/15/2026
Status: ✅ Merged
Merged: 9/15/2026
Merged by: @BlindMaster24

Base: main ← Head: fix/inbound-enable-port-check


📝 Commits (2)

  • f53e0c1 fix(inbounds): check ports when an inbound is enabled, not only when it is saved
  • bd32435 docs(inbounds): state the real reason the enable path needs its own check

📊 Changes

2 files changed (+94 additions, -0 deletions)

View changed files

📝 internal/web/service/inbound.go (+11 -0)
➕ internal/web/service/inbound_enable_port_test.go (+83 -0)

📄 Description

What breaks today

SetInboundEnable (inbound.go:1574) flips the enable column and, for a node row, polls the remote — with no port check on that path at all. Every guard in the panel lives on the save path: checkPortConflictTx runs from AddInbound (inbound.go:1229) and UpdateInbound (inbound.go:1729), and nowhere else. So a row can reach the enabled state having never been checked, whenever the pair was not produced through a guarded save:

  • a row that predates the guards — they landed over three PRs this cycle (#6539, #6540, #6542), so an installation upgrading across them can hold a same-port pair in its DB;
  • a restored backup or a hand-edited row, which never went through AddInbound.

Once the flag moves, the generated config has two inbounds on one socket, and the core does not skip the second one — it exits the whole process on the failed listen (main/run.go:94), taking every protocol on the host down, then the one-second watchdog retries it. Reached from the UI with the enable switch; no unusual steps.

The change

Check the same rules before the flag moves, and only for a local enable — nodes run their own Xray, so a node row sharing a local port stays legal, and enabling is the direction that can create a collision (disabling cannot):

if enable && inbound.NodeID == nil {
    conflict, err := checkPortConflictTx(db, inbound, inbound.Id)
    ...
}

The refusal is common.NewError(conflict.String()), the same shape AddInbound/UpdateInbound already return, so the panel shows which row owns the port ('holder' (#2) port 44431 (tcp)). Because the check runs before the transaction, a refused enable leaves the row disabled — no half-applied switch.

Reuse, not a new rule: this is the existing checker with all of its special cases (tcp/udp coexistence, reserved API and egress ports, AmneziaWG relay slots, node exemption), so an enable can no longer be refused for something a save would have allowed.

Note this is the same funnel question the previous PRs closed from the config side: with #6547 a colliding config is refused before the core is swapped, so the failure is loud either way — but refusing at the switch is where the operator can act, and it keeps the log from being the only report.

Red first, with the exact output

TestSetInboundEnableRefusesAPortAnotherEnabledInboundServes — two rows on 0.0.0.0:44431, the second disabled through the DB, then the enable switch:

--- FAIL: TestSetInboundEnableRefusesAPortAnotherEnabledInboundServes (0.27s)
    inbound_enable_port_test.go:39: enabling a row onto a port another enabled inbound serves must be refused

Green after the change, and it asserts the two things that matter beyond the error: the message names holder, and the row is still disabled afterwards.

The state is seeded with seedInboundConflict (a direct db.Create) plus a raw Update("enable", false) on purpose: AddInbound treats a disabled row as owning its port (port_conflict.go:245-252 has no enable predicate), so the API refuses to build this pair in the first place — which is exactly the class of row this guard is for.

Two guards against the fix being too broad, both green before and after:

  • TestSetInboundEnableAllowsTCPUDPCoexistence — a vless/tcp row enabling onto a port a hysteria/udp row holds, the legal coexistence the rest of the guards allow.
  • TestSetInboundEnableAllowsANodeRowOnALocalPort — a node row enabling onto a local row's port.

The pre-existing TestSetInboundEnableRoutedMtprotoRequestsRestart still passes, so the MTProto restart path through this function is untouched.

Gate

  • go test ./internal/web/service -shuffle=on — green, 255 s (local run through a -overlay supplying a pure-Go SQLite driver; the tree's go.mod/go.sum are unchanged). All TestAddInbound*, TestUpdateInbound* and TestSetInboundEnable* tests included.
  • go test ./internal/web/job and ./internal/web/controller — green.
  • gofmt -l and go vet on the touched packages — clean.
  • CI on this head: go-test, golangci, race.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6549 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/15/2026 **Status:** ✅ Merged **Merged:** 9/15/2026 **Merged by:** [@BlindMaster24](https://github.com/BlindMaster24) **Base:** `main` ← **Head:** `fix/inbound-enable-port-check` --- ### 📝 Commits (2) - [`f53e0c1`](https://github.com/MHSanaei/3x-ui/commit/f53e0c182f5033bbd99e70a3c9d69bf59d78e0d3) fix(inbounds): check ports when an inbound is enabled, not only when it is saved - [`bd32435`](https://github.com/MHSanaei/3x-ui/commit/bd324355d88207ef522d4c9eb1c4235b477a892e) docs(inbounds): state the real reason the enable path needs its own check ### 📊 Changes **2 files changed** (+94 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/inbound.go` (+11 -0) ➕ `internal/web/service/inbound_enable_port_test.go` (+83 -0) </details> ### 📄 Description ## What breaks today `SetInboundEnable` (`inbound.go:1574`) flips the `enable` column and, for a node row, polls the remote — with **no port check on that path at all**. Every guard in the panel lives on the save path: `checkPortConflictTx` runs from `AddInbound` (`inbound.go:1229`) and `UpdateInbound` (`inbound.go:1729`), and nowhere else. So a row can reach the enabled state having never been checked, whenever the pair was not produced through a guarded save: * a row that predates the guards — they landed over three PRs this cycle (`#6539`, `#6540`, `#6542`), so an installation upgrading across them can hold a same-port pair in its DB; * a restored backup or a hand-edited row, which never went through `AddInbound`. Once the flag moves, the generated config has two inbounds on one socket, and the core does not skip the second one — it exits the whole process on the failed listen (`main/run.go:94`), taking every protocol on the host down, then the one-second watchdog retries it. Reached from the UI with the enable switch; no unusual steps. ## The change Check the same rules before the flag moves, and only for a local enable — nodes run their own Xray, so a node row sharing a local port stays legal, and enabling is the direction that can create a collision (disabling cannot): ```go if enable && inbound.NodeID == nil { conflict, err := checkPortConflictTx(db, inbound, inbound.Id) ... } ``` The refusal is `common.NewError(conflict.String())`, the same shape `AddInbound`/`UpdateInbound` already return, so the panel shows *which* row owns the port (`'holder' (#2) port 44431 (tcp)`). Because the check runs before the transaction, a refused enable leaves the row disabled — no half-applied switch. Reuse, not a new rule: this is the existing checker with all of its special cases (tcp/udp coexistence, reserved API and egress ports, AmneziaWG relay slots, node exemption), so an enable can no longer be refused for something a save would have allowed. Note this is the same funnel question the previous PRs closed from the config side: with #6547 a colliding config is refused before the core is swapped, so the failure is loud either way — but refusing at the switch is where the operator can act, and it keeps the log from being the only report. ## Red first, with the exact output `TestSetInboundEnableRefusesAPortAnotherEnabledInboundServes` — two rows on `0.0.0.0:44431`, the second disabled through the DB, then the enable switch: ``` --- FAIL: TestSetInboundEnableRefusesAPortAnotherEnabledInboundServes (0.27s) inbound_enable_port_test.go:39: enabling a row onto a port another enabled inbound serves must be refused ``` Green after the change, and it asserts the two things that matter beyond the error: the message names `holder`, and the row is still disabled afterwards. The state is seeded with `seedInboundConflict` (a direct `db.Create`) plus a raw `Update("enable", false)` on purpose: `AddInbound` treats a **disabled** row as owning its port (`port_conflict.go:245-252` has no `enable` predicate), so the API refuses to build this pair in the first place — which is exactly the class of row this guard is for. Two guards against the fix being too broad, both green before and after: * `TestSetInboundEnableAllowsTCPUDPCoexistence` — a `vless/tcp` row enabling onto a port a `hysteria/udp` row holds, the legal coexistence the rest of the guards allow. * `TestSetInboundEnableAllowsANodeRowOnALocalPort` — a node row enabling onto a local row's port. The pre-existing `TestSetInboundEnableRoutedMtprotoRequestsRestart` still passes, so the MTProto restart path through this function is untouched. ## Gate * `go test ./internal/web/service -shuffle=on` — green, 255 s (local run through a `-overlay` supplying a pure-Go SQLite driver; the tree's `go.mod`/`go.sum` are unchanged). All `TestAddInbound*`, `TestUpdateInbound*` and `TestSetInboundEnable*` tests included. * `go test ./internal/web/job` and `./internal/web/controller` — green. * `gofmt -l` and `go vet` on the touched packages — clean. * CI on this head: `go-test`, `golangci`, `race`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:07 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6347
No description provided.