[PR #6514] [CLOSED] feat(settings): randomize subscription port on fresh installations #6322

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6514
Author: @poise52
Created: 9/13/2026
Status: ❌ Closed

Base: main ← Head: feat/random-subport


📝 Commits (9)

  • 27056ca feat(settings): randomize subscription port on fresh installations
  • eafbbef docs: document XUI_SUB_PORT and unattended install variables
  • 0543328 fix(install): guard subport flag against release skew and fix review findings
  • a288650 Merge branch 'main' into feat/random-subport
  • c9b8452 fix(install): gate reported subport and surface XUI_SUB_PORT override
  • 1b001a4 Merge branch 'main' into feat/random-subport
  • cb336e8 Merge branch 'main' into feat/random-subport
  • 8d8f944 Merge branch 'main' into feat/random-subport
  • 581503e Merge branch 'main' into feat/random-subport

📊 Changes

36 files changed (+265 additions, -48 deletions)

View changed files

📝 .env.example (+9 -0)
📝 CONTRIBUTING.md (+4 -3)
📝 deploy/README.md (+1 -1)
📝 deploy/cloud-init/README.md (+1 -0)
📝 deploy/cloud-init/cloud-init.yaml (+1 -0)
📝 deploy/test/smoke-noninteractive.sh (+2 -1)
📝 docs/content/docs/en/config/subscription.mdx (+1 -1)
📝 docs/content/docs/en/guide/first-login.mdx (+1 -0)
📝 docs/content/docs/en/reference/api/subscription-server.mdx (+5 -5)
📝 docs/content/docs/en/reference/env-vars.mdx (+3 -0)
📝 docs/content/docs/en/reference/ports-firewall.mdx (+1 -1)
📝 docs/content/docs/fa/config/subscription.mdx (+1 -1)
📝 docs/content/docs/fa/guide/first-login.mdx (+1 -0)
📝 docs/content/docs/fa/reference/api/subscription-server.mdx (+5 -5)
📝 docs/content/docs/fa/reference/env-vars.mdx (+3 -0)
📝 docs/content/docs/fa/reference/ports-firewall.mdx (+1 -1)
📝 docs/content/docs/ru/config/subscription.mdx (+1 -1)
📝 docs/content/docs/ru/guide/first-login.mdx (+1 -0)
📝 docs/content/docs/ru/reference/api/subscription-server.mdx (+6 -5)
📝 docs/content/docs/ru/reference/env-vars.mdx (+3 -0)

...and 16 more files

📄 Description

Summary

Randomize the default subscription port (subPort) within the safe non-privileged range (1024-62000) on fresh installations, while ensuring 100% backward compatibility for existing installations.

Why

Using a static default port (2096) makes subscription endpoints easy targets for automated ISP port scanning and censorship blocking (e.g., TSPU/RKN in Russia) and can cause port collisions on single-VPS setups. Generating a distinct random port on fresh installs mitigates fingerprinting and port conflicts without breaking existing setups.
Fixes #6447

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?

  1. Backend & DB tests:
    • go test -v -run "TestInitDB" ./internal/database/... — verified that fresh databases receive a random subPort in [1024, 62000] and that existing databases preserve their existing port unchanged.
    • go test -v ./internal/util/random/... — verified port generator range and distribution.
    • go test -v -run TestRouteRegistryContract ./internal/web — verified route registry contract.
    • go build ./... — compiled cleanly without errors.
  2. Frontend & Docs contracts:
    • cd frontend && npx vitest run src/test/factory-defaults-contract.test.ts — verified factory default contracts pass (138 tests).
    • cd docs && npm run format:check && npm run lint && npm run typecheck && npm test && npm run build — all 269 documentation pages across EN, RU, ZH, FA compiled cleanly.
  3. Collision prevention:
    • Verified that both install.sh and DB seeder guarantee subPort != webPort.
    • Verified CLI x-ui setting -subport and updateSetting reject matching web and sub ports.

Screenshots / recordings

N/A (Backend, database seeding, CLI, and installation script changes).

Breaking changes

None. Existing databases are never modified (FirstOrCreate is gated by isUsersEmpty), and fallback factory defaults remain 2096. Existing subscriptions continue to work seamlessly.

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/6514 **Author:** [@poise52](https://github.com/poise52) **Created:** 9/13/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/random-subport` --- ### 📝 Commits (9) - [`27056ca`](https://github.com/MHSanaei/3x-ui/commit/27056ca9e0b5d657093f23131efb5b87ff552f09) feat(settings): randomize subscription port on fresh installations - [`eafbbef`](https://github.com/MHSanaei/3x-ui/commit/eafbbef2e81ca206a89b479e976a6b26bc53276e) docs: document XUI_SUB_PORT and unattended install variables - [`0543328`](https://github.com/MHSanaei/3x-ui/commit/05433281aef68a9e2cacd974a163d641d6f0be9f) fix(install): guard subport flag against release skew and fix review findings - [`a288650`](https://github.com/MHSanaei/3x-ui/commit/a2886506b18a423b5a7bdb2a72a8288a290389e5) Merge branch 'main' into feat/random-subport - [`c9b8452`](https://github.com/MHSanaei/3x-ui/commit/c9b84523985104d03e2269b4dfa234ec4a9d66e8) fix(install): gate reported subport and surface XUI_SUB_PORT override - [`1b001a4`](https://github.com/MHSanaei/3x-ui/commit/1b001a4fe74db140cad42f7a9a7c92e55c6cd633) Merge branch 'main' into feat/random-subport - [`cb336e8`](https://github.com/MHSanaei/3x-ui/commit/cb336e85de6e103be1b9e55f5c24e7244714b3fd) Merge branch 'main' into feat/random-subport - [`8d8f944`](https://github.com/MHSanaei/3x-ui/commit/8d8f94414e13489536a531d6f941508d2a70bdbb) Merge branch 'main' into feat/random-subport - [`581503e`](https://github.com/MHSanaei/3x-ui/commit/581503e447a35650c048246d11e9115c957e9ae8) Merge branch 'main' into feat/random-subport ### 📊 Changes **36 files changed** (+265 additions, -48 deletions) <details> <summary>View changed files</summary> 📝 `.env.example` (+9 -0) 📝 `CONTRIBUTING.md` (+4 -3) 📝 `deploy/README.md` (+1 -1) 📝 `deploy/cloud-init/README.md` (+1 -0) 📝 `deploy/cloud-init/cloud-init.yaml` (+1 -0) 📝 `deploy/test/smoke-noninteractive.sh` (+2 -1) 📝 `docs/content/docs/en/config/subscription.mdx` (+1 -1) 📝 `docs/content/docs/en/guide/first-login.mdx` (+1 -0) 📝 `docs/content/docs/en/reference/api/subscription-server.mdx` (+5 -5) 📝 `docs/content/docs/en/reference/env-vars.mdx` (+3 -0) 📝 `docs/content/docs/en/reference/ports-firewall.mdx` (+1 -1) 📝 `docs/content/docs/fa/config/subscription.mdx` (+1 -1) 📝 `docs/content/docs/fa/guide/first-login.mdx` (+1 -0) 📝 `docs/content/docs/fa/reference/api/subscription-server.mdx` (+5 -5) 📝 `docs/content/docs/fa/reference/env-vars.mdx` (+3 -0) 📝 `docs/content/docs/fa/reference/ports-firewall.mdx` (+1 -1) 📝 `docs/content/docs/ru/config/subscription.mdx` (+1 -1) 📝 `docs/content/docs/ru/guide/first-login.mdx` (+1 -0) 📝 `docs/content/docs/ru/reference/api/subscription-server.mdx` (+6 -5) 📝 `docs/content/docs/ru/reference/env-vars.mdx` (+3 -0) _...and 16 more files_ </details> ### 📄 Description ## Summary Randomize the default subscription port (`subPort`) within the safe non-privileged range (`1024-62000`) on fresh installations, while ensuring 100% backward compatibility for existing installations. ## Why Using a static default port (`2096`) makes subscription endpoints easy targets for automated ISP port scanning and censorship blocking (e.g., TSPU/RKN in Russia) and can cause port collisions on single-VPS setups. Generating a distinct random port on fresh installs mitigates fingerprinting and port conflicts without breaking existing setups. Fixes #6447 ## Type of change - [ ] Bug fix - [x] New feature - [ ] Refactoring (no behavior change) - [x] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [x] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [x] Database / migrations - [x] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? 1. **Backend & DB tests**: - `go test -v -run "TestInitDB" ./internal/database/...` — verified that fresh databases receive a random `subPort` in `[1024, 62000]` and that existing databases preserve their existing port unchanged. - `go test -v ./internal/util/random/...` — verified port generator range and distribution. - `go test -v -run TestRouteRegistryContract ./internal/web` — verified route registry contract. - `go build ./...` — compiled cleanly without errors. 2. **Frontend & Docs contracts**: - `cd frontend && npx vitest run src/test/factory-defaults-contract.test.ts` — verified factory default contracts pass (138 tests). - `cd docs && npm run format:check && npm run lint && npm run typecheck && npm test && npm run build` — all 269 documentation pages across EN, RU, ZH, FA compiled cleanly. 3. **Collision prevention**: - Verified that both `install.sh` and DB seeder guarantee `subPort != webPort`. - Verified CLI `x-ui setting -subport` and `updateSetting` reject matching web and sub ports. ## Screenshots / recordings N/A (Backend, database seeding, CLI, and installation script changes). ## Breaking changes None. Existing databases are never modified (`FirstOrCreate` is gated by `isUsersEmpty`), and fallback factory defaults remain `2096`. Existing subscriptions continue to work seamlessly. ## 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. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] 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>
bsakaguchi 2026-11-02 04:07:56 +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#6322
No description provided.