[GH-ISSUE #6744] Telegram bot bakes in panel-egress SOCKS proxy URL once at startup, racing Xray's own (re)start #4494

Open
opened 2026-10-31 20:40:02 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @ARBlazeAG on GitHub (Oct 5, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6744

Описание / Description

Tgbot.Start() resolves the panel egress SOCKS bridge (PanelEgressProxyURL()) once, at bot startup, and bakes the resulting socks5://127.0.0.1:<port> URL into the long-lived Telegram API client (telego):

https://github.com/MHSanaei/3x-ui/blob/main/internal/web/service/tgbot/tgbot.go#L340-L352

tgBotProxy, err := t.settingService.GetTgBotProxy()
...
// Fall back to the panel-wide egress bridge when no dedicated bot proxy is
// set. Resolved once at bot start: if Xray comes up later, the bot keeps
// its direct connection until it is restarted.
if tgBotProxy == "" {
    if egress := t.settingService.PanelEgressProxyURL(); egress != "" && isSupportedBotProxyScheme(egress) {
        tgBotProxy = egress
    }
}

On every x-ui process start, Tgbot.Start() ("Telegram bot receiver started") runs a couple of seconds before Xray has actually finished (re)binding the dynamically-injected panel-egress SOCKS inbound (internal/web/service/xray.go, injectPanelEgress, base port 62790). If PanelEgressProxyURL() is called in that narrow window, two outcomes are both broken in slightly different ways:

  1. proc.IsRunning() is true and GetConfig() already contains the panel-egress inbound entry (the Go-side struct was updated before the actual OS-level listener came up) → the bot bakes in a port that isn't actually accepting connections yet → immediate connect: connection refused on setMyCommands/getUpdates.
  2. Even once that specific race closes and the listener comes up, the bot never retries resolution — the comment admits this outright ("the bot keeps its direct connection until it is restarted"). In our case outcome (1) hit instead, but either direction is a real, reproducible one-shot-resolution bug.

Contrast with discord/gateway.go, which stores the resolver itself rather than a resolved value and calls it fresh on every connection attempt:

https://github.com/MHSanaei/3x-ui/blob/main/internal/web/service/discord/gateway.go#L101
https://github.com/MHSanaei/3x-ui/blob/main/internal/web/service/discord/gateway.go#L226

egressProxyURL func() string
...
if raw := g.egressProxyURL(); raw != "" { ... }

Discord's Gateway client self-heals on its own reconnect loop (we see it log panel outbound [ direct ] is set but Xray is not running, using a direct connection and recover automatically within seconds). The Telegram bot has no equivalent reconnect-driven re-resolution path, so a bad snapshot taken at the wrong instant is permanent until someone manually restarts the bot.

Repro

Reliably hit on every x-ui restart when a panelOutbound is configured (even just direct, since injectPanelEgress only checks that the target tag resolves, not that it does anything useful):

Telegram bot receiver started
ERROR Execution error setMyCommands: ... socks connect tcp4 127.0.0.1:62790->api.telegram.org:443: ... connect: connection refused
ERROR Execution error getUpdates: ... connect: connection refused

followed shortly after by a log line proving Xray itself was mid-restart at that exact moment:

WARNING - panel outbound [ direct ] is set but Xray is not running, using a direct connection

x-ui dev channel, confirmed on dev+49fbcdc0, bundled Xray-core pinned at 26.6.27 (unrelated to the bug — same race happens before the version pin even runs).

Suggested fix

Give Tgbot the same shape GatewayClient already uses: store PanelEgressProxyURL as a func() string and call it right before each NewBot/API-client (re)construction inside the bot's own reconnect/retry loop, instead of resolving once in Start(). Alternatively, hook bot restart into whatever event already fires when Xray finishes a (re)start, so the bot picks up a fresh egress URL without a full x-ui restart.

Environment

  • x-ui: dev channel
  • Xray-core: pinned 26.6.27 (via -getApiToken-based external pin tooling, unrelated to this bug)
  • OS: Ubuntu, systemd-managed x-ui.service
Originally created by @ARBlazeAG on GitHub (Oct 5, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6744 ### Описание / Description `Tgbot.Start()` resolves the panel egress SOCKS bridge (`PanelEgressProxyURL()`) **once**, at bot startup, and bakes the resulting `socks5://127.0.0.1:<port>` URL into the long-lived Telegram API client (`telego`): https://github.com/MHSanaei/3x-ui/blob/main/internal/web/service/tgbot/tgbot.go#L340-L352 ```go tgBotProxy, err := t.settingService.GetTgBotProxy() ... // Fall back to the panel-wide egress bridge when no dedicated bot proxy is // set. Resolved once at bot start: if Xray comes up later, the bot keeps // its direct connection until it is restarted. if tgBotProxy == "" { if egress := t.settingService.PanelEgressProxyURL(); egress != "" && isSupportedBotProxyScheme(egress) { tgBotProxy = egress } } ``` On every x-ui process start, `Tgbot.Start()` ("Telegram bot receiver started") runs a couple of seconds **before** Xray has actually finished (re)binding the dynamically-injected `panel-egress` SOCKS inbound (`internal/web/service/xray.go`, `injectPanelEgress`, base port 62790). If `PanelEgressProxyURL()` is called in that narrow window, two outcomes are both broken in slightly different ways: 1. `proc.IsRunning()` is true and `GetConfig()` already contains the `panel-egress` inbound entry (the Go-side struct was updated before the actual OS-level listener came up) → the bot bakes in a port that isn't actually accepting connections yet → immediate `connect: connection refused` on `setMyCommands`/`getUpdates`. 2. Even once that specific race closes and the listener comes up, the bot never retries resolution — the comment admits this outright ("the bot keeps its direct connection until it is restarted"). In our case outcome (1) hit instead, but either direction is a real, reproducible one-shot-resolution bug. Contrast with `discord/gateway.go`, which stores the resolver itself rather than a resolved value and calls it fresh on every connection attempt: https://github.com/MHSanaei/3x-ui/blob/main/internal/web/service/discord/gateway.go#L101 https://github.com/MHSanaei/3x-ui/blob/main/internal/web/service/discord/gateway.go#L226 ```go egressProxyURL func() string ... if raw := g.egressProxyURL(); raw != "" { ... } ``` Discord's Gateway client self-heals on its own reconnect loop (we see it log `panel outbound [ direct ] is set but Xray is not running, using a direct connection` and recover automatically within seconds). The Telegram bot has no equivalent reconnect-driven re-resolution path, so a bad snapshot taken at the wrong instant is permanent until someone manually restarts the bot. ### Repro Reliably hit on every x-ui restart when a `panelOutbound` is configured (even just `direct`, since `injectPanelEgress` only checks that the target tag *resolves*, not that it does anything useful): ``` Telegram bot receiver started ERROR Execution error setMyCommands: ... socks connect tcp4 127.0.0.1:62790->api.telegram.org:443: ... connect: connection refused ERROR Execution error getUpdates: ... connect: connection refused ``` followed shortly after by a log line proving Xray itself was mid-restart at that exact moment: ``` WARNING - panel outbound [ direct ] is set but Xray is not running, using a direct connection ``` x-ui dev channel, confirmed on `dev+49fbcdc0`, bundled Xray-core pinned at 26.6.27 (unrelated to the bug — same race happens before the version pin even runs). ### Suggested fix Give `Tgbot` the same shape `GatewayClient` already uses: store `PanelEgressProxyURL` as a `func() string` and call it right before each `NewBot`/API-client (re)construction inside the bot's own reconnect/retry loop, instead of resolving once in `Start()`. Alternatively, hook bot restart into whatever event already fires when Xray finishes a (re)start, so the bot picks up a fresh egress URL without a full x-ui restart. ### Environment - x-ui: dev channel - Xray-core: pinned 26.6.27 (via `-getApiToken`-based external pin tooling, unrelated to this bug) - OS: Ubuntu, systemd-managed `x-ui.service`
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#4494
No description provided.