[PR #6380] [MERGED] feat(sub): add read-only HWID device-slot status endpoint #6242

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6380
Author: @Namso9
Created: 8/31/2026
Status: ✅ Merged
Merged: 9/11/2026
Merged by: @MHSanaei

Base: main ← Head: feat/sub-hwid-status


📝 Commits (3)

  • 01cf1f3 feat(sub): add read-only HWID device-slot status endpoint
  • 8280ef4 Merge branch 'main' into pr/6380
  • 5aff5a6 fix(sub): document hwid-status as the bare object it returns

📊 Changes

15 files changed (+637 additions, -3 deletions)

View changed files

📝 docs/content/docs/en/reference/api/subscription-server.mdx (+27 -2)
📝 docs/public/openapi.json (+126 -0)
📝 frontend/public/openapi.json (+126 -0)
📝 frontend/scripts/build-openapi.mjs (+18 -1)
📝 frontend/src/generated/examples.ts (+7 -0)
📝 frontend/src/generated/schemas.ts (+33 -0)
📝 frontend/src/generated/types.ts (+8 -0)
📝 frontend/src/generated/zod.ts (+9 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+34 -0)
📝 frontend/src/test/openapi-runtime-contracts.test.ts (+15 -0)
📝 internal/sub/controller.go (+14 -0)
📝 internal/sub/hwid_controller_test.go (+112 -0)
📝 internal/web/service/client_hwid.go (+49 -0)
📝 internal/web/service/client_hwid_test.go (+58 -0)
📝 tools/openapigen/main.go (+1 -0)

📄 Description

Closes #6357

What

Adds one additive, public, read-only endpoint on the subscription router, as requested in #6357:

GET /{subPath}/{subId}/hwid-status
→ {"active":true,"limit":2,"registered":1,"remaining":1,"full":false}

A subscriber (or a customer-facing panel / reseller bot built on the subscription API) can now show "Devices 1/2 — 1 slot remaining" instead of a silently failing connection.

Behaviour

  • SELECT-only. It never registers an HWID, never touches last_seen, and never calls EnforceHwidForSubID, so asking about a slot cannot spend one. A test asserts client_hwids stays empty after a probe that carries X-HWID, and that the next real fetch with that same HWID still succeeds.
  • Aggregate numbers only — no HWID value or hash, no email, no device model, no IP, no User-Agent, and none of the X-Hwid-* gate headers. The test decodes into a map and asserts the exact field set, so an added field fails.
  • No new auth. The subscription id is already the bearer secret for /{subPath}/{subId}.
  • Unknown and disabled subscriptions are indistinguishable: both take the same single-query path to a bare 404, and a test asserts identical status, headers and body.
  • No HWID limit → {"active":false,"limit":0,"registered":0,"remaining":0,"full":false}.
  • No schema change, no migration.
  • Existing contracts are untouched: /{subPath}/{subId}, ?format=info, and the JSON/Clash routes are not modified at all.

Notes for reviewers

  • HwidSlotStatusForSubID scopes the limit through the existing effectiveHwidLimitForSubID helper (enabled clients, MAX(limit_hwid)), so the reported limit is always the enforced limit on a shared sub_id. That is also why existence is "≥1 enabled client with this sub_id" — the same notion the gate uses — rather than the body route's "enabled inbound joined to the sub_id".
  • remaining clamps at zero: deleting or disabling the highest-limit client of a shared sub_id lowers the effective MAX without trimming rows, so registered > limit is reachable. Covered by a test.
  • The three counters come from separate SELECTs, so a limit edit landing mid-request can return a slightly stale tuple. Deliberate: this is an advisory read that self-corrects on the next poll, and a transaction would add write-lock contention on the subscription hot path.
  • HEAD is registered alongside GET, matching the sibling subscription routes.
  • Docs: the endpoint is registered in frontend/src/pages/api-docs/endpoints.ts, make gen was run, and frontend/public/openapi.json → docs/public/openapi.json + cd docs && pnpm gen:api were regenerated. The response is documented in prose because build-openapi.mjs wraps every operation in the {success,msg,obj} panel envelope, which does not apply to subscription-server routes.

How to test

go test ./internal/sub/ -run TestSubscriptionHwid
go test ./internal/web/service/ -run TestClientHwid

Manually, with a client that has limitHwid = 1:

curl -fsS 'http://127.0.0.1:10882/sub/SUB_ID/hwid-status'
# {"active":true,"limit":1,"registered":0,"remaining":1,"full":false}
curl -fsS -H 'X-HWID: device-one' 'http://127.0.0.1:10882/sub/SUB_ID' >/dev/null
curl -fsS 'http://127.0.0.1:10882/sub/SUB_ID/hwid-status'
# {"active":true,"limit":1,"registered":1,"remaining":0,"full":true}

Checks run locally

go build ./..., go vet, go test -shuffle=on -count=1 ./..., go test -race -shuffle=on ./internal/sub/ ./internal/web/service/, frontend typecheck / lint / format:check, and make gen reproducing the committed generated files. (internal/mtproto's two process-spawn timing tests flaked once on a loaded laptop and pass on a re-run; they are untouched by this change.)


🔄 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/6380 **Author:** [@Namso9](https://github.com/Namso9) **Created:** 8/31/2026 **Status:** ✅ Merged **Merged:** 9/11/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/sub-hwid-status` --- ### 📝 Commits (3) - [`01cf1f3`](https://github.com/MHSanaei/3x-ui/commit/01cf1f3f6c0c1fa019fa0f4bb6a34089452cba4d) feat(sub): add read-only HWID device-slot status endpoint - [`8280ef4`](https://github.com/MHSanaei/3x-ui/commit/8280ef47973e5c23e78256569e6c201d24c7a1ab) Merge branch 'main' into pr/6380 - [`5aff5a6`](https://github.com/MHSanaei/3x-ui/commit/5aff5a6a2ea2b4c5c07eb0962f1c4dae6fdfaf44) fix(sub): document hwid-status as the bare object it returns ### 📊 Changes **15 files changed** (+637 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/reference/api/subscription-server.mdx` (+27 -2) 📝 `docs/public/openapi.json` (+126 -0) 📝 `frontend/public/openapi.json` (+126 -0) 📝 `frontend/scripts/build-openapi.mjs` (+18 -1) 📝 `frontend/src/generated/examples.ts` (+7 -0) 📝 `frontend/src/generated/schemas.ts` (+33 -0) 📝 `frontend/src/generated/types.ts` (+8 -0) 📝 `frontend/src/generated/zod.ts` (+9 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+34 -0) 📝 `frontend/src/test/openapi-runtime-contracts.test.ts` (+15 -0) 📝 `internal/sub/controller.go` (+14 -0) 📝 `internal/sub/hwid_controller_test.go` (+112 -0) 📝 `internal/web/service/client_hwid.go` (+49 -0) 📝 `internal/web/service/client_hwid_test.go` (+58 -0) 📝 `tools/openapigen/main.go` (+1 -0) </details> ### 📄 Description Closes #6357 ### What Adds one additive, public, read-only endpoint on the subscription router, as requested in #6357: ``` GET /{subPath}/{subId}/hwid-status → {"active":true,"limit":2,"registered":1,"remaining":1,"full":false} ``` A subscriber (or a customer-facing panel / reseller bot built on the subscription API) can now show "Devices 1/2 — 1 slot remaining" instead of a silently failing connection. ### Behaviour - **SELECT-only.** It never registers an HWID, never touches `last_seen`, and never calls `EnforceHwidForSubID`, so asking about a slot cannot spend one. A test asserts `client_hwids` stays empty after a probe that carries `X-HWID`, and that the next real fetch with that same HWID still succeeds. - **Aggregate numbers only** — no HWID value or hash, no email, no device model, no IP, no User-Agent, and none of the `X-Hwid-*` gate headers. The test decodes into a map and asserts the exact field set, so an added field fails. - **No new auth.** The subscription id is already the bearer secret for `/{subPath}/{subId}`. - **Unknown and disabled subscriptions are indistinguishable**: both take the same single-query path to a bare `404`, and a test asserts identical status, headers and body. - **No HWID limit** → `{"active":false,"limit":0,"registered":0,"remaining":0,"full":false}`. - **No schema change, no migration.** - Existing contracts are untouched: `/{subPath}/{subId}`, `?format=info`, and the JSON/Clash routes are not modified at all. ### Notes for reviewers - `HwidSlotStatusForSubID` scopes the limit through the existing `effectiveHwidLimitForSubID` helper (enabled clients, `MAX(limit_hwid)`), so the *reported* limit is always the *enforced* limit on a shared `sub_id`. That is also why existence is "≥1 enabled client with this sub_id" — the same notion the gate uses — rather than the body route's "enabled inbound joined to the sub_id". - `remaining` clamps at zero: deleting or disabling the highest-limit client of a shared `sub_id` lowers the effective `MAX` without trimming rows, so `registered > limit` is reachable. Covered by a test. - The three counters come from separate SELECTs, so a limit edit landing mid-request can return a slightly stale tuple. Deliberate: this is an advisory read that self-corrects on the next poll, and a transaction would add write-lock contention on the subscription hot path. - `HEAD` is registered alongside `GET`, matching the sibling subscription routes. - Docs: the endpoint is registered in `frontend/src/pages/api-docs/endpoints.ts`, `make gen` was run, and `frontend/public/openapi.json` → `docs/public/openapi.json` + `cd docs && pnpm gen:api` were regenerated. The response is documented in prose because `build-openapi.mjs` wraps every operation in the `{success,msg,obj}` panel envelope, which does not apply to subscription-server routes. ### How to test ```bash go test ./internal/sub/ -run TestSubscriptionHwid go test ./internal/web/service/ -run TestClientHwid ``` Manually, with a client that has `limitHwid = 1`: ```bash curl -fsS 'http://127.0.0.1:10882/sub/SUB_ID/hwid-status' # {"active":true,"limit":1,"registered":0,"remaining":1,"full":false} curl -fsS -H 'X-HWID: device-one' 'http://127.0.0.1:10882/sub/SUB_ID' >/dev/null curl -fsS 'http://127.0.0.1:10882/sub/SUB_ID/hwid-status' # {"active":true,"limit":1,"registered":1,"remaining":0,"full":true} ``` ### Checks run locally `go build ./...`, `go vet`, `go test -shuffle=on -count=1 ./...`, `go test -race -shuffle=on ./internal/sub/ ./internal/web/service/`, frontend `typecheck` / `lint` / `format:check`, and `make gen` reproducing the committed generated files. (`internal/mtproto`'s two process-spawn timing tests flaked once on a loaded laptop and pass on a re-run; they are untouched by this change.) --- <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:21 +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#6242
No description provided.