[PR #5107] [MERGED] fix(mtproto): reap orphaned mtg, fix SysLog viewer, mtg log visibility, export remark (#5105) #5785

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5107
Author: @MHSanaei
Created: 6/9/2026
Status: ✅ Merged
Merged: 6/9/2026
Merged by: @MHSanaei

Base: main ← Head: fix/mtproto-log-visibility


📝 Commits (7)

  • 549428d fix(logs): render journalctl output in the SysLog viewer
  • 216ab53 feat(mtproto): surface mtg output and add status reporting
  • 981e9ed fix(logs): parse all journalctl message shapes in SysLog viewer
  • 723933f fix(mtproto): reap orphaned mtg sidecars so a stale one can't break new clients
  • 421f18a fix(logs): drop dead body initializer flagged by eslint no-useless-assignment
  • 3fffd4d fix(mtproto): drop remark fragment from tg://proxy export link
  • fc0365a fix(x-ui.sh): remove unused check_mtproto_status helper

📊 Changes

13 files changed (+397 additions, -71 deletions)

View changed files

📝 frontend/src/lib/xray/inbound-link.ts (+2 -5)
📝 frontend/src/pages/index/LogModal.tsx (+1 -44)
➕ frontend/src/pages/index/logParse.ts (+113 -0)
➕ frontend/src/test/log-parse.test.ts (+80 -0)
📝 install.sh (+5 -0)
📝 mtproto/manager.go (+23 -4)
➕ mtproto/orphans_linux.go (+79 -0)
➕ mtproto/orphans_other.go (+9 -0)
📝 mtproto/process.go (+46 -12)
📝 mtproto/process_windows.go (+3 -3)
📝 sub/subService.go (+2 -3)
📝 update.sh (+5 -0)
📝 x-ui.sh (+29 -0)

📄 Description

Summary

Started from a report that mtproto works on Windows but not on a Linux server (traffic counted a few MB but Telegram never connected), and grew into fixing the root cause plus the log tooling that made it hard to diagnose. Also folds in a related mtproto export-link bug (#5105).

Root cause — orphaned mtg sidecars (the real bug)

mtproto inbounds run as standalone mtg processes, one per inbound, outside Xray. On Linux x-ui does not kill its mtg children when it dies (no kill-on-exit, unlike the Windows job object). After a crash / kill -9 / update / port change, a stale mtg keeps holding the inbound port with an old secret, so new clients fail the FakeTLS handshake and get silently domain-fronted to the FakeTLS domain instead of proxied to Telegram — a few MB of traffic, never connects. It worked on Windows precisely because Windows does kill the children.

Fix: on the first reconcile at startup — before x-ui starts any of its own mtg — scan /proc and SIGKILL any process whose executable is our mtg-<goos>-<goarch> binary. x-ui is the sole owner of mtg, so anything alive then is an orphan. Runs once per process (swept guard), survives the binary-deleted-during-update case via /proc/<pid>/cmdline, and is a no-op on Windows/other platforms. update.sh/install.sh also clear stray mtg after stopping x-ui, anchored to the mtg-linux-<arch> run invocation so the pattern cannot match unrelated command lines.

The mtproto export link appended the remark as a URL fragment (tg://proxy?server=...&port=...&secret=...#remark). Telegram Desktop rejects a proxy deep link with a trailing fragment as "This proxy link is invalid", breaking one-click import, and a remark is meaningless for proxy links across clients. Removed in both the panel link (genMtprotoLink) and the subscription service.

Log tooling fixes (made the orphan diagnosable)

  • SysLog viewer was showing only timestamps. The parser only understood the app-log format, but journalctl returns four different message shapes (go-logging LEVEL - msg, Go std-log with embedded date, telego [ts] LEVEL msg, systemd). Rewrote it into a pure, tested module that handles all four. Added a unit test from real captured lines.
  • mtg output was invisible. Its stdout/stderr was captured by a writer that kept only the last line and showed it nowhere. Now streamed line-by-line into the x-ui log, tagged per inbound, so mtg's own messages appear in the panel viewer and journald.
  • Mangled log line inbound3on0.0.0.0:8443 (a fmt.Sprint spacing bug) fixed; affected mtproto log calls switched to the formatted variants.
  • x-ui status now reports each mtproto inbound's mtg process state.

Commits

  • 549428d3 fix(logs): render journalctl output in the SysLog viewer
  • 216ab53f feat(mtproto): surface mtg output and add status reporting
  • 981e9ed9 fix(logs): parse all journalctl message shapes in SysLog viewer
  • 723933f4 fix(mtproto): reap orphaned mtg sidecars
  • 421f18a3 fix(logs): drop dead initializer flagged by eslint
  • 3fffd4d4 fix(mtproto): drop remark fragment from tg://proxy export link (#5105)

Testing

  • End-to-end in a clean Linux (Alpine) Docker build of this branch: mtproto inbound created via API, mtg started/bound, FakeTLS handshake + all four Telegram DCs reachable, valid share link generated.
  • go build (linux + windows), go vet, go test ./mtproto/... ./sub/... pass; frontend tsc, eslint, and 462 vitest tests pass; both shell scripts bash -n clean.
  • The process-killing logic was put through an adversarial multi-lens review; the confirmed pattern-breadth issue is fixed in this PR.

Known follow-up (not in this PR)

The startup sweep matches on binary basename. On a host running two x-ui instances that share the binary name, instance A's sweep could kill instance B's live mtg. A follow-up can add a per-instance config-path ownership check before killing. Single-instance deployments (the norm) are unaffected.


🔄 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/5107 **Author:** [@MHSanaei](https://github.com/MHSanaei) **Created:** 6/9/2026 **Status:** ✅ Merged **Merged:** 6/9/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/mtproto-log-visibility` --- ### 📝 Commits (7) - [`549428d`](https://github.com/MHSanaei/3x-ui/commit/549428d3c9a7e0f58d2c4633fe969901317a7f30) fix(logs): render journalctl output in the SysLog viewer - [`216ab53`](https://github.com/MHSanaei/3x-ui/commit/216ab53f7b9fff06cc7bc1e2197c912fd12f50ca) feat(mtproto): surface mtg output and add status reporting - [`981e9ed`](https://github.com/MHSanaei/3x-ui/commit/981e9ed9b1fae7c74a62e6b94f6d7b26474d0bf6) fix(logs): parse all journalctl message shapes in SysLog viewer - [`723933f`](https://github.com/MHSanaei/3x-ui/commit/723933f451655a02f0ce0d64e02d2a2d5f69e77d) fix(mtproto): reap orphaned mtg sidecars so a stale one can't break new clients - [`421f18a`](https://github.com/MHSanaei/3x-ui/commit/421f18a3da087b715838d3f0a064ee1e271e43c3) fix(logs): drop dead body initializer flagged by eslint no-useless-assignment - [`3fffd4d`](https://github.com/MHSanaei/3x-ui/commit/3fffd4d4dedb8ab46826fb53817e0d52cddd2b29) fix(mtproto): drop remark fragment from tg://proxy export link - [`fc0365a`](https://github.com/MHSanaei/3x-ui/commit/fc0365a23a1c5bdbb35a6c5e5d8440a9f40ba5a4) fix(x-ui.sh): remove unused check_mtproto_status helper ### 📊 Changes **13 files changed** (+397 additions, -71 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/lib/xray/inbound-link.ts` (+2 -5) 📝 `frontend/src/pages/index/LogModal.tsx` (+1 -44) ➕ `frontend/src/pages/index/logParse.ts` (+113 -0) ➕ `frontend/src/test/log-parse.test.ts` (+80 -0) 📝 `install.sh` (+5 -0) 📝 `mtproto/manager.go` (+23 -4) ➕ `mtproto/orphans_linux.go` (+79 -0) ➕ `mtproto/orphans_other.go` (+9 -0) 📝 `mtproto/process.go` (+46 -12) 📝 `mtproto/process_windows.go` (+3 -3) 📝 `sub/subService.go` (+2 -3) 📝 `update.sh` (+5 -0) 📝 `x-ui.sh` (+29 -0) </details> ### 📄 Description ## Summary Started from a report that **mtproto works on Windows but not on a Linux server** (traffic counted a few MB but Telegram never connected), and grew into fixing the root cause plus the log tooling that made it hard to diagnose. Also folds in a related mtproto export-link bug (#5105). ### Root cause — orphaned `mtg` sidecars (the real bug) mtproto inbounds run as standalone `mtg` processes, one per inbound, outside Xray. On Linux x-ui does **not** kill its `mtg` children when it dies (no kill-on-exit, unlike the Windows job object). After a crash / `kill -9` / update / port change, a **stale `mtg` keeps holding the inbound port with an old secret**, so new clients fail the FakeTLS handshake and get silently **domain-fronted** to the FakeTLS domain instead of proxied to Telegram — a few MB of traffic, never connects. It worked on Windows precisely because Windows *does* kill the children. **Fix:** on the first reconcile at startup — before x-ui starts any of its own `mtg` — scan `/proc` and `SIGKILL` any process whose executable is our `mtg-<goos>-<goarch>` binary. x-ui is the sole owner of `mtg`, so anything alive then is an orphan. Runs once per process (`swept` guard), survives the binary-deleted-during-update case via `/proc/<pid>/cmdline`, and is a no-op on Windows/other platforms. `update.sh`/`install.sh` also clear stray `mtg` after stopping x-ui, anchored to the `mtg-linux-<arch> run ` invocation so the pattern cannot match unrelated command lines. ### MTProto export link — drop the remark fragment (fixes #5105) The mtproto export link appended the remark as a URL fragment (`tg://proxy?server=...&port=...&secret=...#remark`). Telegram Desktop rejects a proxy deep link with a trailing fragment as "This proxy link is invalid", breaking one-click import, and a remark is meaningless for proxy links across clients. Removed in both the panel link (`genMtprotoLink`) and the subscription service. ### Log tooling fixes (made the orphan diagnosable) - **SysLog viewer was showing only timestamps.** The parser only understood the app-log format, but `journalctl` returns four different message shapes (go-logging `LEVEL - msg`, Go std-log with embedded date, telego `[ts] LEVEL msg`, systemd). Rewrote it into a pure, tested module that handles all four. Added a unit test from real captured lines. - **mtg output was invisible.** Its stdout/stderr was captured by a writer that kept only the last line and showed it nowhere. Now streamed line-by-line into the x-ui log, tagged per inbound, so mtg's own messages appear in the panel viewer and journald. - **Mangled log line** `inbound3on0.0.0.0:8443` (a `fmt.Sprint` spacing bug) fixed; affected mtproto log calls switched to the formatted variants. - `x-ui status` now reports each mtproto inbound's `mtg` process state. ## Commits - `549428d3` fix(logs): render journalctl output in the SysLog viewer - `216ab53f` feat(mtproto): surface mtg output and add status reporting - `981e9ed9` fix(logs): parse all journalctl message shapes in SysLog viewer - `723933f4` fix(mtproto): reap orphaned mtg sidecars - `421f18a3` fix(logs): drop dead initializer flagged by eslint - `3fffd4d4` fix(mtproto): drop remark fragment from tg://proxy export link (#5105) ## Testing - End-to-end in a clean Linux (Alpine) Docker build of this branch: mtproto inbound created via API, `mtg` started/bound, FakeTLS handshake + all four Telegram DCs reachable, valid share link generated. - `go build` (linux + windows), `go vet`, `go test ./mtproto/... ./sub/...` pass; frontend `tsc`, `eslint`, and 462 vitest tests pass; both shell scripts `bash -n` clean. - The process-killing logic was put through an adversarial multi-lens review; the confirmed pattern-breadth issue is fixed in this PR. ## Known follow-up (not in this PR) The startup sweep matches on binary basename. On a host running **two x-ui instances that share the binary name**, instance A's sweep could kill instance B's live `mtg`. A follow-up can add a per-instance config-path ownership check before killing. Single-instance deployments (the norm) are unaffected. --- <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:03 +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#5785
No description provided.