[PR #6765] [CLOSED] feat(mtproto): relay mode - ServerHello splitting (client-mss) for MTProto inbounds #6445

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6765
Author: @AndreyOsipuk
Created: 10/7/2026
Status: ❌ Closed

Base: main ← Head: feat/mtproto-relay


📝 Commits (10+)

  • e6bfe52 feat(mtproto): secured (dd) clients and warm DC pool options
  • 93f885c feat(sub): add dd links for secured MTProto inbounds
  • b7ec51c feat(frontend): secured and warm DC pool settings for MTProto inbounds
  • b7a7bb1 feat(mtproto): expose the remaining mtg-multi options for MTProto inbounds
  • 6c2b49b feat(sub): add tg://webproxy links for MTProto inbounds in WEB mode
  • 38bf58e feat(frontend): advanced mtg settings for MTProto inbounds
  • d0e4b95 fix(mtproto): say which mtg-multi build serves secured and WEB links
  • c8cb2c2 style(mtproto): keep comment blocks to two lines
  • 4e747c0 Merge branch 'main' into feat/mtproto-secured-dcpool
  • 8049e30 fix(mtproto): gate secured and WEB on a confirmed mtg-multi build, drop the DC list

📊 Changes

35 files changed (+4845 additions, -52 deletions)

View changed files

📝 docs/content/docs/en/config/inbounds.mdx (+31 -0)
📝 docs/content/docs/ru/config/inbounds.mdx (+34 -0)
📝 frontend/src/lib/xray/inbound-link.ts (+82 -23)
📝 frontend/src/lib/xray/link-label.tsx (+5 -1)
📝 frontend/src/pages/inbounds/form/protocols/mtproto.tsx (+406 -2)
📝 frontend/src/schemas/forms/inbound-form.ts (+10 -3)
📝 frontend/src/schemas/protocols/inbound/mtproto.ts (+196 -0)
📝 frontend/src/test/link-label.test.ts (+7 -0)
📝 frontend/src/test/mtproto-clients-link.test.ts (+54 -2)
➕ frontend/src/test/mtproto-options.test.tsx (+421 -0)
📝 go.mod (+1 -1)
📝 internal/database/model/model.go (+23 -0)
📝 internal/database/model/model_mtproto_test.go (+34 -0)
📝 internal/mtproto/manager.go (+116 -10)
📝 internal/mtproto/manager_test.go (+164 -1)
➕ internal/mtproto/options.go (+772 -0)
➕ internal/mtproto/options_test.go (+506 -0)
📝 internal/sub/service.go (+23 -8)
📝 internal/sub/service_mtproto_test.go (+127 -1)
📝 internal/web/service/inbound.go (+8 -0)

...and 15 more files

📄 Description

Summary

Lets an MTProto inbound work as a relay inside a censored network: mtg sends its FakeTLS ServerHello in small TCP segments towards clients, and reaches Telegram through an Xray outbound (for example a tunnel to a server abroad).

Stacked on #6761 - merge #6761 first, then this PR. This branch is based on feat/mtproto-secured-dcpool, so the diff below also contains #6761's commits. The changes of this PR are the last five commits: compare.

mtg-multi dependencies (all open): MHSanaei/mtg-multi#10 (client-mss) for this PR; #7 (secured) and #9 (WEB mode) for the stacked #6761, whose Secured and WEB settings stay behind the mtgSecuredWebBuild switch until the host runs a build with them. Until #10 is released, the relay fields save but have no effect.

Problem

On a Russian ISP's DPI (TSPU), mtg's FakeTLS is recognised by the ServerHello when it leaves in full-size TCP segments, and the proxy's IP:port is then banned. We hit this with a relay inside the country that forwarded the TCP stream to an mtg abroad:

Setup on the relay Connection attempts that worked Port banned
plain forwarding to mtg abroad (ServerHello in full-size segments) 3 of 8 yes
mtg on the relay with client-mss = 92, client-mss-bulk = 1400, Telegram reached through a tunnel abroad 8 of 8 no

Holding a small MSS for the whole session is not an option (throughput collapses), so only the ServerHello is split. MHSanaei/mtg-multi#10 adds this as [network] client-mss / client-mss-bulk.

Relay scenario with the panel

  1. On the relay, create an MTProto inbound with the same clients as the server abroad (or new ones).
  2. Relay → ServerHello MSS 92 (session MSS left empty = mtg's 1400).
  3. Route through Xray → Outbound: pick the outbound that leads abroad (a VLESS/WireGuard/... outbound or a balancer).

Step 3 uses the existing "Route through Xray" feature: the panel adds a loopback SOCKS inbound tagged with the inbound's tag, points mtg's [network] proxies at it, and puts a inboundTag -> outboundTag (or balancerTag) rule first in the routing. This PR does not change it; it only ties the two pieces together in the form and docs.

Changes

  • Settings (internal/mtproto/options.go): network.clientMss and network.clientMssBulk (pointer, so an explicit 0 is kept apart from "unset").
    • Validation mirrors mtg-multi: clientMss 0 or 48..1460; clientMssBulk 0 or 536..65495 and greater than clientMss; with clientMssBulk = 0 (whole session at clientMss, set on the socket) clientMss must be at least 88, the kernel's minimum for TCP_MAXSEG.
    • Rendered into [network] only when clientMss is set; client-mss-bulk only next to it (mtg ignores it alone). Unset = nothing rendered, so existing inbounds keep a byte-identical config.
    • As with the other options, a bad value that reaches the settings through a raw API payload is dropped on render, and a change restarts the sidecar (the option is read at start).
    • One source for client-mss. Saving an inbound whose ServerHello MSS is set while its Extra TOML also sets [network] client-mss or client-mss-bulk fails and names the key. Either source alone still works; for raw payloads the render keeps the field value.
  • Route through Xray and the IP preference (internal/web/service/inbound_mtproto.go, internal/mtproto/manager.go): a routed inbound saved without an IP preference stores prefer-ipv4, and mtg gets prefer-ipv4 for such settings from older data too. An explicit preference is kept; unrouted inbounds are unchanged. The form sets prefer-ipv4 when routing is switched on with no preference and warns about prefer-ipv6/only-ipv6 and about ad-tagged clients through the bridge; saving logs the same warnings.
  • Form: a "Relay" section (first of the collapsed sections) with both fields and a note on when they help, how to send Telegram traffic through another server, and the limits below.
  • i18n: 12 keys; en-US and ru-RU translated, English in the other locales.
  • Docs: an "MTProto relay" section in config/inbounds (en, ru).

Limits of the relay egress (SOCKS bridge)

Checked in the mtg-multi and Xray sources:

  • IPv6. Xray's SOCKS inbound replies "succeeded" before it dials the target (proxy/socks: handshake, then dispatch). mtg therefore cannot fall back to the next DC address: a connection to an IPv6 DC address through an outbound without IPv6 fails for that client. mtg's default is prefer-ipv6, so a routed inbound with no IP preference now gets prefer-ipv4, and the form warns about prefer-ipv6/only-ipv6 (fine only when the outbound has IPv6). The form note and the docs say so.
  • Ad-tags. The middle-proxy handshake derives keys from mtg's own source IP and port as the middle proxy sees them. Through SOCKS mtg only knows its loopback socket, so ad-tagged clients cannot work through the bridge (not tested against live middle proxies, follows from the key schedule). Clients without an ad-tag are not affected. The form warns when a routed inbound has ad-tagged clients.
  • Everything else goes through the bridge as expected: direct DC dials, the warm DC pool, the public config updater and doppelganger fetches use the same dialer. Domain fronting does not (mtg dials it directly), which is what a relay wants.
  • client-mss is Linux only; on other systems mtg ignores it.

Compatibility with mtg-multi builds

mtg-multi decodes its config with go-toml in non-strict mode, so a build without #10 ignores the new keys. I rendered relay configs from the panel (client-mss = 92; 92 + client-mss-bulk = 1400; 120 + client-mss-bulk = 0, each together with the Xray bridge proxies) and ran mtg-multi run on Linux:

  • mhsanaei/mtg-multi main: starts with every config and ignores the keys (as a control, it also starts with an invalid client-mss = 87 / bulk 0).
  • #10: starts with every config and rejects the invalid control with network.client-mss must be at least 88 when network.client-mss-bulk = 0. The accepted connections report mss:108 and mss:1388 for 120/0 and 92/1400 (configured value minus the 12-byte timestamp option), so the setting reaches the socket.

Until #10 is released the fields have no effect; the form note says that an mtg-multi build with client-mss support is needed.

Tests

  • Go (internal/mtproto):
    • rendering: client-mss alone, with a bulk, with an explicit bulk 0; bulk without client-mss renders nothing; together with the Xray bridge both land in one [network] table;
    • save-time validation of every bound and both cross-field rules, with the exact message;
    • invalid values are dropped on read;
    • both fields change the structural fingerprint and not the secrets fingerprint;
    • client-mss in both the fields and Extra TOML is refused on save (also through AddInbound), either alone is accepted;
    • routed inbounds: an unset preference renders and stores prefer-ipv4, an explicit one is kept, unrouted ones are unchanged; the IPv6 and ad-tag warnings.
      Each new assertion was checked to fail with the corresponding code reverted.
  • Frontend: schema round-trip, explicit 0 vs cleared bulk, range errors, the two cross-field errors on the right field, the Relay section and its fields, the prefer-ipv4 default and both warnings while routing through Xray (none for a direct inbound).

Run locally: go vet ./..., go test ./..., go test -race ./internal/mtproto/, golangci-lint run (0 issues), make gen (no diff), frontend on Node 26 format:check, lint, typecheck, vitest unit + components (1796 tests), build, docs typecheck, lint, format:check, test, build. The Storybook (Chromium) project was not run locally; no stories changed.


🔄 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/6765 **Author:** [@AndreyOsipuk](https://github.com/AndreyOsipuk) **Created:** 10/7/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/mtproto-relay` --- ### 📝 Commits (10+) - [`e6bfe52`](https://github.com/MHSanaei/3x-ui/commit/e6bfe523bdea2a80e4f390f0cefd4c0d0216bb6c) feat(mtproto): secured (dd) clients and warm DC pool options - [`93f885c`](https://github.com/MHSanaei/3x-ui/commit/93f885ca83ac972bf93c19ef18cda520d6af48d4) feat(sub): add dd links for secured MTProto inbounds - [`b7ec51c`](https://github.com/MHSanaei/3x-ui/commit/b7ec51cd226696de110063d2e3930d0dec0529e8) feat(frontend): secured and warm DC pool settings for MTProto inbounds - [`b7a7bb1`](https://github.com/MHSanaei/3x-ui/commit/b7a7bb15f01a6b276bfab9698fc0f9e508ad7a4d) feat(mtproto): expose the remaining mtg-multi options for MTProto inbounds - [`6c2b49b`](https://github.com/MHSanaei/3x-ui/commit/6c2b49bd78f0fb38f3a3cef21046f33d4a0f5cfd) feat(sub): add tg://webproxy links for MTProto inbounds in WEB mode - [`38bf58e`](https://github.com/MHSanaei/3x-ui/commit/38bf58e57b93d96c517b5c76c93cf39469cf9e7f) feat(frontend): advanced mtg settings for MTProto inbounds - [`d0e4b95`](https://github.com/MHSanaei/3x-ui/commit/d0e4b952fb3e4df5211ae20ad282fc40960a206c) fix(mtproto): say which mtg-multi build serves secured and WEB links - [`c8cb2c2`](https://github.com/MHSanaei/3x-ui/commit/c8cb2c2ded9cbb9a460054f638f5ee25adff3be3) style(mtproto): keep comment blocks to two lines - [`4e747c0`](https://github.com/MHSanaei/3x-ui/commit/4e747c00f9e8a9e565227be3b9b93242e684e8c3) Merge branch 'main' into feat/mtproto-secured-dcpool - [`8049e30`](https://github.com/MHSanaei/3x-ui/commit/8049e3024576e6f29c9b3553452cd288b090e843) fix(mtproto): gate secured and WEB on a confirmed mtg-multi build, drop the DC list ### 📊 Changes **35 files changed** (+4845 additions, -52 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/config/inbounds.mdx` (+31 -0) 📝 `docs/content/docs/ru/config/inbounds.mdx` (+34 -0) 📝 `frontend/src/lib/xray/inbound-link.ts` (+82 -23) 📝 `frontend/src/lib/xray/link-label.tsx` (+5 -1) 📝 `frontend/src/pages/inbounds/form/protocols/mtproto.tsx` (+406 -2) 📝 `frontend/src/schemas/forms/inbound-form.ts` (+10 -3) 📝 `frontend/src/schemas/protocols/inbound/mtproto.ts` (+196 -0) 📝 `frontend/src/test/link-label.test.ts` (+7 -0) 📝 `frontend/src/test/mtproto-clients-link.test.ts` (+54 -2) ➕ `frontend/src/test/mtproto-options.test.tsx` (+421 -0) 📝 `go.mod` (+1 -1) 📝 `internal/database/model/model.go` (+23 -0) 📝 `internal/database/model/model_mtproto_test.go` (+34 -0) 📝 `internal/mtproto/manager.go` (+116 -10) 📝 `internal/mtproto/manager_test.go` (+164 -1) ➕ `internal/mtproto/options.go` (+772 -0) ➕ `internal/mtproto/options_test.go` (+506 -0) 📝 `internal/sub/service.go` (+23 -8) 📝 `internal/sub/service_mtproto_test.go` (+127 -1) 📝 `internal/web/service/inbound.go` (+8 -0) _...and 15 more files_ </details> ### 📄 Description ## Summary Lets an MTProto inbound work as a **relay inside a censored network**: mtg sends its FakeTLS ServerHello in small TCP segments towards clients, and reaches Telegram through an Xray outbound (for example a tunnel to a server abroad). > **Stacked on #6761 - merge #6761 first, then this PR.** This branch is based on `feat/mtproto-secured-dcpool`, so the diff below also contains #6761's commits. The changes of this PR are the last five commits: [compare](https://github.com/AndreyOsipuk/3x-ui/compare/feat/mtproto-secured-dcpool...AndreyOsipuk:3x-ui:feat/mtproto-relay). > > **mtg-multi dependencies (all open):** MHSanaei/mtg-multi#10 (`client-mss`) for this PR; #7 (secured) and #9 (WEB mode) for the stacked #6761, whose Secured and WEB settings stay behind the `mtgSecuredWebBuild` switch until the host runs a build with them. Until #10 is released, the relay fields save but have no effect. ## Problem On a Russian ISP's DPI (TSPU), mtg's FakeTLS is recognised by the ServerHello when it leaves in full-size TCP segments, and the proxy's IP:port is then banned. We hit this with a relay inside the country that forwarded the TCP stream to an mtg abroad: | Setup on the relay | Connection attempts that worked | Port banned | |---|---|---| | plain forwarding to mtg abroad (ServerHello in full-size segments) | 3 of 8 | yes | | mtg on the relay with `client-mss = 92`, `client-mss-bulk = 1400`, Telegram reached through a tunnel abroad | 8 of 8 | no | Holding a small MSS for the whole session is not an option (throughput collapses), so only the ServerHello is split. MHSanaei/mtg-multi#10 adds this as `[network] client-mss` / `client-mss-bulk`. ## Relay scenario with the panel 1. On the relay, create an MTProto inbound with the same clients as the server abroad (or new ones). 2. **Relay → ServerHello MSS** `92` (session MSS left empty = mtg's 1400). 3. **Route through Xray → Outbound**: pick the outbound that leads abroad (a VLESS/WireGuard/... outbound or a balancer). Step 3 uses the existing "Route through Xray" feature: the panel adds a loopback SOCKS inbound tagged with the inbound's tag, points mtg's `[network] proxies` at it, and puts a `inboundTag -> outboundTag` (or `balancerTag`) rule first in the routing. This PR does not change it; it only ties the two pieces together in the form and docs. ## Changes - **Settings** (`internal/mtproto/options.go`): `network.clientMss` and `network.clientMssBulk` (pointer, so an explicit `0` is kept apart from "unset"). - Validation mirrors mtg-multi: `clientMss` 0 or 48..1460; `clientMssBulk` 0 or 536..65495 and greater than `clientMss`; with `clientMssBulk = 0` (whole session at `clientMss`, set on the socket) `clientMss` must be at least 88, the kernel's minimum for `TCP_MAXSEG`. - Rendered into `[network]` only when `clientMss` is set; `client-mss-bulk` only next to it (mtg ignores it alone). Unset = nothing rendered, so existing inbounds keep a byte-identical config. - As with the other options, a bad value that reaches the settings through a raw API payload is dropped on render, and a change restarts the sidecar (the option is read at start). - **One source for client-mss.** Saving an inbound whose ServerHello MSS is set while its Extra TOML also sets `[network] client-mss` or `client-mss-bulk` fails and names the key. Either source alone still works; for raw payloads the render keeps the field value. - **Route through Xray and the IP preference** (`internal/web/service/inbound_mtproto.go`, `internal/mtproto/manager.go`): a routed inbound saved without an IP preference stores `prefer-ipv4`, and mtg gets `prefer-ipv4` for such settings from older data too. An explicit preference is kept; unrouted inbounds are unchanged. The form sets `prefer-ipv4` when routing is switched on with no preference and warns about `prefer-ipv6`/`only-ipv6` and about ad-tagged clients through the bridge; saving logs the same warnings. - **Form**: a "Relay" section (first of the collapsed sections) with both fields and a note on when they help, how to send Telegram traffic through another server, and the limits below. - **i18n**: 12 keys; en-US and ru-RU translated, English in the other locales. - **Docs**: an "MTProto relay" section in `config/inbounds` (en, ru). ## Limits of the relay egress (SOCKS bridge) Checked in the mtg-multi and Xray sources: - **IPv6.** Xray's SOCKS inbound replies "succeeded" before it dials the target (`proxy/socks`: handshake, then dispatch). mtg therefore cannot fall back to the next DC address: a connection to an IPv6 DC address through an outbound without IPv6 fails for that client. mtg's default is `prefer-ipv6`, so a routed inbound with no IP preference now gets `prefer-ipv4`, and the form warns about `prefer-ipv6`/`only-ipv6` (fine only when the outbound has IPv6). The form note and the docs say so. - **Ad-tags.** The middle-proxy handshake derives keys from mtg's own source IP and port as the middle proxy sees them. Through SOCKS mtg only knows its loopback socket, so ad-tagged clients cannot work through the bridge (not tested against live middle proxies, follows from the key schedule). Clients without an ad-tag are not affected. The form warns when a routed inbound has ad-tagged clients. - **Everything else goes through the bridge as expected**: direct DC dials, the warm DC pool, the public config updater and doppelganger fetches use the same dialer. Domain fronting does not (mtg dials it directly), which is what a relay wants. - `client-mss` is Linux only; on other systems mtg ignores it. ## Compatibility with mtg-multi builds mtg-multi decodes its config with go-toml in non-strict mode, so a build without #10 ignores the new keys. I rendered relay configs from the panel (`client-mss = 92`; `92` + `client-mss-bulk = 1400`; `120` + `client-mss-bulk = 0`, each together with the Xray bridge `proxies`) and ran `mtg-multi run` on Linux: - `mhsanaei/mtg-multi` `main`: starts with every config and ignores the keys (as a control, it also starts with an invalid `client-mss = 87` / bulk `0`). - #10: starts with every config and rejects the invalid control with `network.client-mss must be at least 88 when network.client-mss-bulk = 0`. The accepted connections report `mss:108` and `mss:1388` for `120`/`0` and `92`/`1400` (configured value minus the 12-byte timestamp option), so the setting reaches the socket. Until #10 is released the fields have no effect; the form note says that an mtg-multi build with client-mss support is needed. ## Tests - Go (`internal/mtproto`): - rendering: client-mss alone, with a bulk, with an explicit bulk `0`; bulk without client-mss renders nothing; together with the Xray bridge both land in one `[network]` table; - save-time validation of every bound and both cross-field rules, with the exact message; - invalid values are dropped on read; - both fields change the structural fingerprint and not the secrets fingerprint; - client-mss in both the fields and Extra TOML is refused on save (also through `AddInbound`), either alone is accepted; - routed inbounds: an unset preference renders and stores `prefer-ipv4`, an explicit one is kept, unrouted ones are unchanged; the IPv6 and ad-tag warnings. Each new assertion was checked to fail with the corresponding code reverted. - Frontend: schema round-trip, explicit `0` vs cleared bulk, range errors, the two cross-field errors on the right field, the Relay section and its fields, the `prefer-ipv4` default and both warnings while routing through Xray (none for a direct inbound). Run locally: `go vet ./...`, `go test ./...`, `go test -race ./internal/mtproto/`, `golangci-lint run` (0 issues), `make gen` (no diff), frontend on Node 26 `format:check`, `lint`, `typecheck`, `vitest` unit + components (1796 tests), `build`, docs `typecheck`, `lint`, `format:check`, `test`, `build`. The Storybook (Chromium) project was not run locally; no stories changed. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:48 +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#6445
No description provided.