[PR #7090] fix(amneziawg): check forwarded ports against the port the panel listens on #6644

Open
opened 2026-11-07 14:34:21 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/7090
Author: @Dv2Corn
Created: 11/6/2026
Status: 🔄 Open

Base: main ← Head: fix/awg-forward-xui-port


📝 Commits (1)

  • 824e113 fix(amneziawg): check forwarded ports against the port the panel listens on

📊 Changes

5 files changed (+57 additions, -4 deletions)

View changed files

📝 internal/web/service/inbound_amneziawg.go (+1 -1)
📝 internal/web/service/inbound_amneziawg_test.go (+15 -0)
📝 internal/web/service/setting.go (+9 -0)
➕ internal/web/service/setting_listen_port_test.go (+30 -0)
📝 internal/web/web.go (+2 -3)

📄 Description

Summary

The AmneziaWG port-forward conflict check now compares a client's forwarded ports with the port the panel really listens on, XUI_PORT when it is set, instead of the stored webPort. The XUI_PORT-over-webPort rule moves into one helper, SettingService.GetListenPort, which the web server and the check both use.

Why

The web server listens on XUI_PORT when it is set and valid, and on the stored webPort otherwise (web.go#L592-L604). The conflict check that guards AmneziaWG forwardedPorts reads only the stored webPort (inbound_amneziawg.go#L379, used at #L427).

With XUI_PORT set (the Docker setup documents XUI_PORT: "8080" with the same port published), a client can be saved with a forward on the real panel port, for example 8080. The check passes, because the stored webPort is still 2053, but the forward listener can never bind, since the panel already holds that port. Meanwhile a forward on 2053, a port nothing listens on, is refused.

What changes:

  • New SettingService.GetListenPort(): a valid XUI_PORT wins, otherwise the stored webPort. That is the same rule the web server applied inline, so an invalid XUI_PORT is still ignored with the existing warning.
  • Server.start takes its port from GetListenPort(); it keeps the warning/info log lines.
  • loadPortConflictContext uses GetListenPort().

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

New tests:

  • TestCheckForwardedPortsConflict_CollidesWithXUIPort in internal/web/service/inbound_amneziawg_test.go: XUI_PORT=8443 on a fresh database (stored webPort is the default 2053); a forward on 8443 must be reported as a collision with the panel. On main it fails with expected a collision naming the port XUI_PORT makes the panel listen on, got ""; with the fix it passes.
  • TestGetListenPort in internal/web/service/setting_listen_port_test.go: XUI_PORT unset gives 2053, 8443 gives 8443, abc and 70000 fall back to 2053.

The existing TestCheckForwardedPortsConflict_* tests, including CollidesWithPanelPort without XUI_PORT, still pass.

Run with Go 1.27.3 and the internal/web/dist stub the CI uses:

  • go build ./..., go vet ./... and gofmt -l are clean.
  • go test -shuffle=on -count=1 ./internal/web/service ./internal/web passes.
  • go test -race -shuffle=on -count=1 ./internal/web/service passes.

Screenshots / recordings

N/A

Breaking changes

None. Without XUI_PORT nothing changes. With XUI_PORT set, a new AmneziaWG forward on that port is now refused, and a forward on the stored webPort is no longer refused. Every save of an AmneziaWG inbound re-checks all its clients, so an inbound that already has a client forwarding the XUI_PORT port is refused on its next save, with an error naming that client and the panel port, until the forward is changed. That forward could not bind anyway.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 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/7090 **Author:** [@Dv2Corn](https://github.com/Dv2Corn) **Created:** 11/6/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/awg-forward-xui-port` --- ### 📝 Commits (1) - [`824e113`](https://github.com/MHSanaei/3x-ui/commit/824e1134cb8247a527abb023369f25356aaacdfd) fix(amneziawg): check forwarded ports against the port the panel listens on ### 📊 Changes **5 files changed** (+57 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/inbound_amneziawg.go` (+1 -1) 📝 `internal/web/service/inbound_amneziawg_test.go` (+15 -0) 📝 `internal/web/service/setting.go` (+9 -0) ➕ `internal/web/service/setting_listen_port_test.go` (+30 -0) 📝 `internal/web/web.go` (+2 -3) </details> ### 📄 Description ## Summary The AmneziaWG port-forward conflict check now compares a client's forwarded ports with the port the panel really listens on, `XUI_PORT` when it is set, instead of the stored `webPort`. The `XUI_PORT`-over-`webPort` rule moves into one helper, `SettingService.GetListenPort`, which the web server and the check both use. ## Why The web server listens on `XUI_PORT` when it is set and valid, and on the stored `webPort` otherwise ([web.go#L592-L604](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/web.go#L592-L604)). The conflict check that guards AmneziaWG `forwardedPorts` reads only the stored `webPort` ([inbound_amneziawg.go#L379](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/inbound_amneziawg.go#L379), used at [#L427](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/inbound_amneziawg.go#L427)). With `XUI_PORT` set (the Docker setup documents `XUI_PORT: "8080"` with the same port published), a client can be saved with a forward on the real panel port, for example `8080`. The check passes, because the stored `webPort` is still `2053`, but the forward listener can never bind, since the panel already holds that port. Meanwhile a forward on `2053`, a port nothing listens on, is refused. What changes: - New `SettingService.GetListenPort()`: a valid `XUI_PORT` wins, otherwise the stored `webPort`. That is the same rule the web server applied inline, so an invalid `XUI_PORT` is still ignored with the existing warning. - `Server.start` takes its port from `GetListenPort()`; it keeps the warning/info log lines. - `loadPortConflictContext` uses `GetListenPort()`. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? New tests: - `TestCheckForwardedPortsConflict_CollidesWithXUIPort` in `internal/web/service/inbound_amneziawg_test.go`: `XUI_PORT=8443` on a fresh database (stored `webPort` is the default `2053`); a forward on `8443` must be reported as a collision with the panel. On `main` it fails with `expected a collision naming the port XUI_PORT makes the panel listen on, got ""`; with the fix it passes. - `TestGetListenPort` in `internal/web/service/setting_listen_port_test.go`: `XUI_PORT` unset gives `2053`, `8443` gives `8443`, `abc` and `70000` fall back to `2053`. The existing `TestCheckForwardedPortsConflict_*` tests, including `CollidesWithPanelPort` without `XUI_PORT`, still pass. Run with Go 1.27.3 and the `internal/web/dist` stub the CI uses: - `go build ./...`, `go vet ./...` and `gofmt -l` are clean. - `go test -shuffle=on -count=1 ./internal/web/service ./internal/web` passes. - `go test -race -shuffle=on -count=1 ./internal/web/service` passes. ## Screenshots / recordings N/A ## Breaking changes None. Without `XUI_PORT` nothing changes. With `XUI_PORT` set, a new AmneziaWG forward on that port is now refused, and a forward on the stored `webPort` is no longer refused. Every save of an AmneziaWG inbound re-checks all its clients, so an inbound that already has a client forwarding the `XUI_PORT` port is refused on its next save, with an error naming that client and the panel port, until the forward is changed. That forward could not bind anyway. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#6644
No description provided.