mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #5126] [Feature]: Expose MTG reverse-proxy / HAProxy options for MTProto inbounds #3604
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#3604
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @deepru on GitHub (Jun 9, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5126
Is your feature request related to a problem?
Yes. 3x-ui v3.3.0 added MTProto FakeTLS support through a managed
mtgsidecar, and the basic lifecycle works well. However, the current MTG integration is difficult to use safely behind HAProxy / reverse proxy / ELB setups where the public port is443and the MTG inbound itself listens only on loopback.The current generated MTG config is minimal. In my runtime test, 3x-ui created this config for a loopback MTProto inbound:
This works for the sidecar itself, but it does not expose some existing MTG options that are needed for reverse-proxy deployments.
The main problem is domain-fronting fallback behavior. When MTG receives a non-Telegram / stale / HTTPS-like request, it routes the request to the FakeTLS domain from the secret. In a reverse-proxy setup, that domain often points back to the same public
:443listener.For example:
I tested this directly with a 3x-ui-created MTProto inbound using:
Then I sent a direct HTTPS-like request to MTG with SNI set to the FakeTLS domain:
The request returned an nginx
200 OK, and the MTG metricmtg_domain_frontingincreased from0to1. This confirms that MTG fronted the request to the public FakeTLS domain. This is expected MTG behavior, but without a configurable local domain-fronting target it is unsafe for a clean HAProxy/ELB mode.There is also a share-link problem.
mtg accessgenerated a link using the public IP and the internal MTG port:For a reverse-proxy setup, the correct user-facing link should be:
So 3x-ui needs a way to override the public host and public port used for MTProto share links.
Finally, MTG supports PROXY protocol, which is useful when HAProxy / ELB passes the real client IP to the backend. 3x-ui currently does not expose a
proxy-protocol-listeneroption for MTProto inbounds.Describe the solution you'd like
Please add a reverse-proxy / HAProxy / ELB mode for MTProto inbounds, or expose the needed MTG options in the MTProto inbound settings.
The requested options are:
1. Domain-fronting target override
Add fields to the MTProto inbound configuration, for example:
Expected generated MTG config shape could be similar to:
The exact key names can be different if you prefer another schema. The important part is that rejected / stale / HTTPS-like traffic can be forwarded to a local fallback backend instead of always going to
fakeTlsDomain:443.2. Public share-link host/port override
Add MTProto inbound fields such as:
or reuse the existing external proxy/share-link model if possible.
Expected behavior:
This is needed because in a reverse-proxy setup the internal MTG bind port is not publicly reachable and should not be shown to users.
3. PROXY protocol listener option
Expose MTG's PROXY protocol listener option for MTProto inbounds, for example:
This is useful when HAProxy / ELB forwards real client IP information to MTG.
4. Suggested UI placement
On the MTProto inbound edit/create page, add an optional advanced section, for example:
Defaults should preserve the current behavior for simple non-reverse-proxy deployments.
5. API behavior
The same options should be available through
/panel/api/inbounds/addand/panel/api/inbounds/update, so external automation can create MTProto inbounds safely.6. Expected edge-case behavior
listen=127.0.0.1, generated public links should not automatically use127.0.0.1or the internal port when a public override is configured.Alternatives you've considered
1. Keep MTG exposed directly on a public port.
This works for simple deployments, but it does not fit setups where all public TLS traffic is multiplexed on
443through HAProxy or another reverse proxy.2. Manually patch
/usr/local/x-ui/bin/mtproto/mtg-<id>.toml.This is fragile because 3x-ui owns and regenerates the MTG config. It also breaks the panel as the source of truth.
3. Use another standalone MTProto implementation outside 3x-ui.
This works, but it defeats the purpose of adding managed MTProto support to 3x-ui. The new v3.3.0 MTG sidecar lifecycle works well, so it would be better to expose the missing MTG options in 3x-ui instead of maintaining a parallel service.
4. Use outbound proxy support for MTG.
Outbound proxy support is useful for another scenario, but it does not solve this reverse-proxy problem. This request is about inbound HAProxy / ELB mode, domain-fronting fallback target override, generated public share links, and optionally PROXY protocol from the load balancer to MTG.
Concrete use case
A server runs several services behind one public TLS port:
The desired MTProto deployment is:
This allows MTProto FakeTLS to run cleanly behind HAProxy without opening an additional public port and without exposing the internal MTG port to users.
I tested this on a clean Ubuntu 24.04 VPS with 3x-ui v3.3.0:
listen=127.0.0.1port=8689protocol=mtprotofakeTlsDomain=tg2.example.comObserved result:
Generated MTG config:
The MTG lifecycle worked correctly:
Deleting the inbound through the API removed the DB row, stopped the MTG sidecar, removed the generated MTG config, and removed the loopback listeners.
The missing part is not MTG lifecycle stability. The missing part is exposing the MTG options needed for reverse-proxy / HAProxy / ELB mode.
Mockups, screenshots, or examples
Example desired generated MTG config:
Example desired share-link behavior:
Example UI section:
Related but not duplicate:
Which parts of the panel would this affect?
Backend (API endpoints, login, settings)
Are you willing to help?
Before submitting
@github-actions[bot] commented on GitHub (Jun 9, 2026):
Thanks for the detailed report — the use case is clear and the code analysis below confirms none of the three requested options exist yet.
What the code currently does
MTG config generation (
mtproto/manager.go,writeConfig, line 285–292):The generated TOML is always exactly:
No
[domain-fronting]section and noproxy-protocol-listenerkey are ever written. TheInstancestruct (lines 20–26) only carriesId,Tag,Listen,Port, andSecret— no field for any of the three requested options.Share-link generation (
sub/subService.go,genMtprotoLink, lines 383–406):resolveInboundAddressskips loopback/wildcard addresses and falls back to the subscriber's request hostname — so alisten=127.0.0.1inbound will use the subscriber host, not127.0.0.1. But there is no override mechanism: you cannot forceserver=tg2.example.comandport=443from the inbound config today.MTProto settings schema (
frontend/src/schemas/protocols/inbound/mtproto.ts):Only
fakeTlsDomainandsecretare stored. TheexternalProxy/streamSettingsmechanism that other protocols use does not apply to MTProto — it is not an Xray protocol and has nostreamSettings.What would need to change for each requested option
1. Domain-fronting fallback target override
domainFronting: { enabled, host, port }) toMtprotoInboundSettingsin the frontend schema and inInstanceFromInbound/Instanceinmtproto/manager.go.writeConfigand emit the[domain-fronting]TOML block conditionally.frontend/src/pages/inbounds/form/protocols/mtproto.tsx.2. Public share-link host/port override
publicHost,publicPort) toMtprotoInboundSettings.genMtprotoLinkinsub/subService.goto use those fields when set, falling back to the currentresolveInboundAddress/inbound.Portlogic.mtproto.tsx.3. PROXY protocol listener
proxyProtocolListener) toMtprotoInboundSettingsandInstance.proxy-protocol-listener = trueinwriteConfigwhen set.Instance.fingerprint()so a config change triggers a restart.Notes
Instance.fingerprint()method (line 36–38) drives restart detection. Any new config option that should trigger a sidecar restart must be included in that fingerprint.streamSettings— the existingexternalProxymechanism (used for VLESS/VMess) cannot be reused here. A dedicated fields approach insettingsJSON is the right path.settingsJSON column, so API support (point 5 in the request) would come for free once the backend and model are updated.This is a well-scoped feature request with no duplicate. PRs are welcome.
@deepru commented on GitHub (Jun 9, 2026):
Thanks for checking the code paths and confirming the scope.
Your summary matches exactly what I observed in runtime tests. I agree that a dedicated fields approach in the MTProto settings JSON is the cleanest path, since MTProto is managed by
mtgand does not use XraystreamSettings.The proposed implementation plan also looks right to me:
InstanceFromInbound/Instance;writeConfig;Instance.fingerprint();publicHost/publicPortingenMtprotoLinkwhen set;I can help test a PR in a real HAProxy / loopback MTG setup once available. I may also try to prepare a small PR if I have enough time.
@MHSanaei commented on GitHub (Jul 3, 2026):
Status update — two of the three requested knobs landed shortly after this was filed (commit
6c159469): MTProto inbounds now expose domain-fronting overrides (ip/port/proxy-protocol) and a PROXY-protocol listener toggle in the inbound form, so the HAProxy/loopback deployment works.The remaining gap is (2): the generated
tg://proxylink still uses the inbound's own address/port rather than a configurable public host/port. Keeping this open for that part — the likely shape is optionalpublicHost/publicPortfields in the MTProto settings preferred by both link generators.@deepru commented on GitHub (Jul 12, 2026):
Still reproduces on v3.5.0 (now with the multi-client MTProto model), specifically the share-link port exposure part. Adding the exact code location and a concrete fix.
Root cause —
genMtprotoLink(internal/sub/service.go) takes the address fromshareAddrbut hardcodes the port to the inbound's listen port:Why VLESS/VMess don't have this: their links honor
streamSettings.externalProxy[].port; MTProto has nostreamSettings/externalProxy, so no equivalent lever.Minimal fix: let MTProto carry a public share port (a
sharePortfield, orshareAddrashost:port) and use it ingenMtprotoLinkinstead ofinbound.Port. (The domain-fronting-loop and PROXY-protocol items in this issue are separate.)Scope note: pure link generation in 3x-ui; the mtg/mtg-multi backend is not involved.
@SiegfriedSchmidt commented on GitHub (Aug 2, 2026):
problem still persists in v3.6.0