Skip to content

experimental/clashapi: include user in connection metadata#4138

Open
yagjzx wants to merge 93 commits into
SagerNet:testingfrom
yagjzx:clashapi-expose-user-field
Open

experimental/clashapi: include user in connection metadata#4138
yagjzx wants to merge 93 commits into
SagerNet:testingfrom
yagjzx:clashapi-expose-user-field

Conversation

@yagjzx
Copy link
Copy Markdown

@yagjzx yagjzx commented May 14, 2026

The InboundContext.User field is populated by vless / hysteria2 / anytls inbounds with the matched users[].name, but it is not currently included in the JSON output of /connections in clashapi. Without it, downstream consumers (Clash Meta-compatible dashboards, custom traffic accounting) cannot attribute traffic to a configured user.

This adds a single field to the marshalled metadata.

Behaviour

  • For inbounds where a user has been matched, the field returns the configured users[].name (e.g. "user": "alice").
  • For inbounds where no user matching applies (mixed, socks, http, etc.), the field serializes to the empty string. This is non-breaking for any consumer that ignores unknown fields.

Verification

Built locally with the project's standard tags. Spun up a vless inbound with two named users (alice, bob); a client connecting with the alice UUID correctly produced "metadata": { ..., "user": "alice" } in the /connections response. Connections through a non-user-bearing mixed inbound on the same instance correctly produced "user": "".

Why this is useful

Per-user traffic accounting is a common need for any multi-tenant proxy deployment. Today the only way to recover user attribution from clashapi is to give every user their own inbound.tag (which works via metadata.type) — but that explodes the listening-port footprint and requires per-tenant TLS material. Exposing the existing User field is a one-line, zero-cost alternative.

nekohasekai and others added 27 commits May 2, 2026 19:18
Signed-off-by: macronut <4027187+macronut@users.noreply.github.com>
The InboundContext.User field is populated by vless / hysteria2 / anytls
inbounds with the matched users[].name, but it is not currently included
in the JSON output of /connections in clashapi. Without it, downstream
consumers (Clash Meta-compatible dashboards, custom traffic accounting)
cannot attribute traffic to a configured user.

This adds a single field to the marshalled metadata. For inbounds where
no user has been matched (e.g. mixed, socks), the field serializes to
the empty string, which is non-breaking for any consumer that ignores
unknown fields.
@nekohasekai nekohasekai force-pushed the testing branch 3 times, most recently from 057f540 to 6b07a22 Compare May 15, 2026 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants