diff options
| author | MHSanaei <mc.sanaei@gmail.com> | 2023-02-09 22:18:06 +0300 |
|---|---|---|
| committer | MHSanaei <mc.sanaei@gmail.com> | 2023-02-09 22:18:06 +0300 |
| commit | b73e4173a3c1e69e02ad6b4e3b43e425e57a5be9 (patch) | |
| tree | d95d2f5e903d97082e11eb9f9023c165b1bde388 /web/job | |
3x-ui
Diffstat (limited to 'web/job')
| -rw-r--r-- | web/job/check_inbound_job.go | 33 | ||||
| -rw-r--r-- | web/job/check_xray_running_job.go | 25 | ||||
| -rw-r--r-- | web/job/stats_notify_job.go | 248 | ||||
| -rw-r--r-- | web/job/xray_traffic_job.go | 38 |
4 files changed, 344 insertions, 0 deletions
diff --git a/web/job/check_inbound_job.go b/web/job/check_inbound_job.go new file mode 100644 index 00000000..2b24afb0 --- /dev/null +++ b/web/job/check_inbound_job.go @@ -0,0 +1,33 @@ +package job + +import ( + "x-ui/logger" + "x-ui/web/service" +) + +type CheckInboundJob struct { + xrayService service.XrayService + inboundService service.InboundService +} + +func NewCheckInboundJob() *CheckInboundJob { + return new(CheckInboundJob) +} + +func (j *CheckInboundJob) Run() { + count, err := j.inboundService.DisableInvalidClients() + if err != nil { + logger.Warning("disable invalid Client err:", err) + } else if count > 0 { + logger.Debugf("disabled %v Client", count) + j.xrayService.SetToNeedRestart() + } + + count, err = j.inboundService.DisableInvalidInbounds() + if err != nil { + logger.Warning("disable invalid inbounds err:", err) + } else if count > 0 { + logger.Debugf("disabled %v inbounds", count) + j.xrayService.SetToNeedRestart() + } +} diff --git a/web/job/check_xray_running_job.go b/web/job/check_xray_running_job.go new file mode 100644 index 00000000..f1a848bf --- /dev/null +++ b/web/job/check_xray_running_job.go @@ -0,0 +1,25 @@ +package job + +import "x-ui/web/service" + +type CheckXrayRunningJob struct { + xrayService service.XrayService + + checkTime int +} + +func NewCheckXrayRunningJob() *CheckXrayRunningJob { + return new(CheckXrayRunningJob) +} + +func (j *CheckXrayRunningJob) Run() { + if j.xrayService.IsXrayRunning() { + j.checkTime = 0 + return + } + j.checkTime++ + if j.checkTime < 2 { + return + } + j.xrayService.SetToNeedRestart() +} diff --git a/web/job/stats_notify_job.go b/web/job/stats_notify_job.go new file mode 100644 index 00000000..5209e204 --- /dev/null +++ b/web/job/stats_notify_job.go @@ -0,0 +1,248 @@ +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 + +const ( + LoginSuccess LoginStatus = 1 + LoginFail LoginStatus = 0 +) + +type StatsNotifyJob struct { + enable bool + xrayService service.XrayService + inboundService service.InboundService + settingService service.SettingService +} + +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:分节点推送,自动转化格式 + 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 <code>/usage uuid | id</code> \n example : <code>/usage fc3239ed-8f3b-4151-ff51-b183d5182142</code>" + 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 +} diff --git a/web/job/xray_traffic_job.go b/web/job/xray_traffic_job.go new file mode 100644 index 00000000..97f85c24 --- /dev/null +++ b/web/job/xray_traffic_job.go @@ -0,0 +1,38 @@ +package job + +import ( + "x-ui/logger" + "x-ui/web/service" +) + +type XrayTrafficJob struct { + xrayService service.XrayService + inboundService service.InboundService +} + +func NewXrayTrafficJob() *XrayTrafficJob { + return new(XrayTrafficJob) +} + +func (j *XrayTrafficJob) Run() { + if !j.xrayService.IsXrayRunning() { + return + } + + traffics, clientTraffics, err := j.xrayService.GetXrayTraffic() + if err != nil { + logger.Warning("get xray traffic failed:", err) + return + } + err = j.inboundService.AddTraffic(traffics) + if err != nil { + logger.Warning("add traffic failed:", err) + } + + err = j.inboundService.AddClientTraffic(clientTraffics) + if err != nil { + logger.Warning("add client traffic failed:", err) + } + + +} |
