[GH-ISSUE #6894] [Feature]: Respect global Hosts order in subscriptions and support drag-and-drop #4576

Closed
opened 2026-10-31 20:40:38 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @HamedSanaei on GitHub (Oct 21, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6894

### Is your feature request related to a problem?

Yes. The Host groups displayed in /panel/hosts have a persisted sort_order and Up/Down controls, but their global order is not reflected in the final subscription when hosts belong to different inbounds.

The current code retrieves inbounds by sub_sort_index ASC, id ASC (internal/sub/service.go:getInboundsBySubId), then iterates through those inbounds. internal/sub/host_sub.go:hostEndpoints applies sort_order ASC, id ASC only inside each inbound. The raw/base64, Clash/Mihomo, and JSON subscription builders consequently group generated Host endpoints by parent inbound. Moving a Host group above another group from a different inbound does not reliably move it above that group in the exported list. Changing inbound order instead is too coarse.

Separately, /panel/hosts requires repeated Up/Down clicks to reposition Host rows, while /panel/routing already has a convenient drag-and-drop approach for routing rules.

Describe the solution you'd like

1. Make global Host order optionally control the generated subscription

  • Allow administrators to prioritize Host groups across different inbounds, so the final subscription emits their endpoints in the same relative order shown under /panel/hosts, regardless of inbound ID or sub_sort_index.
  • Apply this order to the actual server-generated raw/base64, Clash/Mihomo, and JSON subscription output, not only to the panel display.
  • Preserve backward compatibility: a selectable Inbound-first (current behavior) / Hosts-first subscription ordering mode (or an equivalent opt-in) would avoid changing existing subscribers' order without their administrator's consent.
  • Make grouping/tie-break rules deterministic for a Host group containing multiple addresses and/or assigned to several inbounds. Define a sensible fallback for inbounds without Hosts, old externalProxy entries, external subscription links, and equal sort values.
  • Keep all existing enable/disable, per-format exclusion, hidden/excluded-inbound behavior, client ownership, and output semantics. No additional, missing, or duplicate subscription entries.

2. Add drag-and-drop for Host rows on /panel/hosts

  • Add a visible drag handle for each Host group row, similar to the reorder UX for Routing Rules under /panel/routing.
  • Persist the new order as soon as a row is dropped, show save/error feedback, and recover gracefully if saving fails.
  • Reuse the existing POST /panel/api/hosts/reorder endpoint and the HostService.ReorderHostGroups/sort_order mechanism where practical; no second incompatible ordering source.
  • Retain the existing Up/Down controls as a keyboard/mobile-accessible alternative.
  • The order must survive page refresh and panel restart, and appear in newly fetched subscriptions when the Hosts-first mode is selected.

Expected behavior example

Order in /panel/hosts Parent inbound Host
1 Inbound #20 Preferred Host B
2 Inbound #5 Host A
3 Inbound #20 Backup Host C

With Hosts-first ordering selected, the resulting subscription is B → A → C, even if inbound #5 has the smaller inbound ID or subscription sort index. Dragging A above B should change the next subscription response to A → B → C without editing the inbounds.

(The server can guarantee the order in the generated subscription; some client applications may subsequently apply their own local sorting.)

Alternatives you've considered

  • Changing each inbound's sub_sort_index can only reorder entire inbound blocks, not interleave Host entries across inbounds.
  • Using current Up/Down controls changes the Hosts page order but does not achieve the desired cross-inbound subscription order.
  • Manually recreating inbounds or post-processing subscription URLs externally is disruptive and hard to maintain.

Concrete use case

I operate multiple inbounds and maintain preferred, secondary, and backup Host endpoints. I want the most reliable Host to appear first for subscribers, and need to adjust that priority quickly as conditions change. The Host's priority should not be determined by when I originally created its inbound.

Mockups, screenshots, or examples

Use the current drag-and-drop control on /panel/routing as the UX reference for the Hosts table.

Relevant implementation points:

  • frontend/src/pages/hosts/HostList.tsx and HostsPage.tsx (existing Up/Down UI)
  • internal/web/controller/host.go and internal/web/service/host.go (existing /reorder endpoint, sort_order persistence)
  • internal/sub/service.go, host_sub.go, clash_service.go, json_service.go (subscription ordering across formats)
  • internal/sub/host_sub_test.go: TestSub_HostSortComposesWithSubSortIndex currently asserts inbound-first ordering. Preserve this for the default mode and add regression coverage for Hosts-first ordering across different inbounds, multiple hosts/groups, exclusions, and format parity.

Related but distinct issues: #5424 concerns sorting inbounds in the panel, #4835 concerns reordering attached inbounds per client, and #5332 concerns negative inbound sort indexes. This request is specifically cross-inbound Host ordering in subscription output plus Hosts-page drag-and-drop.

Which parts of the panel would this affect?

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Subscription (share links / Clash / JSON)
  • Database / migrations only if the chosen opt-in setting needs one

Are you willing to help?

  • I'd like to implement this feature myself and open a PR.
  • I can help test once a PR is open.

Before submitting

  • I searched existing issues; related requests address inbound ordering rather than this cross-inbound Host ordering feature.
  • This feature request is written in English.
Originally created by @HamedSanaei on GitHub (Oct 21, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6894 ### Is your feature request related to a problem? Yes. The Host groups displayed in `/panel/hosts` have a persisted `sort_order` and Up/Down controls, but their **global order is not reflected in the final subscription** when hosts belong to different inbounds. The current code retrieves inbounds by `sub_sort_index ASC, id ASC` (`internal/sub/service.go:getInboundsBySubId`), then iterates through those inbounds. `internal/sub/host_sub.go:hostEndpoints` applies `sort_order ASC, id ASC` **only inside each inbound**. The raw/base64, Clash/Mihomo, and JSON subscription builders consequently group generated Host endpoints by parent inbound. Moving a Host group above another group from a different inbound does not reliably move it above that group in the exported list. Changing inbound order instead is too coarse. Separately, `/panel/hosts` requires repeated Up/Down clicks to reposition Host rows, while `/panel/routing` already has a convenient drag-and-drop approach for routing rules. ### Describe the solution you'd like **1. Make global Host order optionally control the generated subscription** - Allow administrators to prioritize Host groups across **different inbounds**, so the final subscription emits their endpoints in the same relative order shown under `/panel/hosts`, regardless of inbound ID or `sub_sort_index`. - Apply this order to the actual **server-generated** raw/base64, Clash/Mihomo, and JSON subscription output, not only to the panel display. - Preserve backward compatibility: a selectable `Inbound-first (current behavior)` / `Hosts-first` subscription ordering mode (or an equivalent opt-in) would avoid changing existing subscribers' order without their administrator's consent. - Make grouping/tie-break rules deterministic for a Host group containing multiple addresses and/or assigned to several inbounds. Define a sensible fallback for inbounds without Hosts, old externalProxy entries, external subscription links, and equal sort values. - Keep all existing enable/disable, per-format exclusion, hidden/excluded-inbound behavior, client ownership, and output semantics. No additional, missing, or duplicate subscription entries. **2. Add drag-and-drop for Host rows on `/panel/hosts`** - Add a visible drag handle for each Host group row, similar to the reorder UX for Routing Rules under `/panel/routing`. - Persist the new order as soon as a row is dropped, show save/error feedback, and recover gracefully if saving fails. - Reuse the existing `POST /panel/api/hosts/reorder` endpoint and the `HostService.ReorderHostGroups`/`sort_order` mechanism where practical; no second incompatible ordering source. - Retain the existing Up/Down controls as a keyboard/mobile-accessible alternative. - The order must survive page refresh and panel restart, and appear in newly fetched subscriptions when the Hosts-first mode is selected. **Expected behavior example** | Order in /panel/hosts | Parent inbound | Host | | --- | --- | --- | | 1 | Inbound #20 | Preferred Host B | | 2 | Inbound #5 | Host A | | 3 | Inbound #20 | Backup Host C | With Hosts-first ordering selected, the resulting subscription is **B → A → C**, even if inbound #5 has the smaller inbound ID or subscription sort index. Dragging A above B should change the next subscription response to **A → B → C** without editing the inbounds. (The server can guarantee the order in the generated subscription; some client applications may subsequently apply their own local sorting.) ### Alternatives you've considered - Changing each inbound's `sub_sort_index` can only reorder entire inbound blocks, not interleave Host entries across inbounds. - Using current Up/Down controls changes the Hosts page order but does not achieve the desired cross-inbound subscription order. - Manually recreating inbounds or post-processing subscription URLs externally is disruptive and hard to maintain. ### Concrete use case I operate multiple inbounds and maintain preferred, secondary, and backup Host endpoints. I want the most reliable Host to appear first for subscribers, and need to adjust that priority quickly as conditions change. The Host's priority should not be determined by when I originally created its inbound. ### Mockups, screenshots, or examples Use the current drag-and-drop control on `/panel/routing` as the UX reference for the Hosts table. **Relevant implementation points:** - `frontend/src/pages/hosts/HostList.tsx` and `HostsPage.tsx` (existing Up/Down UI) - `internal/web/controller/host.go` and `internal/web/service/host.go` (existing `/reorder` endpoint, `sort_order` persistence) - `internal/sub/service.go`, `host_sub.go`, `clash_service.go`, `json_service.go` (subscription ordering across formats) - `internal/sub/host_sub_test.go`: `TestSub_HostSortComposesWithSubSortIndex` currently asserts inbound-first ordering. Preserve this for the default mode and add regression coverage for Hosts-first ordering across different inbounds, multiple hosts/groups, exclusions, and format parity. **Related but distinct issues:** #5424 concerns sorting **inbounds** in the panel, #4835 concerns reordering attached **inbounds** per client, and #5332 concerns negative inbound sort indexes. This request is specifically **cross-inbound Host ordering in subscription output** plus Hosts-page drag-and-drop. ### Which parts of the panel would this affect? - Frontend (UI / panel pages) - Backend (API endpoints, login, settings) - Subscription (share links / Clash / JSON) - Database / migrations only if the chosen opt-in setting needs one ### Are you willing to help? - [x] I'd like to implement this feature myself and open a PR. - [x] I can help test once a PR is open. ### Before submitting - [x] I searched existing issues; related requests address inbound ordering rather than this cross-inbound Host ordering feature. - [x] This feature request is written in English.
Author
Owner

@github-actions[bot] commented on GitHub (Oct 21, 2026):

This is a valid feature request. Your reading of the current code is accurate: Host order is stored globally, but subscriptions only apply it inside each inbound, so a Host can never be placed ahead of a Host that belongs to an inbound sorted earlier. The Hosts page also has no drag-and-drop yet.

What the code does today

  1. The Hosts page writes one global order. ReorderHostGroups gives each group its position in the list you submit, across every inbound (internal/web/service/host.go:305-316):
    if err := tx.Model(&model.Host{}).Where("group_id = ?", groupId).Update("sort_order", i).Error; err != nil {
    
    So the data a "Hosts-first" mode needs is already persisted. No new ordering source or migration is needed for the order itself.
  2. Subscriptions read that value one inbound at a time. Inbounds are loaded with .Order("sub_sort_index ASC").Order("id ASC") (internal/sub/service.go:691). For each inbound, hostEndpoints runs Where("inbound_id = ? AND is_disabled = ?", inbound.Id, false).Order("sort_order asc, id asc") (internal/sub/host_sub.go:21-24). The same call is made from the raw builder (service.go:498), Clash (clash_service.go:74), JSON (json_service.go:152) and the panel's own share links (links.go:41).
  3. This behaviour is deliberate and pinned by a test. TestSub_HostSortComposesWithSubSortIndex (internal/sub/host_sub_test.go:167-169) says: "host sort_order composes with inbound SubSortIndex: inbounds order by SubSortIndex, hosts within an inbound by sort_order." The JSON builder also re-sorts its output by sortIndex, kind and id (json_service.go:174-184) so that balancer entries slot in between inbounds. Hosts-first ordering would have to take that sort into account as well.
  4. Up/Down on the Hosts page swaps two neighbouring groups and sends the whole list to reorder (frontend/src/pages/hosts/HostsPage.tsx:94-104). The arrow buttons are in frontend/src/pages/hosts/HostList.tsx:98-111. The routing page implements its drag with plain pointer events, not a drag-and-drop library (frontend/src/pages/xray/routing/RoutingTab.tsx:288-323). That is the pattern you would follow.

Points a PR would need to settle

  • Same order value across inbounds. One Host group spread over several inbounds stores the same sort_order on every row, because ReorderHostGroups updates by group_id. Those endpoints tie on order, so a deterministic tie-break is needed. Falling back to the existing (sub_sort_index, id) order would keep it stable.
  • Inbounds without Hosts. These inbounds, and any externalProxy fallback, have no sort_order of their own. Placing them before or after all Host entries is a design choice to make explicitly.
  • Per-inbound rendering. Raw, Clash and JSON all build output per inbound and per client. A Hosts-first mode means collecting the rendered endpoints first and re-sorting them, rather than changing the SQL in hostEndpoints. The existing exclusion and ownership checks (ExcludeFromSub, ExcludeFromSubTypes, client filtering) happen inside that loop, so they should carry over unchanged.
  • Repo rules. An opt-in mode would be a new setting. It needs i18n keys in all 13 locale files under internal/web/translation/. Any new endpoint also needs the endpoints.ts / make gen steps described in CLAUDE.md.

I found no existing or duplicate issue for cross-inbound Host ordering. As you noted, #5424, #4835 and #5332 are about inbound ordering, not Host ordering.

Verdict: Feature request

This reply was generated automatically by an issue-analysis assistant; a maintainer may follow up.

<!-- gh-comment-id:6327049424 --> @github-actions[bot] commented on GitHub (Oct 21, 2026): This is a valid feature request. Your reading of the current code is accurate: Host order is stored globally, but subscriptions only apply it inside each inbound, so a Host can never be placed ahead of a Host that belongs to an inbound sorted earlier. The Hosts page also has no drag-and-drop yet. **What the code does today** 1. The Hosts page writes one global order. `ReorderHostGroups` gives each group its position in the list you submit, across every inbound (`internal/web/service/host.go:305-316`): ```go if err := tx.Model(&model.Host{}).Where("group_id = ?", groupId).Update("sort_order", i).Error; err != nil { ``` So the data a "Hosts-first" mode needs is already persisted. No new ordering source or migration is needed for the order itself. 2. Subscriptions read that value one inbound at a time. Inbounds are loaded with `.Order("sub_sort_index ASC").Order("id ASC")` (`internal/sub/service.go:691`). For each inbound, `hostEndpoints` runs `Where("inbound_id = ? AND is_disabled = ?", inbound.Id, false).Order("sort_order asc, id asc")` (`internal/sub/host_sub.go:21-24`). The same call is made from the raw builder (`service.go:498`), Clash (`clash_service.go:74`), JSON (`json_service.go:152`) and the panel's own share links (`links.go:41`). 3. This behaviour is deliberate and pinned by a test. `TestSub_HostSortComposesWithSubSortIndex` (`internal/sub/host_sub_test.go:167-169`) says: *"host sort_order composes with inbound SubSortIndex: inbounds order by SubSortIndex, hosts within an inbound by sort_order."* The JSON builder also re-sorts its output by `sortIndex`, `kind` and `id` (`json_service.go:174-184`) so that balancer entries slot in between inbounds. Hosts-first ordering would have to take that sort into account as well. 4. Up/Down on the Hosts page swaps two neighbouring groups and sends the whole list to `reorder` (`frontend/src/pages/hosts/HostsPage.tsx:94-104`). The arrow buttons are in `frontend/src/pages/hosts/HostList.tsx:98-111`. The routing page implements its drag with plain pointer events, not a drag-and-drop library (`frontend/src/pages/xray/routing/RoutingTab.tsx:288-323`). That is the pattern you would follow. **Points a PR would need to settle** - **Same order value across inbounds.** One Host group spread over several inbounds stores the same `sort_order` on every row, because `ReorderHostGroups` updates by `group_id`. Those endpoints tie on order, so a deterministic tie-break is needed. Falling back to the existing `(sub_sort_index, id)` order would keep it stable. - **Inbounds without Hosts.** These inbounds, and any `externalProxy` fallback, have no `sort_order` of their own. Placing them before or after all Host entries is a design choice to make explicitly. - **Per-inbound rendering.** Raw, Clash and JSON all build output per inbound and per client. A Hosts-first mode means collecting the rendered endpoints first and re-sorting them, rather than changing the SQL in `hostEndpoints`. The existing exclusion and ownership checks (`ExcludeFromSub`, `ExcludeFromSubTypes`, client filtering) happen inside that loop, so they should carry over unchanged. - **Repo rules.** An opt-in mode would be a new setting. It needs i18n keys in all 13 locale files under `internal/web/translation/`. Any new endpoint also needs the `endpoints.ts` / `make gen` steps described in `CLAUDE.md`. I found no existing or duplicate issue for cross-inbound Host ordering. As you noted, #5424, #4835 and #5332 are about inbound ordering, not Host ordering. Verdict: Feature request *This reply was generated automatically by an issue-analysis assistant; a maintainer may follow up.* <!-- claude-issue:analyst -->
Author
Owner

@HamedSanaei commented on GitHub (Oct 22, 2026):

### Implementation update — PR #6895

The two features requested in this issue have been implemented and pushed to PR #6895:

  • Global Hosts-first subscription order: Added the opt-in subHostSortMode setting. The default remains inbound-first for backward compatibility; hosts-first uses the saved cross-inbound Host group order in Raw/Base64, Clash/Mihomo and JSON/Xray subscriptions.
  • Drag-and-drop on /panel/hosts: Host rows can be reordered by dragging. The existing reorder API, Up/Down accessibility controls, optimistic save and rollback remain in use.
  • Latest refactor: In commit a6d3a426, the duplicated pointer gesture handling was extracted into the shared usePointerRowReorder hook, now used by both Hosts and Routing Rules. Their distinct persistence behavior is preserved, and cancelling a drag in Routing no longer changes rule order.
  • Compatibility: No new database migration, backend endpoint, or drag-and-drop dependency was needed.

Reported local validation: 31 focused tests, all 1,877 frontend tests, TypeScript, lint, formatting, frontend build, Go subscription tests, and a real Chromium drag-and-drop check passed.

Status: PR #6895 is open for maintainer review. CI and Docs CI are awaiting maintainer approval to run; upstream CI has not yet been verified.

Maintainers: please review the implementation and approve the pending workflows when convenient. Thank you!

<!-- gh-comment-id:6352766224 --> @HamedSanaei commented on GitHub (Oct 22, 2026): ### Implementation update — PR #6895 The two features requested in this issue have been implemented and pushed to [PR #6895](https://github.com/MHSanaei/3x-ui/pull/6895): - **Global Hosts-first subscription order:** Added the opt-in `subHostSortMode` setting. The default remains `inbound-first` for backward compatibility; `hosts-first` uses the saved cross-inbound Host group order in Raw/Base64, Clash/Mihomo and JSON/Xray subscriptions. - **Drag-and-drop on `/panel/hosts`:** Host rows can be reordered by dragging. The existing reorder API, Up/Down accessibility controls, optimistic save and rollback remain in use. - **Latest refactor:** In [commit `a6d3a426`](https://github.com/HamedSanaei/3x-ui/commit/a6d3a4263fd9c01b35a60cf1b1cb2443005ea2fc), the duplicated pointer gesture handling was extracted into the shared `usePointerRowReorder` hook, now used by **both Hosts and Routing Rules**. Their distinct persistence behavior is preserved, and cancelling a drag in Routing no longer changes rule order. - **Compatibility:** No new database migration, backend endpoint, or drag-and-drop dependency was needed. **Reported local validation:** 31 focused tests, all 1,877 frontend tests, TypeScript, lint, formatting, frontend build, Go subscription tests, and a real Chromium drag-and-drop check passed. **Status:** PR #6895 is open for maintainer review. [CI](https://github.com/MHSanaei/3x-ui/actions/runs/39736157841) and [Docs CI](https://github.com/MHSanaei/3x-ui/actions/runs/39736157840) are awaiting maintainer approval to run; upstream CI has not yet been verified. Maintainers: please review the implementation and approve the pending workflows when convenient. Thank you!
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#4576
No description provided.