| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | package service | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2023-05-20 15:38:01 +00:00
										 |  |  | 	"embed" | 
					
						
							| 
									
										
										
										
											2024-08-18 21:30:56 +00:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2024-01-02 09:42:07 +00:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2024-03-10 21:31:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	"x-ui/config" | 
					
						
							| 
									
										
										
										
											2023-12-08 19:35:10 +00:00
										 |  |  | 	"x-ui/database" | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	"x-ui/database/model" | 
					
						
							|  |  |  | 	"x-ui/logger" | 
					
						
							|  |  |  | 	"x-ui/util/common" | 
					
						
							| 
									
										
										
										
											2023-05-20 15:59:28 +00:00
										 |  |  | 	"x-ui/web/global" | 
					
						
							| 
									
										
										
										
											2023-05-20 15:38:01 +00:00
										 |  |  | 	"x-ui/web/locale" | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	"x-ui/xray" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	"github.com/mymmrac/telego" | 
					
						
							|  |  |  | 	th "github.com/mymmrac/telego/telegohandler" | 
					
						
							|  |  |  | 	tu "github.com/mymmrac/telego/telegoutil" | 
					
						
							| 
									
										
										
										
											2024-01-02 09:42:07 +00:00
										 |  |  | 	"github.com/valyala/fasthttp" | 
					
						
							|  |  |  | 	"github.com/valyala/fasthttp/fasthttpproxy" | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-10 21:31:24 +00:00
										 |  |  | var ( | 
					
						
							|  |  |  | 	bot         *telego.Bot | 
					
						
							|  |  |  | 	botHandler  *th.BotHandler | 
					
						
							|  |  |  | 	adminIds    []int64 | 
					
						
							|  |  |  | 	isRunning   bool | 
					
						
							|  |  |  | 	hostname    string | 
					
						
							|  |  |  | 	hashStorage *global.HashStorage | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | type LoginStatus byte | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 	LoginSuccess        LoginStatus = 1 | 
					
						
							|  |  |  | 	LoginFail           LoginStatus = 0 | 
					
						
							|  |  |  | 	EmptyTelegramUserID             = int64(0) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Tgbot struct { | 
					
						
							|  |  |  | 	inboundService InboundService | 
					
						
							|  |  |  | 	settingService SettingService | 
					
						
							|  |  |  | 	serverService  ServerService | 
					
						
							| 
									
										
										
										
											2023-05-04 23:17:26 +00:00
										 |  |  | 	xrayService    XrayService | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	lastStatus     *Status | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) NewTgbot() *Tgbot { | 
					
						
							|  |  |  | 	return new(Tgbot) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | func (t *Tgbot) I18nBot(name string, params ...string) string { | 
					
						
							| 
									
										
										
										
											2023-05-20 15:38:01 +00:00
										 |  |  | 	return locale.I18n(locale.Bot, name, params...) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:59:28 +00:00
										 |  |  | func (t *Tgbot) GetHashStorage() *global.HashStorage { | 
					
						
							| 
									
										
										
										
											2023-05-21 01:03:01 +00:00
										 |  |  | 	return hashStorage | 
					
						
							| 
									
										
										
										
											2023-05-20 15:59:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:38:01 +00:00
										 |  |  | func (t *Tgbot) Start(i18nFS embed.FS) error { | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 	// Initialize localizer
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:38:01 +00:00
										 |  |  | 	err := locale.InitLocalizer(i18nFS, &t.settingService) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 	// Initialize hash storage to store callback queries
 | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 	hashStorage = global.NewHashStorage(20 * time.Minute) | 
					
						
							| 
									
										
										
										
											2023-05-20 15:59:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	t.SetHostname() | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Get Telegram bot token
 | 
					
						
							|  |  |  | 	tgBotToken, err := t.settingService.GetTgBotToken() | 
					
						
							|  |  |  | 	if err != nil || tgBotToken == "" { | 
					
						
							|  |  |  | 		logger.Warning("Failed to get Telegram bot token:", err) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 	// Get Telegram bot chat ID(s)
 | 
					
						
							|  |  |  | 	tgBotID, err := t.settingService.GetTgBotChatId() | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 		logger.Warning("Failed to get Telegram bot chat ID:", err) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 	// Parse admin IDs from comma-separated string
 | 
					
						
							|  |  |  | 	if tgBotID != "" { | 
					
						
							|  |  |  | 		for _, adminID := range strings.Split(tgBotID, ",") { | 
					
						
							|  |  |  | 			id, err := strconv.Atoi(adminID) | 
					
						
							| 
									
										
										
										
											2023-05-31 01:31:20 +00:00
										 |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 				logger.Warning("Failed to parse admin ID from Telegram bot chat ID:", err) | 
					
						
							| 
									
										
										
										
											2023-05-31 01:31:20 +00:00
										 |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			adminIds = append(adminIds, int64(id)) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 	// Get Telegram bot proxy URL
 | 
					
						
							| 
									
										
										
										
											2024-01-02 09:42:07 +00:00
										 |  |  | 	tgBotProxy, err := t.settingService.GetTgBotProxy() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 		logger.Warning("Failed to get Telegram bot proxy URL:", err) | 
					
						
							| 
									
										
										
										
											2024-01-02 09:42:07 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 08:59:42 +00:00
										 |  |  | 	// Get Telegram bot API server URL
 | 
					
						
							|  |  |  | 	tgBotAPIServer, err := t.settingService.GetTgBotAPIServer() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning("Failed to get Telegram bot API server URL:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 	// Create new Telegram bot instance
 | 
					
						
							| 
									
										
										
										
											2024-10-17 08:59:42 +00:00
										 |  |  | 	bot, err = t.NewBot(tgBotToken, tgBotProxy, tgBotAPIServer) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 		logger.Error("Failed to initialize Telegram bot API:", err) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 	// Start receiving Telegram bot messages
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if !isRunning { | 
					
						
							| 
									
										
										
										
											2024-07-08 21:08:00 +00:00
										 |  |  | 		logger.Info("Telegram bot receiver started") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		go t.OnReceive() | 
					
						
							|  |  |  | 		isRunning = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 08:59:42 +00:00
										 |  |  | func (t *Tgbot) NewBot(token string, proxyUrl string, apiServerUrl string) (*telego.Bot, error) { | 
					
						
							|  |  |  | 	if proxyUrl == "" && apiServerUrl == "" { | 
					
						
							| 
									
										
										
										
											2024-02-03 14:45:47 +00:00
										 |  |  | 		return telego.NewBot(token) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 08:59:42 +00:00
										 |  |  | 	if proxyUrl != "" { | 
					
						
							|  |  |  | 		if !strings.HasPrefix(proxyUrl, "socks5://") { | 
					
						
							|  |  |  | 			logger.Warning("Invalid socks5 URL, using default") | 
					
						
							|  |  |  | 			return telego.NewBot(token) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_, err := url.Parse(proxyUrl) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logger.Warningf("Can't parse proxy URL, using default instance for tgbot: %v", err) | 
					
						
							|  |  |  | 			return telego.NewBot(token) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return telego.NewBot(token, telego.WithFastHTTPClient(&fasthttp.Client{ | 
					
						
							|  |  |  | 			Dial: fasthttpproxy.FasthttpSocksDialer(proxyUrl), | 
					
						
							|  |  |  | 		})) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !strings.HasPrefix(apiServerUrl, "http") { | 
					
						
							|  |  |  | 		logger.Warning("Invalid http(s) URL, using default") | 
					
						
							| 
									
										
										
										
											2024-01-02 09:42:07 +00:00
										 |  |  | 		return telego.NewBot(token) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 08:59:42 +00:00
										 |  |  | 	_, err := url.Parse(apiServerUrl) | 
					
						
							| 
									
										
										
										
											2024-01-02 09:42:07 +00:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-10-17 08:59:42 +00:00
										 |  |  | 		logger.Warningf("Can't parse API server URL, using default instance for tgbot: %v", err) | 
					
						
							| 
									
										
										
										
											2024-01-02 09:42:07 +00:00
										 |  |  | 		return telego.NewBot(token) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 08:59:42 +00:00
										 |  |  | 	return telego.NewBot(token, telego.WithAPIServer(apiServerUrl)) | 
					
						
							| 
									
										
										
										
											2024-01-02 09:42:07 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | func (t *Tgbot) IsRunning() bool { | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	return isRunning | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | func (t *Tgbot) SetHostname() { | 
					
						
							|  |  |  | 	host, err := os.Hostname() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Error("get hostname error:", err) | 
					
						
							|  |  |  | 		hostname = "" | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	hostname = host | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | func (t *Tgbot) Stop() { | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	botHandler.Stop() | 
					
						
							|  |  |  | 	bot.StopLongPolling() | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	logger.Info("Stop Telegram receiver ...") | 
					
						
							|  |  |  | 	isRunning = false | 
					
						
							|  |  |  | 	adminIds = nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | func (t *Tgbot) encodeQuery(query string) string { | 
					
						
							|  |  |  | 	// NOTE: we only need to hash for more than 64 chars
 | 
					
						
							|  |  |  | 	if len(query) <= 64 { | 
					
						
							|  |  |  | 		return query | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return hashStorage.SaveHash(query) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) decodeQuery(query string) (string, error) { | 
					
						
							|  |  |  | 	if !hashStorage.IsMD5(query) { | 
					
						
							|  |  |  | 		return query, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	decoded, exists := hashStorage.GetValue(query) | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							|  |  |  | 		return "", common.NewError("hash not found in storage!") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return decoded, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | func (t *Tgbot) OnReceive() { | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	params := telego.GetUpdatesParams{ | 
					
						
							|  |  |  | 		Timeout: 10, | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	updates, _ := bot.UpdatesViaLongPolling(¶ms) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	botHandler, _ = th.NewBotHandler(bot, updates) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.keyboardClosed"), tu.ReplyKeyboardRemove()) | 
					
						
							|  |  |  | 	}, th.TextEqual(t.I18nBot("tgbot.buttons.closeKeyboard"))) | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) { | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		t.answerCommand(&message, message.Chat.ID, checkAdmin(message.From.ID)) | 
					
						
							|  |  |  | 	}, th.AnyCommand()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	botHandler.HandleCallbackQuery(func(_ *telego.Bot, query telego.CallbackQuery) { | 
					
						
							| 
									
										
										
										
											2024-07-07 09:55:59 +00:00
										 |  |  | 		t.answerCallback(&query, checkAdmin(query.From.ID)) | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	}, th.AnyCallbackQueryWithMessage()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) { | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 		if message.UsersShared != nil { | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 			if checkAdmin(message.From.ID) { | 
					
						
							| 
									
										
										
										
											2024-04-02 12:13:01 +00:00
										 |  |  | 				for _, sharedUser := range message.UsersShared.Users { | 
					
						
							|  |  |  | 					userID := sharedUser.UserID | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 					needRestart, err := t.inboundService.SetClientTelegramUserID(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()) | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.noResult"), tu.ReplyKeyboardRemove()) | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, th.AnyMessage()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	botHandler.Start() | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin bool) { | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 	msg, onlyMessage := "", false | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-25 19:33:17 +00:00
										 |  |  | 	command, _, commandArgs := tu.ParseCommand(message.Text) | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-16 12:39:25 +00:00
										 |  |  | 	// Helper function to handle unknown commands.
 | 
					
						
							|  |  |  | 	handleUnknownCommand := func() { | 
					
						
							|  |  |  | 		msg += t.I18nBot("tgbot.commands.unknown") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Handle the command.
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	switch command { | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	case "help": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg += t.I18nBot("tgbot.commands.help") | 
					
						
							|  |  |  | 		msg += t.I18nBot("tgbot.commands.pleaseChoose") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	case "start": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg += t.I18nBot("tgbot.commands.start", "Firstname=="+message.From.FirstName) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		if isAdmin { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 			msg += t.I18nBot("tgbot.commands.welcome", "Hostname=="+hostname) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg += "\n\n" + t.I18nBot("tgbot.commands.pleaseChoose") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	case "status": | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 		onlyMessage = true | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg += t.I18nBot("tgbot.commands.status") | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 	case "id": | 
					
						
							|  |  |  | 		onlyMessage = true | 
					
						
							|  |  |  | 		msg += t.I18nBot("tgbot.commands.getID", "ID=="+strconv.FormatInt(message.From.ID, 10)) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	case "usage": | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 		onlyMessage = true | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		if len(commandArgs) > 0 { | 
					
						
							| 
									
										
										
										
											2023-03-24 13:10:56 +00:00
										 |  |  | 			if isAdmin { | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 				t.searchClient(chatId, commandArgs[0]) | 
					
						
							| 
									
										
										
										
											2023-03-24 13:10:56 +00:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2024-10-16 12:39:25 +00:00
										 |  |  | 				t.getClientUsage(chatId, int64(message.From.ID), commandArgs[0]) | 
					
						
							| 
									
										
										
										
											2023-03-24 13:10:56 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 			msg += t.I18nBot("tgbot.commands.usage") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 	case "inbound": | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 		onlyMessage = true | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		if isAdmin && len(commandArgs) > 0 { | 
					
						
							|  |  |  | 			t.searchInbound(chatId, commandArgs[0]) | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2024-10-16 12:39:25 +00:00
										 |  |  | 			handleUnknownCommand() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	case "restart": | 
					
						
							|  |  |  | 		onlyMessage = true | 
					
						
							|  |  |  | 		if isAdmin { | 
					
						
							|  |  |  | 			if len(commandArgs) == 0 { | 
					
						
							|  |  |  | 				msg += t.I18nBot("tgbot.commands.restartUsage") | 
					
						
							|  |  |  | 			} else if strings.ToLower(commandArgs[0]) == "force" { | 
					
						
							|  |  |  | 				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() | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2024-10-16 12:39:25 +00:00
										 |  |  | 		handleUnknownCommand() | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 20:06:03 +00:00
										 |  |  | 	if msg != "" { | 
					
						
							| 
									
										
										
										
											2024-10-16 12:39:25 +00:00
										 |  |  | 		t.sendResponse(chatId, msg, onlyMessage, isAdmin) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Helper function to send the message based on onlyMessage flag.
 | 
					
						
							|  |  |  | func (t *Tgbot) sendResponse(chatId int64, msg string, onlyMessage, isAdmin bool) { | 
					
						
							|  |  |  | 	if onlyMessage { | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, msg) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		t.SendAnswer(chatId, msg, isAdmin) | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-07 09:55:59 +00:00
										 |  |  | func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool) { | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 	chatId := callbackQuery.Message.GetChat().ID | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if isAdmin { | 
					
						
							| 
									
										
										
										
											2023-05-20 17:16:42 +00:00
										 |  |  | 		// get query from hash storage
 | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 		decodedQuery, err := t.decodeQuery(callbackQuery.Data) | 
					
						
							| 
									
										
										
										
											2023-05-20 17:16:42 +00:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-05-21 01:03:01 +00:00
										 |  |  | 			t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.noQuery")) | 
					
						
							| 
									
										
										
										
											2023-05-20 17:16:42 +00:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-05-20 15:59:28 +00:00
										 |  |  | 		dataArray := strings.Split(decodedQuery, " ") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 		if len(dataArray) >= 2 && len(dataArray[1]) > 0 { | 
					
						
							|  |  |  | 			email := dataArray[1] | 
					
						
							|  |  |  | 			switch dataArray[0] { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			case "client_get_usage": | 
					
						
							|  |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.messages.email", "Email=="+email)) | 
					
						
							|  |  |  | 				t.searchClient(chatId, email) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 			case "client_refresh": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.clientRefreshSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 			case "client_cancel": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 			case "ips_refresh": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.IpRefreshSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 			case "ips_cancel": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 			case "tgid_refresh": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.TGIdRefreshSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 			case "tgid_cancel": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 			case "reset_traffic": | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 				inlineKeyboard := tu.InlineKeyboard( | 
					
						
							|  |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("client_cancel "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmResetTraffic")).WithCallbackData(t.encodeQuery("reset_traffic_c "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 					), | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2023-05-05 12:32:16 +00:00
										 |  |  | 			case "reset_traffic_c": | 
					
						
							| 
									
										
										
										
											2023-05-05 01:04:39 +00:00
										 |  |  | 				err := t.inboundService.ResetClientTrafficByEmail(email) | 
					
						
							|  |  |  | 				if err == nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 					t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetTrafficSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 					t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-04 23:17:26 +00:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 					t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2023-05-04 23:17:26 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 			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( | 
					
						
							| 
									
										
										
										
											2024-01-29 20:06:03 +00:00
										 |  |  | 						tu.InlineKeyboardButton("50 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 50")), | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 						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")), | 
					
						
							|  |  |  | 					), | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 			case "limit_traffic_c": | 
					
						
							|  |  |  | 				if len(dataArray) == 3 { | 
					
						
							|  |  |  | 					limitTraffic, err := strconv.Atoi(dataArray[2]) | 
					
						
							|  |  |  | 					if err == nil { | 
					
						
							| 
									
										
										
										
											2024-03-15 18:13:20 +00:00
										 |  |  | 						needRestart, err := t.inboundService.ResetClientTrafficLimitByEmail(email, limitTraffic) | 
					
						
							|  |  |  | 						if needRestart { | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 							t.xrayService.SetToNeedRestart() | 
					
						
							| 
									
										
										
										
											2024-03-15 18:13:20 +00:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						if err == nil { | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 							t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.setTrafficLimitSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 							t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 							return | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 			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 { | 
					
						
							|  |  |  | 								if num == -2 { | 
					
						
							|  |  |  | 									inputNumber = 0 | 
					
						
							|  |  |  | 								} else if num == -1 { | 
					
						
							|  |  |  | 									if inputNumber > 0 { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 										inputNumber = (inputNumber / 10) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 									} | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									inputNumber = (inputNumber * 10) + 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 := tu.InlineKeyboard( | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 								tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumberAdd", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" "+strconv.Itoa(inputNumber))), | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 1")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 2")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 3")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 4")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 5")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 6")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 7")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 8")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 9")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" -2")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 0")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" -1")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 						) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 						t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 						return | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 12:32:16 +00:00
										 |  |  | 			case "reset_exp": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				inlineKeyboard := tu.InlineKeyboard( | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("client_cancel "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 0")), | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("reset_exp_in "+email+" 0")), | 
					
						
							| 
									
										
										
										
											2023-05-04 22:18:37 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 						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")), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 						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")), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 						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")), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 						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")), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 					), | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2023-05-05 12:32:16 +00:00
										 |  |  | 			case "reset_exp_c": | 
					
						
							| 
									
										
										
										
											2023-05-05 01:04:39 +00:00
										 |  |  | 				if len(dataArray) == 3 { | 
					
						
							|  |  |  | 					days, err := strconv.Atoi(dataArray[2]) | 
					
						
							|  |  |  | 					if err == nil { | 
					
						
							| 
									
										
										
										
											2023-05-04 22:18:37 +00:00
										 |  |  | 						var date int64 = 0 | 
					
						
							|  |  |  | 						if days > 0 { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 							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 = -int64(days * 24 * 60 * 60000) | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									date = traffic.ExpiryTime + int64(days*24*60*60000) | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								date = traffic.ExpiryTime - int64(days*24*60*60000) | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 22:18:37 +00:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2024-03-15 18:13:20 +00:00
										 |  |  | 						needRestart, err := t.inboundService.ResetClientExpiryTimeByEmail(email, date) | 
					
						
							|  |  |  | 						if needRestart { | 
					
						
							| 
									
										
										
										
											2023-05-04 23:17:26 +00:00
										 |  |  | 							t.xrayService.SetToNeedRestart() | 
					
						
							| 
									
										
										
										
											2024-03-15 18:13:20 +00:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						if err == nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 							t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.expireResetSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 							t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 01:04:39 +00:00
										 |  |  | 							return | 
					
						
							| 
									
										
										
										
											2023-05-04 23:17:26 +00:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 			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 { | 
					
						
							|  |  |  | 								if num == -2 { | 
					
						
							|  |  |  | 									inputNumber = 0 | 
					
						
							|  |  |  | 								} else if num == -1 { | 
					
						
							|  |  |  | 									if inputNumber > 0 { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 										inputNumber = (inputNumber / 10) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 									} | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									inputNumber = (inputNumber * 10) + 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 := 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.confirmNumber", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" "+strconv.Itoa(inputNumber))), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 1")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 2")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 3")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 4")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 5")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 6")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 7")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 8")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 9")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" -2")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 0")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" -1")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 						) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 						t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 						return | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 			case "ip_limit": | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 				inlineKeyboard := tu.InlineKeyboard( | 
					
						
							|  |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelIpLimit")).WithCallbackData(t.encodeQuery("client_cancel "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 0")), | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("ip_limit_in "+email+" 0")), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 1")), | 
					
						
							|  |  |  | 						tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 2")), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 3")), | 
					
						
							|  |  |  | 						tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 4")), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						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")), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						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")), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					), | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 			case "ip_limit_c": | 
					
						
							|  |  |  | 				if len(dataArray) == 3 { | 
					
						
							|  |  |  | 					count, err := strconv.Atoi(dataArray[2]) | 
					
						
							|  |  |  | 					if err == nil { | 
					
						
							| 
									
										
										
										
											2024-03-15 18:13:20 +00:00
										 |  |  | 						needRestart, err := t.inboundService.ResetClientIpLimitByEmail(email, count) | 
					
						
							|  |  |  | 						if needRestart { | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 							t.xrayService.SetToNeedRestart() | 
					
						
							| 
									
										
										
										
											2024-03-15 18:13:20 +00:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						if err == nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 							t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetIpSuccess", "Email=="+email, "Count=="+strconv.Itoa(count))) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 							t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 							return | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 			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 { | 
					
						
							|  |  |  | 								if num == -2 { | 
					
						
							|  |  |  | 									inputNumber = 0 | 
					
						
							|  |  |  | 								} else if num == -1 { | 
					
						
							|  |  |  | 									if inputNumber > 0 { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 										inputNumber = (inputNumber / 10) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 									} | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									inputNumber = (inputNumber * 10) + 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 := 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.confirmNumber", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("ip_limit_c "+email+" "+strconv.Itoa(inputNumber))), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 1")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 2")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 3")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 4")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 5")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 6")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 7")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 8")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 9")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" -2")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 0")), | 
					
						
							|  |  |  | 								tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" -1")), | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 						) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 						t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 						return | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 			case "clear_ips": | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 				inlineKeyboard := tu.InlineKeyboard( | 
					
						
							|  |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("ips_cancel "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmClearIps")).WithCallbackData(t.encodeQuery("clear_ips_c "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					), | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 			case "clear_ips_c": | 
					
						
							|  |  |  | 				err := t.inboundService.ClearClientIps(email) | 
					
						
							|  |  |  | 				if err == nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 					t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.clearIpSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 					t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 					t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			case "ip_log": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getIpLog", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 				t.searchClientIps(chatId, email) | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 			case "tg_user": | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getUserInfo", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 				t.clientTelegramUserInfo(chatId, email) | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 			case "tgid_remove": | 
					
						
							|  |  |  | 				inlineKeyboard := tu.InlineKeyboard( | 
					
						
							|  |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("tgid_cancel "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 					), | 
					
						
							|  |  |  | 					tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 						tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmRemoveTGUser")).WithCallbackData(t.encodeQuery("tgid_remove_c "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 					), | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 			case "tgid_remove_c": | 
					
						
							|  |  |  | 				traffic, err := t.inboundService.GetClientTrafficByEmail(email) | 
					
						
							|  |  |  | 				if err != nil || traffic == nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 					t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 					return | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 				needRestart, err := t.inboundService.SetClientTelegramUserID(traffic.Id, EmptyTelegramUserID) | 
					
						
							| 
									
										
										
										
											2024-03-15 18:13:20 +00:00
										 |  |  | 				if needRestart { | 
					
						
							|  |  |  | 					t.xrayService.SetToNeedRestart() | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 				if err == nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 					t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.removedTGUserSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 					t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 					t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 			case "toggle_enable": | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 				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)), | 
					
						
							|  |  |  | 					), | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			case "toggle_enable_c": | 
					
						
							| 
									
										
										
										
											2024-03-15 18:13:20 +00:00
										 |  |  | 				enabled, needRestart, err := t.inboundService.ToggleClientEnableByEmail(email) | 
					
						
							|  |  |  | 				if needRestart { | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					t.xrayService.SetToNeedRestart() | 
					
						
							| 
									
										
										
										
											2024-03-15 18:13:20 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				if err == nil { | 
					
						
							| 
									
										
										
										
											2023-05-05 16:20:40 +00:00
										 |  |  | 					if enabled { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 						t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.enableSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 						t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.disableSuccess", "Email=="+email)) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 					t.searchClient(chatId, email, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 					t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2024-08-18 21:30:56 +00:00
										 |  |  | 			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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			return | 
					
						
							| 
									
										
										
										
											2024-08-18 21:30:56 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			switch callbackQuery.Data { | 
					
						
							|  |  |  | 			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) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	switch callbackQuery.Data { | 
					
						
							|  |  |  | 	case "get_usage": | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.serverUsage")) | 
					
						
							| 
									
										
										
										
											2024-05-14 12:00:10 +00:00
										 |  |  | 		t.getServerUsage(chatId) | 
					
						
							|  |  |  | 	case "usage_refresh": | 
					
						
							|  |  |  | 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation")) | 
					
						
							|  |  |  | 		t.getServerUsage(chatId, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	case "inbounds": | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getInbounds")) | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, t.getInboundUsages()) | 
					
						
							| 
									
										
										
										
											2023-04-09 19:43:18 +00:00
										 |  |  | 	case "deplete_soon": | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.depleteSoon")) | 
					
						
							|  |  |  | 		t.getExhausted(chatId) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	case "get_backup": | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.dbBackup")) | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		t.sendBackup(chatId) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	case "get_banlogs": | 
					
						
							|  |  |  | 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getBanLogs")) | 
					
						
							|  |  |  | 		t.sendBanLogs(chatId, true) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	case "client_traffic": | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 		tgUserID := callbackQuery.From.ID | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.clientUsage")) | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 		t.getClientUsage(chatId, tgUserID) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	case "client_commands": | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands")) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpClientCommands")) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	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")) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 		t.onlineClients(chatId, callbackQuery.Message.GetMessageID()) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	case "commands": | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands")) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpAdminCommands")) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func checkAdmin(tgId int64) bool { | 
					
						
							|  |  |  | 	for _, adminId := range adminIds { | 
					
						
							|  |  |  | 		if adminId == tgId { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) { | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	numericKeyboard := tu.InlineKeyboard( | 
					
						
							|  |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.serverUsage")).WithCallbackData(t.encodeQuery("get_usage")), | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		), | 
					
						
							|  |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.dbBackup")).WithCallbackData(t.encodeQuery("get_backup")), | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getBanLogs")).WithCallbackData(t.encodeQuery("get_banlogs")), | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getInbounds")).WithCallbackData(t.encodeQuery("inbounds")), | 
					
						
							|  |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.depleteSoon")).WithCallbackData(t.encodeQuery("deplete_soon")), | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("commands")), | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.onlines")).WithCallbackData(t.encodeQuery("onlines")), | 
					
						
							| 
									
										
										
										
											2024-08-18 21:30:56 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.allClients")).WithCallbackData(t.encodeQuery("get_inbounds")), | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2024-10-16 12:39:25 +00:00
										 |  |  | 		// TODOOOOOOOOOOOOOO: Add restart button here.
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	) | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	numericKeyboardClient := tu.InlineKeyboard( | 
					
						
							|  |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.clientUsage")).WithCallbackData(t.encodeQuery("client_traffic")), | 
					
						
							|  |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("client_commands")), | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		), | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	var ReplyMarkup telego.ReplyMarkup | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if isAdmin { | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 		ReplyMarkup = numericKeyboard | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 		ReplyMarkup = numericKeyboardClient | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	t.SendMsgToTgbot(chatId, msg, ReplyMarkup) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | func (t *Tgbot) SendMsgToTgbot(chatId int64, msg string, replyMarkup ...telego.ReplyMarkup) { | 
					
						
							| 
									
										
										
										
											2023-05-13 10:01:46 +00:00
										 |  |  | 	if !isRunning { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	if msg == "" { | 
					
						
							|  |  |  | 		logger.Info("[tgbot] message is empty!") | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	var allMessages []string | 
					
						
							|  |  |  | 	limit := 2000 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	// paging message if it is big
 | 
					
						
							|  |  |  | 	if len(msg) > limit { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		messages := strings.Split(msg, "\r\n\r\n") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		lastIndex := -1 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		for _, message := range messages { | 
					
						
							|  |  |  | 			if (len(allMessages) == 0) || (len(allMessages[lastIndex])+len(message) > limit) { | 
					
						
							|  |  |  | 				allMessages = append(allMessages, message) | 
					
						
							|  |  |  | 				lastIndex++ | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 				allMessages[lastIndex] += "\r\n\r\n" + message | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		if strings.TrimSpace(allMessages[len(allMessages)-1]) == "" { | 
					
						
							|  |  |  | 			allMessages = allMessages[:len(allMessages)-1] | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		allMessages = append(allMessages, msg) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	for n, message := range allMessages { | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		params := telego.SendMessageParams{ | 
					
						
							|  |  |  | 			ChatID:    tu.ID(chatId), | 
					
						
							|  |  |  | 			Text:      message, | 
					
						
							|  |  |  | 			ParseMode: "HTML", | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-03-10 21:31:24 +00:00
										 |  |  | 		// only add replyMarkup to last message
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		if len(replyMarkup) > 0 && n == (len(allMessages)-1) { | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 			params.ReplyMarkup = replyMarkup[0] | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		_, err := bot.SendMessage(¶ms) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logger.Warning("Error sending telegram message :", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		time.Sleep(500 * time.Millisecond) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | func (t *Tgbot) SendMsgToTgbotAdmins(msg string, replyMarkup ...telego.ReplyMarkup) { | 
					
						
							|  |  |  | 	if len(replyMarkup) > 0 { | 
					
						
							|  |  |  | 		for _, adminId := range adminIds { | 
					
						
							|  |  |  | 			t.SendMsgToTgbot(adminId, msg, replyMarkup[0]) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		for _, adminId := range adminIds { | 
					
						
							|  |  |  | 			t.SendMsgToTgbot(adminId, msg) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) SendReport() { | 
					
						
							|  |  |  | 	runTime, err := t.settingService.GetTgbotRuntime() | 
					
						
							|  |  |  | 	if err == nil && len(runTime) > 0 { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg := "" | 
					
						
							|  |  |  | 		msg += t.I18nBot("tgbot.messages.report", "RunTime=="+runTime) | 
					
						
							|  |  |  | 		msg += t.I18nBot("tgbot.messages.datetime", "DateTime=="+time.Now().Format("2006-01-02 15:04:05")) | 
					
						
							|  |  |  | 		t.SendMsgToTgbotAdmins(msg) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 12:00:10 +00:00
										 |  |  | 	info := t.sendServerUsage() | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	t.SendMsgToTgbotAdmins(info) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	t.sendExhaustedToAdmins() | 
					
						
							|  |  |  | 	t.notifyExhausted() | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	backupEnable, err := t.settingService.GetTgBotBackup() | 
					
						
							|  |  |  | 	if err == nil && backupEnable { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		t.SendBackupToAdmins() | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | func (t *Tgbot) SendBackupToAdmins() { | 
					
						
							|  |  |  | 	if !t.IsRunning() { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-18 21:01:05 +00:00
										 |  |  | 	for _, adminId := range adminIds { | 
					
						
							|  |  |  | 		t.sendBackup(int64(adminId)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | func (t *Tgbot) sendExhaustedToAdmins() { | 
					
						
							|  |  |  | 	if !t.IsRunning() { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, adminId := range adminIds { | 
					
						
							|  |  |  | 		t.getExhausted(int64(adminId)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 12:00:10 +00:00
										 |  |  | func (t *Tgbot) getServerUsage(chatId int64, messageID ...int) string { | 
					
						
							|  |  |  | 	info := t.prepareServerUsageInfo() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	keyboard := tu.InlineKeyboard(tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 		tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("usage_refresh")))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(messageID) > 0 { | 
					
						
							|  |  |  | 		t.editMessageTgBot(chatId, messageID[0], info, keyboard) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, info, keyboard) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return info | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-07 09:55:59 +00:00
										 |  |  | // Send server usage without an inline keyboard
 | 
					
						
							| 
									
										
										
										
											2024-05-14 12:00:10 +00:00
										 |  |  | func (t *Tgbot) sendServerUsage() string { | 
					
						
							|  |  |  | 	info := t.prepareServerUsageInfo() | 
					
						
							|  |  |  | 	return info | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) prepareServerUsageInfo() string { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	info, ipv4, ipv6 := "", "", "" | 
					
						
							| 
									
										
										
										
											2024-04-29 06:44:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// get latest status of server
 | 
					
						
							|  |  |  | 	t.lastStatus = t.serverService.GetStatus(t.lastStatus) | 
					
						
							|  |  |  | 	onlines := p.GetOnlineClients() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	info += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname) | 
					
						
							|  |  |  | 	info += t.I18nBot("tgbot.messages.version", "Version=="+config.GetVersion()) | 
					
						
							| 
									
										
										
										
											2024-04-29 06:44:16 +00:00
										 |  |  | 	info += t.I18nBot("tgbot.messages.xrayVersion", "XrayVersion=="+fmt.Sprint(t.lastStatus.Xray.Version)) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// get ip address
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	netInterfaces, err := net.Interfaces() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		logger.Error("net.Interfaces failed, err: ", err.Error()) | 
					
						
							|  |  |  | 		info += t.I18nBot("tgbot.messages.ip", "IP=="+t.I18nBot("tgbot.unknown")) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		info += "\r\n" | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		for i := 0; i < len(netInterfaces); i++ { | 
					
						
							|  |  |  | 			if (netInterfaces[i].Flags & net.FlagUp) != 0 { | 
					
						
							|  |  |  | 				addrs, _ := netInterfaces[i].Addrs() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				for _, address := range addrs { | 
					
						
							|  |  |  | 					if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { | 
					
						
							|  |  |  | 						if ipnet.IP.To4() != nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 							ipv4 += ipnet.IP.String() + " " | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 						} else if ipnet.IP.To16() != nil && !ipnet.IP.IsLinkLocalUnicast() { | 
					
						
							|  |  |  | 							ipv6 += ipnet.IP.String() + " " | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		info += t.I18nBot("tgbot.messages.ipv4", "IPv4=="+ipv4) | 
					
						
							|  |  |  | 		info += t.I18nBot("tgbot.messages.ipv6", "IPv6=="+ipv6) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	info += t.I18nBot("tgbot.messages.serverUpTime", "UpTime=="+strconv.FormatUint(t.lastStatus.Uptime/86400, 10), "Unit=="+t.I18nBot("tgbot.days")) | 
					
						
							|  |  |  | 	info += t.I18nBot("tgbot.messages.serverLoad", "Load1=="+strconv.FormatFloat(t.lastStatus.Loads[0], 'f', 2, 64), "Load2=="+strconv.FormatFloat(t.lastStatus.Loads[1], 'f', 2, 64), "Load3=="+strconv.FormatFloat(t.lastStatus.Loads[2], 'f', 2, 64)) | 
					
						
							|  |  |  | 	info += t.I18nBot("tgbot.messages.serverMemory", "Current=="+common.FormatTraffic(int64(t.lastStatus.Mem.Current)), "Total=="+common.FormatTraffic(int64(t.lastStatus.Mem.Total))) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	info += t.I18nBot("tgbot.messages.onlinesCount", "Count=="+fmt.Sprint(len(onlines))) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	info += t.I18nBot("tgbot.messages.tcpCount", "Count=="+strconv.Itoa(t.lastStatus.TcpCount)) | 
					
						
							|  |  |  | 	info += t.I18nBot("tgbot.messages.udpCount", "Count=="+strconv.Itoa(t.lastStatus.UdpCount)) | 
					
						
							|  |  |  | 	info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent+t.lastStatus.NetTraffic.Recv)), "Upload=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent)), "Download=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Recv))) | 
					
						
							|  |  |  | 	info += t.I18nBot("tgbot.messages.xrayStatus", "State=="+fmt.Sprint(t.lastStatus.Xray.State)) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	return info | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-03 19:53:45 +00:00
										 |  |  | func (t *Tgbot) UserLoginNotify(username string, password string, ip string, time string, status LoginStatus) { | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	if !t.IsRunning() { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if username == "" || ip == "" || time == "" { | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 		logger.Warning("UserLoginNotify failed, invalid info!") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 15:41:16 +00:00
										 |  |  | 	loginNotifyEnabled, err := t.settingService.GetTgBotLoginNotify() | 
					
						
							|  |  |  | 	if err != nil || !loginNotifyEnabled { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	msg := "" | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if status == LoginSuccess { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg += t.I18nBot("tgbot.messages.loginSuccess") | 
					
						
							| 
									
										
										
										
											2024-07-03 19:53:45 +00:00
										 |  |  | 		msg += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} else if status == LoginFail { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg += t.I18nBot("tgbot.messages.loginFailed") | 
					
						
							| 
									
										
										
										
											2024-07-03 19:53:45 +00:00
										 |  |  | 		msg += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname) | 
					
						
							|  |  |  | 		msg += t.I18nBot("tgbot.messages.password", "Password=="+password) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	msg += t.I18nBot("tgbot.messages.username", "Username=="+username) | 
					
						
							|  |  |  | 	msg += t.I18nBot("tgbot.messages.ip", "IP=="+ip) | 
					
						
							|  |  |  | 	msg += t.I18nBot("tgbot.messages.time", "Time=="+time) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	t.SendMsgToTgbotAdmins(msg) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) getInboundUsages() string { | 
					
						
							|  |  |  | 	info := "" | 
					
						
							|  |  |  | 	// get traffic
 | 
					
						
							| 
									
										
										
										
											2024-07-07 09:55:59 +00:00
										 |  |  | 	inbounds, err := t.inboundService.GetAllInbounds() | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning("GetAllInbounds run failed:", err) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		info += t.I18nBot("tgbot.answers.getInboundsFailed") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		// NOTE:If there no any sessions here,need to notify here
 | 
					
						
							|  |  |  | 		// TODO:Sub-node push, automatic conversion format
 | 
					
						
							| 
									
										
										
										
											2024-07-07 09:55:59 +00:00
										 |  |  | 		for _, inbound := range inbounds { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 			info += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark) | 
					
						
							|  |  |  | 			info += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port)) | 
					
						
							|  |  |  | 			info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 			if inbound.ExpiryTime == 0 { | 
					
						
							| 
									
										
										
										
											2023-05-31 08:37:03 +00:00
										 |  |  | 				info += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited")) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2023-05-31 08:37:03 +00:00
										 |  |  | 				info += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			info += "\r\n" | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return info | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-18 21:30:56 +00:00
										 |  |  | func (t *Tgbot) getInbounds() (*telego.InlineKeyboardMarkup, error) { | 
					
						
							|  |  |  | 	inbounds, err := t.inboundService.GetAllInbounds() | 
					
						
							|  |  |  | 	var buttons []telego.InlineKeyboardButton | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning("GetAllInbounds run failed:", err) | 
					
						
							|  |  |  | 		return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed")) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if len(inbounds) > 0 { | 
					
						
							|  |  |  | 			for _, inbound := range inbounds { | 
					
						
							|  |  |  | 				status := "❌" | 
					
						
							|  |  |  | 				if inbound.Enable { | 
					
						
							|  |  |  | 					status = "✅" | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(t.encodeQuery("get_clients "+strconv.Itoa(inbound.Id)))) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			logger.Warning("GetAllInbounds run failed:", err) | 
					
						
							|  |  |  | 			return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed")) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	cols := 0 | 
					
						
							|  |  |  | 	if len(buttons) < 6 { | 
					
						
							|  |  |  | 		cols = 3 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		cols = 2 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...)) | 
					
						
							|  |  |  | 	return keyboard, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) getInboundClients(id int) (*telego.InlineKeyboardMarkup, error) { | 
					
						
							|  |  |  | 	inbound, err := t.inboundService.GetInbound(id) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning("getIboundClients run failed:", err) | 
					
						
							|  |  |  | 		return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed")) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	clients, err := t.inboundService.GetClients(inbound) | 
					
						
							|  |  |  | 	var buttons []telego.InlineKeyboardButton | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning("GetInboundClients run failed:", err) | 
					
						
							|  |  |  | 		return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed")) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if len(clients) > 0 { | 
					
						
							|  |  |  | 			for _, client := range clients { | 
					
						
							|  |  |  | 				buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery("client_get_usage "+client.Email))) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed")) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	cols := 0 | 
					
						
							|  |  |  | 	if len(buttons) < 6 { | 
					
						
							|  |  |  | 		cols = 3 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		cols = 2 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return keyboard, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-10 21:31:24 +00:00
										 |  |  | func (t *Tgbot) clientInfoMsg( | 
					
						
							|  |  |  | 	traffic *xray.ClientTraffic, | 
					
						
							|  |  |  | 	printEnabled bool, | 
					
						
							|  |  |  | 	printOnline bool, | 
					
						
							|  |  |  | 	printActive bool, | 
					
						
							|  |  |  | 	printDate bool, | 
					
						
							|  |  |  | 	printTraffic bool, | 
					
						
							|  |  |  | 	printRefreshed bool, | 
					
						
							|  |  |  | ) string { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	now := time.Now().Unix() | 
					
						
							|  |  |  | 	expiryTime := "" | 
					
						
							|  |  |  | 	flag := false | 
					
						
							|  |  |  | 	diff := traffic.ExpiryTime/1000 - now | 
					
						
							|  |  |  | 	if traffic.ExpiryTime == 0 { | 
					
						
							|  |  |  | 		expiryTime = t.I18nBot("tgbot.unlimited") | 
					
						
							|  |  |  | 	} else if diff > 172800 || !traffic.Enable { | 
					
						
							|  |  |  | 		expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05") | 
					
						
							|  |  |  | 	} else if traffic.ExpiryTime < 0 { | 
					
						
							|  |  |  | 		expiryTime = fmt.Sprintf("%d %s", traffic.ExpiryTime/-86400000, t.I18nBot("tgbot.days")) | 
					
						
							|  |  |  | 		flag = true | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		expiryTime = fmt.Sprintf("%d %s", diff/3600, t.I18nBot("tgbot.hours")) | 
					
						
							|  |  |  | 		flag = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	total := "" | 
					
						
							|  |  |  | 	if traffic.Total == 0 { | 
					
						
							|  |  |  | 		total = t.I18nBot("tgbot.unlimited") | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		total = common.FormatTraffic((traffic.Total)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	enabled := "" | 
					
						
							|  |  |  | 	isEnabled, err := t.inboundService.checkIsEnabledByEmail(traffic.Email) | 
					
						
							| 
									
										
										
										
											2023-05-05 23:06:46 +00:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning(err) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		enabled = t.I18nBot("tgbot.wentWrong") | 
					
						
							|  |  |  | 	} else if isEnabled { | 
					
						
							|  |  |  | 		enabled = t.I18nBot("tgbot.messages.yes") | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		enabled = t.I18nBot("tgbot.messages.no") | 
					
						
							| 
									
										
										
										
											2023-04-09 19:43:18 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	active := "" | 
					
						
							|  |  |  | 	if traffic.Enable { | 
					
						
							|  |  |  | 		active = t.I18nBot("tgbot.messages.yes") | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		active = t.I18nBot("tgbot.messages.no") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	status := t.I18nBot("tgbot.offline") | 
					
						
							|  |  |  | 	if p.IsRunning() { | 
					
						
							|  |  |  | 		for _, online := range p.GetOnlineClients() { | 
					
						
							|  |  |  | 			if online == traffic.Email { | 
					
						
							|  |  |  | 				status = t.I18nBot("tgbot.online") | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	output := "" | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.email", "Email=="+traffic.Email) | 
					
						
							|  |  |  | 	if printEnabled { | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.enabled", "Enable=="+enabled) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if printOnline { | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.online", "Status=="+status) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if printActive { | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.active", "Enable=="+active) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if printDate { | 
					
						
							|  |  |  | 		if flag { | 
					
						
							|  |  |  | 			output += t.I18nBot("tgbot.messages.expireIn", "Time=="+expiryTime) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			output += t.I18nBot("tgbot.messages.expire", "Time=="+expiryTime) | 
					
						
							| 
									
										
										
										
											2023-05-05 23:06:46 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	if printTraffic { | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.upload", "Upload=="+common.FormatTraffic(traffic.Up)) | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.download", "Download=="+common.FormatTraffic(traffic.Down)) | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.total", "UpDown=="+common.FormatTraffic((traffic.Up+traffic.Down)), "Total=="+total) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if printRefreshed { | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05")) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return output | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | func (t *Tgbot) getClientUsage(chatId int64, tgUserID int64, email ...string) { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning(err) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg := t.I18nBot("tgbot.wentWrong") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, msg) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if len(traffics) == 0 { | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10))) | 
					
						
							| 
									
										
										
										
											2023-04-09 19:43:18 +00:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	output := "" | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	if len(traffics) > 0 { | 
					
						
							|  |  |  | 		if len(email) > 0 { | 
					
						
							|  |  |  | 			for _, traffic := range traffics { | 
					
						
							|  |  |  | 				if traffic.Email == email[0] { | 
					
						
							|  |  |  | 					output := t.clientInfoMsg(traffic, true, true, true, true, true, true) | 
					
						
							|  |  |  | 					t.SendMsgToTgbot(chatId, output) | 
					
						
							|  |  |  | 					return | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-05-30 11:37:23 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			msg := t.I18nBot("tgbot.noResult") | 
					
						
							|  |  |  | 			t.SendMsgToTgbot(chatId, msg) | 
					
						
							|  |  |  | 			return | 
					
						
							| 
									
										
										
										
											2023-05-30 11:37:23 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			for _, traffic := range traffics { | 
					
						
							|  |  |  | 				output += t.clientInfoMsg(traffic, true, true, true, true, true, false) | 
					
						
							|  |  |  | 				output += "\r\n" | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-05-30 11:37:23 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05")) | 
					
						
							|  |  |  | 	t.SendMsgToTgbot(chatId, output) | 
					
						
							|  |  |  | 	output = t.I18nBot("tgbot.commands.pleaseChoose") | 
					
						
							|  |  |  | 	t.SendAnswer(chatId, output, false) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | func (t *Tgbot) searchClientIps(chatId int64, email string, messageID ...int) { | 
					
						
							|  |  |  | 	ips, err := t.inboundService.GetInboundClientIps(email) | 
					
						
							|  |  |  | 	if err != nil || len(ips) == 0 { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		ips = t.I18nBot("tgbot.noIpRecord") | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	output := "" | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.email", "Email=="+email) | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.ips", "IPs=="+ips) | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 	output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05")) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	inlineKeyboard := tu.InlineKeyboard( | 
					
						
							|  |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("ips_refresh "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.clearIPs")).WithCallbackData(t.encodeQuery("clear_ips "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 		), | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 	if len(messageID) > 0 { | 
					
						
							|  |  |  | 		t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, output, inlineKeyboard) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | func (t *Tgbot) clientTelegramUserInfo(chatId int64, email string, messageID ...int) { | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 	traffic, client, err := t.inboundService.GetClientByEmail(email) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning(err) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg := t.I18nBot("tgbot.wentWrong") | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, msg) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if client == nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg := t.I18nBot("tgbot.noResult") | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, msg) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 	tgId := "None" | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 	if client.TgID != 0 { | 
					
						
							|  |  |  | 		tgId = strconv.FormatInt(client.TgID, 10) | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	output := "" | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.email", "Email=="+email) | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.TGUser", "TelegramID=="+tgId) | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 	output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05")) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 	inlineKeyboard := tu.InlineKeyboard( | 
					
						
							|  |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("tgid_refresh "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.removeTGUser")).WithCallbackData(t.encodeQuery("tgid_remove "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 	) | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 	if len(messageID) > 0 { | 
					
						
							|  |  |  | 		t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, output, inlineKeyboard) | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 		requestUser := telego.KeyboardButtonRequestUsers{ | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 			RequestID: int32(traffic.Id), | 
					
						
							| 
									
										
										
										
											2023-08-02 13:33:59 +00:00
										 |  |  | 			UserIsBot: new(bool), | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		keyboard := tu.Keyboard( | 
					
						
							|  |  |  | 			tu.KeyboardRow( | 
					
						
							| 
									
										
										
										
											2024-02-17 17:45:53 +00:00
										 |  |  | 				tu.KeyboardButton(t.I18nBot("tgbot.buttons.selectTGUser")).WithRequestUsers(&requestUser), | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 			), | 
					
						
							|  |  |  | 			tu.KeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 				tu.KeyboardButton(t.I18nBot("tgbot.buttons.closeKeyboard")), | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 			), | 
					
						
							| 
									
										
										
										
											2023-05-14 19:25:01 +00:00
										 |  |  | 		).WithIsPersistent().WithResizeKeyboard() | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.buttons.selectOneTGUser"), keyboard) | 
					
						
							| 
									
										
										
										
											2023-05-14 19:13:23 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) { | 
					
						
							| 
									
										
										
										
											2023-04-25 11:08:35 +00:00
										 |  |  | 	traffic, err := t.inboundService.GetClientTrafficByEmail(email) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning(err) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg := t.I18nBot("tgbot.wentWrong") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, msg) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-25 11:08:35 +00:00
										 |  |  | 	if traffic == nil { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg := t.I18nBot("tgbot.noResult") | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, msg) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	output := t.clientInfoMsg(traffic, true, true, true, true, true, true) | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	inlineKeyboard := tu.InlineKeyboard( | 
					
						
							|  |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("client_refresh "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-04 22:18:37 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.resetTraffic")).WithCallbackData(t.encodeQuery("reset_traffic "+email)), | 
					
						
							| 
									
										
										
										
											2023-11-20 14:17:59 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.limitTraffic")).WithCallbackData(t.encodeQuery("limit_traffic "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.resetExpire")).WithCallbackData(t.encodeQuery("reset_exp "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ipLog")).WithCallbackData(t.encodeQuery("ip_log "+email)), | 
					
						
							|  |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ipLimit")).WithCallbackData(t.encodeQuery("ip_limit "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.setTGUser")).WithCallbackData(t.encodeQuery("tg_user "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-14 18:37:49 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		tu.InlineKeyboardRow( | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 			tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.toggle")).WithCallbackData(t.encodeQuery("toggle_enable "+email)), | 
					
						
							| 
									
										
										
										
											2023-05-05 14:50:56 +00:00
										 |  |  | 		), | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 	) | 
					
						
							|  |  |  | 	if len(messageID) > 0 { | 
					
						
							|  |  |  | 		t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, output, inlineKeyboard) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | func (t *Tgbot) searchInbound(chatId int64, remark string) { | 
					
						
							| 
									
										
										
										
											2024-07-07 09:55:59 +00:00
										 |  |  | 	inbounds, err := t.inboundService.SearchInbounds(remark) | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning(err) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg := t.I18nBot("tgbot.wentWrong") | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, msg) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-07 09:55:59 +00:00
										 |  |  | 	if len(inbounds) == 0 { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		msg := t.I18nBot("tgbot.noInbounds") | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 		t.SendMsgToTgbot(chatId, msg) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-30 22:01:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-07 09:55:59 +00:00
										 |  |  | 	for _, inbound := range inbounds { | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 		info := "" | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 		info += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark) | 
					
						
							|  |  |  | 		info += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port)) | 
					
						
							|  |  |  | 		info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 		if inbound.ExpiryTime == 0 { | 
					
						
							| 
									
										
										
										
											2023-05-31 08:37:03 +00:00
										 |  |  | 			info += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited")) | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2023-05-31 08:37:03 +00:00
										 |  |  | 			info += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")) | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, info) | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		if len(inbound.ClientStats) > 0 { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 			output := "" | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			for _, traffic := range inbound.ClientStats { | 
					
						
							|  |  |  | 				output += t.clientInfoMsg(&traffic, true, true, true, true, true, true) | 
					
						
							| 
									
										
										
										
											2023-05-30 11:37:23 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 			t.SendMsgToTgbot(chatId, output) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | func (t *Tgbot) getExhausted(chatId int64) { | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	trDiff := int64(0) | 
					
						
							|  |  |  | 	exDiff := int64(0) | 
					
						
							|  |  |  | 	now := time.Now().Unix() * 1000 | 
					
						
							|  |  |  | 	var exhaustedInbounds []model.Inbound | 
					
						
							|  |  |  | 	var exhaustedClients []xray.ClientTraffic | 
					
						
							|  |  |  | 	var disabledInbounds []model.Inbound | 
					
						
							|  |  |  | 	var disabledClients []xray.ClientTraffic | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-09 19:43:18 +00:00
										 |  |  | 	TrafficThreshold, err := t.settingService.GetTrafficDiff() | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if err == nil && TrafficThreshold > 0 { | 
					
						
							|  |  |  | 		trDiff = int64(TrafficThreshold) * 1073741824 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-09 19:43:18 +00:00
										 |  |  | 	ExpireThreshold, err := t.settingService.GetExpireDiff() | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	if err == nil && ExpireThreshold > 0 { | 
					
						
							| 
									
										
										
										
											2023-04-09 19:43:18 +00:00
										 |  |  | 		exDiff = int64(ExpireThreshold) * 86400000 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	inbounds, err := t.inboundService.GetAllInbounds() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning("Unable to load Inbounds", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	for _, inbound := range inbounds { | 
					
						
							|  |  |  | 		if inbound.Enable { | 
					
						
							| 
									
										
										
										
											2023-03-24 13:20:10 +00:00
										 |  |  | 			if (inbound.ExpiryTime > 0 && (inbound.ExpiryTime-now < exDiff)) || | 
					
						
							| 
									
										
										
										
											2023-04-09 21:25:47 +00:00
										 |  |  | 				(inbound.Total > 0 && (inbound.Total-(inbound.Up+inbound.Down) < trDiff)) { | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 				exhaustedInbounds = append(exhaustedInbounds, *inbound) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if len(inbound.ClientStats) > 0 { | 
					
						
							|  |  |  | 				for _, client := range inbound.ClientStats { | 
					
						
							|  |  |  | 					if client.Enable { | 
					
						
							| 
									
										
										
										
											2023-03-24 13:20:10 +00:00
										 |  |  | 						if (client.ExpiryTime > 0 && (client.ExpiryTime-now < exDiff)) || | 
					
						
							| 
									
										
										
										
											2023-04-09 21:25:47 +00:00
										 |  |  | 							(client.Total > 0 && (client.Total-(client.Up+client.Down) < trDiff)) { | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 							exhaustedClients = append(exhaustedClients, client) | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						disabledClients = append(disabledClients, client) | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			disabledInbounds = append(disabledInbounds, *inbound) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	// Inbounds
 | 
					
						
							|  |  |  | 	output := "" | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.inbounds")) | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledInbounds))) | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(len(exhaustedInbounds))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 13:20:10 +00:00
										 |  |  | 	if len(exhaustedInbounds) > 0 { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+t.I18nBot("tgbot.inbounds")) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		for _, inbound := range exhaustedInbounds { | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 			output += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark) | 
					
						
							|  |  |  | 			output += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port)) | 
					
						
							|  |  |  | 			output += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down)) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 			if inbound.ExpiryTime == 0 { | 
					
						
							| 
									
										
										
										
											2023-05-31 08:37:03 +00:00
										 |  |  | 				output += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited")) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2023-05-31 08:37:03 +00:00
										 |  |  | 				output += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			output += "\r\n" | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-20 15:09:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	// Clients
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	exhaustedCC := len(exhaustedClients) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 	output += t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.clients")) | 
					
						
							|  |  |  | 	output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledClients))) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(exhaustedCC)) | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	if exhaustedCC > 0 { | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+t.I18nBot("tgbot.clients")) | 
					
						
							|  |  |  | 		var buttons []telego.InlineKeyboardButton | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		for _, traffic := range exhaustedClients { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 			output += t.clientInfoMsg(&traffic, true, false, false, true, true, false) | 
					
						
							|  |  |  | 			output += "\r\n" | 
					
						
							|  |  |  | 			buttons = append(buttons, tu.InlineKeyboardButton(traffic.Email).WithCallbackData(t.encodeQuery("client_get_usage "+traffic.Email))) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		cols := 0 | 
					
						
							|  |  |  | 		if exhaustedCC < 11 { | 
					
						
							|  |  |  | 			cols = 1 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			cols = 2 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05")) | 
					
						
							|  |  |  | 		keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...)) | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, output, keyboard) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05")) | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, output) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | func (t *Tgbot) notifyExhausted() { | 
					
						
							|  |  |  | 	trDiff := int64(0) | 
					
						
							|  |  |  | 	exDiff := int64(0) | 
					
						
							|  |  |  | 	now := time.Now().Unix() * 1000 | 
					
						
							| 
									
										
										
										
											2023-05-20 23:00:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	TrafficThreshold, err := t.settingService.GetTrafficDiff() | 
					
						
							|  |  |  | 	if err == nil && TrafficThreshold > 0 { | 
					
						
							|  |  |  | 		trDiff = int64(TrafficThreshold) * 1073741824 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ExpireThreshold, err := t.settingService.GetExpireDiff() | 
					
						
							|  |  |  | 	if err == nil && ExpireThreshold > 0 { | 
					
						
							|  |  |  | 		exDiff = int64(ExpireThreshold) * 86400000 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inbounds, err := t.inboundService.GetAllInbounds() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.Warning("Unable to load Inbounds", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 	var chatIDsDone []int64 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	for _, inbound := range inbounds { | 
					
						
							|  |  |  | 		if inbound.Enable { | 
					
						
							|  |  |  | 			if len(inbound.ClientStats) > 0 { | 
					
						
							|  |  |  | 				clients, err := t.inboundService.GetClients(inbound) | 
					
						
							|  |  |  | 				if err == nil { | 
					
						
							|  |  |  | 					for _, client := range clients { | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 						if client.TgID != 0 { | 
					
						
							|  |  |  | 							chatID := client.TgID | 
					
						
							|  |  |  | 							if !int64Contains(chatIDsDone, chatID) && !checkAdmin(chatID) { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 								var disabledClients []xray.ClientTraffic | 
					
						
							|  |  |  | 								var exhaustedClients []xray.ClientTraffic | 
					
						
							|  |  |  | 								traffics, err := t.inboundService.GetClientTrafficTgBot(client.TgID) | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 								if err == nil && len(traffics) > 0 { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 									output := t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.clients")) | 
					
						
							|  |  |  | 									for _, traffic := range traffics { | 
					
						
							|  |  |  | 										if traffic.Enable { | 
					
						
							|  |  |  | 											if (traffic.ExpiryTime > 0 && (traffic.ExpiryTime-now < exDiff)) || | 
					
						
							|  |  |  | 												(traffic.Total > 0 && (traffic.Total-(traffic.Up+traffic.Down) < trDiff)) { | 
					
						
							|  |  |  | 												exhaustedClients = append(exhaustedClients, *traffic) | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										} else { | 
					
						
							|  |  |  | 											disabledClients = append(disabledClients, *traffic) | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									if len(exhaustedClients) > 0 { | 
					
						
							|  |  |  | 										output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledClients))) | 
					
						
							|  |  |  | 										if len(disabledClients) > 0 { | 
					
						
							|  |  |  | 											output += t.I18nBot("tgbot.clients") + ":\r\n" | 
					
						
							|  |  |  | 											for _, traffic := range disabledClients { | 
					
						
							|  |  |  | 												output += " " + traffic.Email | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 											output += "\r\n" | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 										output += "\r\n" | 
					
						
							|  |  |  | 										output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(len(exhaustedClients))) | 
					
						
							|  |  |  | 										for _, traffic := range exhaustedClients { | 
					
						
							|  |  |  | 											output += t.clientInfoMsg(&traffic, true, false, false, true, true, false) | 
					
						
							|  |  |  | 											output += "\r\n" | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 										t.SendMsgToTgbot(chatID, output) | 
					
						
							|  |  |  | 									} | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | 									chatIDsDone = append(chatIDsDone, chatID) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2023-05-30 11:37:23 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 11:34:44 +00:00
										 |  |  | func int64Contains(slice []int64, item int64) bool { | 
					
						
							|  |  |  | 	for _, s := range slice { | 
					
						
							|  |  |  | 		if s == item { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | func (t *Tgbot) onlineClients(chatId int64, messageID ...int) { | 
					
						
							|  |  |  | 	if !p.IsRunning() { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	onlines := p.GetOnlineClients() | 
					
						
							|  |  |  | 	onlinesCount := len(onlines) | 
					
						
							|  |  |  | 	output := t.I18nBot("tgbot.messages.onlinesCount", "Count=="+fmt.Sprint(onlinesCount)) | 
					
						
							|  |  |  | 	keyboard := tu.InlineKeyboard(tu.InlineKeyboardRow( | 
					
						
							|  |  |  | 		tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("onlines_refresh")))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if onlinesCount > 0 { | 
					
						
							|  |  |  | 		var buttons []telego.InlineKeyboardButton | 
					
						
							|  |  |  | 		for _, online := range onlines { | 
					
						
							|  |  |  | 			buttons = append(buttons, tu.InlineKeyboardButton(online).WithCallbackData(t.encodeQuery("client_get_usage "+online))) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		cols := 0 | 
					
						
							|  |  |  | 		if onlinesCount < 21 { | 
					
						
							|  |  |  | 			cols = 2 | 
					
						
							|  |  |  | 		} else if onlinesCount < 61 { | 
					
						
							|  |  |  | 			cols = 3 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			cols = 4 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, tu.InlineKeyboardCols(cols, buttons...)...) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(messageID) > 0 { | 
					
						
							|  |  |  | 		t.editMessageTgBot(chatId, messageID[0], output, keyboard) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, output, keyboard) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) sendBackup(chatId int64) { | 
					
						
							| 
									
										
										
										
											2023-05-21 03:11:59 +00:00
										 |  |  | 	output := t.I18nBot("tgbot.messages.backupTime", "Time=="+time.Now().Format("2006-01-02 15:04:05")) | 
					
						
							|  |  |  | 	t.SendMsgToTgbot(chatId, output) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-08 19:35:10 +00:00
										 |  |  | 	// Update by manually trigger a checkpoint operation
 | 
					
						
							|  |  |  | 	err := database.Checkpoint() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		logger.Error("Error in trigger a checkpoint operation: ", err) | 
					
						
							| 
									
										
										
										
											2023-12-08 19:35:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	file, err := os.Open(config.GetDBPath()) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		document := tu.Document( | 
					
						
							|  |  |  | 			tu.ID(chatId), | 
					
						
							|  |  |  | 			tu.File(file), | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		_, err = bot.SendDocument(document) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logger.Error("Error in uploading backup: ", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		logger.Error("Error in opening db file for backup: ", err) | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-21 04:03:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	file, err = os.Open(xray.GetConfigPath()) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		document := tu.Document( | 
					
						
							|  |  |  | 			tu.ID(chatId), | 
					
						
							|  |  |  | 			tu.File(file), | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		_, err = bot.SendDocument(document) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logger.Error("Error in uploading config.json: ", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		logger.Error("Error in opening config.json file for backup: ", err) | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) sendBanLogs(chatId int64, dt bool) { | 
					
						
							|  |  |  | 	if dt { | 
					
						
							|  |  |  | 		output := t.I18nBot("tgbot.messages.datetime", "DateTime=="+time.Now().Format("2006-01-02 15:04:05")) | 
					
						
							|  |  |  | 		t.SendMsgToTgbot(chatId, output) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	file, err := os.Open(xray.GetIPLimitBannedPrevLogPath()) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							| 
									
										
										
										
											2024-02-03 22:20:14 +00:00
										 |  |  | 		// Check if the file is non-empty before attempting to upload
 | 
					
						
							|  |  |  | 		fileInfo, _ := file.Stat() | 
					
						
							|  |  |  | 		if fileInfo.Size() > 0 { | 
					
						
							|  |  |  | 			document := tu.Document( | 
					
						
							|  |  |  | 				tu.ID(chatId), | 
					
						
							|  |  |  | 				tu.File(file), | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 			_, err = bot.SendDocument(document) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				logger.Error("Error in uploading IPLimitBannedPrevLog: ", err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			logger.Warning("IPLimitBannedPrevLog file is empty, not uploading.") | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-02-03 22:20:14 +00:00
										 |  |  | 		file.Close() | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2024-02-03 22:20:14 +00:00
										 |  |  | 		logger.Error("Error in opening IPLimitBannedPrevLog file for backup: ", err) | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	file, err = os.Open(xray.GetIPLimitBannedLogPath()) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							| 
									
										
										
										
											2024-02-03 22:20:14 +00:00
										 |  |  | 		// Check if the file is non-empty before attempting to upload
 | 
					
						
							|  |  |  | 		fileInfo, _ := file.Stat() | 
					
						
							|  |  |  | 		if fileInfo.Size() > 0 { | 
					
						
							|  |  |  | 			document := tu.Document( | 
					
						
							|  |  |  | 				tu.ID(chatId), | 
					
						
							|  |  |  | 				tu.File(file), | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 			_, err = bot.SendDocument(document) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				logger.Error("Error in uploading IPLimitBannedLog: ", err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			logger.Warning("IPLimitBannedLog file is empty, not uploading.") | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-02-03 22:20:14 +00:00
										 |  |  | 		file.Close() | 
					
						
							| 
									
										
										
										
											2024-01-01 15:07:56 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2024-02-03 22:20:14 +00:00
										 |  |  | 		logger.Error("Error in opening IPLimitBannedLog file for backup: ", err) | 
					
						
							| 
									
										
										
										
											2023-03-24 13:44:26 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-17 16:07:49 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (t *Tgbot) sendCallbackAnswerTgBot(id string, message string) { | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	params := telego.AnswerCallbackQueryParams{ | 
					
						
							|  |  |  | 		CallbackQueryID: id, | 
					
						
							|  |  |  | 		Text:            message, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := bot.AnswerCallbackQuery(¶ms); err != nil { | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 		logger.Warning(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | func (t *Tgbot) editMessageCallbackTgBot(chatId int64, messageID int, inlineKeyboard *telego.InlineKeyboardMarkup) { | 
					
						
							|  |  |  | 	params := telego.EditMessageReplyMarkupParams{ | 
					
						
							|  |  |  | 		ChatID:      tu.ID(chatId), | 
					
						
							|  |  |  | 		MessageID:   messageID, | 
					
						
							|  |  |  | 		ReplyMarkup: inlineKeyboard, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if _, err := bot.EditMessageReplyMarkup(¶ms); err != nil { | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 		logger.Warning(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | func (t *Tgbot) editMessageTgBot(chatId int64, messageID int, text string, inlineKeyboard ...*telego.InlineKeyboardMarkup) { | 
					
						
							|  |  |  | 	params := telego.EditMessageTextParams{ | 
					
						
							|  |  |  | 		ChatID:    tu.ID(chatId), | 
					
						
							|  |  |  | 		MessageID: messageID, | 
					
						
							|  |  |  | 		Text:      text, | 
					
						
							|  |  |  | 		ParseMode: "HTML", | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 	if len(inlineKeyboard) > 0 { | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 		params.ReplyMarkup = inlineKeyboard[0] | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-14 15:20:01 +00:00
										 |  |  | 	if _, err := bot.EditMessageText(¶ms); err != nil { | 
					
						
							| 
									
										
										
										
											2023-05-04 21:46:43 +00:00
										 |  |  | 		logger.Warning(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |