mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #6322] [Feature]: Named routing profiles, assignable to inbounds and hosts #4296
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#4296
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 @den41apple on GitHub (Aug 25, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6322
Is your feature request related to a problem?
All routing settings for subscriptions are global to the panel:
subEnableRouting/subRoutingRulesgo into theRouting-Enable/Routingheaders (internal/sub/controller.go:733-772,ApplyCommonHeaders)subIncyEnableRouting/subIncyRoutingRulesare appended verbatim to the raw body (internal/sub/controller.go:418-421)subJsonRulesis merged intorouting.rulesonce, in the service constructor, before any request is served (internal/sub/json_service.go:41-49)So a panel can hand out one routing profile and nothing else. I cannot say "this entry uses profile A, that entry uses profile B". I also cannot store a profile once and reuse it: the rules live as an opaque blob inside a single settings field, and if I want them in two places I paste them twice.
Note that a routing profile already has a name, and clients use that name to recognise the profile and update it independently of the others. It behaves like an object everywhere except in the panel, where it is a text field.
For JSON subscriptions the limitation is not just missing UI. Every array element is a full xray config that the client passes to the core as-is, so it could carry its own
routingsection. ButGetJsonbuilds each element with a shallow copy:Only
outboundsandremarksget replaced.routingis not just identical in every element, it is the samemap[string]anybehind the same pointer. Mutating it for one entry would corrupt the rest, so right now there is no way to express per-entry routing at all.Describe the solution you'd like
Make routing profiles named objects that can be assigned to inbounds and to hosts. The override chain is the same one the panel already uses for
FinalMask(global default, per-host override) and forExcludeFromSubTypes, except the payload is stored once instead of being copied into every place that needs it.This splits into two phases. Phase 1 is useful on its own if phase 2 is not wanted.
Phase 1: the profile object
New table
routing_profiles:One row covers both delivery channels, so the rules are edited in one place and every subscription that references the profile picks up the change.
CRUD under
/panel/api/routing-profiles(list,get/:id,add,update/:id,del/:id,setEnable/:id), same shape as the host controller (internal/web/controller/host.go:24-36).Phase 2: assignment
Join table
routing_profile_bindings:Resolution for a subscription entry, most specific first:
The last step keeps installs that never create a profile working exactly as they do now.
The relation is many-to-many on purpose. One entry can legitimately offer more than one profile and let the user pick in the client.
Behaviour per subscription format
Deeplinkof every profile resolved for the entries of this subscription, after the share links and before the existing globalsubIncyRoutingRules, de-duplicated by name. Several routing links in one body are already valid and the panel already writes one that way, so there is nothing to resolve here.configJson["routing"]per entry (needed anyway, see above), then useJsonRulesof the resolved profile instead of the globalsubJsonRules. An element can hold only onerouting, so if several profiles resolve to one entry, the one flaggedIsDefaultwins and the others are skipped for that element.internal/sub/clash_service.go:90-104): take theIsDefaultprofile, otherwise inherit global. Fine to leave for later.Routingheader. Stays global, unchanged. Only one header can be sent, so a per-entry value means nothing there. This is the ambiguity that killed #4479.Edge cases
Deeplinkset,JsonRulesempty): that channel inherits the global setting, the other one still applies.IsDefault.ExcludeFromSubTypes, so a whitelist-only entry can go to JSON clients and stay hidden from raw ones.Migration
One migration for the two tables. Optionally a non-empty
subRoutingRulesorsubIncyRoutingRulescan be lifted into a single profile bound at global scope, which makes the upgrade invisible to current operators.UI
frontend/src/pages/hosts/HostFormModal.tsxand in the inbound form, labelled "Routing profiles, leave empty to inherit".Alternatives you've considered
A plain text field on the host. That was my first idea and it does work, but the same base64 blob ends up stored in every host that needs it. With 15 nodes and two entries each, changing one rule set means editing about 30 fields by hand. A named object avoids that and gives the panel the name the clients already rely on.
A field on the inbound (#4479). Does not cover my own setup. One server is reachable directly and through a relay in Russia, and those are two hosts of the same inbound. An inbound-level field cannot tell them apart, and that is exactly the pair that needs different routing.
A second panel plus external subscription merge. This does deliver two profiles: raw external subscription bodies are copied line by line with the only filter being
strings.Contains(ln, "://")(internal/sub/external_subscription.go:148-168), so a routing deeplink from a second panel does reach the client. But it costs a second xray process, a second database, a second backup and upgrade path, plus a port conflict to sort out, and it still cannot bind a profile to a specific entry. It also fails for JSON and Clash, where external links go throughlink.ParseLinkand non-share schemes are dropped (json_service.go:96-100,clash_external.go:14-18).A reverse proxy rewriting the
Routingheader. Covers the header only. It needs a separate subscription URL per profile, so the same person ends up with duplicate server lists, and it does nothing for JSON subscriptions where routing sits inside the config.One shared profile for everyone. Whitelist users get broad rules or everyone else gets narrow ones. That is what I run today and it is why I am writing this.
Concrete use case
I run about 15 nodes and hand out one subscription per client, roughly 300 clients.
Russian mobile carriers periodically cut everything except a government "white list" of some 500 domains: banks, government services, marketplaces. During a cut those domains have to go direct, because they are the only ones that still resolve. Route them through the tunnel and they go down with it. Outside a cut the same person wants the usual split, all of Russia direct and the rest proxied. Two rule sets, both needed by the same people.
On top of that every server is published twice, once directly and once through a relay hosted in Russia. Those two entries share one inbound and one UUID, only the path differs, and they need different routing because the relay is what survives a cut. So assignment has to reach the host level, while still being convenient to apply in bulk on the inbound. That is why both scopes are in the proposal.
Today I ask each of those 300 people to import a second routing deeplink by hand and switch it in app settings, because the panel can attach one profile to the subscription it serves and no more. With named profiles I define "normal" and "white list" once, bind them to the right hosts, and the choice becomes one tap in the client.
Mockups, screenshots, or examples
Profiles, defined once:
normalhapp://routing/add/eyJOYW1lIjoi…[ ... ]white-listhapp://routing/add/eyJOYW1lIjoi…[ ... ]Bound to entries of one inbound:
NL-1normalNL-1 (relay)normal,white-listNL-1 (white list)white-listResulting raw body, before base64:
Resulting JSON subscription: three array elements, identical except
remarks,outboundsand, with this feature,routing, each taking theIsDefaultprofile bound to its host.Relation to #4479
I opened #4479 myself and it was closed. This one is deliberately different, because the old request was under-specified:
routingis shared by pointer across entries.Happy to close this one in favour of reopening #4479 if you prefer that thread.
Which parts of the panel would this affect?
Subscription (share links / Clash / JSON), Backend (API endpoints, login, settings), Frontend (UI / panel pages), Database / migrations
Which parts of the panel would this affect?
Subscription (share links / Clash / JSON), Backend (API endpoints, login, settings), Frontend (UI / panel pages), Database / migrations
Are you willing to help?
Before submitting
@github-actions[bot] commented on GitHub (Aug 25, 2026):
This is the same request as #6297 — the body is identical, and you closed that one nine seconds before opening this (
closedAt2026-08-25T13:59:23Zvs this issue'screatedAt2026-08-25T13:59:32Z). The analysis posted there still stands, so I won't repeat it; because #6297 is closed, I am leaving this thread open as the live one rather than closing it back onto a dead issue. Nothing has changed at the tip (f727d04f, v3.7.0):Routingdoes not appear anywhere ininternal/database/model/model.go, so there is still no per-inbound or per-host routing override in the schema, and #4479 closed on 2026-05-29 with an emptyclosedByPullRequestsReferences— nothing was implemented for it.Your anchors, re-checked at the current tip (the line numbers have drifted, the claims have not):
resolveRoutingSource(remoteRoutingHapp, profileRoutingRules)—internal/sub/controller.go:843-849, insideApplyCommonHeaders(:812-853), fed the global fields at:429internal/sub/controller.go:431-437subJsonRulesmerged once in the constructorinternal/sub/json_service.go:48-55outbounds/remarksreplacedinternal/sub/json_service.go:545-549(per client) and:143-145(external links)"rules": []string{"MATCH,PROXY"}atinternal/sub/clash_service.go:98, merged globally at:101-109FinalMaskis the precedentinternal/database/model/model.go:1094-1096→internal/sub/host_sub.go:105→:150if strings.Contains(ln, "://")is atinternal/sub/external_subscription.go:197, not148-168Two implementation facts on top of that write-up, which change where the work lands:
1. The JSON substitution cannot happen where you put it.
NewSubJsonServiceis constructed exactly once in production (internal/sub/controller.go:251) and folds the global rules straight into the base document:By the time an entry is rendered,
s.configJson["routing"]["rules"]is global rules + the catch-all frominternal/sub/default.json:81-87({"type":"field","network":"tcp,udp","outboundTag":"proxy"}), and the un-merged base no longer exists. So "useJsonRulesof the resolved profile instead of the globalsubJsonRules" cannot be expressed as a per-entry overwrite ofrouting— the constructor has to keep the pristine rules (or the pristineroutingsubtree) in a field first. Small change, but it belongs inNewSubJsonService, not in the renderer.2. Raw is the expensive channel; JSON is nearly free. For JSON and Clash, per-host values already ride to the renderer through the endpoint map —
ep["finalMask"] = h.FinalMaskatinternal/sub/host_sub.go:105, consumed at:150— which is whyfinalMaskcost so little. Raw has no such channel:hostEndpointsis called and consumed locally insidegetSubs(internal/sub/service.go:323), and the signature isso the controller that would append the deeplink (
internal/sub/controller.go:419-437) never sees a host. Phase 2's raw half needs a new value out ofgetSubs; the JSON half does not.Worth restating because it cuts the other way: the per-entry deep copy you ask for already exists for one document kind. The client-side balancer path added in
da01b763clonesroutingand each rule map per document (internal/sub/json_service.go:392-396, assigned at:430) under the comment "The routing subtree ins.configJsonis shared by every emitted document; clone it (and each rule map) before pointing rules at the balancer." That is generalisation, not new machinery — and note it is a latent hazard, not a live output bug: each element is marshalled immediately and nothing mutatesroutingon the per-inbound path today.Related, and I am leaving both open: #5824 asks for the inbound-level JSON subset of this (a "Routing" tab in the inbound editor) — it overlaps heavily enough that whoever decides one should decide both, but the scope differs and neither obviously satisfies the other. #6062 splits routing by client User-Agent, a different axis.
One thing I still cannot check from here: whether Happ and Incy really keep several
routing/adddeeplinks from a single subscription body as separate, independently selectable profiles. That is client-side behaviour outside this repository, and the entire raw half of the proposal rests on it; a report of two profiles landing side by side in the client from one body would settle it.Verdict: Feature request
This reply was generated automatically; a maintainer may follow up.
@den41apple commented on GitHub (Aug 26, 2026):
I went and tested it. Short answer: yes, Happ keeps them as separate profiles.
Setup was Happ Desktop 4.1.1 (602) on Windows, pointed at a local http server returning a plain text body:
Added via
happ://add/http://127.0.0.1:8899/sub. Here is%LOCALAPPDATA%\Happ\routing.jsonright after the import, trimmed to the interesting fields:Both survived as distinct objects, both pinned to the same subscription id, and the values that differed between them (
globalProxy,domainStrategy) came through per profile rather than one overwriting the other. Each also got its own asset directory on disk.Note
selectedProfile: it is a single string, not a list. One profile is active per subscription at any moment and the user switches between them, which is what phase 2 was after. The subscription log saysSubscription #1492684291 added successfully: 1 server configs added, so the routing lines are consumed as routing and do not leak into the server list.This is Happ only. I have no way to test Incy, so that half is still open.
Two things came out of the run that I did not expect.
Routing on the client is already per subscription
Happ 4.x moved it there (dev-docs/routing, "Decentralised architecture"): every subscription owns its own isolated profile set with an independent lifecycle, and removing the subscription cascade-deletes the profiles and their cached geofiles. The old global fields still exist in the file but stayed empty for the whole run (
"activeRoutingName": "","routingNames": []), so they look like v1 leftovers.Which means the panel is now the only side of this pipe that is still global-only. That is worth knowing before deciding scope on this and #5824.
Profiles cost about 27 MB each and are not deduplicated
Both of my profiles carried the same default Loyalsoldier URLs. Happ downloaded the files twice anyway, once per profile:
There is no shared cache keyed by URL, the path is
routing\<subId>\<profileName>\. So emitting N profiles for one subscription costs the user N x ~27 MB on every device. The de-dup by name in the raw proposal is not just tidiness then, and it is probably worth capping how many profiles one entry may resolve to.One correction on the JSON side
Happ's docs put JSON subscriptions in a restricted mode: strictly 0 or 1 routing profile, no manual creation, no copying, no clipboard import, master toggle force-locked. I first read that as killing the JSON half of the proposal, but it does not. That rule is about Happ's own profile object. The
routingsection inside each JSON element is a different thing and still reaches the core, because Happ hands JSON configs to Xray unchanged ("standard HAPP routing rules and interface settings are not applied to the JSON file", dev-docs/examples-of-links-and-parameters section 3.1). So a per-entryroutingproduced by the deep copy is honoured.There is a sharp edge though. With
useChunkFileson, Happ trims the geofiles by the tags declared in the routing profile, not by the tags used in the JSON config, and the docs warn about keeping the two in sync. A per-entryroutingreferencinggeosite:orgeoip:categories that the single allowed profile does not declare can end up reading a geofile with exactly those sections cut out. So the JSON half works, but a per-entry rule set is only as good as the one profile the subscription is allowed to carry.