[GH-ISSUE #5115] [Feature]: Outbound proxy for mtg #3593

Closed
opened 2026-10-31 20:33:34 +07:00 by bsakaguchi · 4 comments
Owner

Originally created by @Ld-Hagen on GitHub (Jun 9, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5115

As far as I know, the MtProto protocol isn't as resistant to DPI detection as the VPN protocols supported by 3x-ui.
So it seems like a good idea to use mtg on an internal server and route traffic across the border via VPN tunnels.
However, with the current implementation of mtg, it's not possible to specify a proxy for outgoing traffic.

Describe the solution you'd like

Add the option to select a Socks5 proxy for outbound traffic to the mtg settings. If possible, allow users to select it from existing mixed inbound connections. So all TG traffic will be routed using xray-core using outbound rules from panel.

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?

Frontend (UI / panel pages)

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 @Ld-Hagen on GitHub (Jun 9, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5115 ### Is your feature request related to a problem? As far as I know, the MtProto protocol isn't as resistant to DPI detection as the VPN protocols supported by 3x-ui. So it seems like a good idea to use mtg on an internal server and route traffic across the border via VPN tunnels. However, with the current implementation of mtg, it's not possible to specify a proxy for outgoing traffic. ### Describe the solution you'd like Add the option to select a Socks5 proxy for outbound traffic to the mtg settings. If possible, allow users to select it from existing mixed inbound connections. So all TG traffic will be routed using xray-core using outbound rules from panel. ### 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? Frontend (UI / panel pages) ### 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 - [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:33:34 +07:00
Author
Owner

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

Thanks for the well-described request. Here is exactly what would need to change to implement it.

Current state

mtg processes are launched via mtproto/manager.go. The config file written for each instance (writeConfig, line 285) currently contains only:

secret  = "..."
bind-to = "..."

[stats.prometheus]
enabled     = true
bind-to     = "127.0.0.1:<port>"
http-path   = "/metrics"
metric-prefix = "mtg"

No proxy section. The Instance struct only carries Id, Tag, Listen, Port, Secret — no proxy field. The settings JSON parsed from the inbound (InstanceFromInbound) reads only secret; likewise the frontend schema (MtprotoInboundSettingsSchema in frontend/src/schemas/protocols/inbound/mtproto.ts) only exposes fakeTlsDomain and secret.

The bundled binary is mtg v2.2.8 (9seconds/mtg). mtg v2 supports a SOCKS5 proxy via its TOML config (the exact key should be verified against the mtg v2 docs, but it is typically a [network] section with a proxies array).

What a PR would need to touch

Backend (mtproto/)

  1. Instance struct (manager.go) — add a Proxy string field (the SOCKS5 URL, e.g. socks5://user:pass@127.0.0.1:1080).
  2. writeConfig — when inst.Proxy \!= "", append the mtg v2 proxy config block to the generated TOML.
  3. InstanceFromInbound — extend the parsed struct to also decode a proxy key from ib.Settings JSON and populate Instance.Proxy.

Frontend

  1. frontend/src/schemas/protocols/inbound/mtproto.ts — add an optional proxy field (e.g. z.string().optional()).
  2. frontend/src/pages/inbounds/form/protocols/mtproto.tsx — add an input for the SOCKS5 URL. For the "select from existing mixed inbound" UX you described, the form would need to query the inbound list, filter for protocol === "mixed" entries, and construct the URL from the selected inbound's listen address, port, and credentials.

Note on the "select from mixed inbound" idea

Mixed inbounds (database/model/model.go, Protocol = "mixed") combine HTTP+SOCKS and have optional accounts (user/pass). Building the SOCKS5 URL from a selected mixed inbound is feasible but adds some complexity: the inbound might use noauth (no credentials needed) or password auth (credentials required in the URL). The simplest first step would be a free-text SOCKS5 URL input; the "pick from existing inbounds" convenience could be added on top.

If you or someone else plans to open a PR, the files to start with are mtproto/manager.go (backend config generation) and frontend/src/schemas/protocols/inbound/mtproto.ts + frontend/src/pages/inbounds/form/protocols/mtproto.tsx (frontend schema and form).

<!-- gh-comment-id:4657399711 --> @github-actions[bot] commented on GitHub (Jun 9, 2026): Thanks for the well-described request. Here is exactly what would need to change to implement it. ## Current state mtg processes are launched via `mtproto/manager.go`. The config file written for each instance (`writeConfig`, line 285) currently contains only: ```toml secret = "..." bind-to = "..." [stats.prometheus] enabled = true bind-to = "127.0.0.1:<port>" http-path = "/metrics" metric-prefix = "mtg" ``` No proxy section. The `Instance` struct only carries `Id`, `Tag`, `Listen`, `Port`, `Secret` — no proxy field. The settings JSON parsed from the inbound (`InstanceFromInbound`) reads only `secret`; likewise the frontend schema (`MtprotoInboundSettingsSchema` in `frontend/src/schemas/protocols/inbound/mtproto.ts`) only exposes `fakeTlsDomain` and `secret`. The bundled binary is **mtg v2.2.8** (`9seconds/mtg`). mtg v2 supports a SOCKS5 proxy via its TOML config (the exact key should be verified against the mtg v2 docs, but it is typically a `[network]` section with a `proxies` array). ## What a PR would need to touch ### Backend (`mtproto/`) 1. **`Instance` struct** (`manager.go`) — add a `Proxy string` field (the SOCKS5 URL, e.g. `socks5://user:pass@127.0.0.1:1080`). 2. **`writeConfig`** — when `inst.Proxy \!= ""`, append the mtg v2 proxy config block to the generated TOML. 3. **`InstanceFromInbound`** — extend the `parsed` struct to also decode a `proxy` key from `ib.Settings` JSON and populate `Instance.Proxy`. ### Frontend 1. **`frontend/src/schemas/protocols/inbound/mtproto.ts`** — add an optional `proxy` field (e.g. `z.string().optional()`). 2. **`frontend/src/pages/inbounds/form/protocols/mtproto.tsx`** — add an input for the SOCKS5 URL. For the "select from existing mixed inbound" UX you described, the form would need to query the inbound list, filter for `protocol === "mixed"` entries, and construct the URL from the selected inbound's listen address, port, and credentials. ## Note on the "select from mixed inbound" idea `Mixed` inbounds (`database/model/model.go`, `Protocol = "mixed"`) combine HTTP+SOCKS and have optional `accounts` (user/pass). Building the SOCKS5 URL from a selected mixed inbound is feasible but adds some complexity: the inbound might use `noauth` (no credentials needed) or `password` auth (credentials required in the URL). The simplest first step would be a free-text SOCKS5 URL input; the "pick from existing inbounds" convenience could be added on top. If you or someone else plans to open a PR, the files to start with are `mtproto/manager.go` (backend config generation) and `frontend/src/schemas/protocols/inbound/mtproto.ts` + `frontend/src/pages/inbounds/form/protocols/mtproto.tsx` (frontend schema and form).
Author
Owner

@JohniGo76 commented on GitHub (Jun 9, 2026):

I tried using Mixed Inbound with UDP support as a proxy for the Telegram client. Text messages get through, but voice calls don't. HTTP Proxy Inbound works similarly. As outbound for cascade proxy I tried VLES+RAW and Hysteria.
Therefore, I don't think routing MTProxy to local Socks (Mixed) will help.

<!-- gh-comment-id:4658120692 --> @JohniGo76 commented on GitHub (Jun 9, 2026): I tried using Mixed Inbound with UDP support as a proxy for the Telegram client. Text messages get through, but voice calls don't. HTTP Proxy Inbound works similarly. As outbound for cascade proxy I tried VLES+RAW and Hysteria. Therefore, I don't think routing MTProxy to local Socks (Mixed) will help.
Author
Owner

@deepru commented on GitHub (Jun 9, 2026):

This request would also be very useful for a reverse-proxy / Double-Hop MTProto setup.

I opened a related feature request here: #5126

That issue covers the inbound side of the deployment:

User Telegram client
-> public HAProxy :443
-> Entry MTG on 127.0.0.1:<internal_mtg_port>

This issue covers the outbound side:

Entry MTG
-> local SOCKS5 / mixed inbound on 127.0.0.1:<local_proxy_port>
-> Xray outbound / routing
-> Exit server
-> Telegram DC

So together, #5126 + this request would allow a clean managed MTG setup like:

User
-> Entry HAProxy :443
-> Entry MTG loopback
-> Entry local Xray SOCKS5/mixed inbound
-> Entry Xray outbound to Exit
-> Exit server
-> Telegram

This is useful when the Entry server is the public connection point, but the actual Telegram egress should go through another server or another Xray route.

I agree that SOCKS5 is probably the most practical first step, because mtg already supports proxy chaining and many 3x-ui/Xray deployments can expose a local SOCKS5 or mixed inbound on 127.0.0.1.

For 3x-ui, the useful MTProto settings could be something like:

{
  "outboundProxy": {
    "enabled": true,
    "type": "socks5",
    "host": "127.0.0.1",
    "port": 10808,
    "username": "",
    "password": ""
  }
}

Expected generated MTG config would include the corresponding proxy/chaining section supported by mtg.

Important behavior:

  • default should remain unchanged when outbound proxy is disabled;
  • the outbound proxy setting should be stored in MTProto settings JSON;
  • config-affecting fields should be included in Instance.fingerprint() so the MTG sidecar restarts when proxy settings change;
  • the proxy target should support at least loopback SOCKS5;
  • later it could optionally integrate with existing 3x-ui inbound/outbound objects, but a simple explicit host/port SOCKS5 setting would already solve the main use case.

This is not the same as #5126, but the two features complement each other:

  • #5126: make MTG safe and usable behind HAProxy / reverse proxy on the inbound side;
  • #5115: allow MTG outgoing Telegram traffic to be routed through Xray / another proxy / another server.
<!-- gh-comment-id:4658714768 --> @deepru commented on GitHub (Jun 9, 2026): This request would also be very useful for a reverse-proxy / Double-Hop MTProto setup. I opened a related feature request here: #5126 That issue covers the inbound side of the deployment: ```text User Telegram client -> public HAProxy :443 -> Entry MTG on 127.0.0.1:<internal_mtg_port> ``` This issue covers the outbound side: ```text Entry MTG -> local SOCKS5 / mixed inbound on 127.0.0.1:<local_proxy_port> -> Xray outbound / routing -> Exit server -> Telegram DC ``` So together, #5126 + this request would allow a clean managed MTG setup like: ```text User -> Entry HAProxy :443 -> Entry MTG loopback -> Entry local Xray SOCKS5/mixed inbound -> Entry Xray outbound to Exit -> Exit server -> Telegram ``` This is useful when the Entry server is the public connection point, but the actual Telegram egress should go through another server or another Xray route. I agree that SOCKS5 is probably the most practical first step, because `mtg` already supports proxy chaining and many 3x-ui/Xray deployments can expose a local SOCKS5 or mixed inbound on `127.0.0.1`. For 3x-ui, the useful MTProto settings could be something like: ```json { "outboundProxy": { "enabled": true, "type": "socks5", "host": "127.0.0.1", "port": 10808, "username": "", "password": "" } } ``` Expected generated MTG config would include the corresponding proxy/chaining section supported by `mtg`. Important behavior: * default should remain unchanged when outbound proxy is disabled; * the outbound proxy setting should be stored in MTProto settings JSON; * config-affecting fields should be included in `Instance.fingerprint()` so the MTG sidecar restarts when proxy settings change; * the proxy target should support at least loopback SOCKS5; * later it could optionally integrate with existing 3x-ui inbound/outbound objects, but a simple explicit host/port SOCKS5 setting would already solve the main use case. This is not the same as #5126, but the two features complement each other: * #5126: make MTG safe and usable behind HAProxy / reverse proxy on the inbound side; * #5115: allow MTG outgoing Telegram traffic to be routed through Xray / another proxy / another server.
Author
Owner

@MHSanaei commented on GitHub (Jul 3, 2026):

This has been implemented since the issue was opened. Edit (or create) an MTProto inbound and enable Route through Xray: the panel injects a loopback SOCKS5 bridge into the Xray config and points the mtg sidecar at it, so all Telegram egress follows your Xray routing rules — and you can pick a specific outbound tag for that traffic in the same form.

This covers the requested SOCKS5 chaining without manually maintaining a mixed inbound. Closing as implemented — please open a new issue if your HAProxy scenario needs something the current toggle doesn't cover.

<!-- gh-comment-id:4873840792 --> @MHSanaei commented on GitHub (Jul 3, 2026): This has been implemented since the issue was opened. Edit (or create) an MTProto inbound and enable **Route through Xray**: the panel injects a loopback SOCKS5 bridge into the Xray config and points the mtg sidecar at it, so all Telegram egress follows your Xray routing rules — and you can pick a specific outbound tag for that traffic in the same form. This covers the requested SOCKS5 chaining without manually maintaining a mixed inbound. Closing as implemented — please open a new issue if your HAProxy scenario needs something the current toggle doesn't cover.
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#3593
No description provided.