[PR #6599] [CLOSED] feat(links): replicate the master's library onto its nodes #6370

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

📋 Pull Request Information

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

Base: main ← Head: feat/external-links-node-sync


📝 Commits (10+)

  • 5afa867 feat(links): make external links a panel-wide library with scopes
  • eac7ef0 feat(links): add the panel-side UI for the external links library
  • a82ce6d feat(links): replicate the master's library onto its nodes
  • c2547e1 fix(nodes): let a node-scoped token reach the link sync endpoint
  • fad5981 fix(nodes): bound the link push by the reconcile's own deadline
  • 2e094b7 perf(links): mark the nodes dirty instead of pushing on a library edit
  • e17a383 chore(links): drop the unused non-transactional MarkAllNodesDirty
  • ad51f67 test(links): cover the assign surface and the Clash link rendering
  • 51faacd test(links): cover the node-sync endpoints, the push wire and the Clash kinds
  • 7e64616 fix(links): store an edited row's headers as the JSON the column holds

📊 Changes

82 files changed (+13944 additions, -2866 deletions)

View changed files

📝 docs/content/docs/en/reference/api/clients.mdx (+29 -21)
➕ docs/content/docs/en/reference/api/external-links.mdx (+133 -0)
📝 docs/public/openapi.json (+2543 -1273)
📝 frontend/public/openapi.json (+2543 -1273)
➕ frontend/src/api/queries/useGroupOptions.ts (+22 -0)
➕ frontend/src/api/queries/useLinkMutations.ts (+85 -0)
➕ frontend/src/api/queries/useLinksQuery.ts (+80 -0)
📝 frontend/src/api/queryKeys.ts (+6 -0)
📝 frontend/src/generated/examples.ts (+137 -0)
📝 frontend/src/generated/schemas.ts (+466 -0)
📝 frontend/src/generated/types.ts (+109 -0)
📝 frontend/src/generated/zod.ts (+120 -0)
📝 frontend/src/layouts/AppSidebar.tsx (+5 -1)
📝 frontend/src/lib/clients/external-link.ts (+13 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+283 -3)
📝 frontend/src/pages/clients/ClientFormModal.tsx (+42 -1)
➕ frontend/src/pages/links/LinkFormModal.tsx (+148 -0)
➕ frontend/src/pages/links/LinkTargetsModal.tsx (+194 -0)
➕ frontend/src/pages/links/LinksPage.tsx (+362 -0)
📝 frontend/src/routes.tsx (+2 -0)

...and 62 more files

📄 Description

What this is

Delivery C of the external-links plan: the master's library now reaches its nodes.

  • A — API: #6571 · B — UI: #6598 · C — node replication: this PR.

The diff therefore contains A and B as well while they are open; once they land, this
PR collapses to the C commit. Nothing here is merged.

Why it is needed

internal/sub runs on every panel, node included, and resolves external links from the
local store (external_config.go:68). A node has no library, no scopes and no groups —
groups are not replicated to nodes at all — so a client whose links were granted by a group
or by the global scope received none of them when its subscription was fetched from a node.

What it does

  • master: ExternalLinkSyncForNode resolves the effective set for one node and flattens
    group/inbound scopes into per-client bindings, because the node cannot evaluate a scope it
    does not hold. PushExternalLinksToNode(s) sends it; a per-client add or edit sends only
    the clients it touched.
  • node: a new POST /panel/api/links/sync materialises the snapshot — upsert by
    kind+value, replace only the bindings carrying origin=node, dropUnusedPushedExternalLinks
    retires rows nobody inherits.
  • read-only on the node: pushed rows and bindings are left alone by a local save
    (SetExternalLinksForRecord), so an operator editing on a node cannot contradict the master.
  • convergence: ReconcileNode re-pushes on the same sweep that already converges the
    node's inbounds, so an offline or hung node catches up by itself.

The defects this change found and fixed, and fixed here

  1. TestNodePushGivesUpBeforeTheRemoteTimeout (pre-existing, a5e68f41) went red with
    the first version: the per-client edit path spent the pushed-link deadline on a node that
    had just refused the client push — 8.01s against a hung node instead of the 4s bound.
    The edit path now clears push on a failed push, exactly as the add path already did, and
    the node's already-set dirty flag converges it. Red 8.23s → green 4.28s, same command.
  2. The delivery did nothing in production. Every master-to-node push authenticates with
    the node's own bearer token, whose scope is node-sync
    (internal/web/runtime/remote.go:233-237), and enforceTokenScope denies a node-sync
    caller whose route is not in nodeSyncScopeAllow (internal/web/controller/api.go:146).
    /panel/api/links/sync was missing from that allowlist, so every push was answered
    403 "this API token is not permitted to access this endpoint" and the links never
    landed — while all of the delivery's own tests passed, because they call the transport
    directly and never meet the middleware. TestNodeSyncScopeUsesFullPathPatterns now drives
    the route through the real auth chain: 403 before the entry, 200 after.
  3. /panel/api/links/sync accepted any kind/value, while /links/add validates both;
    a push could store a link this panel would have refused. The same validation now runs on
    the receiving side.
  4. ReconcileNode hands over a context carrying its timeout and every call it makes honours
    it; the link push built a fresh four-second deadline of its own instead, so a reconcile that
    had already given up kept spending its budget on that node. PushExternalLinksToNode now
    takes the caller's context (TestPushExternalLinksHonoursTheCallersDeadline: with the
    caller's context cancelled the fake node received the request, and now receives none).

Verification

  • gofmt clean; golangci-lint is not installed on this machine, so Go lint is left to CI.
  • Go tests, CGO_ENABLED=0 with the pure-Go SQLite overlay (this box has no C compiler):
    internal/web/service ok 139s, internal/web/runtime / controller / web (route
    contract) / job / internal/sub ok, internal/database migration tests ok.
    Re-run after the 403 fix: internal/web/controller ok 9.8s, internal/web ok 0.6s.
    The CGo-only database tests (backup_test.go, dump_sqlite_test.go, *_index_test.go,
    inbound_port_unique_migration_test.go) cannot run here at all — the overlay replaces the
    驱动 they open directly — so they are unverified locally and left to CI.
  • Frontend: tsc --noEmit, oxlint, oxfmt --check clean, vite build ok,
    vitest --project unit 78/80 files, --project storybook 26/27.
    The three failures are this machine, not the change, each with evidence:
    input-number-guard fails because node_modules holds oxlint 1.82.0 while
    package.json pins 1.83.0 (the rule landed in 1.83); language-scope passes when run
    alone (3.4s) and only exceeds its 5s limit under the full parallel run;
    FormField.stories > Traffic Transform expects en-US grouping and the host browser is
    ru-RU. None of the three files is in this diff, and CI's frontend job is green.
  • The endpoint chain is complete including its fourth step: endpoints.ts, npm run gen,
    docs/public/openapi.json copied over and pnpm gen:api re-run.

Propagation is level-triggered

A library edit marks every enabled node dirty inside the write's own transaction and lets the
5s reconcile sweep push the set; it never waits on a node. That is the design this panel
already documents for every other node-backed edit (node.go's IsNodePending comment: the
flag "is set on EVERY node-backed edit as the reconcile self-heal marker"), and it is the only
thing that also converges a node that happens to be down during the edit. The push itself was
already wired into ReconcileNode, so no second propagation path exists.

TestLibraryEditsDeferToTheReconcileSweep: on a hanging node the edit took 4.006s before this
change and returns in 0.24s now, leaves the node dirty, and the reconcile delivers exactly one
push.

Tests for the write path

The library's own write path had no service-level test, so it was covered next:

  • assign is asserted to bind five targets from one request and four clients are
    asserted to resolve through four different scopes (client, inbound, group, global) - the
    precedence rule driven through the real resolver.
  • the selector table pins every rejection, that unassign refuses an unknown group while
    assign creates it, that re-assigning refreshes overrides in place, and that unassigning a
    client leaves the group's binding alone.
  • AppliedPushedRowsAreReadOnly gained reorder; apply is asserted all-or-nothing; a
    positive deadline survives a push while 0 stays "never"; a save during boot (no runtime
    manager) does not panic.
  • internal/sub: Clash rendering for hysteria2 and wireguard library rows, the host/port
    split behind it, and Retry-After parsing.

Coverage moved from 64.9% to 65.9% in internal/web/service (ExternalLinkAssign 0 -> 94.7,
Unassign 0 -> 92.3, Targets 0 -> 86.2, ClientExternalLinkViews 0 -> 85.0) and from 85.2%
to 86.2% in internal/sub (clashHysteriaFromExternal 0 -> 100,
clashWireguardFromExternal 0 -> 95.8, splitClashHostPort 0 -> 100, parseRetryAfter
27.3 -> 100). Three mutation checks were run and reverted to prove the tests bite.


🔄 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/6599 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/18/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/external-links-node-sync` --- ### 📝 Commits (10+) - [`5afa867`](https://github.com/MHSanaei/3x-ui/commit/5afa86772f75ea3591ff6b9f5d70a4395006dc9d) feat(links): make external links a panel-wide library with scopes - [`eac7ef0`](https://github.com/MHSanaei/3x-ui/commit/eac7ef04d33605c3cb3521d7cfbb81ad549f0de9) feat(links): add the panel-side UI for the external links library - [`a82ce6d`](https://github.com/MHSanaei/3x-ui/commit/a82ce6dde38211486a29572dee54c380152e971a) feat(links): replicate the master's library onto its nodes - [`c2547e1`](https://github.com/MHSanaei/3x-ui/commit/c2547e14816510cddd22fe7f29106fc45f914775) fix(nodes): let a node-scoped token reach the link sync endpoint - [`fad5981`](https://github.com/MHSanaei/3x-ui/commit/fad598150558314b44fabafd2b6290b6db067647) fix(nodes): bound the link push by the reconcile's own deadline - [`2e094b7`](https://github.com/MHSanaei/3x-ui/commit/2e094b742625ef7e94a868beb5c9ddc799a5da6d) perf(links): mark the nodes dirty instead of pushing on a library edit - [`e17a383`](https://github.com/MHSanaei/3x-ui/commit/e17a383f3e0bc07b21193353c118f2941711a242) chore(links): drop the unused non-transactional MarkAllNodesDirty - [`ad51f67`](https://github.com/MHSanaei/3x-ui/commit/ad51f670858c8a4a6746a88e97df654ffb7bdf53) test(links): cover the assign surface and the Clash link rendering - [`51faacd`](https://github.com/MHSanaei/3x-ui/commit/51faacdc08f9ffe89ca5befa1e99a68b5cb4c9c4) test(links): cover the node-sync endpoints, the push wire and the Clash kinds - [`7e64616`](https://github.com/MHSanaei/3x-ui/commit/7e64616756a3b357bbc98deb77a2a4a9e2b6842a) fix(links): store an edited row's headers as the JSON the column holds ### 📊 Changes **82 files changed** (+13944 additions, -2866 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` (+133 -0) 📝 `docs/public/openapi.json` (+2543 -1273) 📝 `frontend/public/openapi.json` (+2543 -1273) ➕ `frontend/src/api/queries/useGroupOptions.ts` (+22 -0) ➕ `frontend/src/api/queries/useLinkMutations.ts` (+85 -0) ➕ `frontend/src/api/queries/useLinksQuery.ts` (+80 -0) 📝 `frontend/src/api/queryKeys.ts` (+6 -0) 📝 `frontend/src/generated/examples.ts` (+137 -0) 📝 `frontend/src/generated/schemas.ts` (+466 -0) 📝 `frontend/src/generated/types.ts` (+109 -0) 📝 `frontend/src/generated/zod.ts` (+120 -0) 📝 `frontend/src/layouts/AppSidebar.tsx` (+5 -1) 📝 `frontend/src/lib/clients/external-link.ts` (+13 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+283 -3) 📝 `frontend/src/pages/clients/ClientFormModal.tsx` (+42 -1) ➕ `frontend/src/pages/links/LinkFormModal.tsx` (+148 -0) ➕ `frontend/src/pages/links/LinkTargetsModal.tsx` (+194 -0) ➕ `frontend/src/pages/links/LinksPage.tsx` (+362 -0) 📝 `frontend/src/routes.tsx` (+2 -0) _...and 62 more files_ </details> ### 📄 Description ## What this is Delivery **C** of the external-links plan: the master's library now reaches its nodes. - **A — API:** #6571 · **B — UI:** #6598 · **C — node replication:** this PR. The diff therefore contains A and B as well while they are open; once they land, this PR collapses to the C commit. Nothing here is merged. ## Why it is needed `internal/sub` runs on every panel, node included, and resolves external links from the **local** store (`external_config.go:68`). A node has no library, no scopes and no groups — groups are not replicated to nodes at all — so a client whose links were granted by a group or by the global scope received none of them when its subscription was fetched from a node. ## What it does - **master:** `ExternalLinkSyncForNode` resolves the effective set for one node and flattens group/inbound scopes into per-client bindings, because the node cannot evaluate a scope it does not hold. `PushExternalLinksToNode(s)` sends it; a per-client add or edit sends only the clients it touched. - **node:** a new `POST /panel/api/links/sync` materialises the snapshot — upsert by `kind+value`, replace only the bindings carrying `origin=node`, `dropUnusedPushedExternalLinks` retires rows nobody inherits. - **read-only on the node:** pushed rows and bindings are left alone by a local save (`SetExternalLinksForRecord`), so an operator editing on a node cannot contradict the master. - **convergence:** `ReconcileNode` re-pushes on the same sweep that already converges the node's inbounds, so an offline or hung node catches up by itself. ## The defects this change found and fixed, and fixed here 1. `TestNodePushGivesUpBeforeTheRemoteTimeout` (pre-existing, `a5e68f41`) went **red** with the first version: the per-client edit path spent the pushed-link deadline on a node that had just refused the client push — 8.01s against a hung node instead of the 4s bound. The edit path now clears `push` on a failed push, exactly as the add path already did, and the node's already-set dirty flag converges it. Red 8.23s → green 4.28s, same command. 2. **The delivery did nothing in production.** Every master-to-node push authenticates with the node's own bearer token, whose scope is `node-sync` (`internal/web/runtime/remote.go:233-237`), and `enforceTokenScope` denies a `node-sync` caller whose route is not in `nodeSyncScopeAllow` (`internal/web/controller/api.go:146`). `/panel/api/links/sync` was missing from that allowlist, so every push was answered **403 "this API token is not permitted to access this endpoint"** and the links never landed — while all of the delivery's own tests passed, because they call the transport directly and never meet the middleware. `TestNodeSyncScopeUsesFullPathPatterns` now drives the route through the real auth chain: 403 before the entry, 200 after. 3. `/panel/api/links/sync` accepted any `kind`/`value`, while `/links/add` validates both; a push could store a link this panel would have refused. The same validation now runs on the receiving side. 4. `ReconcileNode` hands over a context carrying its timeout and every call it makes honours it; the link push built a fresh four-second deadline of its own instead, so a reconcile that had already given up kept spending its budget on that node. `PushExternalLinksToNode` now takes the caller's context (`TestPushExternalLinksHonoursTheCallersDeadline`: with the caller's context cancelled the fake node received the request, and now receives none). ## Verification - `gofmt` clean; `golangci-lint` is not installed on this machine, so Go lint is left to CI. - Go tests, `CGO_ENABLED=0` with the pure-Go SQLite overlay (this box has no C compiler): `internal/web/service` ok 139s, `internal/web/runtime` / `controller` / `web` (route contract) / `job` / `internal/sub` ok, `internal/database` migration tests ok. Re-run after the 403 fix: `internal/web/controller` ok 9.8s, `internal/web` ok 0.6s. The CGo-only database tests (`backup_test.go`, `dump_sqlite_test.go`, `*_index_test.go`, `inbound_port_unique_migration_test.go`) cannot run here at all — the overlay replaces the 驱动 they open directly — so they are unverified locally and left to CI. - Frontend: `tsc --noEmit`, `oxlint`, `oxfmt --check` clean, `vite build` ok, `vitest --project unit` 78/80 files, `--project storybook` 26/27. The three failures are this machine, not the change, each with evidence: `input-number-guard` fails because `node_modules` holds **oxlint 1.82.0** while `package.json` pins **1.83.0** (the rule landed in 1.83); `language-scope` passes when run alone (3.4s) and only exceeds its 5s limit under the full parallel run; `FormField.stories > Traffic Transform` expects en-US grouping and the host browser is `ru-RU`. None of the three files is in this diff, and CI's `frontend` job is green. - The endpoint chain is complete including its fourth step: `endpoints.ts`, `npm run gen`, `docs/public/openapi.json` copied over and `pnpm gen:api` re-run. ## Propagation is level-triggered A library edit marks every enabled node dirty inside the write's own transaction and lets the 5s reconcile sweep push the set; it never waits on a node. That is the design this panel already documents for every other node-backed edit (`node.go`'s `IsNodePending` comment: the flag "is set on EVERY node-backed edit as the reconcile self-heal marker"), and it is the only thing that also converges a node that happens to be down during the edit. The push itself was already wired into `ReconcileNode`, so no second propagation path exists. `TestLibraryEditsDeferToTheReconcileSweep`: on a hanging node the edit took 4.006s before this change and returns in 0.24s now, leaves the node dirty, and the reconcile delivers exactly one push. ## Tests for the write path The library's own write path had no service-level test, so it was covered next: - `assign` is asserted to bind five targets from one request and four clients are asserted to resolve through four different scopes (client, inbound, group, global) - the precedence rule driven through the real resolver. - the selector table pins every rejection, that unassign refuses an unknown group while assign creates it, that re-assigning refreshes overrides in place, and that unassigning a client leaves the group's binding alone. - `AppliedPushedRowsAreReadOnly` gained reorder; apply is asserted all-or-nothing; a positive deadline survives a push while 0 stays "never"; a save during boot (no runtime manager) does not panic. - `internal/sub`: Clash rendering for hysteria2 and wireguard library rows, the host/port split behind it, and `Retry-After` parsing. Coverage moved from 64.9% to 65.9% in `internal/web/service` (`ExternalLinkAssign` 0 -> 94.7, `Unassign` 0 -> 92.3, `Targets` 0 -> 86.2, `ClientExternalLinkViews` 0 -> 85.0) and from 85.2% to 86.2% in `internal/sub` (`clashHysteriaFromExternal` 0 -> 100, `clashWireguardFromExternal` 0 -> 95.8, `splitClashHostPort` 0 -> 100, `parseRetryAfter` 27.3 -> 100). Three mutation checks were run and reverted to prove the tests bite. --- <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:17 +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#6370
No description provided.