mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-07 06:32:07 +03:00
7aa5fc085f
* feat(tgbot): gate the bot behind three user levels Every Telegram account that found the bot could run /help, /status and /usage, and tap any client button it could forge: nothing separated an account no admin had bound from a customer. Each update now resolves to stranger, client or admin, and commands are allowlisted per level so a command added later stays admin-only until it is listed. A stranger may run /start and /id only, and /start answers with the ChatID an admin needs to bind it; a stranger's callbacks are answered and dropped. Client detection reads the same tgId lookup as clientOwnedByTgUser, so the level gate and the ownership check agree. The bot also ignores everything outside private chats: authorization keys on the sender while wizard state keys on the chat, and the two are the same identity only in a private chat. * feat(tgbot): bind Telegram accounts through /start deep links Linking a customer meant the customer sending /id and an admin copying the ChatID into the client by hand, which does not scale past a few customers and is easy to get wrong. The admin client card now offers an invite link, t.me/<bot>?start=<subId>, and the first account to open it is bound through the existing SetClientTelegramUserID. A subId already grants the subscription, so binding gives the holder nothing the token did not. A subscription that spans several clients binds all of them, and is refused if any part belongs to another account; re-opening your own link is idempotent. Unknown and already-claimed tokens share one reply, so the link cannot be used to probe for valid subIds. * fix(tgbot): harden invite claims after review Review of the access-level and binding change found five problems: - Concurrent claims of one link all read the client as unbound, all bound and all were told so, while only the last write held. Resolving and binding now share one lock, and a bind that fails part-way through a multi-client subscription undoes the bindings it already made. - A subId has no minimum strength and the bot needs only its public username, so /start was an unthrottled guessing oracle. Non-admin claim attempts are capped at five per account per hour, the first refused one notifies the admins, and the Subscription ID field now says it doubles as the bot invite code. - levelOf expanded every inbound's client JSON on every non-admin update. It now reads the indexed tg_id column of the clients table. - A button tapped in a group chat was dropped unanswered and kept spinning, with nothing logged. It is answered now, and each ignored chat is logged once. - The subId was pasted raw into the t.me link, so '#' or '&' truncated it and Telegram rejects anything outside A-Za-z0-9_-. The payload is now base64url, and a subId too long for the 64-character limit is refused. * fix(tgbot): answer group chats again and make the claim race test bite ignoredChat dropped every non-private chat because wizard state was keyed by chat while authorization keyed on the sender. #6604 on main re-keyed that state by (chat, user) so admins can drive the bot from a group, so after the merge the drop only took the whole bot away from those admins, report keyboards sent to a group included. The level gate already keys on the sender, so group chats need no special case. TestConcurrentClaimsBindOnlyOneAccount passed with inviteClaimMu removed: the first claimant took the pool's idle connection and bound before the rest had opened theirs, so no two ever raced. It now holds the inbound write the binds need until every claimant has resolved, and fails without the lock ("6 accounts told they bound"). TestCommandAllowed restated the commandsByLevel map; TestGateCommand drives the same allowlist through gateCommand. TestIgnoredChat goes with the code it pinned. * docs(tgbot): document access levels and invite links The command table still said /help and /status answer anyone. An account no admin has linked now reaches only /start and /id, and a customer is linked through the client card's Invite Link, whose token is the Subscription ID. Updated in en, fa, ru and zh. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
1417 lines
59 KiB
Go
1417 lines
59 KiB
Go
package tgbot
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"html"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/logger"
|
|
|
|
"github.com/mymmrac/telego"
|
|
th "github.com/mymmrac/telego/telegohandler"
|
|
tu "github.com/mymmrac/telego/telegoutil"
|
|
)
|
|
|
|
// recoverBotPanic must be deferred by every bot handler entry point: telego's
|
|
// dispatch has no recovery of its own, so one bad update would kill the panel.
|
|
func recoverBotPanic() {
|
|
if r := recover(); r != nil {
|
|
logger.Error("Recovered panic in Telegram bot handler:", r)
|
|
}
|
|
}
|
|
|
|
// runBotHandler runs a bot handler on a worker slot and recovers panics: a bad
|
|
// callback must not take down the whole panel, the way a cron panic would not.
|
|
func runBotHandler(fn func()) {
|
|
messageWorkerPool <- struct{}{}
|
|
defer func() { <-messageWorkerPool }()
|
|
defer recoverBotPanic()
|
|
fn()
|
|
}
|
|
|
|
// chooseInboundClient fetches the inbound once and reuses the row: the inline
|
|
// keyboard outlives the inbound, so a stale tap must answer an error, not panic.
|
|
func (t *Tgbot) chooseInboundClient(callbackQuery *telego.CallbackQuery, chatId int64, inboundID int, action string) {
|
|
inbound, err := t.inboundService.GetInbound(inboundID)
|
|
if err != nil {
|
|
logger.Warning("chooseInboundClient GetInbound failed:", err)
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getInboundsFailed"))
|
|
return
|
|
}
|
|
clientsKB, err := t.getInboundClientsFor(inbound, action)
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseClient", "Inbound=="+inbound.Remark), clientsKB)
|
|
}
|
|
|
|
// OnReceive starts the message receiving loop for the Telegram bot.
|
|
func (t *Tgbot) OnReceive() {
|
|
params := telego.GetUpdatesParams{
|
|
Timeout: 20, // Reduced timeout to detect connection issues faster
|
|
}
|
|
// Strict singleton: never start a second long-polling loop.
|
|
tgBotMutex.Lock()
|
|
if botCancel != nil || isRunning {
|
|
tgBotMutex.Unlock()
|
|
logger.Warning("TgBot OnReceive called while already running; ignoring.")
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
botCancel = cancel
|
|
isRunning = true
|
|
// Add to WaitGroup before releasing the lock so StopBot() can't return
|
|
// before this receiver goroutine is accounted for.
|
|
botWG.Add(1)
|
|
tgBotMutex.Unlock()
|
|
|
|
// Get updates channel using the context with shorter timeout for better error recovery
|
|
updates, _ := bot.UpdatesViaLongPolling(ctx, ¶ms)
|
|
go func() {
|
|
defer botWG.Done()
|
|
h, _ := th.NewBotHandler(bot, updates)
|
|
tgBotMutex.Lock()
|
|
botHandler = h
|
|
tgBotMutex.Unlock()
|
|
|
|
h.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
|
defer recoverBotPanic()
|
|
userStateMgr.clear(messageActor(message))
|
|
t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.keyboardClosed"), tu.ReplyKeyboardRemove())
|
|
return nil
|
|
}, th.TextEqual(t.I18nBot("tgbot.buttons.closeKeyboard")))
|
|
|
|
h.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
|
defer recoverBotPanic()
|
|
if !t.isCommandForCurrentBot(&message) {
|
|
return nil
|
|
}
|
|
|
|
// Use goroutine with worker pool for concurrent command processing
|
|
go runBotHandler(func() {
|
|
userStateMgr.clear(messageActor(message))
|
|
if isAdmin, ok := t.gateCommand(&message); ok {
|
|
t.answerCommand(&message, message.Chat.ID, isAdmin)
|
|
}
|
|
})
|
|
return nil
|
|
}, th.AnyCommand())
|
|
|
|
h.HandleCallbackQuery(func(ctx *th.Context, query telego.CallbackQuery) error {
|
|
// Use goroutine with worker pool for concurrent callback processing
|
|
go runBotHandler(func() {
|
|
userStateMgr.clear(callbackActor(&query))
|
|
if isAdmin, ok := t.gateCallback(&query); ok {
|
|
t.answerCallback(&query, isAdmin)
|
|
}
|
|
})
|
|
return nil
|
|
}, th.AnyCallbackQueryWithMessage())
|
|
|
|
h.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
|
defer recoverBotPanic()
|
|
userStateMgr.maybePrune(time.Hour)
|
|
actor := messageActor(message)
|
|
if userState, exists := userStateMgr.get(actor); exists {
|
|
if userState == broadcastAwaitingText {
|
|
t.handleBroadcastInput(&message, actor)
|
|
return nil
|
|
}
|
|
// Only a wizard step touches the draft, so only it takes the lock.
|
|
draft := addClientDrafts.forActor(actor)
|
|
draft.Lock()
|
|
defer draft.Unlock()
|
|
switch userState {
|
|
case "awaiting_email":
|
|
if draft.email == strings.TrimSpace(message.Text) {
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
userStateMgr.clear(actor)
|
|
return nil
|
|
}
|
|
|
|
draft.email = strings.TrimSpace(message.Text)
|
|
if t.isSingleWord(draft.email) {
|
|
userStateMgr.set(actor, "awaiting_email")
|
|
|
|
cancel_btn_markup := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
|
|
),
|
|
)
|
|
|
|
t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.messages.incorrect_input"), cancel_btn_markup)
|
|
} else {
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.received_email"), 3, tu.ReplyKeyboardRemove())
|
|
userStateMgr.clear(actor)
|
|
t.addClient(message.Chat.ID, draft, t.BuildClientDraftMessage(draft))
|
|
}
|
|
case "awaiting_comment":
|
|
if draft.comment == strings.TrimSpace(message.Text) {
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
userStateMgr.clear(actor)
|
|
return nil
|
|
}
|
|
|
|
draft.comment = strings.TrimSpace(message.Text)
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.received_comment"), 3, tu.ReplyKeyboardRemove())
|
|
userStateMgr.clear(actor)
|
|
t.addClient(message.Chat.ID, draft, t.BuildClientDraftMessage(draft))
|
|
case "awaiting_tg_id":
|
|
input := strings.TrimSpace(message.Text)
|
|
if input == "" || input == "-" || strings.EqualFold(input, "none") {
|
|
draft.tgID = ""
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
userStateMgr.clear(actor)
|
|
t.addClient(message.Chat.ID, draft, t.BuildClientDraftMessage(draft))
|
|
return nil
|
|
}
|
|
if _, err := strconv.ParseInt(input, 10, 64); err != nil {
|
|
cancel_btn_markup := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
|
|
),
|
|
)
|
|
t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.messages.incorrect_input"), cancel_btn_markup)
|
|
return nil
|
|
}
|
|
draft.tgID = input
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.userSaved"), 3, tu.ReplyKeyboardRemove())
|
|
userStateMgr.clear(actor)
|
|
t.addClient(message.Chat.ID, draft, t.BuildClientDraftMessage(draft))
|
|
}
|
|
} else {
|
|
if message.UsersShared != nil {
|
|
if checkAdmin(message.From.ID) {
|
|
for _, sharedUser := range message.UsersShared.Users {
|
|
userID := sharedUser.UserID
|
|
needRestart, err := t.clientService.SetClientTelegramUserID(&t.inboundService, message.UsersShared.RequestID, userID)
|
|
if needRestart {
|
|
t.xrayService.SetToNeedRestart()
|
|
}
|
|
output := ""
|
|
if err != nil {
|
|
output += t.I18nBot("tgbot.messages.selectUserFailed")
|
|
} else {
|
|
output += t.I18nBot("tgbot.messages.userSaved")
|
|
}
|
|
t.SendMsgToTgbot(message.Chat.ID, output, tu.ReplyKeyboardRemove())
|
|
}
|
|
} else {
|
|
t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.noResult"), tu.ReplyKeyboardRemove())
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}, th.AnyMessage())
|
|
|
|
_ = h.Start()
|
|
}()
|
|
}
|
|
|
|
// answerCommand processes incoming command messages from Telegram users.
|
|
func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin bool) {
|
|
msg, onlyMessage := "", false
|
|
|
|
command, _, commandArgs := tu.ParseCommand(message.Text)
|
|
|
|
// Helper function to handle unknown commands.
|
|
handleUnknownCommand := func() {
|
|
msg += t.I18nBot("tgbot.commands.unknown")
|
|
}
|
|
|
|
// Handle the command.
|
|
switch command {
|
|
case "help":
|
|
msg += t.I18nBot("tgbot.commands.help")
|
|
msg += t.I18nBot("tgbot.commands.pleaseChoose")
|
|
case "start":
|
|
if len(commandArgs) > 0 {
|
|
if !isAdmin && !t.allowInviteAttempt(message.From) {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.messages.inviteRateLimited"))
|
|
return
|
|
}
|
|
t.claimInvite(chatId, message.From.ID, commandArgs[0])
|
|
}
|
|
// A stranger learns only its ChatID, which is what an admin needs to bind it.
|
|
if !isAdmin && t.levelOf(message.From.ID) == levelStranger {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(message.From.ID, 10)))
|
|
return
|
|
}
|
|
msg += t.I18nBot("tgbot.commands.start", "Firstname=="+html.EscapeString(message.From.FirstName))
|
|
if isAdmin {
|
|
msg += t.I18nBot("tgbot.commands.welcome", "Hostname=="+hostname)
|
|
}
|
|
msg += "\n\n" + t.I18nBot("tgbot.commands.pleaseChoose")
|
|
case "status":
|
|
onlyMessage = true
|
|
msg += t.I18nBot("tgbot.commands.status")
|
|
case "id":
|
|
onlyMessage = true
|
|
msg += t.I18nBot("tgbot.commands.getID", "ID=="+strconv.FormatInt(message.From.ID, 10))
|
|
case "usage":
|
|
onlyMessage = true
|
|
if len(commandArgs) > 0 {
|
|
if isAdmin {
|
|
t.searchClient(chatId, commandArgs[0])
|
|
} else {
|
|
t.getClientUsage(chatId, message.From.ID, commandArgs[0])
|
|
}
|
|
} else {
|
|
msg += t.I18nBot("tgbot.commands.usage")
|
|
}
|
|
case "inbound":
|
|
onlyMessage = true
|
|
if isAdmin && len(commandArgs) > 0 {
|
|
t.searchInbound(chatId, commandArgs[0])
|
|
} else {
|
|
handleUnknownCommand()
|
|
}
|
|
case "restart":
|
|
onlyMessage = true
|
|
if isAdmin {
|
|
if len(commandArgs) == 0 {
|
|
if t.xrayService.IsXrayRunning() {
|
|
err := t.xrayService.RestartXray(true)
|
|
if err != nil {
|
|
msg += t.I18nBot("tgbot.commands.restartFailed", "Error=="+err.Error())
|
|
} else {
|
|
msg += t.I18nBot("tgbot.commands.restartSuccess")
|
|
}
|
|
} else {
|
|
msg += t.I18nBot("tgbot.commands.xrayNotRunning")
|
|
}
|
|
} else {
|
|
handleUnknownCommand()
|
|
msg += t.I18nBot("tgbot.commands.restartUsage")
|
|
}
|
|
} else {
|
|
handleUnknownCommand()
|
|
}
|
|
case "clearall":
|
|
onlyMessage = true
|
|
if isAdmin {
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("reset_all_traffics_cancel")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmResetTraffic")).WithCallbackData(t.encodeQuery("reset_all_traffics_c")),
|
|
),
|
|
)
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.messages.AreYouSure"), inlineKeyboard)
|
|
} else {
|
|
handleUnknownCommand()
|
|
}
|
|
case "broadcast":
|
|
onlyMessage = true
|
|
if isAdmin {
|
|
t.startBroadcast(messageActor(*message))
|
|
} else {
|
|
handleUnknownCommand()
|
|
}
|
|
default:
|
|
handleUnknownCommand()
|
|
}
|
|
|
|
if msg != "" {
|
|
t.sendResponse(chatId, msg, onlyMessage, isAdmin)
|
|
}
|
|
}
|
|
|
|
func (t *Tgbot) isCommandForCurrentBot(message *telego.Message) bool {
|
|
return isCommandForBot(message.Text, botUsername())
|
|
}
|
|
|
|
func botUsername() string {
|
|
if bot == nil {
|
|
return ""
|
|
}
|
|
return bot.Username()
|
|
}
|
|
|
|
func isCommandForBot(text string, username string) bool {
|
|
_, commandUsername, _ := tu.ParseCommand(text)
|
|
return commandUsername == "" || username == "" || strings.EqualFold(commandUsername, username)
|
|
}
|
|
|
|
// answerCallback processes callback queries from inline keyboards.
|
|
func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool) {
|
|
chatId := callbackQuery.Message.GetChat().ID
|
|
actor := callbackActor(callbackQuery)
|
|
|
|
// Only an admin's wizard callbacks touch a draft, so only they take its lock:
|
|
// a report tap must not wait on a slot, a rejected chat must not be stored.
|
|
var draft *clientDraft
|
|
if isAdmin && isAddClientStep(callbackQuery.Data) {
|
|
draft = addClientDrafts.forActor(actor)
|
|
draft.Lock()
|
|
defer draft.Unlock()
|
|
}
|
|
|
|
if isAdmin {
|
|
// get query from hash storage
|
|
decodedQuery, err := t.decodeQuery(callbackQuery.Data)
|
|
if err != nil {
|
|
// A button older than the 20-minute hash window is the common case
|
|
// here; the answer clears it, the message outlives a failed send.
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.noQuery"))
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.noQuery"))
|
|
return
|
|
}
|
|
dataArray := strings.Split(decodedQuery, " ")
|
|
|
|
if len(dataArray) >= 2 && len(dataArray[1]) > 0 {
|
|
email := dataArray[1]
|
|
switch dataArray[0] {
|
|
case "broadcast_confirm":
|
|
t.confirmBroadcast(actor, dataArray[1], callbackQuery.Message.GetMessageID(), callbackQuery.ID)
|
|
case "get_clients_for_sub":
|
|
inboundIdInt, err := strconv.Atoi(dataArray[1])
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.chooseInboundClient(callbackQuery, chatId, inboundIdInt, "client_sub_links")
|
|
case "get_clients_for_individual":
|
|
inboundIdInt, err := strconv.Atoi(dataArray[1])
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.chooseInboundClient(callbackQuery, chatId, inboundIdInt, "client_individual_links")
|
|
case "get_clients_for_qr":
|
|
inboundIdInt, err := strconv.Atoi(dataArray[1])
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.chooseInboundClient(callbackQuery, chatId, inboundIdInt, "client_qr_links")
|
|
case "client_sub_links":
|
|
t.sendClientSubLinks(chatId, email)
|
|
return
|
|
case "client_individual_links":
|
|
t.sendClientIndividualLinks(chatId, email)
|
|
return
|
|
case "client_qr_links":
|
|
t.sendClientQRLinks(chatId, email)
|
|
return
|
|
case "client_get_usage":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.messages.email", "Email=="+email))
|
|
t.searchClient(chatId, email)
|
|
case "client_refresh":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.clientRefreshSuccess", "Email=="+email))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "client_cancel":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "ips_refresh":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.IpRefreshSuccess", "Email=="+email))
|
|
t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "ips_cancel":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email))
|
|
t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "tgid_refresh":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.TGIdRefreshSuccess", "Email=="+email))
|
|
t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "tgid_cancel":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email))
|
|
t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "reset_traffic":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmResetTraffic")).WithCallbackData(t.encodeQuery("reset_traffic_c "+email)),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "reset_traffic_c":
|
|
err := t.inboundService.ResetClientTrafficByEmail(email)
|
|
if err == nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetTrafficSuccess", "Email=="+email))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
} else {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
}
|
|
case "limit_traffic":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 0")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" 0")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("1 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 1")),
|
|
tu.InlineKeyboardButton("5 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 5")),
|
|
tu.InlineKeyboardButton("10 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 10")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("20 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 20")),
|
|
tu.InlineKeyboardButton("30 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 30")),
|
|
tu.InlineKeyboardButton("40 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 40")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("50 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 50")),
|
|
tu.InlineKeyboardButton("60 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 60")),
|
|
tu.InlineKeyboardButton("80 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 80")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("100 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 100")),
|
|
tu.InlineKeyboardButton("150 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 150")),
|
|
tu.InlineKeyboardButton("200 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 200")),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "limit_traffic_c":
|
|
if len(dataArray) == 3 {
|
|
limitTraffic, err := strconv.Atoi(dataArray[2])
|
|
if err == nil {
|
|
needRestart, err := t.clientService.ResetClientTrafficLimitByEmail(&t.inboundService, email, limitTraffic)
|
|
if needRestart {
|
|
t.xrayService.SetToNeedRestart()
|
|
}
|
|
if err == nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.setTrafficLimitSuccess", "Email=="+email))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "limit_traffic_in":
|
|
if len(dataArray) >= 3 {
|
|
oldInputNumber, err := strconv.Atoi(dataArray[2])
|
|
inputNumber := oldInputNumber
|
|
if err == nil {
|
|
if len(dataArray) == 4 {
|
|
num, err := strconv.Atoi(dataArray[3])
|
|
if err == nil {
|
|
inputNumber = updateNumericInput(inputNumber, num)
|
|
}
|
|
if inputNumber == oldInputNumber {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
return
|
|
}
|
|
if inputNumber >= 999999 {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
return
|
|
}
|
|
}
|
|
inlineKeyboard := t.numericKeypad(numericKeypadSpec{dataBase: "limit_traffic", dataArgs: email + " ", cancelData: "client_cancel " + email, confirmLabelKey: "tgbot.buttons.confirmNumberAdd"}, inputNumber)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
return
|
|
}
|
|
}
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "add_client_limit_traffic_c":
|
|
limitTraffic, _ := strconv.ParseInt(dataArray[1], 10, 64)
|
|
draft.totalGB = limitTraffic * 1024 * 1024 * 1024
|
|
messageId := callbackQuery.Message.GetMessageID()
|
|
message_text := t.BuildClientDraftMessage(draft)
|
|
|
|
t.addClient(callbackQuery.Message.GetChat().ID, draft, message_text, messageId)
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
case "add_client_limit_traffic_in":
|
|
if len(dataArray) >= 2 {
|
|
oldInputNumber, err := strconv.Atoi(dataArray[1])
|
|
inputNumber := oldInputNumber
|
|
if err == nil {
|
|
if len(dataArray) == 3 {
|
|
num, err := strconv.Atoi(dataArray[2])
|
|
if err == nil {
|
|
inputNumber = updateNumericInput(inputNumber, num)
|
|
}
|
|
if inputNumber == oldInputNumber {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
return
|
|
}
|
|
if inputNumber >= 999999 {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
return
|
|
}
|
|
}
|
|
inlineKeyboard := t.numericKeypad(numericKeypadSpec{dataBase: "add_client_limit_traffic", cancelData: "add_client_default_traffic_exp", confirmLabelKey: "tgbot.buttons.confirmNumberAdd"}, inputNumber)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
return
|
|
}
|
|
}
|
|
case "reset_exp":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 0")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("reset_exp_in "+email+" 0")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 7 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 7")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 10 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 10")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 14 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 14")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 20 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 20")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 1 "+t.I18nBot("tgbot.month")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 30")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 3 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 90")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 6 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 180")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 12 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 365")),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "reset_exp_c":
|
|
if len(dataArray) == 3 {
|
|
days, err := strconv.ParseInt(dataArray[2], 10, 64)
|
|
if err == nil {
|
|
var date int64
|
|
if days > 0 {
|
|
traffic, err := t.inboundService.GetClientTrafficByEmail(email)
|
|
if err != nil {
|
|
logger.Warning(err)
|
|
msg := t.I18nBot("tgbot.wentWrong")
|
|
t.SendMsgToTgbot(chatId, msg)
|
|
return
|
|
}
|
|
if traffic == nil {
|
|
msg := t.I18nBot("tgbot.noResult")
|
|
t.SendMsgToTgbot(chatId, msg)
|
|
return
|
|
}
|
|
|
|
if traffic.ExpiryTime > 0 {
|
|
if traffic.ExpiryTime-time.Now().Unix()*1000 < 0 {
|
|
date = -(days * 24 * 60 * 60000)
|
|
} else {
|
|
date = traffic.ExpiryTime + days*24*60*60000
|
|
}
|
|
} else {
|
|
date = traffic.ExpiryTime - days*24*60*60000
|
|
}
|
|
|
|
}
|
|
needRestart, err := t.clientService.ResetClientExpiryTimeByEmail(&t.inboundService, email, date)
|
|
if needRestart {
|
|
t.xrayService.SetToNeedRestart()
|
|
}
|
|
if err == nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.expireResetSuccess", "Email=="+email))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "reset_exp_in":
|
|
if len(dataArray) >= 3 {
|
|
oldInputNumber, err := strconv.Atoi(dataArray[2])
|
|
inputNumber := oldInputNumber
|
|
if err == nil {
|
|
if len(dataArray) == 4 {
|
|
num, err := strconv.Atoi(dataArray[3])
|
|
if err == nil {
|
|
inputNumber = updateNumericInput(inputNumber, num)
|
|
}
|
|
if inputNumber == oldInputNumber {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
return
|
|
}
|
|
if inputNumber >= 999999 {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
return
|
|
}
|
|
}
|
|
inlineKeyboard := t.numericKeypad(numericKeypadSpec{dataBase: "reset_exp", dataArgs: email + " ", cancelData: "client_cancel " + email, confirmLabelKey: "tgbot.buttons.confirmNumber"}, inputNumber)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
return
|
|
}
|
|
}
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "add_client_reset_exp_c":
|
|
// The wizard's presets and its custom keypad land in this one case, so a
|
|
// second tap replaces the term it set; 0 is the Unlimited button.
|
|
days, _ := strconv.ParseInt(dataArray[1], 10, 64)
|
|
draft.expiryTime = -days * 24 * 60 * 60000
|
|
|
|
messageId := callbackQuery.Message.GetMessageID()
|
|
message_text := t.BuildClientDraftMessage(draft)
|
|
|
|
t.addClient(callbackQuery.Message.GetChat().ID, draft, message_text, messageId)
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
case "add_client_reset_exp_in":
|
|
if len(dataArray) >= 2 {
|
|
oldInputNumber, err := strconv.Atoi(dataArray[1])
|
|
inputNumber := oldInputNumber
|
|
if err == nil {
|
|
if len(dataArray) == 3 {
|
|
num, err := strconv.Atoi(dataArray[2])
|
|
if err == nil {
|
|
inputNumber = updateNumericInput(inputNumber, num)
|
|
}
|
|
if inputNumber == oldInputNumber {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
return
|
|
}
|
|
if inputNumber >= 999999 {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
return
|
|
}
|
|
}
|
|
inlineKeyboard := t.numericKeypad(numericKeypadSpec{dataBase: "add_client_reset_exp", cancelData: "add_client_default_traffic_exp", confirmLabelKey: "tgbot.buttons.confirmNumberAdd"}, inputNumber)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
return
|
|
}
|
|
}
|
|
case "ip_limit":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelIpLimit")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 0")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("ip_limit_in "+email+" 0")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 1")),
|
|
tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 2")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 3")),
|
|
tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 4")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 5")),
|
|
tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 6")),
|
|
tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 7")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 8")),
|
|
tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 9")),
|
|
tu.InlineKeyboardButton("10").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 10")),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "ip_limit_c":
|
|
if len(dataArray) == 3 {
|
|
count, err := strconv.Atoi(dataArray[2])
|
|
if err == nil {
|
|
needRestart, err := t.clientService.ResetClientIpLimitByEmail(&t.inboundService, email, count)
|
|
if needRestart {
|
|
t.xrayService.SetToNeedRestart()
|
|
}
|
|
if err == nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetIpSuccess", "Email=="+email, "Count=="+strconv.Itoa(count)))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "ip_limit_in":
|
|
if len(dataArray) >= 3 {
|
|
oldInputNumber, err := strconv.Atoi(dataArray[2])
|
|
inputNumber := oldInputNumber
|
|
if err == nil {
|
|
if len(dataArray) == 4 {
|
|
num, err := strconv.Atoi(dataArray[3])
|
|
if err == nil {
|
|
inputNumber = updateNumericInput(inputNumber, num)
|
|
}
|
|
if inputNumber == oldInputNumber {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
return
|
|
}
|
|
if inputNumber >= 999999 {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
return
|
|
}
|
|
}
|
|
inlineKeyboard := t.numericKeypad(numericKeypadSpec{dataBase: "ip_limit", dataArgs: email + " ", cancelData: "client_cancel " + email, confirmLabelKey: "tgbot.buttons.confirmNumber"}, inputNumber)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
return
|
|
}
|
|
}
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
case "add_client_ip_limit_c":
|
|
if len(dataArray) == 2 {
|
|
count, _ := strconv.Atoi(dataArray[1])
|
|
draft.limitIP = count
|
|
}
|
|
|
|
messageId := callbackQuery.Message.GetMessageID()
|
|
message_text := t.BuildClientDraftMessage(draft)
|
|
|
|
t.addClient(callbackQuery.Message.GetChat().ID, draft, message_text, messageId)
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
case "add_client_ip_limit_in":
|
|
if len(dataArray) >= 2 {
|
|
oldInputNumber, err := strconv.Atoi(dataArray[1])
|
|
inputNumber := oldInputNumber
|
|
if err == nil {
|
|
if len(dataArray) == 3 {
|
|
num, err := strconv.Atoi(dataArray[2])
|
|
if err == nil {
|
|
inputNumber = updateNumericInput(inputNumber, num)
|
|
}
|
|
if inputNumber == oldInputNumber {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
return
|
|
}
|
|
if inputNumber >= 999999 {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
return
|
|
}
|
|
}
|
|
inlineKeyboard := t.numericKeypad(numericKeypadSpec{dataBase: "add_client_ip_limit", cancelData: "add_client_default_ip_limit", confirmLabelKey: "tgbot.buttons.confirmNumber"}, inputNumber)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
return
|
|
}
|
|
}
|
|
case "clear_ips":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("ips_cancel "+email)),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmClearIps")).WithCallbackData(t.encodeQuery("clear_ips_c "+email)),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "clear_ips_c":
|
|
err := t.inboundService.ClearClientIps(email)
|
|
if err == nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.clearIpSuccess", "Email=="+email))
|
|
t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID())
|
|
} else {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
}
|
|
case "ip_log":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getIpLog", "Email=="+email))
|
|
t.searchClientIps(chatId, email)
|
|
case "tg_user":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getUserInfo", "Email=="+email))
|
|
t.clientTelegramUserInfo(chatId, email)
|
|
case "client_invite_link":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.inviteLink"))
|
|
t.sendInviteLink(chatId, email)
|
|
case "tgid_remove":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("tgid_cancel "+email)),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmRemoveTGUser")).WithCallbackData(t.encodeQuery("tgid_remove_c "+email)),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "tgid_remove_c":
|
|
traffic, err := t.inboundService.GetClientTrafficByEmail(email)
|
|
if err != nil || traffic == nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
return
|
|
}
|
|
needRestart, err := t.clientService.SetClientTelegramUserID(&t.inboundService, traffic.Id, EmptyTelegramUserID)
|
|
if needRestart {
|
|
t.xrayService.SetToNeedRestart()
|
|
}
|
|
if err == nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.removedTGUserSuccess", "Email=="+email))
|
|
t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
|
|
} else {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
}
|
|
case "toggle_enable":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmToggle")).WithCallbackData(t.encodeQuery("toggle_enable_c "+email)),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "toggle_enable_c":
|
|
enabled, needRestart, err := t.clientService.ToggleClientEnableByEmail(&t.inboundService, email)
|
|
if needRestart {
|
|
t.xrayService.SetToNeedRestart()
|
|
}
|
|
if err == nil {
|
|
if enabled {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.enableSuccess", "Email=="+email))
|
|
} else {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.disableSuccess", "Email=="+email))
|
|
}
|
|
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
} else {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
}
|
|
case "get_clients":
|
|
inboundId := dataArray[1]
|
|
inboundIdInt, err := strconv.Atoi(inboundId)
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
inbound, err := t.inboundService.GetInbound(inboundIdInt)
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
clients, err := t.getInboundClients(inboundIdInt)
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseClient", "Inbound=="+inbound.Remark), clients)
|
|
case "add_client_to":
|
|
draft.email = t.randomLowerAndNum(8)
|
|
draft.limitIP = 0
|
|
draft.totalGB = 0
|
|
draft.expiryTime = 0
|
|
draft.enable = true
|
|
draft.tgID = ""
|
|
draft.subID = t.randomLowerAndNum(16)
|
|
draft.comment = ""
|
|
draft.reset = 0
|
|
|
|
inboundId := dataArray[1]
|
|
inboundIdInt, err := strconv.Atoi(inboundId)
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
draft.receiverInboundID = inboundIdInt
|
|
draft.receiverInboundIDs = []int{inboundIdInt}
|
|
t.addClient(callbackQuery.Message.GetChat().ID, draft, t.BuildClientDraftMessage(draft))
|
|
case "add_client_toggle_attach":
|
|
inboundIdStr := dataArray[1]
|
|
inboundIdInt, err := strconv.Atoi(inboundIdStr)
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
found := -1
|
|
for i, id := range draft.receiverInboundIDs {
|
|
if id == inboundIdInt {
|
|
found = i
|
|
break
|
|
}
|
|
}
|
|
if found >= 0 {
|
|
draft.receiverInboundIDs = append(draft.receiverInboundIDs[:found], draft.receiverInboundIDs[found+1:]...)
|
|
} else {
|
|
draft.receiverInboundIDs = append(draft.receiverInboundIDs, inboundIdInt)
|
|
}
|
|
picker, err := t.getInboundsAttachPicker(draft)
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.editMessageCallbackTgBot(callbackQuery.Message.GetChat().ID, callbackQuery.Message.GetMessageID(), picker)
|
|
default:
|
|
// An unknown action with arguments is still a tap, and an
|
|
// unanswered tap spins until Telegram times it out.
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
}
|
|
return
|
|
} else {
|
|
switch callbackQuery.Data {
|
|
case "broadcast_cancel":
|
|
t.cancelBroadcast(actor, callbackQuery.Message.GetMessageID(), callbackQuery.ID)
|
|
return
|
|
case "get_inbounds":
|
|
inbounds, err := t.getInbounds()
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
|
|
}
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.allClients"))
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
|
|
case "admin_client_sub_links":
|
|
inbounds, err := t.getInboundsFor("get_clients_for_sub")
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
|
|
case "admin_client_individual_links":
|
|
inbounds, err := t.getInboundsFor("get_clients_for_individual")
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
|
|
case "admin_client_qr_links":
|
|
inbounds, err := t.getInboundsFor("get_clients_for_qr")
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
|
|
}
|
|
}
|
|
}
|
|
|
|
if !isAdmin {
|
|
// encodeQuery hashes any payload past 64 chars, so a long email's button
|
|
// must be decoded before the gate can see which client it names.
|
|
if decoded, err := t.decodeQuery(callbackQuery.Data); err == nil {
|
|
callbackQuery.Data = decoded
|
|
}
|
|
if !isClientSelfCallback(callbackQuery.Data) {
|
|
return
|
|
}
|
|
}
|
|
|
|
switch callbackQuery.Data {
|
|
case "get_usage":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.serverUsage"))
|
|
t.getServerUsage(chatId)
|
|
case "usage_refresh":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
t.getServerUsage(chatId, callbackQuery.Message.GetMessageID())
|
|
case "inbounds":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getInbounds"))
|
|
t.SendMsgToTgbot(chatId, t.getInboundUsages())
|
|
case "deplete_soon":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.depleteSoon"))
|
|
t.getExhausted(chatId)
|
|
case "get_backup":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.dbBackup"))
|
|
t.sendBackup(chatId)
|
|
case "get_banlogs":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getBanLogs"))
|
|
t.sendBanLogs(chatId, true)
|
|
case "client_traffic":
|
|
tgUserID := callbackQuery.From.ID
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.clientUsage"))
|
|
t.getClientUsage(chatId, tgUserID)
|
|
case "client_commands":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands"))
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpClientCommands"))
|
|
case "client_sub_links":
|
|
// show user's own clients to choose one for sub links
|
|
tgUserID := callbackQuery.From.ID
|
|
traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID)
|
|
if err != nil {
|
|
// fallback to message
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
|
|
return
|
|
}
|
|
if len(traffics) == 0 {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10)))
|
|
return
|
|
}
|
|
var buttons []telego.InlineKeyboardButton
|
|
for _, tr := range traffics {
|
|
buttons = append(buttons, tu.InlineKeyboardButton(tr.Email).WithCallbackData(t.encodeQuery("client_sub_links "+tr.Email)))
|
|
}
|
|
cols := 1
|
|
if len(buttons) >= 6 {
|
|
cols = 2
|
|
}
|
|
keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.pleaseChoose"), keyboard)
|
|
case "client_individual_links":
|
|
// show user's clients to choose for individual links
|
|
tgUserID := callbackQuery.From.ID
|
|
traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID)
|
|
if err != nil {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
|
|
return
|
|
}
|
|
if len(traffics) == 0 {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10)))
|
|
return
|
|
}
|
|
var buttons2 []telego.InlineKeyboardButton
|
|
for _, tr := range traffics {
|
|
buttons2 = append(buttons2, tu.InlineKeyboardButton(tr.Email).WithCallbackData(t.encodeQuery("client_individual_links "+tr.Email)))
|
|
}
|
|
cols2 := 1
|
|
if len(buttons2) >= 6 {
|
|
cols2 = 2
|
|
}
|
|
keyboard2 := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols2, buttons2...))
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.pleaseChoose"), keyboard2)
|
|
case "client_qr_links":
|
|
// show user's clients to choose for QR codes
|
|
tgUserID := callbackQuery.From.ID
|
|
traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID)
|
|
if err != nil {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOccurred")+"\r\n"+err.Error())
|
|
return
|
|
}
|
|
if len(traffics) == 0 {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10)))
|
|
return
|
|
}
|
|
var buttons3 []telego.InlineKeyboardButton
|
|
for _, tr := range traffics {
|
|
buttons3 = append(buttons3, tu.InlineKeyboardButton(tr.Email).WithCallbackData(t.encodeQuery("client_qr_links "+tr.Email)))
|
|
}
|
|
cols3 := 1
|
|
if len(buttons3) >= 6 {
|
|
cols3 = 2
|
|
}
|
|
keyboard3 := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols3, buttons3...))
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.pleaseChoose"), keyboard3)
|
|
case "onlines":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.onlines"))
|
|
t.onlineClients(chatId)
|
|
case "onlines_refresh":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
t.onlineClients(chatId, callbackQuery.Message.GetMessageID())
|
|
case "commands":
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands"))
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpAdminCommands"))
|
|
case "add_client":
|
|
draft.email = t.randomLowerAndNum(8)
|
|
draft.limitIP = 0
|
|
draft.totalGB = 0
|
|
draft.expiryTime = 0
|
|
draft.enable = true
|
|
draft.tgID = ""
|
|
draft.subID = t.randomLowerAndNum(16)
|
|
draft.comment = ""
|
|
draft.reset = 0
|
|
|
|
inbounds, err := t.getInboundsAddClient()
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.addClient"))
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
|
|
case "add_client_ch_default_email":
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
userStateMgr.set(actor, "awaiting_email")
|
|
cancel_btn_markup := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
|
|
),
|
|
)
|
|
prompt_message := t.I18nBot("tgbot.messages.email_prompt", "ClientEmail=="+html.EscapeString(draft.email))
|
|
t.SendMsgToTgbot(chatId, prompt_message, cancel_btn_markup)
|
|
case "add_client_ch_default_comment":
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
userStateMgr.set(actor, "awaiting_comment")
|
|
cancel_btn_markup := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
|
|
),
|
|
)
|
|
prompt_message := t.I18nBot("tgbot.messages.comment_prompt", "ClientComment=="+html.EscapeString(draft.comment))
|
|
t.SendMsgToTgbot(chatId, prompt_message, cancel_btn_markup)
|
|
case "add_client_ch_default_tg_id":
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
userStateMgr.set(actor, "awaiting_tg_id")
|
|
cancel_btn_markup := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
|
|
),
|
|
)
|
|
current := draft.tgID
|
|
if current == "" {
|
|
current = "—"
|
|
}
|
|
t.SendMsgToTgbot(chatId, fmt.Sprintf("Send the Telegram user id (numeric) to attach to this client, or send <code>-</code> / <code>none</code> to clear.\nCurrent: <code>%s</code>", html.EscapeString(current)), cancel_btn_markup)
|
|
case "add_client_ch_default_traffic":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("add_client_default_traffic_exp")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 0")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("add_client_limit_traffic_in 0")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("1 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 1")),
|
|
tu.InlineKeyboardButton("5 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 5")),
|
|
tu.InlineKeyboardButton("10 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 10")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("20 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 20")),
|
|
tu.InlineKeyboardButton("30 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 30")),
|
|
tu.InlineKeyboardButton("40 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 40")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("50 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 50")),
|
|
tu.InlineKeyboardButton("60 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 60")),
|
|
tu.InlineKeyboardButton("80 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 80")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("100 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 100")),
|
|
tu.InlineKeyboardButton("150 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 150")),
|
|
tu.InlineKeyboardButton("200 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 200")),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "add_client_ch_default_exp":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("add_client_default_traffic_exp")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 0")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("add_client_reset_exp_in 0")),
|
|
),
|
|
// No "Add" verb: these replace the term the draft carries, unlike the
|
|
// renewal keyboard, whose reset_exp_c handler really does add to it.
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("7 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 7")),
|
|
tu.InlineKeyboardButton("10 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 10")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("14 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 14")),
|
|
tu.InlineKeyboardButton("20 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 20")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("1 "+t.I18nBot("tgbot.month")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 30")),
|
|
tu.InlineKeyboardButton("3 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 90")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("6 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 180")),
|
|
tu.InlineKeyboardButton("12 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 365")),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "add_client_ch_default_ip_limit":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("add_client_default_ip_limit")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("add_client_ip_limit_c 0")),
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("add_client_ip_limit_in 0")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 1")),
|
|
tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 2")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 3")),
|
|
tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 4")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 5")),
|
|
tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 6")),
|
|
tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 7")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 8")),
|
|
tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 9")),
|
|
tu.InlineKeyboardButton("10").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 10")),
|
|
),
|
|
)
|
|
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
|
|
case "add_client_default_info":
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
t.SendMsgToTgbotDeleteAfter(chatId, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
userStateMgr.clear(actor)
|
|
t.addClient(chatId, draft, t.BuildClientDraftMessage(draft))
|
|
case "add_client_cancel":
|
|
userStateMgr.clear(actor)
|
|
addClientDrafts.reset(actor)
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
t.SendMsgToTgbotDeleteAfter(chatId, t.I18nBot("tgbot.messages.cancel"), 3, tu.ReplyKeyboardRemove())
|
|
case "add_client_default_traffic_exp":
|
|
messageId := callbackQuery.Message.GetMessageID()
|
|
message_text := t.BuildClientDraftMessage(draft)
|
|
t.addClient(chatId, draft, message_text, messageId)
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+draft.email))
|
|
case "add_client_default_ip_limit":
|
|
messageId := callbackQuery.Message.GetMessageID()
|
|
message_text := t.BuildClientDraftMessage(draft)
|
|
t.addClient(chatId, draft, message_text, messageId)
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+draft.email))
|
|
case "add_client_attach_more":
|
|
picker, err := t.getInboundsAttachPicker(draft)
|
|
if err != nil {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
|
|
return
|
|
}
|
|
t.SendMsgToTgbot(chatId, "Pick inbound(s) to attach:", picker)
|
|
case "add_client_attach_done":
|
|
if draft.receiverInboundID == 0 && len(draft.receiverInboundIDs) > 0 {
|
|
draft.receiverInboundID = draft.receiverInboundIDs[0]
|
|
}
|
|
if draft.receiverInboundID == 0 {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getInboundsFailed"))
|
|
return
|
|
}
|
|
message_text := t.BuildClientDraftMessage(draft)
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
t.addClient(chatId, draft, message_text)
|
|
case "add_client_submit_disable":
|
|
draft.enable = false
|
|
_, err := t.SubmitAddClient(draft)
|
|
if err != nil {
|
|
errorMessage := fmt.Sprintf("%v", err)
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.messages.error_add_client", "error=="+errorMessage), tu.ReplyKeyboardRemove())
|
|
} else {
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.successfulOperation"), tu.ReplyKeyboardRemove())
|
|
t.sendClientIndividualLinks(chatId, draft.email)
|
|
t.sendClientQRLinks(chatId, draft.email)
|
|
addClientDrafts.reset(actor)
|
|
}
|
|
case "add_client_submit_enable":
|
|
draft.enable = true
|
|
_, err := t.SubmitAddClient(draft)
|
|
if err != nil {
|
|
errorMessage := fmt.Sprintf("%v", err)
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.messages.error_add_client", "error=="+errorMessage), tu.ReplyKeyboardRemove())
|
|
} else {
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.successfulOperation"), tu.ReplyKeyboardRemove())
|
|
t.sendClientIndividualLinks(chatId, draft.email)
|
|
t.sendClientQRLinks(chatId, draft.email)
|
|
addClientDrafts.reset(actor)
|
|
}
|
|
case "reset_all_traffics_cancel":
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
t.SendMsgToTgbotDeleteAfter(chatId, t.I18nBot("tgbot.messages.cancel"), 1, tu.ReplyKeyboardRemove())
|
|
case "reset_all_traffics":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("reset_all_traffics_cancel")),
|
|
),
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmResetTraffic")).WithCallbackData(t.encodeQuery("reset_all_traffics_c")),
|
|
),
|
|
)
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.messages.AreYouSure"), inlineKeyboard)
|
|
case "reset_all_traffics_c":
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
emails, err := t.inboundService.GetAllEmails()
|
|
if err != nil {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation"), tu.ReplyKeyboardRemove())
|
|
return
|
|
}
|
|
|
|
// One report per tap, not one message per client: a large panel would
|
|
// otherwise burst past Telegram's rate limit. SendMsgToTgbot pages it.
|
|
var report strings.Builder
|
|
for _, email := range emails {
|
|
if err := t.inboundService.ResetClientTrafficByEmail(email); err == nil {
|
|
report.WriteString(t.I18nBot("tgbot.messages.SuccessResetTraffic", "ClientEmail=="+email))
|
|
} else {
|
|
report.WriteString(t.I18nBot("tgbot.messages.FailedResetTraffic", "ClientEmail=="+email, "ErrorMessage=="+err.Error()))
|
|
}
|
|
report.WriteString("\r\n\r\n")
|
|
}
|
|
report.WriteString(t.I18nBot("tgbot.messages.FinishProcess"))
|
|
|
|
// Escaped whole: one stray "<" in a remark or email otherwise makes
|
|
// Telegram reject the page it landed on, losing ~15 clients at once.
|
|
t.SendMsgToTgbot(chatId, html.EscapeString(report.String()), tu.ReplyKeyboardRemove())
|
|
case "get_sorted_traffic_usage_report":
|
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
|
emails, err := t.inboundService.GetAllEmails()
|
|
if err != nil {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation"), tu.ReplyKeyboardRemove())
|
|
return
|
|
}
|
|
validEmails, missingEmails, err := t.inboundService.FilterAndSortClientEmails(emails)
|
|
if err != nil {
|
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation"), tu.ReplyKeyboardRemove())
|
|
return
|
|
}
|
|
|
|
// Batched for the same reason as the reset report above: one message
|
|
// per client hits Telegram's rate limit on a large panel.
|
|
var report strings.Builder
|
|
for _, email := range validEmails {
|
|
traffic, err := t.inboundService.GetClientTrafficByEmail(email)
|
|
if err != nil {
|
|
logger.Warning(err)
|
|
report.WriteString(t.I18nBot("tgbot.wentWrong"))
|
|
report.WriteString("\r\n\r\n")
|
|
continue
|
|
}
|
|
if traffic == nil {
|
|
report.WriteString(t.I18nBot("tgbot.noResult"))
|
|
report.WriteString("\r\n\r\n")
|
|
continue
|
|
}
|
|
report.WriteString(t.clientInfoMsg(traffic, false, false, false, false, true, false))
|
|
report.WriteString("\r\n\r\n")
|
|
}
|
|
for _, email := range missingEmails {
|
|
fmt.Fprintf(&report, "📧 %s\r\n%s\r\n\r\n", email, t.I18nBot("tgbot.noResult"))
|
|
}
|
|
if report.Len() > 0 {
|
|
t.SendMsgToTgbot(chatId, html.EscapeString(report.String()), tu.ReplyKeyboardRemove())
|
|
}
|
|
default:
|
|
action, email, ok := splitClientLinkCallback(callbackQuery.Data)
|
|
if !ok {
|
|
// Nothing matched: an unknown button still has to be answered, or it
|
|
// keeps spinning until Telegram times the callback out.
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
return
|
|
}
|
|
// The keyboard outlives the chat it was sent to, so the email in it
|
|
// cannot authorise itself: a non-admin only reaches their own clients.
|
|
if !isAdmin && !t.clientOwnedByTgUser(callbackQuery.From.ID, email) {
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
return
|
|
}
|
|
switch action {
|
|
case "client_sub_links":
|
|
t.sendClientSubLinks(chatId, email)
|
|
case "client_individual_links":
|
|
t.sendClientIndividualLinks(chatId, email)
|
|
case "client_qr_links":
|
|
t.sendClientQRLinks(chatId, email)
|
|
}
|
|
}
|
|
}
|
|
|
|
// checkAdmin checks if the given Telegram ID is an admin.
|
|
func checkAdmin(tgId int64) bool {
|
|
return slices.Contains(adminSnapshot(), tgId)
|
|
}
|
|
|
|
// isClientSelfCallback reports whether a callback is per-user rather than
|
|
// admin-only; the caller still has to prove the client is its own.
|
|
func isClientSelfCallback(data string) bool {
|
|
switch data {
|
|
case "client_traffic", "client_commands", "client_sub_links",
|
|
"client_individual_links", "client_qr_links":
|
|
return true
|
|
}
|
|
_, _, ok := splitClientLinkCallback(data)
|
|
return ok
|
|
}
|
|
|
|
// splitClientLinkCallback splits "<action> <email>" for the per-client link
|
|
// callbacks; ok is false for every other data.
|
|
func splitClientLinkCallback(data string) (action, email string, ok bool) {
|
|
for _, candidate := range []string{"client_sub_links", "client_individual_links", "client_qr_links"} {
|
|
if rest, found := strings.CutPrefix(data, candidate+" "); found && rest != "" {
|
|
return candidate, rest, true
|
|
}
|
|
}
|
|
return "", "", false
|
|
}
|