[PR #6784] [MERGED] feat(sub): support random IP generation from CIDR host addresses #6453

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6784
Author: @Pejman-Yousefi-Official
Created: 10/10/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @MHSanaei

Base: main ← Head: main


📝 Commits (10+)

  • dd1d9e6 feat(sub): support random IP generation from CIDR host addresses
  • d74a57b fix(ui): compute hasCidr without useMemo to satisfy oxlint rules
  • 54b15f6 fix(ui): format randomCount form field to satisfy oxfmt rules
  • 0d631b0 Merge branch 'main' into main
  • 72f4720 Merge branch 'main' into main
  • 24cb8f0 Merge branch 'main' into main
  • 90688d0 fix(ci): stop the PR review ending mid-wait and posting its draft
  • 1f6b5ae Merge branch 'main' into main
  • 32f3618 Merge remote-tracking branch 'origin/main' into pr/6784
  • 265238f fix(sub): mirror CIDR expansion in frontend and clamp randomCount

📊 Changes

33 files changed (+886 additions, -8 deletions)

View changed files

📝 docs/public/openapi.json (+17 -0)
📝 frontend/public/openapi.json (+17 -0)
📝 frontend/src/generated/examples.ts (+2 -0)
📝 frontend/src/generated/schemas.ts (+11 -0)
📝 frontend/src/generated/types.ts (+2 -0)
📝 frontend/src/generated/zod.ts (+2 -0)
➕ frontend/src/lib/hosts/host-cidr.ts (+266 -0)
📝 frontend/src/lib/hosts/host-link.ts (+41 -7)
📝 frontend/src/pages/hosts/HostFormModal.tsx (+8 -0)
📝 frontend/src/schemas/api/host.ts (+2 -0)
📝 frontend/src/test/tunnel-host-endpoints.test.ts (+21 -0)
📝 internal/database/db.go (+1 -0)
📝 internal/database/host_test.go (+1 -1)
📝 internal/database/model/model.go (+1 -0)
➕ internal/sub/host_cidr.go (+156 -0)
➕ internal/sub/host_cidr_test.go (+175 -0)
📝 internal/sub/host_sub.go (+23 -0)
📝 internal/sub/host_sub_test.go (+111 -0)
📝 internal/web/entity/entity.go (+1 -0)
📝 internal/web/service/host.go (+15 -0)

...and 13 more files

📄 Description

Summary

Add support for IPv4 and IPv6 CIDR notations in Host addresses. When a host address contains a CIDR subnet, subscription feeds (raw links, Clash proxies, and Xray JSON outbounds) dynamically expand it into up to randomCount distinct random IPs with numbered remarks.

Why

Operators commonly assign broad IPv6 subnets (such as /64) or IPv4 subnets across inbounds and reverse-proxy frontends. Previously, Host entries only accepted literal IP addresses or domain names, requiring operators to manually create dozens of duplicate host entries to achieve IP diversification. Supporting CIDR blocks enables automatic per-subscription IP rotation without database bloat or manual maintenance.

Type of change

  • New feature
  • Bug fix
  • 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?

  • Unit tests:

    • Added internal/sub/host_cidr_test.go covering IPv4 (/32, /31, /24), IPv6 (/128, /64, bracketed [2001:db8::]/64), network/broadcast avoidance, subnet-router anycast exclusion, and collision handling.
    • Added internal/sub/host_sub_test.go asserting endpoint expansion across raw share links, Clash YAML proxies, and Xray JSON outbounds with remark suffixes (Remark - 1, Remark - 2, etc.).
    • Updated internal/database/host_test.go verifying GORM auto-migration for random_count across both SQLite and PostgreSQL.
    • All repository tests pass locally: go test ./....
  • Manual verification:

    1. Opened Hosts modal in the panel.
    2. Input literal host address (cdn.example.com) — confirmed randomCount input remains hidden.
    3. Input CIDR address (2a01:4f8::/64) — confirmed randomCount input dynamically appears below the Address field.
    4. Configured randomCount = 3 and fetched /sub/... raw, Clash, and JSON endpoints. Verified 3 unique random IPv6 addresses generated with sequential remark suffixes.
    5. Verified mixed entries (standard domain + CIDR) correctly preserve the static address alongside generated CIDR IPs.
    6. Verified OpenAPI spec synchronization (tools/openapigen and docs/public/openapi.json).

Breaking changes

None. The database field defaults to 1 via GORM column default and automatic migration. Hosts without CIDR notation retain identical behavior.

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/6784 **Author:** [@Pejman-Yousefi-Official](https://github.com/Pejman-Yousefi-Official) **Created:** 10/10/2026 **Status:** ✅ Merged **Merged:** 10/24/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (10+) - [`dd1d9e6`](https://github.com/MHSanaei/3x-ui/commit/dd1d9e67c75959bf312a563f193620403e9f882d) feat(sub): support random IP generation from CIDR host addresses - [`d74a57b`](https://github.com/MHSanaei/3x-ui/commit/d74a57b482145aaf22fd0efc449742d148e5bcee) fix(ui): compute hasCidr without useMemo to satisfy oxlint rules - [`54b15f6`](https://github.com/MHSanaei/3x-ui/commit/54b15f61c623966502c2b09ad1b5d495c74db672) fix(ui): format randomCount form field to satisfy oxfmt rules - [`0d631b0`](https://github.com/MHSanaei/3x-ui/commit/0d631b0f329bc7759be2f6e53b6e72d52079c9fa) Merge branch 'main' into main - [`72f4720`](https://github.com/MHSanaei/3x-ui/commit/72f4720d22573ec63e36045b36e17099e13c6673) Merge branch 'main' into main - [`24cb8f0`](https://github.com/MHSanaei/3x-ui/commit/24cb8f03e6f0970eee88732ed859625d7db5330d) Merge branch 'main' into main - [`90688d0`](https://github.com/MHSanaei/3x-ui/commit/90688d04af16d9e0ce45cfdbc0e9bba1a5d0ebd6) fix(ci): stop the PR review ending mid-wait and posting its draft - [`1f6b5ae`](https://github.com/MHSanaei/3x-ui/commit/1f6b5ae8fa4a7025ac43cd9b095943679fcca16c) Merge branch 'main' into main - [`32f3618`](https://github.com/MHSanaei/3x-ui/commit/32f3618b6b4d44c2210bd9f241c97b65cd4e9964) Merge remote-tracking branch 'origin/main' into pr/6784 - [`265238f`](https://github.com/MHSanaei/3x-ui/commit/265238f630baf32f0d7da831e979f270d783d84a) fix(sub): mirror CIDR expansion in frontend and clamp randomCount ### 📊 Changes **33 files changed** (+886 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `docs/public/openapi.json` (+17 -0) 📝 `frontend/public/openapi.json` (+17 -0) 📝 `frontend/src/generated/examples.ts` (+2 -0) 📝 `frontend/src/generated/schemas.ts` (+11 -0) 📝 `frontend/src/generated/types.ts` (+2 -0) 📝 `frontend/src/generated/zod.ts` (+2 -0) ➕ `frontend/src/lib/hosts/host-cidr.ts` (+266 -0) 📝 `frontend/src/lib/hosts/host-link.ts` (+41 -7) 📝 `frontend/src/pages/hosts/HostFormModal.tsx` (+8 -0) 📝 `frontend/src/schemas/api/host.ts` (+2 -0) 📝 `frontend/src/test/tunnel-host-endpoints.test.ts` (+21 -0) 📝 `internal/database/db.go` (+1 -0) 📝 `internal/database/host_test.go` (+1 -1) 📝 `internal/database/model/model.go` (+1 -0) ➕ `internal/sub/host_cidr.go` (+156 -0) ➕ `internal/sub/host_cidr_test.go` (+175 -0) 📝 `internal/sub/host_sub.go` (+23 -0) 📝 `internal/sub/host_sub_test.go` (+111 -0) 📝 `internal/web/entity/entity.go` (+1 -0) 📝 `internal/web/service/host.go` (+15 -0) _...and 13 more files_ </details> ### 📄 Description ## Summary Add support for IPv4 and IPv6 CIDR notations in Host addresses. When a host address contains a CIDR subnet, subscription feeds (raw links, Clash proxies, and Xray JSON outbounds) dynamically expand it into up to `randomCount` distinct random IPs with numbered remarks. ## Why Operators commonly assign broad IPv6 subnets (such as `/64`) or IPv4 subnets across inbounds and reverse-proxy frontends. Previously, Host entries only accepted literal IP addresses or domain names, requiring operators to manually create dozens of duplicate host entries to achieve IP diversification. Supporting CIDR blocks enables automatic per-subscription IP rotation without database bloat or manual maintenance. ## Type of change - [x] New feature - [ ] Bug fix - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] 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 - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? - **Unit tests:** - Added `internal/sub/host_cidr_test.go` covering IPv4 (`/32`, `/31`, `/24`), IPv6 (`/128`, `/64`, bracketed `[2001:db8::]/64`), network/broadcast avoidance, subnet-router anycast exclusion, and collision handling. - Added `internal/sub/host_sub_test.go` asserting endpoint expansion across raw share links, Clash YAML proxies, and Xray JSON outbounds with remark suffixes (`Remark - 1`, `Remark - 2`, etc.). - Updated `internal/database/host_test.go` verifying GORM auto-migration for `random_count` across both SQLite and PostgreSQL. - All repository tests pass locally: `go test ./...`. - **Manual verification:** 1. Opened **Hosts** modal in the panel. 2. Input literal host address (`cdn.example.com`) — confirmed `randomCount` input remains hidden. 3. Input CIDR address (`2a01:4f8::/64`) — confirmed `randomCount` input dynamically appears below the Address field. 4. Configured `randomCount = 3` and fetched `/sub/...` raw, Clash, and JSON endpoints. Verified 3 unique random IPv6 addresses generated with sequential remark suffixes. 5. Verified mixed entries (standard domain + CIDR) correctly preserve the static address alongside generated CIDR IPs. 6. Verified OpenAPI spec synchronization (`tools/openapigen` and `docs/public/openapi.json`). ## Breaking changes None. The database field defaults to `1` via GORM column default and automatic migration. Hosts without CIDR notation retain identical behavior. ## 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:08:52 +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#6453
No description provided.