mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-17 15:47:14 +00:00
fix(tgbot): keep the add-client draft with the chat that owns it (#6499)
* fix(tgbot): keep the add-client draft with the chat that owns it The wizard held one package-level draft for the whole bot. Its steps run on the ten-goroutine worker pool, so two admins adding a client at the same time wrote into the same form: whichever step ran last decided the email, the limits and the attached inbounds of a client the other chat went on to create, and the attach picker mutated one shared slice from several goroutines at once as well. Each chat now gets its own draft, reached only through the chat that owns it and held for the duration of a step, so a client is created from the values its own chat collected. * fix(tgbot): take the wizard's draft lock only for the wizard A queued report tap held one of the ten worker slots while it waited on the chat's draft, and every chat that reached answerCallback grew the draft map even when the admin gate rejected it. Both follow from acquiring the draft before the gate; the wizard's own steps are the only callers that read it. The draft is now looked up under the same admin-and-wizard check, addClient takes the draft its caller locked instead of looking it up again, a submit drops the entry, and StopBot clears the map with the conversation states.
This commit is contained in:
@@ -30,52 +30,52 @@ import (
|
||||
// shown in the multi-inbound add flow. Per-protocol secrets (UUID, password,
|
||||
// flow, method) are generated by fillProtocolDefaults on submit, so the bot
|
||||
// never has to track them per inbound itself.
|
||||
func (t *Tgbot) BuildClientDraftMessage() string {
|
||||
func (t *Tgbot) BuildClientDraftMessage(draft *clientDraft) string {
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
expiry := ""
|
||||
switch {
|
||||
case client_ExpiryTime == 0:
|
||||
case draft.expiryTime == 0:
|
||||
expiry = t.I18nBot("tgbot.unlimited")
|
||||
case client_ExpiryTime < 0:
|
||||
expiry = fmt.Sprintf("%d %s", client_ExpiryTime/-86400000, t.I18nBot("tgbot.days"))
|
||||
case draft.expiryTime < 0:
|
||||
expiry = fmt.Sprintf("%d %s", draft.expiryTime/-86400000, t.I18nBot("tgbot.days"))
|
||||
default:
|
||||
diff := client_ExpiryTime - now
|
||||
diff := draft.expiryTime - now
|
||||
if diff > 172800000 {
|
||||
expiry = time.UnixMilli(client_ExpiryTime).Format("2006-01-02 15:04:05")
|
||||
expiry = time.UnixMilli(draft.expiryTime).Format("2006-01-02 15:04:05")
|
||||
} else {
|
||||
expiry = fmt.Sprintf("%d %s", diff/3600000, t.I18nBot("tgbot.hours"))
|
||||
}
|
||||
}
|
||||
|
||||
traffic := "♾️ Unlimited(Reset)"
|
||||
if client_TotalGB > 0 {
|
||||
traffic = common.FormatTraffic(client_TotalGB)
|
||||
if draft.totalGB > 0 {
|
||||
traffic = common.FormatTraffic(draft.totalGB)
|
||||
}
|
||||
|
||||
ipLimit := "♾️ Unlimited(Reset)"
|
||||
if client_LimitIP > 0 {
|
||||
ipLimit = fmt.Sprint(client_LimitIP)
|
||||
if draft.limitIP > 0 {
|
||||
ipLimit = fmt.Sprint(draft.limitIP)
|
||||
}
|
||||
|
||||
attached := t.describeAttachedInbounds(receiver_inbound_IDs)
|
||||
attached := t.describeAttachedInbounds(draft.receiverInboundIDs)
|
||||
if attached == "" {
|
||||
attached = "—"
|
||||
}
|
||||
|
||||
comment := client_Comment
|
||||
comment := draft.comment
|
||||
if comment == "" {
|
||||
comment = "—"
|
||||
}
|
||||
|
||||
tgID := client_TgID
|
||||
tgID := draft.tgID
|
||||
if tgID == "" {
|
||||
tgID = "—"
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString("📝 <b>New client draft</b>\r\n")
|
||||
fmt.Fprintf(&b, "📧 Email: <code>%s</code>\r\n", html.EscapeString(client_Email))
|
||||
fmt.Fprintf(&b, "📧 Email: <code>%s</code>\r\n", html.EscapeString(draft.email))
|
||||
fmt.Fprintf(&b, "🔗 Attached: %s\r\n", html.EscapeString(attached))
|
||||
fmt.Fprintf(&b, "📊 Traffic: %s\r\n", traffic)
|
||||
fmt.Fprintf(&b, "📅 Expire: %s\r\n", expiry)
|
||||
@@ -111,25 +111,25 @@ func (t *Tgbot) describeAttachedInbounds(ids []int) string {
|
||||
// the full set of attached inbound ids. Per-inbound fillProtocolDefaults on
|
||||
// the panel generates UUID/password/auth per protocol, so the bot only
|
||||
// supplies the universal fields it actually collected.
|
||||
func (t *Tgbot) SubmitAddClient() (bool, error) {
|
||||
inboundIDs := receiver_inbound_IDs
|
||||
if len(inboundIDs) == 0 && receiver_inbound_ID > 0 {
|
||||
inboundIDs = []int{receiver_inbound_ID}
|
||||
func (t *Tgbot) SubmitAddClient(draft *clientDraft) (bool, error) {
|
||||
inboundIDs := draft.receiverInboundIDs
|
||||
if len(inboundIDs) == 0 && draft.receiverInboundID > 0 {
|
||||
inboundIDs = []int{draft.receiverInboundID}
|
||||
}
|
||||
if len(inboundIDs) == 0 {
|
||||
return false, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
|
||||
}
|
||||
|
||||
tgIDInt, _ := strconv.ParseInt(client_TgID, 10, 64)
|
||||
tgIDInt, _ := strconv.ParseInt(draft.tgID, 10, 64)
|
||||
client := model.Client{
|
||||
Email: client_Email,
|
||||
Enable: client_Enable,
|
||||
LimitIP: client_LimitIP,
|
||||
TotalGB: client_TotalGB,
|
||||
ExpiryTime: client_ExpiryTime,
|
||||
SubID: client_SubID,
|
||||
Comment: client_Comment,
|
||||
Reset: client_Reset,
|
||||
Email: draft.email,
|
||||
Enable: draft.enable,
|
||||
LimitIP: draft.limitIP,
|
||||
TotalGB: draft.totalGB,
|
||||
ExpiryTime: draft.expiryTime,
|
||||
SubID: draft.subID,
|
||||
Comment: draft.comment,
|
||||
Reset: draft.reset,
|
||||
TgID: tgIDInt,
|
||||
}
|
||||
|
||||
@@ -761,8 +761,8 @@ func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) {
|
||||
// client-first multi-inbound add flow. Per-protocol secrets (UUID, password,
|
||||
// flow, method) are generated by fillProtocolDefaults on submit, so the bot
|
||||
// only exposes the universal client fields here.
|
||||
func (t *Tgbot) getCommonClientButtons() [][]telego.InlineKeyboardButton {
|
||||
attachLabel := fmt.Sprintf("➕ Attach inbound (%d)", len(receiver_inbound_IDs))
|
||||
func (t *Tgbot) getCommonClientButtons(draft *clientDraft) [][]telego.InlineKeyboardButton {
|
||||
attachLabel := fmt.Sprintf("➕ Attach inbound (%d)", len(draft.receiverInboundIDs))
|
||||
return [][]telego.InlineKeyboardButton{
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_email")).WithCallbackData("add_client_ch_default_email"),
|
||||
@@ -790,8 +790,8 @@ func (t *Tgbot) getCommonClientButtons() [][]telego.InlineKeyboardButton {
|
||||
}
|
||||
|
||||
// addClient renders the draft message + shared client-first keyboard.
|
||||
func (t *Tgbot) addClient(chatId int64, msg string, messageID ...int) {
|
||||
inlineKeyboard := tu.InlineKeyboard(t.getCommonClientButtons()...)
|
||||
func (t *Tgbot) addClient(chatId int64, draft *clientDraft, msg string, messageID ...int) {
|
||||
inlineKeyboard := tu.InlineKeyboard(t.getCommonClientButtons(draft)...)
|
||||
if len(messageID) > 0 {
|
||||
t.editMessageTgBot(chatId, messageID[0], msg, inlineKeyboard)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user