From edc911c2f3b224e1607c0bcc231394c698ea416a Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Fri, 3 Mar 2023 17:22:37 +0100 Subject: [PATCH 1/6] Move TgBot to service + fix pannel restart bug #34 --- web/controller/index.go | 6 +- web/job/stats_notify_job.go | 163 +---------------------------- web/service/tgbot.go | 197 ++++++++++++++++++++++++++++++++++++ web/web.go | 13 ++- 4 files changed, 212 insertions(+), 167 deletions(-) create mode 100644 web/service/tgbot.go diff --git a/web/controller/index.go b/web/controller/index.go index 71a1a34a..b4f981e8 100644 --- a/web/controller/index.go +++ b/web/controller/index.go @@ -4,7 +4,6 @@ import ( "net/http" "time" "x-ui/logger" - "x-ui/web/job" "x-ui/web/service" "x-ui/web/session" @@ -20,6 +19,7 @@ type IndexController struct { BaseController userService service.UserService + tgbot service.Tgbot } func NewIndexController(g *gin.RouterGroup) *IndexController { @@ -60,13 +60,13 @@ func (a *IndexController) login(c *gin.Context) { user := a.userService.CheckUser(form.Username, form.Password) timeStr := time.Now().Format("2006-01-02 15:04:05") if user == nil { - job.NewStatsNotifyJob().UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 0) + a.tgbot.UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 0) logger.Infof("wrong username or password: \"%s\" \"%s\"", form.Username, form.Password) pureJsonMsg(c, false, I18n(c, "pages.login.toasts.wrongUsernameOrPassword")) return } else { logger.Infof("%s login success,Ip Address:%s\n", form.Username, getRemoteIp(c)) - job.NewStatsNotifyJob().UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 1) + a.tgbot.UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 1) } err = session.SetLoginUser(c, user) diff --git a/web/job/stats_notify_job.go b/web/job/stats_notify_job.go index 92963127..689ba200 100644 --- a/web/job/stats_notify_job.go +++ b/web/job/stats_notify_job.go @@ -8,8 +8,6 @@ import ( "x-ui/logger" "x-ui/util/common" "x-ui/web/service" - - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) type LoginStatus byte @@ -24,37 +22,13 @@ type StatsNotifyJob struct { xrayService service.XrayService inboundService service.InboundService settingService service.SettingService + tgbotService service.Tgbot } func NewStatsNotifyJob() *StatsNotifyJob { return new(StatsNotifyJob) } -func (j *StatsNotifyJob) SendMsgToTgbot(msg string) { - //Telegram bot basic info - tgBottoken, err := j.settingService.GetTgBotToken() - if err != nil || tgBottoken == "" { - logger.Warning("sendMsgToTgbot failed,GetTgBotToken fail:", err) - return - } - tgBotid, err := j.settingService.GetTgBotChatId() - if err != nil { - logger.Warning("sendMsgToTgbot failed,GetTgBotChatId fail:", err) - return - } - - bot, err := tgbotapi.NewBotAPI(tgBottoken) - if err != nil { - fmt.Println("get tgbot error:", err) - return - } - bot.Debug = true - fmt.Printf("Authorized on account %s", bot.Self.UserName) - info := tgbotapi.NewMessage(int64(tgBotid), msg) - //msg.ReplyToMessageID = int(tgBotid) - bot.Send(info) -} - // Here run is a interface method of Job interface func (j *StatsNotifyJob) Run() { if !j.xrayService.IsXrayRunning() { @@ -111,138 +85,5 @@ func (j *StatsNotifyJob) Run() { info += fmt.Sprintf("Expire date:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")) } } - j.SendMsgToTgbot(info) -} - -func (j *StatsNotifyJob) UserLoginNotify(username string, ip string, time string, status LoginStatus) { - if username == "" || ip == "" || time == "" { - logger.Warning("UserLoginNotify failed,invalid info") - return - } - var msg string - // Get hostname - name, err := os.Hostname() - if err != nil { - fmt.Println("get hostname error:", err) - return - } - if status == LoginSuccess { - msg = fmt.Sprintf("Successfully logged-in to the panel\r\nHostname:%s\r\n", name) - } else if status == LoginFail { - msg = fmt.Sprintf("Login to the panel was unsuccessful\r\nHostname:%s\r\n", name) - } - msg += fmt.Sprintf("Time:%s\r\n", time) - msg += fmt.Sprintf("Username:%s\r\n", username) - msg += fmt.Sprintf("IP:%s\r\n", ip) - j.SendMsgToTgbot(msg) -} - -var numericKeyboard = tgbotapi.NewInlineKeyboardMarkup( - tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("Get Usage", "get_usage"), - ), -) - -func (j *StatsNotifyJob) OnReceive() *StatsNotifyJob { - tgBottoken, err := j.settingService.GetTgBotToken() - if err != nil || tgBottoken == "" { - logger.Warning("sendMsgToTgbot failed,GetTgBotToken fail:", err) - return j - } - bot, err := tgbotapi.NewBotAPI(tgBottoken) - if err != nil { - fmt.Println("get tgbot error:", err) - return j - } - bot.Debug = false - u := tgbotapi.NewUpdate(0) - u.Timeout = 10 - - updates := bot.GetUpdatesChan(u) - - for update := range updates { - if update.Message == nil { - - if update.CallbackQuery != nil { - // Respond to the callback query, telling Telegram to show the user - // a message with the data received. - callback := tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data) - if _, err := bot.Request(callback); err != nil { - logger.Warning(err) - } - - // And finally, send a message containing the data received. - msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, "") - - switch update.CallbackQuery.Data { - case "get_usage": - msg.Text = "for get your usage send command like this : \n /usage uuid | id \n example : /usage fc3239ed-8f3b-4151-ff51-b183d5182142" - msg.ParseMode = "HTML" - } - if _, err := bot.Send(msg); err != nil { - logger.Warning(err) - } - } - - continue - } - - if !update.Message.IsCommand() { // ignore any non-command Messages - continue - } - - // Create a new MessageConfig. We don't have text yet, - // so we leave it empty. - msg := tgbotapi.NewMessage(update.Message.Chat.ID, "") - - // Extract the command from the Message. - switch update.Message.Command() { - case "help": - msg.Text = "What you need?" - msg.ReplyMarkup = numericKeyboard - case "start": - msg.Text = "Hi :) \n What you need?" - msg.ReplyMarkup = numericKeyboard - - case "status": - msg.Text = "bot is ok." - - case "usage": - msg.Text = j.getClientUsage(update.Message.CommandArguments()) - default: - msg.Text = "I don't know that command, /help" - msg.ReplyMarkup = numericKeyboard - - } - - if _, err := bot.Send(msg); err != nil { - logger.Warning(err) - } - } - return j - -} -func (j *StatsNotifyJob) getClientUsage(id string) string { - traffic, err := j.inboundService.GetClientTrafficById(id) - if err != nil { - logger.Warning(err) - return "something wrong!" - } - expiryTime := "" - if traffic.ExpiryTime == 0 { - expiryTime = fmt.Sprintf("unlimited") - } else { - expiryTime = fmt.Sprintf("%s", time.Unix((traffic.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")) - } - total := "" - if traffic.Total == 0 { - total = fmt.Sprintf("unlimited") - } else { - total = fmt.Sprintf("%s", common.FormatTraffic((traffic.Total))) - } - output := fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n", - traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)), - total, expiryTime) - - return output + j.tgbotService.SendMsgToTgbot(info) } diff --git a/web/service/tgbot.go b/web/service/tgbot.go new file mode 100644 index 00000000..b7fd288b --- /dev/null +++ b/web/service/tgbot.go @@ -0,0 +1,197 @@ +package service + +import ( + "fmt" + "os" + "time" + "x-ui/logger" + "x-ui/util/common" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +var bot *tgbotapi.BotAPI +var tgBotid int +var isRunning bool + +var numericKeyboard = tgbotapi.NewInlineKeyboardMarkup( + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("Get Usage", "get_usage"), + ), +) + +type LoginStatus byte + +const ( + LoginSuccess LoginStatus = 1 + LoginFail LoginStatus = 0 +) + +type Tgbot struct { + inboundService InboundService + settingService SettingService +} + +func (t *Tgbot) NewTgbot() *Tgbot { + return new(Tgbot) +} + +func (t *Tgbot) Start() error { + tgBottoken, err := t.settingService.GetTgBotToken() + if err != nil || tgBottoken == "" { + logger.Warning("Get TgBotToken failed:", err) + return err + } + + tgBotid, err = t.settingService.GetTgBotChatId() + if err != nil { + logger.Warning("Get GetTgBotChatId failed:", err) + return err + } + + bot, err = tgbotapi.NewBotAPI(tgBottoken) + if err != nil { + fmt.Println("Get tgbot's api error:", err) + return err + } + bot.Debug = false + + // listen for TG bot income messages + if !isRunning { + logger.Info("Telegram receiver starting") + go t.OnReceive() + isRunning = true + } + + return nil +} + +func (t *Tgbot) IsRunnging() bool { + return isRunning +} + +func (t *Tgbot) Stop() { + bot.StopReceivingUpdates() + logger.Info("Send Kill to Telegram listener ...") + isRunning = false +} + +func (t *Tgbot) OnReceive() { + u := tgbotapi.NewUpdate(0) + u.Timeout = 10 + + updates := bot.GetUpdatesChan(u) + + for update := range updates { + if update.Message == nil { + + if update.CallbackQuery != nil { + // Respond to the callback query, telling Telegram to show the user + // a message with the data received. + callback := tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data) + if _, err := bot.Request(callback); err != nil { + logger.Warning(err) + } + + // And finally, send a message containing the data received. + msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, "") + + switch update.CallbackQuery.Data { + case "get_usage": + msg.Text = "for get your usage send command like this : \n /usage uuid | id \n example : /usage fc3239ed-8f3b-4151-ff51-b183d5182142" + msg.ParseMode = "HTML" + } + if _, err := bot.Send(msg); err != nil { + logger.Warning(err) + } + } + + continue + } + + if !update.Message.IsCommand() { // ignore any non-command Messages + continue + } + + // Create a new MessageConfig. We don't have text yet, + // so we leave it empty. + msg := tgbotapi.NewMessage(update.Message.Chat.ID, "") + + // Extract the command from the Message. + switch update.Message.Command() { + case "help": + msg.Text = "What you need?" + msg.ReplyMarkup = numericKeyboard + case "start": + msg.Text = "Hi :) \n What you need?" + msg.ReplyMarkup = numericKeyboard + + case "status": + msg.Text = "bot is ok." + + case "usage": + msg.Text = t.getClientUsage(update.Message.CommandArguments()) + default: + msg.Text = "I don't know that command, /help" + msg.ReplyMarkup = numericKeyboard + } + + if _, err := bot.Send(msg); err != nil { + logger.Warning(err) + } + } +} + +func (t *Tgbot) SendMsgToTgbot(msg string) { + info := tgbotapi.NewMessage(int64(tgBotid), msg) + //msg.ReplyToMessageID = int(tgBotid) + bot.Send(info) +} + +func (t *Tgbot) UserLoginNotify(username string, ip string, time string, status LoginStatus) { + if username == "" || ip == "" || time == "" { + logger.Warning("UserLoginNotify failed,invalid info") + return + } + var msg string + // Get hostname + name, err := os.Hostname() + if err != nil { + fmt.Println("get hostname error:", err) + return + } + if status == LoginSuccess { + msg = fmt.Sprintf("Successfully logged-in to the panel\r\nHostname:%s\r\n", name) + } else if status == LoginFail { + msg = fmt.Sprintf("Login to the panel was unsuccessful\r\nHostname:%s\r\n", name) + } + msg += fmt.Sprintf("Time:%s\r\n", time) + msg += fmt.Sprintf("Username:%s\r\n", username) + msg += fmt.Sprintf("IP:%s\r\n", ip) + t.SendMsgToTgbot(msg) +} + +func (t *Tgbot) getClientUsage(id string) string { + traffic, err := t.inboundService.GetClientTrafficById(id) + if err != nil { + logger.Warning(err) + return "something wrong!" + } + expiryTime := "" + if traffic.ExpiryTime == 0 { + expiryTime = "unlimited" + } else { + expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05") + } + total := "" + if traffic.Total == 0 { + total = "unlimited" + } else { + total = common.FormatTraffic((traffic.Total)) + } + output := fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n", + traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)), + total, expiryTime) + + return output +} diff --git a/web/web.go b/web/web.go index 3f9344d5..7a68d1ee 100644 --- a/web/web.go +++ b/web/web.go @@ -88,7 +88,7 @@ type Server struct { xrayService service.XrayService settingService service.SettingService - inboundService service.InboundService + tgbotService service.Tgbot cron *cron.Cron @@ -325,8 +325,6 @@ func (s *Server) startTask() { logger.Warning("Add NewStatsNotifyJob error", err) return } - // listen for TG bot income messages - go job.NewStatsNotifyJob().OnReceive() } else { s.cron.Remove(entry) } @@ -403,6 +401,12 @@ func (s *Server) Start() (err error) { s.httpServer.Serve(listener) }() + isTgbotenabled, err := s.settingService.GetTgbotenabled() + if (err == nil) && (isTgbotenabled) { + tgBot := s.tgbotService.NewTgbot() + tgBot.Start() + } + return nil } @@ -412,6 +416,9 @@ func (s *Server) Stop() error { if s.cron != nil { s.cron.Stop() } + if s.tgbotService.IsRunnging() { + s.tgbotService.Stop() + } var err1 error var err2 error if s.httpServer != nil { From 2951b5f94c37ee3dcf14c75147cd729283571ad7 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Mon, 6 Mar 2023 19:15:20 +0100 Subject: [PATCH 2/6] Support multi telegramId for admins #34 --- main.go | 10 +++---- web/assets/js/model/models.js | 2 +- web/entity/entity.go | 2 +- web/job/stats_notify_job.go | 6 ++-- web/service/setting.go | 10 +++---- web/service/tgbot.go | 39 +++++++++++++++++++------- web/translation/translate.en_US.toml | 4 +-- web/translation/translate.fa_IR.toml | 4 +-- web/translation/translate.zh_Hans.toml | 2 +- 9 files changed, 48 insertions(+), 31 deletions(-) diff --git a/main.go b/main.go index f5da0704..d857ab46 100644 --- a/main.go +++ b/main.go @@ -136,7 +136,7 @@ func updateTgbotEnableSts(status bool) { return } -func updateTgbotSetting(tgBotToken string, tgBotChatid int, tgBotRuntime string) { +func updateTgbotSetting(tgBotToken string, tgBotChatid string, tgBotRuntime string) { err := database.InitDB(config.GetDBPath()) if err != nil { fmt.Println(err) @@ -165,7 +165,7 @@ func updateTgbotSetting(tgBotToken string, tgBotChatid int, tgBotRuntime string) } } - if tgBotChatid != 0 { + if tgBotChatid != "" { err := settingService.SetTgBotChatId(tgBotChatid) if err != nil { fmt.Println(err) @@ -224,7 +224,7 @@ func main() { var username string var password string var tgbottoken string - var tgbotchatid int + var tgbotchatid string var enabletgbot bool var tgbotRuntime string var reset bool @@ -236,7 +236,7 @@ func main() { settingCmd.StringVar(&password, "password", "", "set login password") settingCmd.StringVar(&tgbottoken, "tgbottoken", "", "set telegrame bot token") settingCmd.StringVar(&tgbotRuntime, "tgbotRuntime", "", "set telegrame bot cron time") - settingCmd.IntVar(&tgbotchatid, "tgbotchatid", 0, "set telegrame bot chat id") + settingCmd.StringVar(&tgbotchatid, "tgbotchatid", "", "set telegrame bot chat id") settingCmd.BoolVar(&enabletgbot, "enabletgbot", false, "enable telegram bot notify") oldUsage := flag.Usage @@ -287,7 +287,7 @@ func main() { if show { showSetting(show) } - if (tgbottoken != "") || (tgbotchatid != 0) || (tgbotRuntime != "") { + if (tgbottoken != "") || (tgbotchatid != "") || (tgbotRuntime != "") { updateTgbotSetting(tgbottoken, tgbotchatid, tgbotRuntime) } default: diff --git a/web/assets/js/model/models.js b/web/assets/js/model/models.js index 5e8bf6e3..50cf1d99 100644 --- a/web/assets/js/model/models.js +++ b/web/assets/js/model/models.js @@ -172,7 +172,7 @@ class AllSetting { this.webBasePath = "/"; this.tgBotEnable = false; this.tgBotToken = ""; - this.tgBotChatId = 0; + this.tgBotChatId = ""; this.tgRunTime = ""; this.xrayTemplateConfig = ""; diff --git a/web/entity/entity.go b/web/entity/entity.go index f8572f66..ef8efdc1 100644 --- a/web/entity/entity.go +++ b/web/entity/entity.go @@ -34,7 +34,7 @@ type AllSetting struct { WebBasePath string `json:"webBasePath" form:"webBasePath"` TgBotEnable bool `json:"tgBotEnable" form:"tgBotEnable"` TgBotToken string `json:"tgBotToken" form:"tgBotToken"` - TgBotChatId int `json:"tgBotChatId" form:"tgBotChatId"` + TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"` TgRunTime string `json:"tgRunTime" form:"tgRunTime"` XrayTemplateConfig string `json:"xrayTemplateConfig" form:"xrayTemplateConfig"` diff --git a/web/job/stats_notify_job.go b/web/job/stats_notify_job.go index 689ba200..f23ff0a9 100644 --- a/web/job/stats_notify_job.go +++ b/web/job/stats_notify_job.go @@ -18,10 +18,8 @@ const ( ) type StatsNotifyJob struct { - enable bool xrayService service.XrayService inboundService service.InboundService - settingService service.SettingService tgbotService service.Tgbot } @@ -80,10 +78,10 @@ func (j *StatsNotifyJob) Run() { for _, inbound := range inbouds { info += fmt.Sprintf("Node name:%s\r\nPort:%d\r\nUpload↑:%s\r\nDownload↓:%s\r\nTotal:%s\r\n", inbound.Remark, inbound.Port, common.FormatTraffic(inbound.Up), common.FormatTraffic(inbound.Down), common.FormatTraffic((inbound.Up + inbound.Down))) if inbound.ExpiryTime == 0 { - info += fmt.Sprintf("Expire date:unlimited\r\n \r\n") + info += "Expire date:unlimited\r\n \r\n" } else { info += fmt.Sprintf("Expire date:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")) } } - j.tgbotService.SendMsgToTgbot(info) + j.tgbotService.SendMsgToTgbotAdmins(info) } diff --git a/web/service/setting.go b/web/service/setting.go index 4302af34..e21729db 100644 --- a/web/service/setting.go +++ b/web/service/setting.go @@ -31,7 +31,7 @@ var defaultValueMap = map[string]string{ "timeLocation": "Asia/Tehran", "tgBotEnable": "false", "tgBotToken": "", - "tgBotChatId": "0", + "tgBotChatId": "", "tgRunTime": "", } @@ -202,12 +202,12 @@ func (s *SettingService) SetTgBotToken(token string) error { return s.setString("tgBotToken", token) } -func (s *SettingService) GetTgBotChatId() (int, error) { - return s.getInt("tgBotChatId") +func (s *SettingService) GetTgBotChatId() (string, error) { + return s.getString("tgBotChatId") } -func (s *SettingService) SetTgBotChatId(chatId int) error { - return s.setInt("tgBotChatId", chatId) +func (s *SettingService) SetTgBotChatId(chatIds string) error { + return s.setString("tgBotChatId", chatIds) } func (s *SettingService) SetTgbotenabled(value bool) error { diff --git a/web/service/tgbot.go b/web/service/tgbot.go index b7fd288b..74c380df 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -3,6 +3,8 @@ package service import ( "fmt" "os" + "strconv" + "strings" "time" "x-ui/logger" "x-ui/util/common" @@ -11,7 +13,7 @@ import ( ) var bot *tgbotapi.BotAPI -var tgBotid int +var adminIds []int64 var isRunning bool var numericKeyboard = tgbotapi.NewInlineKeyboardMarkup( @@ -43,12 +45,21 @@ func (t *Tgbot) Start() error { return err } - tgBotid, err = t.settingService.GetTgBotChatId() + tgBotid, err := t.settingService.GetTgBotChatId() if err != nil { logger.Warning("Get GetTgBotChatId failed:", err) return err } + for _, adminId := range strings.Split(tgBotid, ",") { + id, err := strconv.Atoi(adminId) + if err != nil { + logger.Warning("Failed to get IDs from GetTgBotChatId:", err) + return err + } + adminIds = append(adminIds, int64(id)) + } + bot, err = tgbotapi.NewBotAPI(tgBottoken) if err != nil { fmt.Println("Get tgbot's api error:", err) @@ -58,7 +69,7 @@ func (t *Tgbot) Start() error { // listen for TG bot income messages if !isRunning { - logger.Info("Telegram receiver starting") + logger.Info("Starting Telegram receiver ...") go t.OnReceive() isRunning = true } @@ -72,8 +83,9 @@ func (t *Tgbot) IsRunnging() bool { func (t *Tgbot) Stop() { bot.StopReceivingUpdates() - logger.Info("Send Kill to Telegram listener ...") + logger.Info("Stop Telegram receiver ...") isRunning = false + adminIds = nil } func (t *Tgbot) OnReceive() { @@ -84,7 +96,6 @@ func (t *Tgbot) OnReceive() { for update := range updates { if update.Message == nil { - if update.CallbackQuery != nil { // Respond to the callback query, telling Telegram to show the user // a message with the data received. @@ -142,10 +153,18 @@ func (t *Tgbot) OnReceive() { } } -func (t *Tgbot) SendMsgToTgbot(msg string) { - info := tgbotapi.NewMessage(int64(tgBotid), msg) - //msg.ReplyToMessageID = int(tgBotid) - bot.Send(info) +func (t *Tgbot) SendMsgToTgbot(tgid int64, msg string) { + info := tgbotapi.NewMessage(tgid, msg) + _, err := bot.Send(info) + if err != nil { + logger.Warning("Error sending telegram message :", err) + } +} + +func (t *Tgbot) SendMsgToTgbotAdmins(msg string) { + for _, adminId := range adminIds { + t.SendMsgToTgbot(adminId, msg) + } } func (t *Tgbot) UserLoginNotify(username string, ip string, time string, status LoginStatus) { @@ -168,7 +187,7 @@ func (t *Tgbot) UserLoginNotify(username string, ip string, time string, status msg += fmt.Sprintf("Time:%s\r\n", time) msg += fmt.Sprintf("Username:%s\r\n", username) msg += fmt.Sprintf("IP:%s\r\n", ip) - t.SendMsgToTgbot(msg) + t.SendMsgToTgbotAdmins(msg) } func (t *Tgbot) getClientUsage(id string) string { diff --git a/web/translation/translate.en_US.toml b/web/translation/translate.en_US.toml index 7529a16b..f019e180 100644 --- a/web/translation/translate.en_US.toml +++ b/web/translation/translate.en_US.toml @@ -198,8 +198,8 @@ "telegramBotEnableDesc" = "Restart the panel to take effect" "telegramToken" = "Telegram Token" "telegramTokenDesc" = "Restart the panel to take effect" -"telegramChatId" = "Telegram ChatId" -"telegramChatIdDesc" = "Restart the panel to take effect" +"telegramChatId" = "Telegram Admin ChatIds" +"telegramChatIdDesc" = "Multi chatIDs separated by comma. Restart the panel to take effect" "telegramNotifyTime" = "Telegram bot notification time" "telegramNotifyTimeDesc" = "Using Crontab timing format, restart the panel to take effect" "timeZonee" = "Time Zone" diff --git a/web/translation/translate.fa_IR.toml b/web/translation/translate.fa_IR.toml index 48b2f16e..bdfb27be 100644 --- a/web/translation/translate.fa_IR.toml +++ b/web/translation/translate.fa_IR.toml @@ -198,8 +198,8 @@ "telegramBotEnableDesc" = "پنل را مجدداً راه اندازی کنید تا اعمال شود" "telegramToken" = "توکن تلگرام" "telegramTokenDesc" = "پنل را مجدداً راه اندازی کنید تا اعمال شود" -"telegramChatId" = "آی دی تلگرام مدیریت . از ربات @getidsbot آی دی خود را دریافت کنید" -"telegramChatIdDesc" = "پنل را مجدداً راه اندازی کنید تا اعمال شود" +"telegramChatId" = "آی دی تلگرام مدیریت" +"telegramChatIdDesc" = "با استفاده از کاما میتونید چند آی دی را از هم جدا کنید. پنل را مجدداً راه اندازی کنید تا اعمال شود" "telegramNotifyTime" = "مدت زمان نوتیفیکیشن ربات تلگرام" "telegramNotifyTimeDesc" = "از فرمت زمان بندی Crontab استفاده کنید . پنل را مجدداً راه اندازی کنید تا اعمال شود" "timeZonee" = "منظقه زمانی" diff --git a/web/translation/translate.zh_Hans.toml b/web/translation/translate.zh_Hans.toml index 5fb63076..d877a56c 100644 --- a/web/translation/translate.zh_Hans.toml +++ b/web/translation/translate.zh_Hans.toml @@ -198,7 +198,7 @@ "telegramBotEnableDesc" = "重启面板生效" "telegramToken" = "电报机器人TOKEN" "telegramTokenDesc" = "重启面板生效" -"telegramChatId" = "电报机器人ChatId" +"telegramChatId" = "以逗号分隔的多个 chatID 重启面板生效" "telegramChatIdDesc" = "重启面板生效" "telegramNotifyTime" = "电报机器人通知时间" "telegramNotifyTimeDesc" = "采用Crontab定时格式,重启面板生效" From 5bac00b4195f461ff9689265a3b1de69c58522d8 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Thu, 9 Mar 2023 15:49:28 +0100 Subject: [PATCH 3/6] tgbot admin & client separation #34 --- web/job/stats_notify_job.go | 59 +------ web/service/inbound.go | 25 +-- web/service/tgbot.go | 318 +++++++++++++++++++++++++++--------- 3 files changed, 245 insertions(+), 157 deletions(-) diff --git a/web/job/stats_notify_job.go b/web/job/stats_notify_job.go index f23ff0a9..365f41ae 100644 --- a/web/job/stats_notify_job.go +++ b/web/job/stats_notify_job.go @@ -1,12 +1,6 @@ package job import ( - "fmt" - "net" - "os" - "time" - "x-ui/logger" - "x-ui/util/common" "x-ui/web/service" ) @@ -32,56 +26,5 @@ func (j *StatsNotifyJob) Run() { if !j.xrayService.IsXrayRunning() { return } - var info string - //get hostname - name, err := os.Hostname() - if err != nil { - fmt.Println("get hostname error:", err) - return - } - info = fmt.Sprintf("Hostname:%s\r\n", name) - //get ip address - var ip string - netInterfaces, err := net.Interfaces() - if err != nil { - fmt.Println("net.Interfaces failed, err:", err.Error()) - return - } - - 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 { - ip = ipnet.IP.String() - break - } else { - ip = ipnet.IP.String() - break - } - } - } - } - } - info += fmt.Sprintf("IP:%s\r\n \r\n", ip) - - // get traffic - inbouds, err := j.inboundService.GetAllInbounds() - if err != nil { - logger.Warning("StatsNotifyJob run failed:", err) - return - } - // NOTE:If there no any sessions here,need to notify here - // TODO:Sub-node push, automatic conversion format - for _, inbound := range inbouds { - info += fmt.Sprintf("Node name:%s\r\nPort:%d\r\nUpload↑:%s\r\nDownload↓:%s\r\nTotal:%s\r\n", inbound.Remark, inbound.Port, common.FormatTraffic(inbound.Up), common.FormatTraffic(inbound.Down), common.FormatTraffic((inbound.Up + inbound.Down))) - if inbound.ExpiryTime == 0 { - info += "Expire date:unlimited\r\n \r\n" - } else { - info += fmt.Sprintf("Expire date:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")) - } - } - j.tgbotService.SendMsgToTgbotAdmins(info) + j.tgbotService.SendReport() } diff --git a/web/service/inbound.go b/web/service/inbound.go index a020b01e..91db8720 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -501,33 +501,16 @@ func (s *InboundService) ResetClientTraffic(id int, clientEmail string) error { } return nil } -func (s *InboundService) GetClientTrafficById(uuid string) (traffic *xray.ClientTraffic, err error) { +func (s *InboundService) GetClientTrafficTgBot(tguname string) (traffic []*xray.ClientTraffic, err error) { db := database.GetDB() - inbound := &model.Inbound{} - traffic = &xray.ClientTraffic{} + var traffics []*xray.ClientTraffic - err = db.Model(model.Inbound{}).Where("settings like ?", "%"+uuid+"%").First(inbound).Error + err = db.Model(xray.ClientTraffic{}).Where("email like ?", "%@"+tguname).Find(&traffics).Error if err != nil { if err == gorm.ErrRecordNotFound { logger.Warning(err) return nil, err } } - traffic.InboundId = inbound.Id - - // get settings clients - settings := map[string][]model.Client{} - json.Unmarshal([]byte(inbound.Settings), &settings) - clients := settings["clients"] - for _, client := range clients { - if uuid == client.ID { - traffic.Email = client.Email - } - } - err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error - if err != nil { - logger.Warning(err) - return nil, err - } - return traffic, err + return traffics, err } diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 74c380df..15ca47c7 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -2,10 +2,12 @@ package service import ( "fmt" + "net" "os" "strconv" "strings" "time" + "x-ui/config" "x-ui/logger" "x-ui/util/common" @@ -16,12 +18,6 @@ var bot *tgbotapi.BotAPI var adminIds []int64 var isRunning bool -var numericKeyboard = tgbotapi.NewInlineKeyboardMarkup( - tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("Get Usage", "get_usage"), - ), -) - type LoginStatus byte const ( @@ -32,6 +28,8 @@ const ( type Tgbot struct { inboundService InboundService settingService SettingService + serverService ServerService + lastStatus *Status } func (t *Tgbot) NewTgbot() *Tgbot { @@ -95,78 +93,201 @@ func (t *Tgbot) OnReceive() { updates := bot.GetUpdatesChan(u) for update := range updates { + tgId := update.FromChat().ID + chatId := update.FromChat().ChatConfig().ChatID + isAdmin := checkAdmin(tgId) if update.Message == nil { if update.CallbackQuery != nil { - // Respond to the callback query, telling Telegram to show the user - // a message with the data received. - callback := tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data) - if _, err := bot.Request(callback); err != nil { - logger.Warning(err) - } - - // And finally, send a message containing the data received. - msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, "") - - switch update.CallbackQuery.Data { - case "get_usage": - msg.Text = "for get your usage send command like this : \n /usage uuid | id \n example : /usage fc3239ed-8f3b-4151-ff51-b183d5182142" - msg.ParseMode = "HTML" - } - if _, err := bot.Send(msg); err != nil { - logger.Warning(err) - } + t.asnwerCallback(update.CallbackQuery, isAdmin) + } + } else { + if update.Message.IsCommand() { + t.answerCommand(update.Message, chatId, isAdmin) + } else { + t.aswerChat(update.Message.Text, chatId, isAdmin) } - - continue - } - - if !update.Message.IsCommand() { // ignore any non-command Messages - continue - } - - // Create a new MessageConfig. We don't have text yet, - // so we leave it empty. - msg := tgbotapi.NewMessage(update.Message.Chat.ID, "") - - // Extract the command from the Message. - switch update.Message.Command() { - case "help": - msg.Text = "What you need?" - msg.ReplyMarkup = numericKeyboard - case "start": - msg.Text = "Hi :) \n What you need?" - msg.ReplyMarkup = numericKeyboard - - case "status": - msg.Text = "bot is ok." - - case "usage": - msg.Text = t.getClientUsage(update.Message.CommandArguments()) - default: - msg.Text = "I don't know that command, /help" - msg.ReplyMarkup = numericKeyboard - } - - if _, err := bot.Send(msg); err != nil { - logger.Warning(err) } } } -func (t *Tgbot) SendMsgToTgbot(tgid int64, msg string) { - info := tgbotapi.NewMessage(tgid, msg) - _, err := bot.Send(info) +func (t *Tgbot) answerCommand(message *tgbotapi.Message, chatId int64, isAdmin bool) { + msg := "" + // Extract the command from the Message. + switch message.Command() { + case "help": + msg = "This bot is providing you some specefic data from the server.\n\n Please choose:" + case "start": + msg = "Hello " + message.From.FirstName + " :)" + if isAdmin { + hostname, _ := os.Hostname() + msg += "\nWelcome to " + hostname + " management bot" + } + msg += "\n\nI can do some magics for you, please choose:" + case "status": + msg = "bot is ok." + default: + msg = "Unknown command" + } + t.SendAnswer(chatId, msg, isAdmin) +} + +func (t *Tgbot) aswerChat(message string, chatId int64, isAdmin bool) { + t.SendAnswer(chatId, "Unknown message", isAdmin) +} + +func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bool) { + // Respond to the callback query, telling Telegram to show the user + // a message with the data received. + callback := tgbotapi.NewCallback(callbackQuery.ID, callbackQuery.Data) + if _, err := bot.Request(callback); err != nil { + logger.Warning(err) + } + + switch callbackQuery.Data { + case "get_usage": + t.SendMsgToTgbot(callbackQuery.From.ID, t.getServerUsage()) + case "inbounds": + t.SendMsgToTgbot(callbackQuery.From.ID, t.getInboundUsages()) + case "get_backup": + t.sendBackup(callbackQuery.From.ID) + case "client_traffic": + t.getClientUsage(callbackQuery.From.ID, callbackQuery.From.UserName) + } +} + +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) { + var numericKeyboard = tgbotapi.NewInlineKeyboardMarkup( + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("Server Usage", "get_usage"), + tgbotapi.NewInlineKeyboardButtonData("Get DB Backup", "get_backup"), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("Get Inbounds", "inbounds"), + tgbotapi.NewInlineKeyboardButtonData("Disabled Inbounds", "dis_inbounds"), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("Exhausted soon", "exhausted_soon"), + tgbotapi.NewInlineKeyboardButtonData("User info", "user_info"), + ), + ) + var numericKeyboardClient = tgbotapi.NewInlineKeyboardMarkup( + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("Get Usage", "client_traffic"), + ), + ) + msgConfig := tgbotapi.NewMessage(chatId, msg) + msgConfig.ParseMode = "HTML" + if isAdmin { + msgConfig.ReplyMarkup = numericKeyboard + } else { + msgConfig.ReplyMarkup = numericKeyboardClient + } + _, err := bot.Send(msgConfig) if err != nil { logger.Warning("Error sending telegram message :", err) } } +func (t *Tgbot) SendMsgToTgbot(tgid int64, msg string) { + var allMessages []string + limit := 1000 + // paging message if it is big + if len(msg) > limit { + messages := strings.Split(msg, "\r\n \r\n") + lastIndex := -1 + for _, message := range messages { + if (len(allMessages) == 0) || (len(allMessages[lastIndex])+len(message) > limit) { + allMessages = append(allMessages, message) + lastIndex++ + } else { + allMessages[lastIndex] += "\r\n \r\n" + message + } + } + } else { + allMessages = append(allMessages, msg) + } + for _, message := range allMessages { + info := tgbotapi.NewMessage(tgid, message) + info.ParseMode = "HTML" + _, err := bot.Send(info) + if err != nil { + logger.Warning("Error sending telegram message :", err) + } + time.Sleep(500 * time.Millisecond) + } +} + func (t *Tgbot) SendMsgToTgbotAdmins(msg string) { for _, adminId := range adminIds { t.SendMsgToTgbot(adminId, msg) } } +func (t *Tgbot) getServerUsage() string { + var info string + //get hostname + name, err := os.Hostname() + if err != nil { + logger.Error("get hostname error:", err) + name = "" + } + info = fmt.Sprintf("Hostname:%s\r\n", name) + //get ip address + var ip string + var ipv6 string + netInterfaces, err := net.Interfaces() + if err != nil { + logger.Error("net.Interfaces failed, err:", err.Error()) + info += "IP: Unknown\r\n \r\n" + } 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 { + ip += ipnet.IP.String() + " " + } else if ipnet.IP.To16() != nil && !ipnet.IP.IsLinkLocalUnicast() { + ipv6 += ipnet.IP.String() + " " + } + } + } + } + } + info += fmt.Sprintf("IP:%s\r\nIPv6:%s\r\n", ip, ipv6) + } + + // get latest status of server + t.lastStatus = t.serverService.GetStatus(t.lastStatus) + info += fmt.Sprintf("Server Uptime: %d days\r\n", int(t.lastStatus.Uptime/86400)) + info += fmt.Sprintf("Server Load: %.1f, %.1f, %.1f\r\n", t.lastStatus.Loads[0], t.lastStatus.Loads[1], t.lastStatus.Loads[2]) + info += fmt.Sprintf("Server Memory: %s/%s\r\n", common.FormatTraffic(int64(t.lastStatus.Mem.Current)), common.FormatTraffic(int64(t.lastStatus.Mem.Total))) + info += fmt.Sprintf("TcpCount: %d\r\n", t.lastStatus.TcpCount) + info += fmt.Sprintf("UdpCount: %d\r\n", t.lastStatus.UdpCount) + info += fmt.Sprintf("Total traffic: %s: ↑%s,↓%s\r\n", common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent+t.lastStatus.NetTraffic.Recv)), common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent)), common.FormatTraffic(int64(t.lastStatus.NetTraffic.Recv))) + info += fmt.Sprintf("Xray status: %s", t.lastStatus.Xray.State) + + return info +} + +func (t *Tgbot) SendReport() { + info := t.getServerUsage() + t.SendMsgToTgbotAdmins(info) + inbounds := t.getInboundUsages() + t.SendMsgToTgbotAdmins(inbounds) + +} + func (t *Tgbot) UserLoginNotify(username string, ip string, time string, status LoginStatus) { if username == "" || ip == "" || time == "" { logger.Warning("UserLoginNotify failed,invalid info") @@ -190,27 +311,68 @@ func (t *Tgbot) UserLoginNotify(username string, ip string, time string, status t.SendMsgToTgbotAdmins(msg) } -func (t *Tgbot) getClientUsage(id string) string { - traffic, err := t.inboundService.GetClientTrafficById(id) +func (t *Tgbot) getInboundUsages() string { + info := "" + // get traffic + inbouds, err := t.inboundService.GetAllInbounds() + if err != nil { + logger.Warning("GetAllInbounds run failed:", err) + info += "Failed to get inbounds" + } else { + // NOTE:If there no any sessions here,need to notify here + // TODO:Sub-node push, automatic conversion format + for _, inbound := range inbouds { + info += fmt.Sprintf("Inbound:%s\r\nPort:%d\r\nTraffic: %s (↑%s,↓%s)\r\n", inbound.Remark, inbound.Port, common.FormatTraffic((inbound.Up + inbound.Down)), common.FormatTraffic(inbound.Up), common.FormatTraffic(inbound.Down)) + if inbound.ExpiryTime == 0 { + info += "Expire date:unlimited\r\n \r\n" + } else { + info += fmt.Sprintf("Expire date:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")) + } + } + } + return info +} + +func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) { + traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserName) if err != nil { logger.Warning(err) - return "something wrong!" + msg := "Something went wrong!" + t.SendMsgToTgbot(chatId, msg) + return } - expiryTime := "" - if traffic.ExpiryTime == 0 { - expiryTime = "unlimited" - } else { - expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05") + if len(traffics) == 0 { + msg := "Your configuration is not found!\nPlease ask your Admin to use your telegram username in your configuration(s).\n\nYour username: @" + tgUserName + "" + t.SendMsgToTgbot(chatId, msg) } - total := "" - if traffic.Total == 0 { - total = "unlimited" - } else { - total = common.FormatTraffic((traffic.Total)) + for _, traffic := range traffics { + expiryTime := "" + if traffic.ExpiryTime == 0 { + expiryTime = "unlimited" + } else { + expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05") + } + total := "" + if traffic.Total == 0 { + total = "unlimited" + } else { + total = common.FormatTraffic((traffic.Total)) + } + output := fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n", + traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)), + total, expiryTime) + t.SendMsgToTgbot(chatId, output) + } + t.SendAnswer(chatId, "Please choose:", false) +} + +func (t *Tgbot) sendBackup(chatId int64) { + sendingTime := time.Now().Format("2006-01-02 15:04:05") + t.SendMsgToTgbot(chatId, "Backup time: "+sendingTime) + file := tgbotapi.FilePath(config.GetDBPath()) + msg := tgbotapi.NewDocument(chatId, file) + _, err := bot.Send(msg) + if err != nil { + logger.Warning("Error in uploading backup: ", err) } - output := fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n", - traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)), - total, expiryTime) - - return output } From 2e2f066c4b60b7b1022294cfdb09fa9e1e79eeed Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Thu, 9 Mar 2023 22:01:08 +0100 Subject: [PATCH 4/6] Telegram complete reports + Frontend #34 --- web/assets/js/model/models.js | 6 +- web/controller/setting.go | 3 +- web/entity/entity.go | 4 + web/html/xui/setting.html | 4 + web/service/inbound.go | 14 +++ web/service/setting.go | 36 ++++++ web/service/tgbot.go | 151 +++++++++++++++++++++++-- web/translation/translate.en_US.toml | 10 +- web/translation/translate.fa_IR.toml | 8 ++ web/translation/translate.zh_Hans.toml | 8 ++ 10 files changed, 230 insertions(+), 14 deletions(-) diff --git a/web/assets/js/model/models.js b/web/assets/js/model/models.js index 50cf1d99..ab6631a6 100644 --- a/web/assets/js/model/models.js +++ b/web/assets/js/model/models.js @@ -173,7 +173,11 @@ class AllSetting { this.tgBotEnable = false; this.tgBotToken = ""; this.tgBotChatId = ""; - this.tgRunTime = ""; + this.tgRunTime = "@daily"; + this.tgBotBackup = false; + this.tgExpireDiff = ""; + this.tgTrafficDiff = ""; + this.tgCpu = ""; this.xrayTemplateConfig = ""; this.timeLocation = "Asia/Tehran"; diff --git a/web/controller/setting.go b/web/controller/setting.go index 922544fe..0456bca3 100644 --- a/web/controller/setting.go +++ b/web/controller/setting.go @@ -2,11 +2,12 @@ package controller import ( "errors" - "github.com/gin-gonic/gin" "time" "x-ui/web/entity" "x-ui/web/service" "x-ui/web/session" + + "github.com/gin-gonic/gin" ) type updateUserForm struct { diff --git a/web/entity/entity.go b/web/entity/entity.go index ef8efdc1..6982f18e 100644 --- a/web/entity/entity.go +++ b/web/entity/entity.go @@ -36,6 +36,10 @@ type AllSetting struct { TgBotToken string `json:"tgBotToken" form:"tgBotToken"` TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"` TgRunTime string `json:"tgRunTime" form:"tgRunTime"` + TgBotBackup bool `json:"tgBotBackup" form:"tgBotBackup"` + TgExpireDiff int `json:"tgExpireDiff" form:"tgExpireDiff"` + TgTrafficDiff int `json:"tgTrafficDiff" form:"tgTrafficDiff"` + TgCpu int `json:"tgCpu" form:"tgCpu"` XrayTemplateConfig string `json:"xrayTemplateConfig" form:"xrayTemplateConfig"` TimeLocation string `json:"timeLocation" form:"timeLocation"` diff --git a/web/html/xui/setting.html b/web/html/xui/setting.html index e213b50f..ca32a601 100644 --- a/web/html/xui/setting.html +++ b/web/html/xui/setting.html @@ -118,6 +118,10 @@ + + + + diff --git a/web/service/inbound.go b/web/service/inbound.go index 91db8720..a0d77638 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -514,3 +514,17 @@ func (s *InboundService) GetClientTrafficTgBot(tguname string) (traffic []*xray. } return traffics, err } + +func (s *InboundService) GetClientTrafficByEmail(email string) (traffic []*xray.ClientTraffic, err error) { + db := database.GetDB() + var traffics []*xray.ClientTraffic + + err = db.Model(xray.ClientTraffic{}).Where("email like ?", "%"+email+"%").Find(&traffics).Error + if err != nil { + if err == gorm.ErrRecordNotFound { + logger.Warning(err) + return nil, err + } + } + return traffics, err +} diff --git a/web/service/setting.go b/web/service/setting.go index e21729db..06eded2d 100644 --- a/web/service/setting.go +++ b/web/service/setting.go @@ -33,6 +33,10 @@ var defaultValueMap = map[string]string{ "tgBotToken": "", "tgBotChatId": "", "tgRunTime": "", + "tgBotBackup": "false", + "tgExpireDiff": "", + "tgTrafficDiff": "", + "tgCpu": "", } type SettingService struct { @@ -226,6 +230,38 @@ func (s *SettingService) GetTgbotRuntime() (string, error) { return s.getString("tgRunTime") } +func (s *SettingService) SetTgBotBackup(value bool) error { + return s.setBool("tgBotBackup", value) +} + +func (s *SettingService) GetTgBotBackup() (bool, error) { + return s.getBool("tgBotBackup") +} + +func (s *SettingService) SetTgExpireDiff(value int) error { + return s.setInt("tgExpireDiff", value) +} + +func (s *SettingService) GetTgExpireDiff() (int, error) { + return s.getInt("tgExpireDiff") +} + +func (s *SettingService) SetTgTrafficDiff(value int) error { + return s.setInt("tgTrafficDiff", value) +} + +func (s *SettingService) GetTgTrafficDiff() (int, error) { + return s.getInt("tgTrafficDiff") +} + +func (s *SettingService) SetTgCpu(value int) error { + return s.setInt("tgCpu", value) +} + +func (s *SettingService) GetTgCpu() (int, error) { + return s.getInt("tgCpu") +} + func (s *SettingService) GetPort() (int, error) { return s.getInt("webPort") } diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 15ca47c7..c68cb286 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -8,8 +8,10 @@ import ( "strings" "time" "x-ui/config" + "x-ui/database/model" "x-ui/logger" "x-ui/util/common" + "x-ui/xray" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) @@ -125,6 +127,8 @@ func (t *Tgbot) answerCommand(message *tgbotapi.Message, chatId int64, isAdmin b msg += "\n\nI can do some magics for you, please choose:" case "status": msg = "bot is ok." + case "usage": + t.searchClient(chatId, message.CommandArguments()) default: msg = "Unknown command" } @@ -148,10 +152,14 @@ func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bo t.SendMsgToTgbot(callbackQuery.From.ID, t.getServerUsage()) case "inbounds": t.SendMsgToTgbot(callbackQuery.From.ID, t.getInboundUsages()) + case "exhausted_soon": + t.SendMsgToTgbot(callbackQuery.From.ID, t.getExhausted()) case "get_backup": t.sendBackup(callbackQuery.From.ID) case "client_traffic": t.getClientUsage(callbackQuery.From.ID, callbackQuery.From.UserName) + case "commands": + t.SendMsgToTgbot(callbackQuery.From.ID, "To search for a client email, just use folowing command:\r\n \r\n/usage email") } } @@ -172,11 +180,10 @@ func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) { ), tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("Get Inbounds", "inbounds"), - tgbotapi.NewInlineKeyboardButtonData("Disabled Inbounds", "dis_inbounds"), + tgbotapi.NewInlineKeyboardButtonData("Exhausted soon", "exhausted_soon"), ), tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData("Exhausted soon", "exhausted_soon"), - tgbotapi.NewInlineKeyboardButtonData("User info", "user_info"), + tgbotapi.NewInlineKeyboardButtonData("Commands", "commands"), ), ) var numericKeyboardClient = tgbotapi.NewInlineKeyboardMarkup( @@ -232,6 +239,23 @@ func (t *Tgbot) SendMsgToTgbotAdmins(msg string) { } } +func (t *Tgbot) SendReport() { + runTime, err := t.settingService.GetTgbotRuntime() + if err == nil && len(runTime) > 0 { + t.SendMsgToTgbotAdmins("Scheduled reports: " + runTime + "\r\nDate-Time: " + time.Now().Format("2006-01-02 15:04:05")) + } + info := t.getServerUsage() + t.SendMsgToTgbotAdmins(info) + exhausted := t.getExhausted() + t.SendMsgToTgbotAdmins(exhausted) + backupEnable, err := t.settingService.GetTgBotBackup() + if err == nil && backupEnable { + for _, adminId := range adminIds { + t.sendBackup(int64(adminId)) + } + } +} + func (t *Tgbot) getServerUsage() string { var info string //get hostname @@ -280,14 +304,6 @@ func (t *Tgbot) getServerUsage() string { return info } -func (t *Tgbot) SendReport() { - info := t.getServerUsage() - t.SendMsgToTgbotAdmins(info) - inbounds := t.getInboundUsages() - t.SendMsgToTgbotAdmins(inbounds) - -} - func (t *Tgbot) UserLoginNotify(username string, ip string, time string, status LoginStatus) { if username == "" || ip == "" || time == "" { logger.Warning("UserLoginNotify failed,invalid info") @@ -366,6 +382,119 @@ func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) { t.SendAnswer(chatId, "Please choose:", false) } +func (t *Tgbot) searchClient(chatId int64, email string) { + traffics, err := t.inboundService.GetClientTrafficByEmail(email) + if err != nil { + logger.Warning(err) + msg := "Something went wrong!" + t.SendMsgToTgbot(chatId, msg) + return + } + if len(traffics) == 0 { + msg := "No result!" + t.SendMsgToTgbot(chatId, msg) + return + } + for _, traffic := range traffics { + expiryTime := "" + if traffic.ExpiryTime == 0 { + expiryTime = "unlimited" + } else { + expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05") + } + total := "" + if traffic.Total == 0 { + total = "unlimited" + } else { + total = common.FormatTraffic((traffic.Total)) + } + output := fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n", + traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)), + total, expiryTime) + t.SendMsgToTgbot(chatId, output) + } +} + +func (t *Tgbot) getExhausted() string { + 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 + output := "" + TrafficThreshold, err := t.settingService.GetTgTrafficDiff() + if err == nil && TrafficThreshold > 0 { + trDiff = int64(TrafficThreshold) * 1073741824 + } + ExpireThreshold, err := t.settingService.GetTgExpireDiff() + if err == nil && ExpireThreshold > 0 { + exDiff = int64(ExpireThreshold) * 84600 + } + inbounds, err := t.inboundService.GetAllInbounds() + if err != nil { + logger.Warning("Unable to load Inbounds", err) + } + for _, inbound := range inbounds { + if inbound.Enable { + if (inbound.ExpiryTime > 0 && (now-inbound.ExpiryTime < exDiff)) || + (inbound.Total > 0 && (inbound.Total-inbound.Up+inbound.Down < trDiff)) { + exhaustedInbounds = append(exhaustedInbounds, *inbound) + } + if len(inbound.ClientStats) > 0 { + for _, client := range inbound.ClientStats { + if client.Enable { + if (client.ExpiryTime > 0 && (now-client.ExpiryTime < exDiff)) || + (client.Total > 0 && (client.Total-client.Up+client.Down < trDiff)) { + exhaustedClients = append(exhaustedClients, client) + } + } else { + disabledClients = append(disabledClients, client) + } + } + } + } else { + disabledInbounds = append(disabledInbounds, *inbound) + } + } + output += fmt.Sprintf("Exhausted Inbounds count:\r\nDisabled: %d\r\nExhaust soon: %d\r\n \r\n", len(disabledInbounds), len(exhaustedInbounds)) + if len(disabledInbounds)+len(exhaustedInbounds) > 0 { + output += "Exhausted Inbounds:\r\n" + for _, inbound := range exhaustedInbounds { + output += fmt.Sprintf("Inbound:%s\r\nPort:%d\r\nTraffic: %s (↑%s,↓%s)\r\n", inbound.Remark, inbound.Port, common.FormatTraffic((inbound.Up + inbound.Down)), common.FormatTraffic(inbound.Up), common.FormatTraffic(inbound.Down)) + if inbound.ExpiryTime == 0 { + output += "Expire date:unlimited\r\n \r\n" + } else { + output += fmt.Sprintf("Expire date:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")) + } + } + } + output += fmt.Sprintf("Exhausted Clients count:\r\nDisabled: %d\r\nExhaust soon: %d\r\n \r\n", len(disabledClients), len(exhaustedClients)) + if len(disabledClients)+len(exhaustedClients) > 0 { + output += "Exhausted Clients:\r\n" + for _, traffic := range exhaustedClients { + expiryTime := "" + if traffic.ExpiryTime == 0 { + expiryTime = "unlimited" + } else { + expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05") + } + total := "" + if traffic.Total == 0 { + total = "unlimited" + } else { + total = common.FormatTraffic((traffic.Total)) + } + output += fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n", + traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)), + total, expiryTime) + } + } + + return output +} + func (t *Tgbot) sendBackup(chatId int64) { sendingTime := time.Now().Format("2006-01-02 15:04:05") t.SendMsgToTgbot(chatId, "Backup time: "+sendingTime) diff --git a/web/translation/translate.en_US.toml b/web/translation/translate.en_US.toml index f019e180..ecf0532b 100644 --- a/web/translation/translate.en_US.toml +++ b/web/translation/translate.en_US.toml @@ -201,7 +201,15 @@ "telegramChatId" = "Telegram Admin ChatIds" "telegramChatIdDesc" = "Multi chatIDs separated by comma. Restart the panel to take effect" "telegramNotifyTime" = "Telegram bot notification time" -"telegramNotifyTimeDesc" = "Using Crontab timing format, restart the panel to take effect" +"telegramNotifyTimeDesc" = "Using Crontab timing format. Restart the panel to take effect" +"tgNotifyBackup" = "Database backup" +"tgNotifyBackupDesc" = "Sending database backup file with report notification. Restart the panel to take effect" +"tgNotifyExpireTimeDiff" = "Remained time threshold" +"tgNotifyExpireTimeDiffDesc" = "This telegram bot will send you a notification before expiration (unit:day)" +"tgNotifyTrafficDiff" = "Remained traffic threshold" +"tgNotifyTrafficDiffDesc" = "This telegram bot will send you a notification before finishing traffic (unit:GB)" +"tgNotifyCpu" = "CPU percentage alert threshold" +"tgNotifyCpuDesc" = "This telegram bot will send you a notification if CPU usage is more than this percentage (unit:%)" "timeZonee" = "Time Zone" "timeZoneDesc" = "The scheduled task runs according to the time in the time zone, and restarts the panel to take effect" diff --git a/web/translation/translate.fa_IR.toml b/web/translation/translate.fa_IR.toml index bdfb27be..18ebc1a8 100644 --- a/web/translation/translate.fa_IR.toml +++ b/web/translation/translate.fa_IR.toml @@ -202,6 +202,14 @@ "telegramChatIdDesc" = "با استفاده از کاما میتونید چند آی دی را از هم جدا کنید. پنل را مجدداً راه اندازی کنید تا اعمال شود" "telegramNotifyTime" = "مدت زمان نوتیفیکیشن ربات تلگرام" "telegramNotifyTimeDesc" = "از فرمت زمان بندی Crontab استفاده کنید . پنل را مجدداً راه اندازی کنید تا اعمال شود" +"tgNotifyBackup" = "پشتیبان گیری از پایگاه داده" +"tgNotifyBackupDesc" = "ارسال کپی فایل پایگاه داده به همراه گزارش دوره ای" +"tgNotifyExpireTimeDiff" = "آستانه زمان باقی مانده" +"tgNotifyExpireTimeDiffDesc" = "این ربات تلگرام قبل از انقضا برای شما پیام ارسال می کند (واحد: روز)" +"tgNotifyTrafficDiff" = "آستانه ترافیک باقی مانده" +"tgNotifyTrafficDiffDesc" = "این ربات تلگرام قبل از اتمام ترافیک برای شما پیام ارسال می کند (واحد: گیگابایت)" +"tgNotifyCpu" = "آستانه هشدار درصد پردازنده" +"tgNotifyCpuDesc" = "این ربات تلگرام در صورت استفاده پردازنده بیشتر از این درصد برای شما پیام ارسال می کند.(واحد: درصد)" "timeZonee" = "منظقه زمانی" "timeZoneDesc" = "وظایف برنامه ریزی شده بر اساس این منطقه زمانی اجرا می شوند. پنل را مجدداً راه اندازی می کند تا اعمال شود" diff --git a/web/translation/translate.zh_Hans.toml b/web/translation/translate.zh_Hans.toml index d877a56c..a7710b10 100644 --- a/web/translation/translate.zh_Hans.toml +++ b/web/translation/translate.zh_Hans.toml @@ -202,6 +202,14 @@ "telegramChatIdDesc" = "重启面板生效" "telegramNotifyTime" = "电报机器人通知时间" "telegramNotifyTimeDesc" = "采用Crontab定时格式,重启面板生效" +"tgNotifyBackup" = "数据库备份" +"tgNotifyBackupDesc" = "正在发送数据库备份文件和报告通知。重启面板生效" +"tgNotifyExpireTimeDiff" = "剩余时间阈值" +"tgNotifyExpireTimeDiffDesc" = "这个 talegram bot 会在到期前给你发送通知(单位:天)" +"tgNotifyTrafficDiff" = "剩余流量阈值" +"tgNotifyTrafficDiffDesc" = "这个 talegram bot 会在流量结束前给你发送通知(单位:GB)" +"tgNotifyCpu" = "CPU 百分比警报阈值" +"tgNotifyCpuDesc" = "如果 CPU 使用率超过此百分比(单位:%),此 talegram bot 将向您发送通知" "timeZonee" = "时区" "timeZoneDesc" = "定时任务按照该时区的时间运行,重启面板生效" From 546b66a61c89c9ff875de0c92cd861240ea0840e Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Thu, 9 Mar 2023 22:40:34 +0100 Subject: [PATCH 5/6] Fix initiate DB #34 --- web/entity/entity.go | 3 +-- web/service/setting.go | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/web/entity/entity.go b/web/entity/entity.go index 6982f18e..372f5caa 100644 --- a/web/entity/entity.go +++ b/web/entity/entity.go @@ -41,8 +41,7 @@ type AllSetting struct { TgTrafficDiff int `json:"tgTrafficDiff" form:"tgTrafficDiff"` TgCpu int `json:"tgCpu" form:"tgCpu"` XrayTemplateConfig string `json:"xrayTemplateConfig" form:"xrayTemplateConfig"` - - TimeLocation string `json:"timeLocation" form:"timeLocation"` + TimeLocation string `json:"timeLocation" form:"timeLocation"` } func (s *AllSetting) CheckValid() error { diff --git a/web/service/setting.go b/web/service/setting.go index 06eded2d..d9f73e5a 100644 --- a/web/service/setting.go +++ b/web/service/setting.go @@ -32,11 +32,11 @@ var defaultValueMap = map[string]string{ "tgBotEnable": "false", "tgBotToken": "", "tgBotChatId": "", - "tgRunTime": "", + "tgRunTime": "@daily", "tgBotBackup": "false", - "tgExpireDiff": "", - "tgTrafficDiff": "", - "tgCpu": "", + "tgExpireDiff": "0", + "tgTrafficDiff": "0", + "tgCpu": "0", } type SettingService struct { @@ -214,54 +214,54 @@ func (s *SettingService) SetTgBotChatId(chatIds string) error { return s.setString("tgBotChatId", chatIds) } -func (s *SettingService) SetTgbotenabled(value bool) error { - return s.setBool("tgBotEnable", value) -} - func (s *SettingService) GetTgbotenabled() (bool, error) { return s.getBool("tgBotEnable") } -func (s *SettingService) SetTgbotRuntime(time string) error { - return s.setString("tgRunTime", time) +func (s *SettingService) SetTgbotenabled(value bool) error { + return s.setBool("tgBotEnable", value) } func (s *SettingService) GetTgbotRuntime() (string, error) { return s.getString("tgRunTime") } -func (s *SettingService) SetTgBotBackup(value bool) error { - return s.setBool("tgBotBackup", value) +func (s *SettingService) SetTgbotRuntime(time string) error { + return s.setString("tgRunTime", time) } func (s *SettingService) GetTgBotBackup() (bool, error) { return s.getBool("tgBotBackup") } -func (s *SettingService) SetTgExpireDiff(value int) error { - return s.setInt("tgExpireDiff", value) +func (s *SettingService) SetTgBotBackup(value bool) error { + return s.setBool("tgBotBackup", value) } func (s *SettingService) GetTgExpireDiff() (int, error) { return s.getInt("tgExpireDiff") } -func (s *SettingService) SetTgTrafficDiff(value int) error { - return s.setInt("tgTrafficDiff", value) +func (s *SettingService) SetTgExpireDiff(value int) error { + return s.setInt("tgExpireDiff", value) } func (s *SettingService) GetTgTrafficDiff() (int, error) { return s.getInt("tgTrafficDiff") } -func (s *SettingService) SetTgCpu(value int) error { - return s.setInt("tgCpu", value) +func (s *SettingService) SetTgTrafficDiff(value int) error { + return s.setInt("tgTrafficDiff", value) } func (s *SettingService) GetTgCpu() (int, error) { return s.getInt("tgCpu") } +func (s *SettingService) SetTgCpu(value int) error { + return s.setInt("tgCpu", value) +} + func (s *SettingService) GetPort() (int, error) { return s.getInt("webPort") } From e1c76fdc7aa66973508d30a453d6192c4a44d978 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Fri, 10 Mar 2023 14:11:31 +0100 Subject: [PATCH 6/6] Add CPU load alarm #34 --- web/job/check_cpu_usage.go | 30 ++++++++++++++++++++++++++++ web/job/stats_notify_job.go | 5 ++--- web/translation/translate.fa_IR.toml | 2 +- web/web.go | 7 +++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 web/job/check_cpu_usage.go diff --git a/web/job/check_cpu_usage.go b/web/job/check_cpu_usage.go new file mode 100644 index 00000000..cfc86b60 --- /dev/null +++ b/web/job/check_cpu_usage.go @@ -0,0 +1,30 @@ +package job + +import ( + "fmt" + "time" + "x-ui/web/service" + + "github.com/shirou/gopsutil/v3/cpu" +) + +type CheckCpuJob struct { + tgbotService service.Tgbot + settingService service.SettingService +} + +func NewCheckCpuJob() *CheckCpuJob { + return new(CheckCpuJob) +} + +// Here run is a interface method of Job interface +func (j *CheckCpuJob) Run() { + threshold, _ := j.settingService.GetTgCpu() + + // get latest status of server + percent, err := cpu.Percent(1*time.Second, false) + if err == nil && percent[0] > float64(threshold) { + msg := fmt.Sprintf("🔴 CPU usage %.2f%% is more than threshold %d%%", percent[0], threshold) + j.tgbotService.SendMsgToTgbotAdmins(msg) + } +} diff --git a/web/job/stats_notify_job.go b/web/job/stats_notify_job.go index 365f41ae..ae5eba70 100644 --- a/web/job/stats_notify_job.go +++ b/web/job/stats_notify_job.go @@ -12,9 +12,8 @@ const ( ) type StatsNotifyJob struct { - xrayService service.XrayService - inboundService service.InboundService - tgbotService service.Tgbot + xrayService service.XrayService + tgbotService service.Tgbot } func NewStatsNotifyJob() *StatsNotifyJob { diff --git a/web/translation/translate.fa_IR.toml b/web/translation/translate.fa_IR.toml index 18ebc1a8..9c0e092c 100644 --- a/web/translation/translate.fa_IR.toml +++ b/web/translation/translate.fa_IR.toml @@ -74,7 +74,7 @@ "xraySwitchClickDesk" = "لطفا با دقت انتخاب کنید ، در صورت انتخاب اشتباه امکان قطعی سیستم وجود دارد ." "operationHours" = "ساعت فعال" "operationHoursDesc" = "ساعت فعال بعد از شروع سیستم" -"systemLoad" = "سرعت لود سیستم" +"systemLoad" = "بار روی سیستم" "connectionCount" = "تعداد کانکشن ها" "connectionCountDesc" = "تعداد کانکشن ها برای کل شبکه" "upSpeed" = "سرعت آپلود در حال حاضر سیستم" diff --git a/web/web.go b/web/web.go index 7a68d1ee..4880ff19 100644 --- a/web/web.go +++ b/web/web.go @@ -325,6 +325,13 @@ func (s *Server) startTask() { logger.Warning("Add NewStatsNotifyJob error", err) return } + + // Check CPU load and alarm to TgBot if threshold passes + cpuThreshold, err := s.settingService.GetTgCpu() + if (err == nil) && (cpuThreshold > 0) { + s.cron.AddJob("@every 10s", job.NewCheckCpuJob()) + } + } else { s.cron.Remove(entry) }