mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #7069] Add a read-only "metrics" API-token scope backed by a dedicated aggregate endpoint #6608
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6608
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @OlegEnot on GitHub (Nov 4, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/7069
Motivation
A metrics collector cannot authenticate with this panel today unless it is given a full-access
admintoken:
panel.UserService.CheckUserverifiestwoFactorEnable/twoFactorTokenwithtotp.VerifyWithSkew(...)on every login, so a non-interactive client has noway through. Tokens bypass
CheckUserentirely (checkAPIAuth→c.Set("api_authed", true)),which is exactly the right mechanism — but the only usable scope is
admin.counters and a 59-minute re-auth cycle in the client.
So the ask is narrow: a token that can read numbers, not configuration.
Current state (v3.11.0)
Scopes:
admin,monitor,node-sync,node-admin(
internal/database/model/model.go:167,NormalizeScopeininternal/web/service/panel/api_token.go).monitorScopeAllow(internal/web/controller/api.go:82) coversGET /server/status,GET /server/history/*,GET /server/xrayMetrics*,GET /nodes/history/*, andenforceTokenScope(api.go:142) only accepts it forGET/HEAD:What a metrics collector actually reads
/panel/api/server/statusid/remark/up/down, per-clientid/email/up/down/panel/api/clients/onlines[]email— count of online usersmonitorcovers the first, misses the other two.admincovers all three and also lets the holderrewrite inbounds, read the DB, and mint more tokens.
Why the existing endpoints cannot simply be reused
GET /panel/api/inbounds/listreturns themodel.Inboundrow as-is, including:StreamSettings— for VLESS Reality this containsrealitySettings.privateKey;Settings— top-level keys such as WireGuardsecretKeyare not removed by the slimminghelper (
slimSettingsClients,internal/web/service/inbound.go:257); it only rewritessettings.clients[];GET /panel/api/inbounds/list/slim(controller/inbound.go:113) strips onlysettings.clients[],but still ships
streamSettingsand all othersettingskeys. Handing that to a container in amonitoring stack is a credential leak, not a read-only metrics grant.
Allowing
POST /clients/onlinesin a "read-only" scope is also a smell: the scope's safety propertytoday is "GET/HEAD only", and it is worth keeping.
Proposal: one aggregate GET endpoint + a GET-only scope
Endpoint
GET /panel/api/server/metricssettings, nostreamSettings, no UUIDs,passwords, sub-ids or node credentials.
onlineClientsis a count, so the collector needs no POST at all, and the scope stays GET-only.GetOnlineClients()for the count and a newGetInboundsMetrics(userId)that selectsid, remark, enable, up, downplus the preloadedClientStats(samePreloadasGetInbounds, no settings columns). No schema change, nomigration.
ServerController(controller/server.go) plus one service method; theOpenAPI spec and docs regenerate from the existing tooling.
Scope
New scope name
metrics:internal/database/model/model.go:ApiScopeMetrics = "metrics", add toIsKnownApiScope;NormalizeScopeinpanel/api_token.go: accept it;enforceTokenScope:case model.ApiScopeMetrics:with a method-scoped allowlist, reusing theexisting
scopeAllows/nodeSyncScopeAllowpattern (api.go:131):/server/statusis included on purpose: it is already available tomonitortokens and lets a clientrun a cheap preflight without touching the aggregate endpoint.
metricsto the scope selector + the four i18n locales;docs/content/docs/*/reference/api/api-tokens.mdxare generated — regenerate and note thatmetricsis read-only over counters and thatmonitoris unchanged.Properties
monitorsafe stay intact: no POST, no config, no secrets.docs should say "treat like any other credential, set an expiry".
admin,monitor,node-sync,node-adminuntouched.Non-goals
monitorscope — that would silently change the meaning of tokensalready in the wild.
CheckUsercorrectly and is the safer primitive.
Tests
internal/web/service/panel/api_token_scope_test.go:metricsis known, normalized, and rejectedfor mutating routes.
metricstoken → 200 on/server/metricsand/server/status, 403 with theexisting
"this API token is not permitted to access this endpoint"on/inbounds/add,/setting/update,/clients/onlines.GetInboundsMetricsnever selectssettings/stream_settings.Acceptance criteria
metricstoken can fetch everything a Prometheus exporter needs with GET-only calls.monitor,admin,node-sync,node-adminbehaviour is byte-for-byte unchanged.@OlegEnot commented on GitHub (Nov 4, 2026):
Client side: the metrics exporter that needs this is hteppl/3x-ui-exporter#19 (Bearer auth + preflight). With a
metricstoken it would call onlyGET /server/statusandGET /server/metrics, and needs neitherinbounds/listnor thePOST /clients/onlinesroute.