[GH-ISSUE #5332] [Feature]: Allow to use negative sorting order (index) in inbounds #3736

Closed
opened 2026-10-31 20:34:35 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @Ponywka on GitHub (Jun 14, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5332

I have servers that stand out for their stability and speed. I’d like to set them as the primary inbound in the subscription. Currently, to do this, I have to assign a higher index to each slower inbounds. I’d prefer to simply assign a negative index to the primary inbound server once, and leave the rest at their default values.

Describe the solution you'd like

I already did it in https://github.com/MHSanaei/3x-ui/pull/5214.

Allow to users sets a negative sorting index

Alternatives you've considered

No response

Concrete use case

No response

Mockups, screenshots, or examples

No response

Which parts of the panel would this affect?

No response

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 and this feature has not been requested.
  • This feature request is written in English.
Originally created by @Ponywka on GitHub (Jun 14, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5332 ### Is your feature request related to a problem? I have servers that stand out for their stability and speed. I’d like to set them as the primary inbound in the subscription. Currently, to do this, I have to assign a higher index to each slower inbounds. I’d prefer to simply assign a negative index to the primary inbound server once, and leave the rest at their default values. ### Describe the solution you'd like I already did it in https://github.com/MHSanaei/3x-ui/pull/5214. Allow to users sets a negative sorting index ### Alternatives you've considered _No response_ ### Concrete use case _No response_ ### Mockups, screenshots, or examples _No response_ ### Which parts of the panel would this affect? _No response_ ### 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](../issues?q=is%3Aissue) and this feature has not been requested. - [x] This feature request is written in English.
bsakaguchi 2026-10-31 20:34:35 +07:00
Author
Owner

@github-actions[bot] commented on GitHub (Jun 14, 2026):

The subSortIndex field you described from PR #5214 was already landed in a rescoped form — it controls subscription output order (ORDER BY sub_sort_index ASC, id ASC in internal/sub/service.go:304). The field is present in the inbound form and in the panel's column list. However, the current implementation enforces a minimum of 1 at every layer, which prevents the use case you describe.

Where the constraint is enforced

  • internal/database/model/model.go:53 — GORM model tag validate:"omitempty,gte=1"; column default 1
  • internal/web/service/inbound_util.go:13-18 — normalizeSubSortIndex() clamps any value < 1 to 1 on both the add and update paths
  • internal/database/db.go:133-134 — normalizeInboundSubSortIndex() runs at startup and executes UPDATE inbounds SET sub_sort_index = 1 WHERE sub_sort_index < 1, so any row written with a negative value would be lifted to 1 on the next restart
  • frontend/src/schemas/forms/inbound-form.ts:42 — z.number().int().min(1).default(1)
  • frontend/src/lib/xray/inbound-form-adapter.ts:193 — Math.max(1, row.subSortIndex ?? 1) on read
  • frontend/src/generated/zod.ts:317 — generated API schema also enforces min(1)

Why the minimum was set to 1

The comment in inbound_util.go explains: an omitted subSortIndex key in the request body binds to Go's zero value (0), and the intent was that unset fields must not sort ahead of inbounds that have been explicitly ranked. The 1-floor serves as both the default and the sentinel for "no explicit rank."

What allowing negatives would require

To support negative values, all six enforcement points above would need to change consistently:

  1. Lower the validate tag floor (e.g. gte=-32768) and adjust the GORM column default if a non-zero default is desired for backward compatibility.
  2. Rewrite normalizeSubSortIndex to treat only the true zero-from-missing-field case specially (e.g. map 0 → a positive default, allow all other values through), or remove the clamping entirely if 0 is no longer a sentinel.
  3. Remove or adjust the startup SQL in normalizeInboundSubSortIndex so existing rows are not lifted.
  4. Update the Zod schemas in the frontend (inbound-form.ts, re-run npm run gen for generated/zod.ts) and the form adapter's Math.max call.

The backend service tests in internal/web/service/inbound_sub_sort_test.go explicitly assert that -3 and 0 are clamped to 1; those would need to be updated to match the new contract.

If you want to contribute this change, a PR that adjusts the minimum consistently across all six sites and updates the tests would be the right approach. The maintainer confirmed in the PR #5214 comment that the underlying use case is considered valid.

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:4703443176 --> @github-actions[bot] commented on GitHub (Jun 14, 2026): The `subSortIndex` field you described from PR #5214 was already landed in a rescoped form — it controls subscription output order (`ORDER BY sub_sort_index ASC, id ASC` in `internal/sub/service.go:304`). The field is present in the inbound form and in the panel's column list. However, the current implementation enforces a **minimum of 1** at every layer, which prevents the use case you describe. **Where the constraint is enforced** - `internal/database/model/model.go:53` — GORM model tag `validate:"omitempty,gte=1"`; column default `1` - `internal/web/service/inbound_util.go:13-18` — `normalizeSubSortIndex()` clamps any value `< 1` to `1` on both the add and update paths - `internal/database/db.go:133-134` — `normalizeInboundSubSortIndex()` runs at startup and executes `UPDATE inbounds SET sub_sort_index = 1 WHERE sub_sort_index < 1`, so any row written with a negative value would be lifted to `1` on the next restart - `frontend/src/schemas/forms/inbound-form.ts:42` — `z.number().int().min(1).default(1)` - `frontend/src/lib/xray/inbound-form-adapter.ts:193` — `Math.max(1, row.subSortIndex ?? 1)` on read - `frontend/src/generated/zod.ts:317` — generated API schema also enforces `min(1)` **Why the minimum was set to 1** The comment in `inbound_util.go` explains: an omitted `subSortIndex` key in the request body binds to Go's zero value (`0`), and the intent was that unset fields must not sort ahead of inbounds that have been explicitly ranked. The `1`-floor serves as both the default and the sentinel for "no explicit rank." **What allowing negatives would require** To support negative values, all six enforcement points above would need to change consistently: 1. Lower the `validate` tag floor (e.g. `gte=-32768`) and adjust the GORM column default if a non-zero default is desired for backward compatibility. 2. Rewrite `normalizeSubSortIndex` to treat only the true zero-from-missing-field case specially (e.g. map `0` → a positive default, allow all other values through), or remove the clamping entirely if `0` is no longer a sentinel. 3. Remove or adjust the startup SQL in `normalizeInboundSubSortIndex` so existing rows are not lifted. 4. Update the Zod schemas in the frontend (`inbound-form.ts`, re-run `npm run gen` for `generated/zod.ts`) and the form adapter's `Math.max` call. The backend service tests in `internal/web/service/inbound_sub_sort_test.go` explicitly assert that `-3` and `0` are clamped to `1`; those would need to be updated to match the new contract. If you want to contribute this change, a PR that adjusts the minimum consistently across all six sites and updates the tests would be the right approach. The maintainer confirmed in the PR #5214 comment that the underlying use case is considered valid. *This reply was generated automatically; a maintainer may follow up.*
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#3736
No description provided.