[PR #5838] [MERGED] fix(mtproto): stop dropping connections on client/inbound edits; add live updates + ad-tag #6003

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

📋 Pull Request Information

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

Base: main ← Head: feat/mtproto-live-updates


📝 Commits (10+)

  • 2ea824a fix(mtproto): split the mtg fingerprint into structural and secrets parts
  • bdabba7 fix(mtproto): stop restarting mtg on every inbound edit
  • fb8f7ad fix(mtproto): exclude depleted clients from the reconcile job to match the sync push
  • 55845c6 feat(mtproto): hot-reload mtg secrets in place instead of restarting
  • 1e6091a feat(mtproto): apply single-client edits to the sidecar immediately
  • e4b8c75 chore(mtproto): pin mtg-multi to the mhsanaei fork v1.13.3
  • ca6ebf0 feat(mtproto): apply live secret updates via the management API and add ad-tag
  • 7bf5d5f feat(inbounds): expose the mtproto ad-tag and public IP in the inbound form
  • 534deac ci(mtproto): install mtg-multi from prebuilt release binaries
  • dd4b100 i18n(mtproto): localize the ad-tag and public IP strings

📊 Changes

40 files changed (+1163 additions, -94 deletions)

View changed files

📝 .github/workflows/release.yml (+17 -20)
📝 CLAUDE.md (+4 -2)
📝 DockerInit.sh (+13 -5)
📝 README.ar_EG.md (+0 -1)
📝 README.es_ES.md (+0 -1)
📝 README.fa_IR.md (+0 -1)
📝 README.md (+0 -1)
📝 README.ru_RU.md (+0 -1)
📝 README.tr_TR.md (+0 -1)
📝 README.zh_CN.md (+0 -1)
📝 docs/architecture.md (+4 -2)
📝 frontend/src/pages/inbounds/form/protocols/mtproto.tsx (+24 -0)
📝 frontend/src/schemas/protocols/inbound/mtproto.ts (+11 -0)
📝 internal/mtproto/manager.go (+146 -20)
➕ internal/mtproto/manager_reload_test.go (+252 -0)
📝 internal/mtproto/manager_test.go (+51 -9)
📝 internal/mtproto/process.go (+3 -1)
📝 internal/web/job/mtproto_job.go (+7 -15)
📝 internal/web/runtime/local.go (+32 -0)
📝 internal/web/service/client_inbound_apply.go (+12 -3)

...and 20 more files

📄 Description

Problem

MTProto connections dropped whenever a client or inbound was edited. Three causes:

  1. Every inbound save restarted the sidecar — even a remark-only edit. The update path did DelInbound+AddInbound, and Remove deleted the manager's tracking entry, so the existing "config unchanged → no-op" fingerprint gate could never fire.
  2. The reconcile job and the save path disagreed on the served client set — the 10s job read raw settings while the save path filtered depleted clients, so depleted (out-of-traffic) MTProto clients kept access indefinitely, and the disagreement caused spurious restarts.
  3. No runtime API — genuine secret changes could only be applied by killing and respawning the process, dropping every client on that inbound.

Changes

Panel-side (fixes the drops on their own):

  • Route MTProto inbound updates through the fingerprint-gated Ensure instead of Remove+Ensure, so an edit that doesn't change the generated config keeps the process and its connections. The fingerprint is split into structural (restart-on-change) vs reloadable (secrets + ad-tag) parts.
  • Unify depletion filtering between the reconcile job (DesiredMtprotoInstances) and the interactive push, so depleted clients are actually cut off and the two paths never flap.
  • Fixes a latent leak: changing an inbound away from MTProto never stopped the sidecar, leaving an orphaned process holding the port.

Live updates via the fork's management API:

  • Apply client add / remove / re-key / enable-toggle and ad-tag changes through PUT /secrets (mhsanaei/mtg-multi ≥ v1.13.3): one atomic JSON call that keeps unchanged connections and closes only removed or re-keyed ones. The config file is still written first for restart/crash recovery, and any non-200 falls back to a full restart, so older binaries keep working unchanged.
  • Single-client edits now apply immediately instead of waiting for the 10s reconcile; bulk edits still ride the reconcile job (which converges to the same state).

Per-inbound ad-tag (sponsored channel):

  • Optional 32-hex ad-tag plus Public IPv4/IPv6 overrides on the MTProto inbound form (leave the public IP blank to let mtg auto-detect the reachable address the middle proxy needs). Ad-tag changes hot-apply without dropping connections.

CI:

  • Pin to mhsanaei/mtg-multi v1.13.3 and download the prebuilt release binaries for every packaged platform in DockerInit.sh + release.yml, instead of building from source. Produced mtg-<os>-<arch> filenames are unchanged.

Verification

  • Drove the real v1.13.3 binary end to end: a secret + ad-tag change applied in place — same process, uptime kept climbing, both secrets served, /adtag returned the tag.
  • Backend go build / go vet / go test -shuffle=on, frontend typecheck + 702 tests + production build, and the release download flow all pass.
  • New tests: fingerprint split, ensureActionFor, the PUT /secrets client, hot-reload-keeps-process / fallback-restarts / no-op-keeps-process (fake mtg via test-binary re-exec), job depletion parity, and an "unchanged save doesn't restart" regression.

Notes

  • The mtg-multi fork lives in a separate repo (mhsanaei/mtg-multi, released v1.13.3) — this PR is self-contained for the panel and only requires that release.
  • The 6 new i18n keys (mtgAdTag*, mtgPublicIp*) carry English text in the non-English locales until translated — present in all 13 files per the repo convention, so they fall back to English meanwhile.

🔄 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/5838 **Author:** [@MHSanaei](https://github.com/MHSanaei) **Created:** 7/6/2026 **Status:** ✅ Merged **Merged:** 7/6/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/mtproto-live-updates` --- ### 📝 Commits (10+) - [`2ea824a`](https://github.com/MHSanaei/3x-ui/commit/2ea824a6960c878db061dab7e7404e1cf2c07661) fix(mtproto): split the mtg fingerprint into structural and secrets parts - [`bdabba7`](https://github.com/MHSanaei/3x-ui/commit/bdabba7214e45247a6c36a24b4576d5e6b9f734d) fix(mtproto): stop restarting mtg on every inbound edit - [`fb8f7ad`](https://github.com/MHSanaei/3x-ui/commit/fb8f7ad3510c605a2f9960e46b320023236d3584) fix(mtproto): exclude depleted clients from the reconcile job to match the sync push - [`55845c6`](https://github.com/MHSanaei/3x-ui/commit/55845c6bc20bc2ecf91297c553798bf82dd60ba7) feat(mtproto): hot-reload mtg secrets in place instead of restarting - [`1e6091a`](https://github.com/MHSanaei/3x-ui/commit/1e6091a0ab56758c7707f824006d7a53c8cabbe9) feat(mtproto): apply single-client edits to the sidecar immediately - [`e4b8c75`](https://github.com/MHSanaei/3x-ui/commit/e4b8c7553b38a9f31053e899b96133db0107aed4) chore(mtproto): pin mtg-multi to the mhsanaei fork v1.13.3 - [`ca6ebf0`](https://github.com/MHSanaei/3x-ui/commit/ca6ebf0f36953b002a69674ccaf3de1318780437) feat(mtproto): apply live secret updates via the management API and add ad-tag - [`7bf5d5f`](https://github.com/MHSanaei/3x-ui/commit/7bf5d5f3bf705ee36f012d981ffbc054dd7f5e7f) feat(inbounds): expose the mtproto ad-tag and public IP in the inbound form - [`534deac`](https://github.com/MHSanaei/3x-ui/commit/534deac028638daf7680347d4da87e9728247047) ci(mtproto): install mtg-multi from prebuilt release binaries - [`dd4b100`](https://github.com/MHSanaei/3x-ui/commit/dd4b1001cafb1074d7e3eb84e61e94a6e4d8a8b9) i18n(mtproto): localize the ad-tag and public IP strings ### 📊 Changes **40 files changed** (+1163 additions, -94 deletions) <details> <summary>View changed files</summary> 📝 `.github/workflows/release.yml` (+17 -20) 📝 `CLAUDE.md` (+4 -2) 📝 `DockerInit.sh` (+13 -5) 📝 `README.ar_EG.md` (+0 -1) 📝 `README.es_ES.md` (+0 -1) 📝 `README.fa_IR.md` (+0 -1) 📝 `README.md` (+0 -1) 📝 `README.ru_RU.md` (+0 -1) 📝 `README.tr_TR.md` (+0 -1) 📝 `README.zh_CN.md` (+0 -1) 📝 `docs/architecture.md` (+4 -2) 📝 `frontend/src/pages/inbounds/form/protocols/mtproto.tsx` (+24 -0) 📝 `frontend/src/schemas/protocols/inbound/mtproto.ts` (+11 -0) 📝 `internal/mtproto/manager.go` (+146 -20) ➕ `internal/mtproto/manager_reload_test.go` (+252 -0) 📝 `internal/mtproto/manager_test.go` (+51 -9) 📝 `internal/mtproto/process.go` (+3 -1) 📝 `internal/web/job/mtproto_job.go` (+7 -15) 📝 `internal/web/runtime/local.go` (+32 -0) 📝 `internal/web/service/client_inbound_apply.go` (+12 -3) _...and 20 more files_ </details> ### 📄 Description ## Problem MTProto connections dropped whenever a client or inbound was edited. Three causes: 1. **Every inbound save restarted the sidecar** — even a remark-only edit. The update path did `DelInbound`+`AddInbound`, and `Remove` deleted the manager's tracking entry, so the existing "config unchanged → no-op" fingerprint gate could never fire. 2. **The reconcile job and the save path disagreed on the served client set** — the 10s job read raw settings while the save path filtered depleted clients, so depleted (out-of-traffic) MTProto clients kept access indefinitely, and the disagreement caused spurious restarts. 3. **No runtime API** — genuine secret changes could only be applied by killing and respawning the process, dropping every client on that inbound. ## Changes **Panel-side (fixes the drops on their own):** - Route MTProto inbound updates through the fingerprint-gated `Ensure` instead of `Remove`+`Ensure`, so an edit that doesn't change the generated config keeps the process and its connections. The fingerprint is split into structural (restart-on-change) vs reloadable (secrets + ad-tag) parts. - Unify depletion filtering between the reconcile job (`DesiredMtprotoInstances`) and the interactive push, so depleted clients are actually cut off and the two paths never flap. - Fixes a latent leak: changing an inbound away from MTProto never stopped the sidecar, leaving an orphaned process holding the port. **Live updates via the fork's management API:** - Apply client add / remove / re-key / enable-toggle and ad-tag changes through `PUT /secrets` (mhsanaei/mtg-multi ≥ v1.13.3): one atomic JSON call that keeps unchanged connections and closes only removed or re-keyed ones. The config file is still written first for restart/crash recovery, and any non-200 falls back to a full restart, so older binaries keep working unchanged. - Single-client edits now apply immediately instead of waiting for the 10s reconcile; bulk edits still ride the reconcile job (which converges to the same state). **Per-inbound ad-tag (sponsored channel):** - Optional 32-hex ad-tag plus Public IPv4/IPv6 overrides on the MTProto inbound form (leave the public IP blank to let mtg auto-detect the reachable address the middle proxy needs). Ad-tag changes hot-apply without dropping connections. **CI:** - Pin to mhsanaei/mtg-multi v1.13.3 and download the prebuilt release binaries for every packaged platform in `DockerInit.sh` + `release.yml`, instead of building from source. Produced `mtg-<os>-<arch>` filenames are unchanged. ## Verification - Drove the **real v1.13.3 binary** end to end: a secret + ad-tag change applied **in place** — same process, uptime kept climbing, both secrets served, `/adtag` returned the tag. - Backend `go build` / `go vet` / `go test -shuffle=on`, frontend typecheck + 702 tests + production build, and the release download flow all pass. - New tests: fingerprint split, `ensureActionFor`, the `PUT /secrets` client, hot-reload-keeps-process / fallback-restarts / no-op-keeps-process (fake mtg via test-binary re-exec), job depletion parity, and an "unchanged save doesn't restart" regression. ## Notes - The `mtg-multi` fork lives in a separate repo (mhsanaei/mtg-multi, released **v1.13.3**) — this PR is self-contained for the panel and only requires that release. - The 6 new i18n keys (`mtgAdTag*`, `mtgPublicIp*`) carry English text in the non-English locales until translated — present in all 13 files per the repo convention, so they fall back to English meanwhile. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:05:40 +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#6003
No description provided.