[PR #6571] [CLOSED] feat(links): make external links a panel-wide library with scopes #6358

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6571
Author: @BlindMaster24
Created: 9/16/2026
Status: ❌ Closed

Base: main ← Head: feat/external-links-library


📝 Commits (1)

  • 5afa867 feat(links): make external links a panel-wide library with scopes

📊 Changes

42 files changed (+6346 additions, -322 deletions)

View changed files

📝 docs/content/docs/en/reference/api/clients.mdx (+29 -21)
➕ docs/content/docs/en/reference/api/external-links.mdx (+122 -0)
📝 docs/public/openapi.json (+1069 -2)
📝 frontend/public/openapi.json (+1069 -2)
📝 frontend/src/generated/examples.ts (+69 -0)
📝 frontend/src/generated/schemas.ts (+338 -0)
📝 frontend/src/generated/types.ts (+74 -0)
📝 frontend/src/generated/zod.ts (+79 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+275 -3)
📝 internal/database/db.go (+88 -28)
➕ internal/database/external_link_library_migration_test.go (+159 -0)
📝 internal/database/migrate_data.go (+2 -1)
📝 internal/database/model/model.go (+82 -0)
📝 internal/sub/clash_external_quota_test.go (+2 -6)
📝 internal/sub/external_config.go (+52 -33)
➕ internal/sub/external_link_flow_test.go (+244 -0)
➕ internal/sub/external_link_testseed_test.go (+52 -0)
📝 internal/sub/external_only_sub_test.go (+1 -3)
📝 internal/sub/external_subscription.go (+326 -75)
📝 internal/sub/external_subscription_test.go (+64 -46)

...and 22 more files

📄 Description

What this is

Turns external links from a per-client list into a panel-wide library with scopes, and makes the panel a conforming client when it fetches someone else's subscription.

Today a link can only belong to one client (client_external_links.client_id), so "the same backup provider for 300 clients" is 300 copies that drift, and none of the requests behind it (#5533, #6022, #5293, #5694) can be built on top.

Model

  • external_links — the shared row: kind (link / subscription), value, remark, namePrefix, enable, expiryTime, sortIndex, its own fetch identity (userAgent, headers, cacheTtl) and the last good expansion (lastLinks). Unique on (kind, value).
  • external_link_assignments — binds a library row to a target with targetType = client / group / inbound / global / new_clients, plus per-binding overrides (enable, expiryTime, remark, namePrefix, sortIndex). Unique on (link_id, target_type, target_id). expiryTime there is -1 (never) / 0 (inherit) / a timestamp, because a client's own "never" has to stay distinguishable from inheriting a shared expiry.
  • Resolver: effective links for a client are the union over its own, its group, its inbounds and the panel, most specific scope winning for overrides, display order stable.
  • Migration migrateClientExternalLinksToLibrary is idempotent: one library row per distinct (kind, value), one assignment per legacy row keeping that client's own overrides, order and expiry, then the legacy table is retired as client_external_links_legacy. No client's expiry is copied onto the shared row.

Fetch layer (internal/sub)

  • Conditional requests — If-None-Match / If-Modified-Since; a 304 refreshes the entry nearly for free.
  • Stale-while-revalidate — a client is answered from cache while the refresh runs behind it, instead of waiting on a 6 s fetch.
  • Negative cache with backoff honouring Retry-After, plus jitter and a per-host bound that covers only background revalidations, so a wave after a restart does not serialise behind them.
  • Cache key = URL + request identity (UA / headers), so two rows pointing at one URL with different identity do not poison each other.
  • Stored expansion (lastLinks) is read back when a fetch yields nothing, so a restart with the provider down still serves the client.
  • X-HWID the panel sends is the mechanism merged in #6567 (serverHwid, persisted per installation), which is the fix for the 404 from an HWID-limited donor (#6559); this PR builds on it rather than duplicating it. Per-row headers are layered on top.
  • Manual refresh endpoint bypasses the cache on the operator's command.

Wiring

  • GET/POST /panel/api/links/{list,add,del/:id,enable/:id,reorder,targets/:id,assign/:id,unassign/:id,client/:clientId,refresh/:id}, registered in internal/web/controller/link.go, documented in endpoints.ts, openapi.json regenerated, docs/ reference page regenerated.
  • Cascades: deleting a client, a group or an inbound removes the assignments that pointed at it.
  • Client export/import carries the client's own assignments instead of dropping them. ExportAll/ImportClients are used only by the panel's export/import endpoints, so those links stay panel-local and are never pushed to a node.
  • New clients inherit the new_clients scope through a batched materialise on creation and on bulk creation.

Review round 1

Nine findings were posted and each has a disposition in this reply: both HIGHs (the usage aggregate had no GROUP BY; an assignment could not express "never expires") and the MEDIUMs (last_links was written but never read, stale generated output, a partial library edit resetting order and identity, a DB read on the fetch path without a nil guard, the host gate serialising the cold wave) are fixed, and the LOW documentation gap is closed. Each fix that can be tested carries a test that fails without it.

While re-reading the same code one more such hole was found and fixed before the review reached it: a body that omits enable used to re-enable a disabled row, so a "rename only" edit flipped its state.

Verification

CI on this branch: build (386, amd64, arm64, armv5/6/7, s390x) and Build for Windows, go-test, race, golangci, govulncheck, fuzz-smoke, codegen, frontend, postgres-durable-first, CodeQL and Docs CI — all green.

What the local machine could and could not do, plainly: this workstation has no C toolchain and its commit limit is exhausted, so go build / go test / golangci-lint die with out of memory allocating heap arena map — no local Go build or test run happens for this branch at all, which is why every test here is proven by CI rather than by me. What was run locally: gofmt -e -l on every touched file, the unused-import and comment-cap checks, go run ./tools/openapigen + node scripts/build-openapi.mjs + the docs generator (all of which produced the committed output), and the route ⇄ endpoints.ts ⇄ openapi.json comparison.

Still unverified by any job: the PostgreSQL dialect run beyond postgres-durable-first (which runs a single test), so the GROUP BY fix rests on PostgreSQL's documented rule plus a SQLite-executed test, not on an executed Postgres query. The Xray gRPC end-to-end tests need XRAY_E2E_BINARY, which CI does not set; this change does not touch that path.


🔄 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/6571 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/16/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/external-links-library` --- ### 📝 Commits (1) - [`5afa867`](https://github.com/MHSanaei/3x-ui/commit/5afa86772f75ea3591ff6b9f5d70a4395006dc9d) feat(links): make external links a panel-wide library with scopes ### 📊 Changes **42 files changed** (+6346 additions, -322 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/reference/api/clients.mdx` (+29 -21) ➕ `docs/content/docs/en/reference/api/external-links.mdx` (+122 -0) 📝 `docs/public/openapi.json` (+1069 -2) 📝 `frontend/public/openapi.json` (+1069 -2) 📝 `frontend/src/generated/examples.ts` (+69 -0) 📝 `frontend/src/generated/schemas.ts` (+338 -0) 📝 `frontend/src/generated/types.ts` (+74 -0) 📝 `frontend/src/generated/zod.ts` (+79 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+275 -3) 📝 `internal/database/db.go` (+88 -28) ➕ `internal/database/external_link_library_migration_test.go` (+159 -0) 📝 `internal/database/migrate_data.go` (+2 -1) 📝 `internal/database/model/model.go` (+82 -0) 📝 `internal/sub/clash_external_quota_test.go` (+2 -6) 📝 `internal/sub/external_config.go` (+52 -33) ➕ `internal/sub/external_link_flow_test.go` (+244 -0) ➕ `internal/sub/external_link_testseed_test.go` (+52 -0) 📝 `internal/sub/external_only_sub_test.go` (+1 -3) 📝 `internal/sub/external_subscription.go` (+326 -75) 📝 `internal/sub/external_subscription_test.go` (+64 -46) _...and 22 more files_ </details> ### 📄 Description ## What this is Turns external links from a per-client list into a **panel-wide library with scopes**, and makes the panel a conforming client when it fetches someone else's subscription. Today a link can only belong to one client (`client_external_links.client_id`), so "the same backup provider for 300 clients" is 300 copies that drift, and none of the requests behind it (#5533, #6022, #5293, #5694) can be built on top. ## Model - `external_links` — the shared row: `kind` (`link` / `subscription`), `value`, `remark`, `namePrefix`, `enable`, `expiryTime`, `sortIndex`, its own fetch identity (`userAgent`, `headers`, `cacheTtl`) and the last good expansion (`lastLinks`). Unique on `(kind, value)`. - `external_link_assignments` — binds a library row to a target with `targetType` = `client` / `group` / `inbound` / `global` / `new_clients`, plus per-binding overrides (`enable`, `expiryTime`, `remark`, `namePrefix`, `sortIndex`). Unique on `(link_id, target_type, target_id)`. `expiryTime` there is `-1` (never) / `0` (inherit) / a timestamp, because a client's own "never" has to stay distinguishable from inheriting a shared expiry. - Resolver: effective links for a client are the union over its own, its group, its inbounds and the panel, most specific scope winning for overrides, display order stable. - Migration `migrateClientExternalLinksToLibrary` is idempotent: one library row per distinct `(kind, value)`, one assignment per legacy row keeping that client's own overrides, order and expiry, then the legacy table is retired as `client_external_links_legacy`. No client's expiry is copied onto the shared row. ## Fetch layer (`internal/sub`) - **Conditional requests** — `If-None-Match` / `If-Modified-Since`; a `304` refreshes the entry nearly for free. - **Stale-while-revalidate** — a client is answered from cache while the refresh runs behind it, instead of waiting on a 6 s fetch. - **Negative cache with backoff** honouring `Retry-After`, plus jitter and a per-host bound that covers only background revalidations, so a wave after a restart does not serialise behind them. - **Cache key = URL + request identity** (UA / headers), so two rows pointing at one URL with different identity do not poison each other. - **Stored expansion** (`lastLinks`) is read back when a fetch yields nothing, so a restart with the provider down still serves the client. - **`X-HWID`** the panel sends is the mechanism merged in #6567 (`serverHwid`, persisted per installation), which is the fix for the `404` from an HWID-limited donor (#6559); this PR builds on it rather than duplicating it. Per-row `headers` are layered on top. - Manual **refresh** endpoint bypasses the cache on the operator's command. ## Wiring - `GET/POST /panel/api/links/{list,add,del/:id,enable/:id,reorder,targets/:id,assign/:id,unassign/:id,client/:clientId,refresh/:id}`, registered in `internal/web/controller/link.go`, documented in `endpoints.ts`, `openapi.json` regenerated, `docs/` reference page regenerated. - Cascades: deleting a client, a group or an inbound removes the assignments that pointed at it. - Client export/import carries the client's own assignments instead of dropping them. `ExportAll`/`ImportClients` are used only by the panel's export/import endpoints, so those links stay panel-local and are never pushed to a node. - New clients inherit the `new_clients` scope through a batched materialise on creation and on bulk creation. ## Review round 1 Nine findings were posted and each has a disposition in [this reply](https://github.com/MHSanaei/3x-ui/pull/6571#issuecomment-5696370990): both HIGHs (the usage aggregate had no `GROUP BY`; an assignment could not express "never expires") and the MEDIUMs (`last_links` was written but never read, stale generated output, a partial library edit resetting order and identity, a DB read on the fetch path without a nil guard, the host gate serialising the cold wave) are fixed, and the LOW documentation gap is closed. Each fix that can be tested carries a test that fails without it. While re-reading the same code one more such hole was found and fixed before the review reached it: a body that omits `enable` used to re-enable a disabled row, so a "rename only" edit flipped its state. ## Verification CI on this branch: `build` (386, amd64, arm64, armv5/6/7, s390x) and `Build for Windows`, `go-test`, `race`, `golangci`, `govulncheck`, `fuzz-smoke`, `codegen`, `frontend`, `postgres-durable-first`, `CodeQL` and `Docs CI` — all green. What the local machine could and could not do, plainly: this workstation has no C toolchain and its commit limit is exhausted, so `go build` / `go test` / `golangci-lint` die with `out of memory allocating heap arena map` — no local Go build or test run happens for this branch at all, which is why every test here is proven by CI rather than by me. What was run locally: `gofmt -e -l` on every touched file, the unused-import and comment-cap checks, `go run ./tools/openapigen` + `node scripts/build-openapi.mjs` + the docs generator (all of which produced the committed output), and the route ⇄ `endpoints.ts` ⇄ `openapi.json` comparison. Still unverified by any job: the PostgreSQL **dialect** run beyond `postgres-durable-first` (which runs a single test), so the `GROUP BY` fix rests on PostgreSQL's documented rule plus a SQLite-executed test, not on an executed Postgres query. The Xray gRPC end-to-end tests need `XRAY_E2E_BINARY`, which CI does not set; this change does not touch that path. --- <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:12 +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#6358
No description provided.