[PR #5417] [MERGED] feat(sub): full XHTTP field mapping for Clash/Mihomo subscriptions #5878

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5417
Author: @w3struk
Created: 6/17/2026
Status: ✅ Merged
Merged: 6/20/2026
Merged by: @MHSanaei

Base: main ← Head: fix/clash-xhttp-opts-clean


📝 Commits (6)

  • 23e7367 feat(sub): add full XHTTP field mapping for Clash subscriptions
  • 1800026 test(sub): add comprehensive tests for buildXhttpClashOpts
  • 1af0903 Merge branch 'main' into fix/clash-xhttp-opts-clean
  • ec402a5 Merge branch 'main' into fix/clash-xhttp-opts-clean
  • cd9f1f8 Merge branch 'main' into fix/clash-xhttp-opts-clean
  • 54f0f61 fix(sub): clean up redundant skipValue check and add missing xhttp no-settings test

📊 Changes

2 files changed (+594 additions, -19 deletions)

View changed files

📝 internal/sub/clash_service.go (+124 -19)
📝 internal/sub/clash_service_test.go (+470 -0)

📄 Description

Summary

The Clash subscription generator (clash_service.go) only emitted path, host, mode in xhttp-opts. Mihomo supports ALL XHTTP parameters including padding, xmux (as reuse-settings), session/seq placement, and more.

This PR adds buildXhttpClashOpts() — an allowlist-based function that maps all client-relevant XHTTP fields from 3x-ui's camelCase JSON storage to Mihomo's kebab-case YAML format.

Changes

internal/sub/clash_service.go

  • New function buildXhttpClashOpts() (lines 363-473): Converts xhttpSettings map to Mihomo-compatible xhttp-opts map
    • String field mapping with DPI default filtering (scMaxEachPostBytes="1000000", scMinPostsIntervalMs="30")
    • Bool fields (truthy only): noGRPCHeader, xPaddingObfsMode
    • Padding obfs gated fields: only emitted when xPaddingObfsMode=true
    • Nested xmux → reuse-settings with kebab-case sub-field mapping
    • Headers with case-insensitive Host key drop
    • Returns nil for empty opts (consistent with buildXhttpExtra)
  • Updated applyTransport xhttp case (lines 547-554): Delegates to buildXhttpClashOpts

internal/sub/clash_service_test.go

  • 9 new test functions (454 lines) covering:
    • TestBuildXhttpClashOpts_FullFieldMapping — every kebab-case key verified
    • TestBuildXhttpClashOpts_DPIDefaultsFiltered — DPI fingerprint defaults filtered
    • TestBuildXhttpClashOpts_PaddingObfsGate — false/absent/true-with-no-gated-fields
    • TestBuildXhttpClashOpts_XmuxMapsToReuseSettings — full mapping, empty, int/float64/zero hKeepAlivePeriod
    • TestBuildXhttpClashOpts_ServerOnlyFieldsExcluded — noSSEHeader, scMaxBufferedPosts, etc.
    • TestBuildXhttpClashOpts_NilInput / _EmptyInput — return nil
    • TestBuildXhttpClashOpts_HostFallbackFromHeaders — headers.Host, only-Host, case-insensitive drop
    • TestBuildXhttpClashOpts_NoGRPCHeaderFalsey — false and absent both produce no key

Field Mapping (source-verified against Mihomo adapter/outbound/vless.go)

3x-ui (camelCase) Mihomo (kebab-case) Type Filter
path path string direct
host host string direct + headers fallback
mode mode string direct
headers headers map drop Host key
noGRPCHeader no-grpc-header bool truthy only
xPaddingBytes x-padding-bytes string non-empty
xPaddingObfsMode x-padding-obfs-mode bool truthy only
xPaddingKey x-padding-key string non-empty, gated by obfs mode
xPaddingHeader x-padding-header string non-empty, gated by obfs mode
xPaddingPlacement x-padding-placement string non-empty, gated by obfs mode
xPaddingMethod x-padding-method string non-empty, gated by obfs mode
uplinkHTTPMethod uplink-http-method string non-empty
sessionPlacement session-placement string non-empty
sessionKey session-key string non-empty
seqPlacement seq-placement string non-empty
seqKey seq-key string non-empty
uplinkDataPlacement uplink-data-placement string non-empty
uplinkDataKey uplink-data-key string non-empty
uplinkChunkSize uplink-chunk-size string non-zero
scMaxEachPostBytes sc-max-each-post-bytes string non-empty, filter "1000000"
scMinPostsIntervalMs sc-min-posts-interval-ms string non-empty, filter "30"
xmux reuse-settings nested non-empty object

XMUX → reuse-settings sub-fields

| maxConcurrency | max-concurrency | string | non-empty |
| maxConnections | max-connections | string | non-empty |
| cMaxReuseTimes | c-max-reuse-times | string | non-empty |
| hMaxRequestTimes | h-max-request-times | string | non-empty |
| hMaxReusableSecs | h-max-reusable-secs | string | non-empty |
| hKeepAlivePeriod | h-keep-alive-period | int | non-zero |

Server-only fields (EXCLUDED — not in allowlist)

  • noSSEHeader, scMaxBufferedPosts, scStreamUpServerSecs, serverMaxHeaderBytes

Deferred to follow-up PR

  • downloadSettings → download-settings (complex nested structure)

Testing

  • All 15 test cases pass (9 functions with sub-tests)
  • Existing TestApplyTransport_XHTTP and TestApplyTransport_XHTTP_HostFromHeaders still pass
  • Tested locally with Mihomo v1.19.27 — connection successful, all XHTTP fields accepted
  • Complements #5393 (JSON subscription XHTTP client fields fix)
  • Mihomo XHTTP field names verified against source code (adapter/outbound/vless.go struct tags)

🔄 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/5417 **Author:** [@w3struk](https://github.com/w3struk) **Created:** 6/17/2026 **Status:** ✅ Merged **Merged:** 6/20/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/clash-xhttp-opts-clean` --- ### 📝 Commits (6) - [`23e7367`](https://github.com/MHSanaei/3x-ui/commit/23e73675f799eff10d3e083640c6f54af94b62b4) feat(sub): add full XHTTP field mapping for Clash subscriptions - [`1800026`](https://github.com/MHSanaei/3x-ui/commit/1800026719a516b2cd8a2270e0ff7c3303feb07a) test(sub): add comprehensive tests for buildXhttpClashOpts - [`1af0903`](https://github.com/MHSanaei/3x-ui/commit/1af09031ded261597818ad962373f8e7943fcb5f) Merge branch 'main' into fix/clash-xhttp-opts-clean - [`ec402a5`](https://github.com/MHSanaei/3x-ui/commit/ec402a56dbad3e7bcd5ac9f5214eed3147441a9c) Merge branch 'main' into fix/clash-xhttp-opts-clean - [`cd9f1f8`](https://github.com/MHSanaei/3x-ui/commit/cd9f1f8a8a61740d9ecf574e517c292ea3dcdc25) Merge branch 'main' into fix/clash-xhttp-opts-clean - [`54f0f61`](https://github.com/MHSanaei/3x-ui/commit/54f0f619dcd74364e2ff01a963800ed2efc20185) fix(sub): clean up redundant skipValue check and add missing xhttp no-settings test ### 📊 Changes **2 files changed** (+594 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `internal/sub/clash_service.go` (+124 -19) 📝 `internal/sub/clash_service_test.go` (+470 -0) </details> ### 📄 Description ## Summary The Clash subscription generator (`clash_service.go`) only emitted `path`, `host`, `mode` in `xhttp-opts`. Mihomo supports ALL XHTTP parameters including padding, xmux (as `reuse-settings`), session/seq placement, and more. This PR adds `buildXhttpClashOpts()` — an allowlist-based function that maps all client-relevant XHTTP fields from 3x-ui's camelCase JSON storage to Mihomo's kebab-case YAML format. ## Changes ### `internal/sub/clash_service.go` - **New function `buildXhttpClashOpts()`** (lines 363-473): Converts `xhttpSettings` map to Mihomo-compatible `xhttp-opts` map - String field mapping with DPI default filtering (`scMaxEachPostBytes="1000000"`, `scMinPostsIntervalMs="30"`) - Bool fields (truthy only): `noGRPCHeader`, `xPaddingObfsMode` - Padding obfs gated fields: only emitted when `xPaddingObfsMode=true` - Nested `xmux` → `reuse-settings` with kebab-case sub-field mapping - Headers with case-insensitive Host key drop - Returns `nil` for empty opts (consistent with `buildXhttpExtra`) - **Updated `applyTransport` xhttp case** (lines 547-554): Delegates to `buildXhttpClashOpts` ### `internal/sub/clash_service_test.go` - **9 new test functions** (454 lines) covering: - `TestBuildXhttpClashOpts_FullFieldMapping` — every kebab-case key verified - `TestBuildXhttpClashOpts_DPIDefaultsFiltered` — DPI fingerprint defaults filtered - `TestBuildXhttpClashOpts_PaddingObfsGate` — false/absent/true-with-no-gated-fields - `TestBuildXhttpClashOpts_XmuxMapsToReuseSettings` — full mapping, empty, int/float64/zero hKeepAlivePeriod - `TestBuildXhttpClashOpts_ServerOnlyFieldsExcluded` — noSSEHeader, scMaxBufferedPosts, etc. - `TestBuildXhttpClashOpts_NilInput` / `_EmptyInput` — return nil - `TestBuildXhttpClashOpts_HostFallbackFromHeaders` — headers.Host, only-Host, case-insensitive drop - `TestBuildXhttpClashOpts_NoGRPCHeaderFalsey` — false and absent both produce no key ## Field Mapping (source-verified against Mihomo `adapter/outbound/vless.go`) | 3x-ui (camelCase) | Mihomo (kebab-case) | Type | Filter | |---|---|---|---| | `path` | `path` | string | direct | | `host` | `host` | string | direct + headers fallback | | `mode` | `mode` | string | direct | | `headers` | `headers` | map | drop Host key | | `noGRPCHeader` | `no-grpc-header` | bool | truthy only | | `xPaddingBytes` | `x-padding-bytes` | string | non-empty | | `xPaddingObfsMode` | `x-padding-obfs-mode` | bool | truthy only | | `xPaddingKey` | `x-padding-key` | string | non-empty, gated by obfs mode | | `xPaddingHeader` | `x-padding-header` | string | non-empty, gated by obfs mode | | `xPaddingPlacement` | `x-padding-placement` | string | non-empty, gated by obfs mode | | `xPaddingMethod` | `x-padding-method` | string | non-empty, gated by obfs mode | | `uplinkHTTPMethod` | `uplink-http-method` | string | non-empty | | `sessionPlacement` | `session-placement` | string | non-empty | | `sessionKey` | `session-key` | string | non-empty | | `seqPlacement` | `seq-placement` | string | non-empty | | `seqKey` | `seq-key` | string | non-empty | | `uplinkDataPlacement` | `uplink-data-placement` | string | non-empty | | `uplinkDataKey` | `uplink-data-key` | string | non-empty | | `uplinkChunkSize` | `uplink-chunk-size` | string | non-zero | | `scMaxEachPostBytes` | `sc-max-each-post-bytes` | string | non-empty, filter "1000000" | | `scMinPostsIntervalMs` | `sc-min-posts-interval-ms` | string | non-empty, filter "30" | | `xmux` | `reuse-settings` | nested | non-empty object | ### XMUX → reuse-settings sub-fields | `maxConcurrency` | `max-concurrency` | string | non-empty | | `maxConnections` | `max-connections` | string | non-empty | | `cMaxReuseTimes` | `c-max-reuse-times` | string | non-empty | | `hMaxRequestTimes` | `h-max-request-times` | string | non-empty | | `hMaxReusableSecs` | `h-max-reusable-secs` | string | non-empty | | `hKeepAlivePeriod` | `h-keep-alive-period` | int | non-zero | ### Server-only fields (EXCLUDED — not in allowlist) - `noSSEHeader`, `scMaxBufferedPosts`, `scStreamUpServerSecs`, `serverMaxHeaderBytes` ### Deferred to follow-up PR - `downloadSettings` → `download-settings` (complex nested structure) ## Testing - All 15 test cases pass (9 functions with sub-tests) - Existing `TestApplyTransport_XHTTP` and `TestApplyTransport_XHTTP_HostFromHeaders` still pass - Tested locally with Mihomo v1.19.27 — connection successful, all XHTTP fields accepted ## Related - Complements #5393 (JSON subscription XHTTP client fields fix) - Mihomo XHTTP field names verified against source code (`adapter/outbound/vless.go` struct tags) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:04:43 +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#5878
No description provided.