[PR #5802] [MERGED] feat(sub): add per-client subscription HWID limits #5991

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5802
Author: @rqzbeh
Created: 7/5/2026
Status: ✅ Merged
Merged: 8/15/2026
Merged by: @MHSanaei

Base: main ← Head: feat/hwid-limits


📝 Commits (5)

  • 7b1cb85 feat(sub): add per-client subscription HWID limits
  • f3b598d fix(sub): address HWID review on shared subId and bulk create
  • b80faca fix(sub): store HWID devices by sub_id and drop anchor client workaround
  • 6243f78 fix(sub): restore UA auto-detect and HTML page routing in subs()
  • 5cb99c5 fix(frontend): drop axios from HttpUtil.delete

📊 Changes

45 files changed (+1212 additions, -50 deletions)

View changed files

📝 frontend/public/openapi.json (+105 -1)
📝 frontend/src/generated/examples.ts (+1 -0)
📝 frontend/src/generated/schemas.ts (+4 -0)
📝 frontend/src/generated/types.ts (+1 -0)
📝 frontend/src/generated/zod.ts (+1 -0)
📝 frontend/src/hooks/useClients.ts (+1 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+24 -7)
📝 frontend/src/pages/clients/ClientBulkAddModal.tsx (+11 -0)
📝 frontend/src/pages/clients/ClientFormModal.tsx (+117 -3)
📝 frontend/src/schemas/client.ts (+3 -0)
📝 frontend/src/utils/index.ts (+17 -0)
➕ internal/database/client_hwid_schema_test.go (+62 -0)
📝 internal/database/db.go (+1 -0)
📝 internal/database/migrate_data.go (+1 -0)
📝 internal/database/model/model.go (+25 -0)
📝 internal/sub/controller.go (+45 -0)
➕ internal/sub/hwid_controller_test.go (+134 -0)
📝 internal/web/controller/client.go (+21 -3)
📝 internal/web/service/api_scale_postgres_test.go (+1 -1)
📝 internal/web/service/client.go (+30 -0)

...and 25 more files

📄 Description

Summary

This PR adds an optional per-client HWID limit for subscription downloads only. Admins set limitHwid on a client (same place as traffic and IP limits). When the limit is greater than zero, the subscription server only serves link/config bodies to clients that send a stable device id in X-HWID (and optionally device metadata headers). Registered devices are stored as SHA-256 hashes; raw HWIDs are never persisted.

This does not change Xray inbounds, proxy auth, or live connection handling. It is enforced on the panel’s subscription HTTP routes before config is returned.

Behavior before this PR

  • Any client that knew a valid subscription URL could fetch /sub, /json, and /clash config without proving which device was asking.
  • Subscription sharing could not be limited by device identity at fetch time; only separate mechanisms (e.g. IP limits on live connections via fail2ban) applied elsewhere.
  • There was no panel field to cap how many distinct devices may register for a subscription, and no way to list or reset registered devices.

Behavior after this PR

Admin / panel

  • New client field limitHwid (default 0 = unlimited, same semantics as “0 means off” for other limits).
  • Client add/edit and bulk add send limitHwid with the rest of the client record.
  • Next to the limit: view registered devices (metadata only: first/last seen, user agent, OS fields) and clear all registrations for that client (by email), similar to the existing IP log flow.
  • New API:
    • POST /panel/api/clients/hwids/:email — list registered device metadata (no raw HWID / hash exposed as an identifier).
    • DELETE /panel/api/clients/hwids/:email — delete all client_hwids rows for that client.

Database

  • New column clients.limit_hwid.
  • New table client_hwids keyed by client_id (not inbound), with unique (client_id, hwid_hash) and optional metadata columns.
  • Migrations run through existing GORM auto-migrate paths (SQLite and PostgreSQL). Orphan HWID rows are cleared when clients are deleted; lowering the limit trims excess registrations (keeps most recently seen).

Subscription HTTP (what end users see)

Enforcement applies to subscription bodies:

  • GET / HEAD on /sub/:subid, /json/:subid, /clash/:subid

Not gated (unchanged on purpose):

  • HTML subscription info page when the client requests Accept: text/html (or ?html=1 / ?view=html) — users can still open the summary page without X-HWID.

When limitHwid > 0 for the enabled client behind that subId:

Request Result
No X-HWID, or value shorter than 6 characters 404 empty body; headers include X-Hwid-Active: true, X-Hwid-Not-Supported: true
X-HWID matches an already registered device 200 — config served; last_seen and metadata updated
New X-HWID and registered count < limit 200 — device registered, then config served
New X-HWID and registered count ≥ limit 404; X-Hwid-Max-Devices-Reached: true, X-Hwid-Limit: true

Optional headers stored on registration / update (when sent by the app):

  • User-Agent
  • X-Device-OS, X-Ver-OS, X-Device-Model

Clients that support subscription HWID must send X-HWID on sub/json/clash fetches when the admin has set a non-zero limit.

Multi-node

Subscription is served from the panel that owns the client record and subId. HWID state lives in that panel’s database only. Remote nodes are not involved in subscription fetch or HWID registration, so this PR does not add cross-node sync for HWIDs.

Explicitly out of scope (v1)

  • Disconnecting or blocking active proxy sessions by HWID inside Xray (online stats do not expose HWID).
  • Per-device revoke UI (only clear all).
  • Storing or displaying raw HWID strings in the panel.

Why

Operators asked for a way to reduce subscription link sharing across many physical devices without modifying Xray core. Gating at the subscription endpoint matches how many mobile/desktop apps already identify themselves when pulling configs.

Type of change

  • New feature

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints)
  • Subscription (share links / Clash / JSON)
  • Database / migrations

How was this tested?

  • go test ./internal/sub -run Hwid
  • go test ./internal/web/service -run Hwid
  • go test ./internal/database -run Hwid (PostgreSQL schema test runs when XUI_TEST_PG_DSN is set; otherwise skipped)
  • Manual: set limitHwid on a client → fetch /sub, /json, /clash with and without X-HWID → confirm registration, block at limit, allow known device after limit → clear HWIDs from client form

Screenshots / recordings

N/A (UI follows existing client form / IP log patterns).

Breaking changes

None. limitHwid defaults to 0 (disabled). Existing subscriptions behave as before until an admin sets a non-zero limit.

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 targeted tests pass locally.
  • OpenAPI / generated frontend types updated for new fields and endpoints.
  • 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/5802 **Author:** [@rqzbeh](https://github.com/rqzbeh) **Created:** 7/5/2026 **Status:** ✅ Merged **Merged:** 8/15/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/hwid-limits` --- ### 📝 Commits (5) - [`7b1cb85`](https://github.com/MHSanaei/3x-ui/commit/7b1cb852987c058a1fcc094cbdc93c76e29014f2) feat(sub): add per-client subscription HWID limits - [`f3b598d`](https://github.com/MHSanaei/3x-ui/commit/f3b598ded810953321ebd093db8095ee30f86868) fix(sub): address HWID review on shared subId and bulk create - [`b80faca`](https://github.com/MHSanaei/3x-ui/commit/b80faca7cbbeb7348b7094c5971d4f6986abdf6e) fix(sub): store HWID devices by sub_id and drop anchor client workaround - [`6243f78`](https://github.com/MHSanaei/3x-ui/commit/6243f788d9d7a727495164d819f4fda79b2bff10) fix(sub): restore UA auto-detect and HTML page routing in subs() - [`5cb99c5`](https://github.com/MHSanaei/3x-ui/commit/5cb99c581ab6c6ace5bf21c85c8d06a1e64791c1) fix(frontend): drop axios from HttpUtil.delete ### 📊 Changes **45 files changed** (+1212 additions, -50 deletions) <details> <summary>View changed files</summary> 📝 `frontend/public/openapi.json` (+105 -1) 📝 `frontend/src/generated/examples.ts` (+1 -0) 📝 `frontend/src/generated/schemas.ts` (+4 -0) 📝 `frontend/src/generated/types.ts` (+1 -0) 📝 `frontend/src/generated/zod.ts` (+1 -0) 📝 `frontend/src/hooks/useClients.ts` (+1 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+24 -7) 📝 `frontend/src/pages/clients/ClientBulkAddModal.tsx` (+11 -0) 📝 `frontend/src/pages/clients/ClientFormModal.tsx` (+117 -3) 📝 `frontend/src/schemas/client.ts` (+3 -0) 📝 `frontend/src/utils/index.ts` (+17 -0) ➕ `internal/database/client_hwid_schema_test.go` (+62 -0) 📝 `internal/database/db.go` (+1 -0) 📝 `internal/database/migrate_data.go` (+1 -0) 📝 `internal/database/model/model.go` (+25 -0) 📝 `internal/sub/controller.go` (+45 -0) ➕ `internal/sub/hwid_controller_test.go` (+134 -0) 📝 `internal/web/controller/client.go` (+21 -3) 📝 `internal/web/service/api_scale_postgres_test.go` (+1 -1) 📝 `internal/web/service/client.go` (+30 -0) _...and 25 more files_ </details> ### 📄 Description ## Summary This PR adds an optional **per-client HWID limit** for **subscription downloads** only. Admins set `limitHwid` on a client (same place as traffic and IP limits). When the limit is greater than zero, the subscription server only serves **link/config bodies** to clients that send a stable device id in `X-HWID` (and optionally device metadata headers). Registered devices are stored as **SHA-256 hashes**; raw HWIDs are never persisted. This does **not** change Xray inbounds, proxy auth, or live connection handling. It is enforced on the panel’s subscription HTTP routes before config is returned. ## Behavior before this PR - Any client that knew a valid subscription URL could fetch `/sub`, `/json`, and `/clash` config without proving which device was asking. - Subscription sharing could not be limited by **device identity** at fetch time; only separate mechanisms (e.g. IP limits on **live** connections via fail2ban) applied elsewhere. - There was no panel field to cap how many distinct devices may register for a subscription, and no way to list or reset registered devices. ## Behavior after this PR ### Admin / panel - New client field **`limitHwid`** (default **0** = unlimited, same semantics as “0 means off” for other limits). - Client add/edit and bulk add send `limitHwid` with the rest of the client record. - Next to the limit: view registered devices (metadata only: first/last seen, user agent, OS fields) and **clear** all registrations for that client (by email), similar to the existing IP log flow. - New API: - `POST /panel/api/clients/hwids/:email` — list registered device metadata (no raw HWID / hash exposed as an identifier). - `DELETE /panel/api/clients/hwids/:email` — delete all `client_hwids` rows for that client. ### Database - New column `clients.limit_hwid`. - New table `client_hwids` keyed by **`client_id`** (not inbound), with unique `(client_id, hwid_hash)` and optional metadata columns. - Migrations run through existing GORM auto-migrate paths (SQLite and PostgreSQL). Orphan HWID rows are cleared when clients are deleted; lowering the limit trims excess registrations (keeps most recently seen). ### Subscription HTTP (what end users see) Enforcement applies to **subscription bodies**: - `GET` / `HEAD` on `/sub/:subid`, `/json/:subid`, `/clash/:subid` **Not** gated (unchanged on purpose): - HTML subscription **info** page when the client requests `Accept: text/html` (or `?html=1` / `?view=html`) — users can still open the summary page without `X-HWID`. When `limitHwid > 0` for the enabled client behind that `subId`: | Request | Result | |--------|--------| | No `X-HWID`, or value shorter than 6 characters | **404** empty body; headers include `X-Hwid-Active: true`, `X-Hwid-Not-Supported: true` | | `X-HWID` matches an already registered device | **200** — config served; `last_seen` and metadata updated | | New `X-HWID` and registered count **&lt;** limit | **200** — device registered, then config served | | New `X-HWID` and registered count **≥** limit | **404**; `X-Hwid-Max-Devices-Reached: true`, `X-Hwid-Limit: true` | Optional headers stored on registration / update (when sent by the app): - `User-Agent` - `X-Device-OS`, `X-Ver-OS`, `X-Device-Model` Clients that support subscription HWID must send `X-HWID` on sub/json/clash fetches when the admin has set a non-zero limit. ### Multi-node Subscription is served from the panel that owns the client record and `subId`. HWID state lives in that panel’s database only. Remote nodes are not involved in subscription fetch or HWID registration, so this PR does not add cross-node sync for HWIDs. ### Explicitly out of scope (v1) - Disconnecting or blocking **active** proxy sessions by HWID inside Xray (online stats do not expose HWID). - Per-device revoke UI (only **clear all**). - Storing or displaying raw HWID strings in the panel. ## Why Operators asked for a way to reduce **subscription link sharing** across many physical devices without modifying Xray core. Gating at the subscription endpoint matches how many mobile/desktop apps already identify themselves when pulling configs. ## Type of change - [x] New feature ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints) - [x] Subscription (share links / Clash / JSON) - [x] Database / migrations ## How was this tested? - `go test ./internal/sub -run Hwid` - `go test ./internal/web/service -run Hwid` - `go test ./internal/database -run Hwid` (PostgreSQL schema test runs when `XUI_TEST_PG_DSN` is set; otherwise skipped) - Manual: set `limitHwid` on a client → fetch `/sub`, `/json`, `/clash` with and without `X-HWID` → confirm registration, block at limit, allow known device after limit → clear HWIDs from client form ## Screenshots / recordings N/A (UI follows existing client form / IP log patterns). ## Breaking changes None. `limitHwid` defaults to `0` (disabled). Existing subscriptions behave as before until an admin sets a non-zero limit. ## 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 targeted tests pass locally. - [x] OpenAPI / generated frontend types updated for new fields and endpoints. - [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:05:35 +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#5991
No description provided.