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" = "定时任务按照该时区的时间运行,重启面板生效"