[PR #6521] [MERGED] fix(xray): read an outbound protocol id the way the core does #6328

Closed
opened 2026-11-02 04:07:58 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6521
Author: @BlindMaster24
Created: 9/14/2026
Status: ✅ Merged
Merged: 9/14/2026
Merged by: @BlindMaster24

Base: main ← Head: fix/rewriter-protocol-case


📝 Commits (3)

  • 35e4d75 fix(xray): read an outbound protocol id the way the core does
  • f9f84f9 fix(xray): match an outbound protocol id case-insensitively
  • 11cabce fix(xray): re-run the freedom finalRules rewrite where its seeder was gated

📊 Changes

3 files changed (+336 additions, -5 deletions)

View changed files

📝 internal/database/db.go (+82 -5)
📝 internal/database/freedom_domain_strategy_migration_test.go (+5 -0)
➕ internal/database/rewriter_protocol_case_test.go (+249 -0)

📄 Description

Summary

The three seeders that rewrite xrayTemplateConfig compared protocol case-sensitively (proto != "freedom"), while xray-core lowercases that id before it looks up the handler. A template that spells the direct outbound Freedom therefore runs as freedom and escapes all three rewrites — and each seeder still records itself as applied, so it never runs again.

Why

The core's loader, infra/conf/loader.go:

func (v *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
	id = strings.ToLower(id)

infra/conf/xray.go:329 calls it with c.Protocol for every outbound, so "protocol": "Freedom" is the freedom handler, not an unknown protocol. That id is also the only thing the panel's rewriters look at, which is where the two sides disagree.

User story — a panel whose Xray template was hand-edited (the template is an editable JSON setting in the panel, and it is also what an imported config lands in) with "Freedom" for the direct outbound:

  1. xray refuses to start. rewriteRemovedOutboundKeys exists precisely because sockopt.addressPortStrategy on freedom is a hard error in the core (infra/conf/xray.go:340-344): a panel carrying that key cannot start xray at all. Spelled Freedom, the key is never dropped, the seed is recorded as applied, and the install stays dead on a key the seeder was written to remove.
  2. A documented loopback exposure stays open. rewriteFreedomFinalRulesPrivateEgress is the fix for #6037: with domainStrategy: AsIs the router never resolves domains, so a domain with a private A record (e.g. 127-0-0-1.nip.io) sails past the geoip:private routing block, and freedom's allow-all finalRules let it reach loopback services such as the xray gRPC API and metrics listener. Skipped, the outbound keeps the allow-only rules, the seed is recorded, and no later start ever retries the hardening.
  3. The reverse migration is skipped too. rewriteFreedomFinalRules replaces the legacy private-only-allow finalRules, a state an intermediate release shipped, with allow-all; skipped, that outbound keeps its broken egress.

All three are one root cause in one file, so the change is one predicate each.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

internal/database/rewriter_protocol_case_test.go adds two tests, both red before the change and green after (verified by reverting the fix and watching each subtest fail):

  • TestRewriteRemovedOutboundKeysSeesAnUppercaseFreedom drives the vendored core: it asserts xray.ValidateOutboundConfig refuses the "Freedom" outbound carrying sockopt.addressPortStrategy (which is the proof the core loaded it as freedom rather than rejecting the id), then requires the rewrite to drop the key and the core to accept the result.
  • TestRewritersTreatProtocolCaseAlike runs all three rewriters on the same outbound spelled freedom and Freedom and requires the same verdict and the same migrated keys.

Reproducing locally: go test ./internal/database/ -run 'RewritersTreatProtocolCaseAlike|SeesAnUppercaseFreedom'.

Honest note about my machine: it has no C compiler, so internal/database builds only under CGO_ENABLED=0 with the cgo-only sqlite3.SQLiteConn.Backup call stubbed by a -overlay file (no file of this PR is substituted). The pure-function tests above run that way; every DB-backed test in the package needs cgo and is left to CI, as is go build ./....

Screenshots / recordings

N/A

Breaking changes

None. The seeders only match strictly more configs than before, and every config they newly match is one the core already treats as freedom.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally. (no C compiler here — CI verifies)
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6521 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/14/2026 **Status:** ✅ Merged **Merged:** 9/14/2026 **Merged by:** [@BlindMaster24](https://github.com/BlindMaster24) **Base:** `main` ← **Head:** `fix/rewriter-protocol-case` --- ### 📝 Commits (3) - [`35e4d75`](https://github.com/MHSanaei/3x-ui/commit/35e4d758e2e0a4ebacdaa71efe4fb666bb7ea6b5) fix(xray): read an outbound protocol id the way the core does - [`f9f84f9`](https://github.com/MHSanaei/3x-ui/commit/f9f84f959fd72a969382c287b09fe645795c0b69) fix(xray): match an outbound protocol id case-insensitively - [`11cabce`](https://github.com/MHSanaei/3x-ui/commit/11cabce4140cef3c8d8f4e4f31458cb33e5ac1b1) fix(xray): re-run the freedom finalRules rewrite where its seeder was gated ### 📊 Changes **3 files changed** (+336 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `internal/database/db.go` (+82 -5) 📝 `internal/database/freedom_domain_strategy_migration_test.go` (+5 -0) ➕ `internal/database/rewriter_protocol_case_test.go` (+249 -0) </details> ### 📄 Description ## Summary The three seeders that rewrite `xrayTemplateConfig` compared `protocol` case-sensitively (`proto != "freedom"`), while xray-core lowercases that id before it looks up the handler. A template that spells the direct outbound `Freedom` therefore runs as freedom and escapes all three rewrites — and each seeder still records itself as applied, so it never runs again. ## Why The core's loader, `infra/conf/loader.go`: ```go func (v *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) { id = strings.ToLower(id) ``` `infra/conf/xray.go:329` calls it with `c.Protocol` for every outbound, so `"protocol": "Freedom"` is the freedom handler, not an unknown protocol. That id is also the only thing the panel's rewriters look at, which is where the two sides disagree. User story — a panel whose Xray template was hand-edited (the template is an editable JSON setting in the panel, and it is also what an imported config lands in) with `"Freedom"` for the direct outbound: 1. **xray refuses to start.** `rewriteRemovedOutboundKeys` exists precisely because `sockopt.addressPortStrategy` on freedom is a hard error in the core (`infra/conf/xray.go:340-344`): a panel carrying that key cannot start xray at all. Spelled `Freedom`, the key is never dropped, the seed is recorded as applied, and the install stays dead on a key the seeder was written to remove. 2. **A documented loopback exposure stays open.** `rewriteFreedomFinalRulesPrivateEgress` is the fix for #6037: with `domainStrategy: AsIs` the router never resolves domains, so a domain with a private A record (e.g. `127-0-0-1.nip.io`) sails past the `geoip:private` routing block, and freedom's allow-all `finalRules` let it reach loopback services such as the xray gRPC API and metrics listener. Skipped, the outbound keeps the allow-only rules, the seed is recorded, and no later start ever retries the hardening. 3. **The reverse migration is skipped too.** `rewriteFreedomFinalRules` replaces the legacy private-only-allow `finalRules`, a state an intermediate release shipped, with allow-all; skipped, that outbound keeps its broken egress. All three are one root cause in one file, so the change is one predicate each. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [x] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [x] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? `internal/database/rewriter_protocol_case_test.go` adds two tests, both red before the change and green after (verified by reverting the fix and watching each subtest fail): - `TestRewriteRemovedOutboundKeysSeesAnUppercaseFreedom` drives the vendored core: it asserts `xray.ValidateOutboundConfig` **refuses** the `"Freedom"` outbound carrying `sockopt.addressPortStrategy` (which is the proof the core loaded it as freedom rather than rejecting the id), then requires the rewrite to drop the key and the core to accept the result. - `TestRewritersTreatProtocolCaseAlike` runs all three rewriters on the same outbound spelled `freedom` and `Freedom` and requires the same verdict and the same migrated keys. Reproducing locally: `go test ./internal/database/ -run 'RewritersTreatProtocolCaseAlike|SeesAnUppercaseFreedom'`. Honest note about my machine: it has no C compiler, so `internal/database` builds only under `CGO_ENABLED=0` with the cgo-only `sqlite3.SQLiteConn.Backup` call stubbed by a `-overlay` file (no file of this PR is substituted). The pure-function tests above run that way; every DB-backed test in the package needs cgo and is left to CI, as is `go build ./...`. ## Screenshots / recordings N/A ## Breaking changes None. The seeders only match strictly more configs than before, and every config they newly match is one the core already treats as freedom. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [ ] `go build ./...` and the test suite pass locally. (no C compiler here — CI verifies) - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:58 +07:00
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#6328
No description provided.