[PR #6499] [MERGED] fix(tgbot): keep the add-client draft with the chat that owns it #6311

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

📋 Pull Request Information

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

Base: main ← Head: fix/tgbot-per-chat-add-client-draft


📝 Commits (2)

  • d913cc9 fix(tgbot): keep the add-client draft with the chat that owns it
  • da9a4f2 fix(tgbot): take the wizard's draft lock only for the wizard

📊 Changes

6 files changed (+374 additions, -150 deletions)

View changed files

📝 internal/web/service/tgbot/tgbot.go (+56 -18)
📝 internal/web/service/tgbot/tgbot_client.go (+31 -31)
➕ internal/web/service/tgbot/tgbot_client_draft_per_chat_test.go (+178 -0)
📝 internal/web/service/tgbot/tgbot_draft_render_test.go (+16 -18)
📝 internal/web/service/tgbot/tgbot_inbound.go (+3 -3)
📝 internal/web/service/tgbot/tgbot_router.go (+90 -80)

📄 Description

Summary

The add-client wizard now keeps one draft per chat instead of one package-level draft for the whole bot, so two admins adding a client at the same time cannot fill in each other's client.

Why

The wizard's state lived in package-level variables — receiver_inbound_ID, receiver_inbound_IDs, client_Email, client_LimitIP, client_TotalGB, client_ExpiryTime, client_Enable, client_TgID, client_SubID, client_Comment, client_Reset (internal/web/service/tgbot/tgbot.go) — while its steps run on the ten-goroutine pool (messageWorkerPool = make(chan struct{}, 10), wired through runBotHandler in internal/web/service/tgbot/tgbot_router.go). Two admins sharing the bot is the documented case: the bot serves every id in adminIds, and the wizard is the one flow that carries state between messages.

What that produces:

  • A client is created from another chat's answers. Admin A opens the wizard on inbound 3 and sets a 50 GB limit; admin B starts a client on inbound 7 before A submits. SubmitAddClient then builds the client from the shared values, so A's "Create" writes B's inbound, traffic limit and expiry — and the links the bot then sends back are for the wrong client.
  • The rendered card lies about the draft. A's card is re-rendered from the shared state, so the email, limits and attached inbounds shown are the ones the other chat typed.
  • The attach picker mutates one shared slice from several goroutines. add_client_toggle_attach splices receiver_inbound_IDs in place, which is a data race, not just a last-writer-wins mistake.

User story: two operators run one panel and both use the Telegram bot (that is what adminIds is for). During a bulk client migration they each open Add Client and work through the card at the same time. Each ends up with a client built from a mixture of the two forms — and the share links they forward belong to a client whose limits neither of them chose.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

New TestAddClientDraftIsPerChat drives the real callback path (answerCallback → addClient → the bot API) for two chats against an httptest bot server, and reads the draft card that reaches each chat:

  1. Chat A starts a client (add_client_to 1), chat B starts one (add_client_to 2). Each card must carry the email the wizard just generated for that chat.
  2. Chat A re-renders its card (add_client_default_traffic_exp) with chat B's wizard already past its start. A's card must still show A's own email.

With the per-chat keying reverted — one shared draft, the old behaviour — the test reddens exactly on the bug:

chat A's card shows email "w91oofmd", want its own "npjv7vys" from chat B's draft

The whole internal/web/service/tgbot package suite passes with the change, including the two existing tests that drive this wizard (TestClientDraftMessageRendersHTML, TestAddClientPromptsEscapeDraftValues, updated to seed the draft of the chat they use). go vet is clean for the package.

Two things stated rather than glossed:

  • -race cannot run on this machine (it needs cgo and there is no C compiler), so CI's race job is what runs the detector over this. The behavioural test above needs no race detector to fail.
  • A single chat's own steps are now serialized, because whatever the chat's draft touches is done under that chat's draft lock. That is deliberate: it also settles a double-tap on the same card. Other chats are unaffected.

Screenshots / recordings

N/A — no UI change.

Breaking changes

None for users. BuildClientDraftMessage, SubmitAddClient, getInboundsAttachPicker and getCommonClientButtons take the chat's draft as an argument; all four are package-private and used only inside internal/web/service/tgbot (checked with a repository-wide grep), so no other package is affected.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally. (no C compiler here — CI verifies the CGo build)
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed. (no endpoint, setting or i18n key changed)
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 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/6499 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/13/2026 **Status:** ✅ Merged **Merged:** 9/13/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/tgbot-per-chat-add-client-draft` --- ### 📝 Commits (2) - [`d913cc9`](https://github.com/MHSanaei/3x-ui/commit/d913cc9186ed7e60fddaed58ae57e171cf1efebc) fix(tgbot): keep the add-client draft with the chat that owns it - [`da9a4f2`](https://github.com/MHSanaei/3x-ui/commit/da9a4f244b2bcb07838c1e827e46b55671c21a88) fix(tgbot): take the wizard's draft lock only for the wizard ### 📊 Changes **6 files changed** (+374 additions, -150 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/tgbot/tgbot.go` (+56 -18) 📝 `internal/web/service/tgbot/tgbot_client.go` (+31 -31) ➕ `internal/web/service/tgbot/tgbot_client_draft_per_chat_test.go` (+178 -0) 📝 `internal/web/service/tgbot/tgbot_draft_render_test.go` (+16 -18) 📝 `internal/web/service/tgbot/tgbot_inbound.go` (+3 -3) 📝 `internal/web/service/tgbot/tgbot_router.go` (+90 -80) </details> ### 📄 Description ## Summary The add-client wizard now keeps one draft per chat instead of one package-level draft for the whole bot, so two admins adding a client at the same time cannot fill in each other's client. ## Why The wizard's state lived in package-level variables — `receiver_inbound_ID`, `receiver_inbound_IDs`, `client_Email`, `client_LimitIP`, `client_TotalGB`, `client_ExpiryTime`, `client_Enable`, `client_TgID`, `client_SubID`, `client_Comment`, `client_Reset` (`internal/web/service/tgbot/tgbot.go`) — while its steps run on the ten-goroutine pool (`messageWorkerPool = make(chan struct{}, 10)`, wired through `runBotHandler` in `internal/web/service/tgbot/tgbot_router.go`). Two admins sharing the bot is the documented case: the bot serves every id in `adminIds`, and the wizard is the one flow that carries state between messages. What that produces: - **A client is created from another chat's answers.** Admin A opens the wizard on inbound 3 and sets a 50 GB limit; admin B starts a client on inbound 7 before A submits. `SubmitAddClient` then builds the client from the shared values, so A's "Create" writes B's inbound, traffic limit and expiry — and the links the bot then sends back are for the wrong client. - **The rendered card lies about the draft.** A's card is re-rendered from the shared state, so the email, limits and attached inbounds shown are the ones the other chat typed. - **The attach picker mutates one shared slice from several goroutines.** `add_client_toggle_attach` splices `receiver_inbound_IDs` in place, which is a data race, not just a last-writer-wins mistake. User story: two operators run one panel and both use the Telegram bot (that is what `adminIds` is for). During a bulk client migration they each open Add Client and work through the card at the same time. Each ends up with a client built from a mixture of the two forms — and the share links they forward belong to a client whose limits neither of them chose. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [x] Telegram bot ## How was this tested? New `TestAddClientDraftIsPerChat` drives the real callback path (`answerCallback` → `addClient` → the bot API) for two chats against an `httptest` bot server, and reads the draft card that reaches each chat: 1. Chat A starts a client (`add_client_to 1`), chat B starts one (`add_client_to 2`). Each card must carry the email the wizard just generated for that chat. 2. Chat A re-renders its card (`add_client_default_traffic_exp`) with chat B's wizard already past its start. A's card must still show A's own email. With the per-chat keying reverted — one shared draft, the old behaviour — the test reddens exactly on the bug: ``` chat A's card shows email "w91oofmd", want its own "npjv7vys" from chat B's draft ``` The whole `internal/web/service/tgbot` package suite passes with the change, including the two existing tests that drive this wizard (`TestClientDraftMessageRendersHTML`, `TestAddClientPromptsEscapeDraftValues`, updated to seed the draft of the chat they use). `go vet` is clean for the package. Two things stated rather than glossed: - `-race` cannot run on this machine (it needs cgo and there is no C compiler), so CI's `race` job is what runs the detector over this. The behavioural test above needs no race detector to fail. - A single chat's own steps are now serialized, because whatever the chat's draft touches is done under that chat's draft lock. That is deliberate: it also settles a double-tap on the same card. Other chats are unaffected. ## Screenshots / recordings N/A — no UI change. ## Breaking changes None for users. `BuildClientDraftMessage`, `SubmitAddClient`, `getInboundsAttachPicker` and `getCommonClientButtons` take the chat's draft as an argument; all four are package-private and used only inside `internal/web/service/tgbot` (checked with a repository-wide grep), so no other package is affected. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [ ] `go build ./...` and the test suite pass locally. (no C compiler here — CI verifies the CGo build) - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. (no endpoint, setting or i18n key changed) - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:52 +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#6311
No description provided.