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..ab6631a6 100644 --- a/web/assets/js/model/models.js +++ b/web/assets/js/model/models.js @@ -172,8 +172,12 @@ class AllSetting { this.webBasePath = "/"; this.tgBotEnable = false; this.tgBotToken = ""; - this.tgBotChatId = 0; - this.tgRunTime = ""; + this.tgBotChatId = ""; + this.tgRunTime = "@daily"; + this.tgBotBackup = false; + this.tgExpireDiff = ""; + this.tgTrafficDiff = ""; + this.tgCpu = ""; this.xrayTemplateConfig = ""; this.timeLocation = "Asia/Tehran"; 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/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 f8572f66..372f5caa 100644 --- a/web/entity/entity.go +++ b/web/entity/entity.go @@ -34,11 +34,14 @@ 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"` + 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"` + TimeLocation string `json:"timeLocation" form:"timeLocation"` } func (s *AllSetting) CheckValid() error { 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/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 92963127..ae5eba70 100644 --- a/web/job/stats_notify_job.go +++ b/web/job/stats_notify_job.go @@ -1,15 +1,7 @@ package job import ( - "fmt" - "net" - "os" - "time" - "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 @@ -20,229 +12,18 @@ const ( ) type StatsNotifyJob struct { - enable bool - xrayService service.XrayService - inboundService service.InboundService - settingService service.SettingService + xrayService service.XrayService + 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() { 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 += fmt.Sprintf("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.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.SendReport() } diff --git a/web/service/inbound.go b/web/service/inbound.go index a020b01e..a0d77638 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -501,33 +501,30 @@ 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 + return traffics, err +} - // 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 +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 } } - 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/setting.go b/web/service/setting.go index 4302af34..d9f73e5a 100644 --- a/web/service/setting.go +++ b/web/service/setting.go @@ -31,8 +31,12 @@ var defaultValueMap = map[string]string{ "timeLocation": "Asia/Tehran", "tgBotEnable": "false", "tgBotToken": "", - "tgBotChatId": "0", - "tgRunTime": "", + "tgBotChatId": "", + "tgRunTime": "@daily", + "tgBotBackup": "false", + "tgExpireDiff": "0", + "tgTrafficDiff": "0", + "tgCpu": "0", } type SettingService struct { @@ -202,30 +206,62 @@ 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) SetTgbotenabled(value bool) error { - return s.setBool("tgBotEnable", value) +func (s *SettingService) SetTgBotChatId(chatIds string) error { + return s.setString("tgBotChatId", chatIds) } 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) SetTgbotRuntime(time string) error { + return s.setString("tgRunTime", time) +} + +func (s *SettingService) GetTgBotBackup() (bool, error) { + return s.getBool("tgBotBackup") +} + +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) SetTgExpireDiff(value int) error { + return s.setInt("tgExpireDiff", value) +} + +func (s *SettingService) GetTgTrafficDiff() (int, error) { + return s.getInt("tgTrafficDiff") +} + +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") } diff --git a/web/service/tgbot.go b/web/service/tgbot.go new file mode 100644 index 00000000..c68cb286 --- /dev/null +++ b/web/service/tgbot.go @@ -0,0 +1,507 @@ +package service + +import ( + "fmt" + "net" + "os" + "strconv" + "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" +) + +var bot *tgbotapi.BotAPI +var adminIds []int64 +var isRunning bool + +type LoginStatus byte + +const ( + LoginSuccess LoginStatus = 1 + LoginFail LoginStatus = 0 +) + +type Tgbot struct { + inboundService InboundService + settingService SettingService + serverService ServerService + lastStatus *Status +} + +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 + } + + 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) + return err + } + bot.Debug = false + + // listen for TG bot income messages + if !isRunning { + logger.Info("Starting Telegram receiver ...") + go t.OnReceive() + isRunning = true + } + + return nil +} + +func (t *Tgbot) IsRunnging() bool { + return isRunning +} + +func (t *Tgbot) Stop() { + bot.StopReceivingUpdates() + logger.Info("Stop Telegram receiver ...") + isRunning = false + adminIds = nil +} + +func (t *Tgbot) OnReceive() { + u := tgbotapi.NewUpdate(0) + u.Timeout = 10 + + 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 { + t.asnwerCallback(update.CallbackQuery, isAdmin) + } + } else { + if update.Message.IsCommand() { + t.answerCommand(update.Message, chatId, isAdmin) + } else { + t.aswerChat(update.Message.Text, chatId, isAdmin) + } + } + } +} + +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." + case "usage": + t.searchClient(chatId, message.CommandArguments()) + 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 "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") + } +} + +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("Exhausted soon", "exhausted_soon"), + ), + tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData("Commands", "commands"), + ), + ) + 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) 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 + 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) 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.SendMsgToTgbotAdmins(msg) +} + +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) + msg := "Something went wrong!" + t.SendMsgToTgbot(chatId, msg) + return + } + 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) + } + 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) 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) + file := tgbotapi.FilePath(config.GetDBPath()) + msg := tgbotapi.NewDocument(chatId, file) + _, err := bot.Send(msg) + if err != nil { + logger.Warning("Error in uploading backup: ", err) + } +} diff --git a/web/translation/translate.en_US.toml b/web/translation/translate.en_US.toml index 7529a16b..ecf0532b 100644 --- a/web/translation/translate.en_US.toml +++ b/web/translation/translate.en_US.toml @@ -198,10 +198,18 @@ "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" +"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 48b2f16e..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" = "سرعت آپلود در حال حاضر سیستم" @@ -198,10 +198,18 @@ "telegramBotEnableDesc" = "پنل را مجدداً راه اندازی کنید تا اعمال شود" "telegramToken" = "توکن تلگرام" "telegramTokenDesc" = "پنل را مجدداً راه اندازی کنید تا اعمال شود" -"telegramChatId" = "آی دی تلگرام مدیریت . از ربات @getidsbot آی دی خود را دریافت کنید" -"telegramChatIdDesc" = "پنل را مجدداً راه اندازی کنید تا اعمال شود" +"telegramChatId" = "آی دی تلگرام مدیریت" +"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 5fb63076..a7710b10 100644 --- a/web/translation/translate.zh_Hans.toml +++ b/web/translation/translate.zh_Hans.toml @@ -198,10 +198,18 @@ "telegramBotEnableDesc" = "重启面板生效" "telegramToken" = "电报机器人TOKEN" "telegramTokenDesc" = "重启面板生效" -"telegramChatId" = "电报机器人ChatId" +"telegramChatId" = "以逗号分隔的多个 chatID 重启面板生效" "telegramChatIdDesc" = "重启面板生效" "telegramNotifyTime" = "电报机器人通知时间" "telegramNotifyTimeDesc" = "采用Crontab定时格式,重启面板生效" +"tgNotifyBackup" = "数据库备份" +"tgNotifyBackupDesc" = "正在发送数据库备份文件和报告通知。重启面板生效" +"tgNotifyExpireTimeDiff" = "剩余时间阈值" +"tgNotifyExpireTimeDiffDesc" = "这个 talegram bot 会在到期前给你发送通知(单位:天)" +"tgNotifyTrafficDiff" = "剩余流量阈值" +"tgNotifyTrafficDiffDesc" = "这个 talegram bot 会在流量结束前给你发送通知(单位:GB)" +"tgNotifyCpu" = "CPU 百分比警报阈值" +"tgNotifyCpuDesc" = "如果 CPU 使用率超过此百分比(单位:%),此 talegram bot 将向您发送通知" "timeZonee" = "时区" "timeZoneDesc" = "定时任务按照该时区的时间运行,重启面板生效" diff --git a/web/web.go b/web/web.go index 3f9344d5..4880ff19 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,13 @@ func (s *Server) startTask() { logger.Warning("Add NewStatsNotifyJob error", err) return } - // listen for TG bot income messages - go job.NewStatsNotifyJob().OnReceive() + + // 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) } @@ -403,6 +408,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 +423,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 {