tgbot_report.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. package tgbot
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/mhsanaei/3x-ui/v3/internal/config"
  11. "github.com/mhsanaei/3x-ui/v3/internal/database"
  12. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  13. "github.com/mhsanaei/3x-ui/v3/internal/eventbus"
  14. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  15. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  16. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  17. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  18. "github.com/mymmrac/telego"
  19. tu "github.com/mymmrac/telego/telegoutil"
  20. )
  21. // SendReport sends a periodic report to admin chats.
  22. func (t *Tgbot) SendReport() {
  23. runTime, err := t.settingService.GetTgbotRuntime()
  24. if err == nil && len(runTime) > 0 {
  25. msg := ""
  26. msg += t.I18nBot("tgbot.messages.report", "RunTime=="+runTime)
  27. msg += t.I18nBot("tgbot.messages.datetime", "DateTime=="+time.Now().Format("2006-01-02 15:04:05"))
  28. t.SendMsgToTgbotAdmins(msg)
  29. }
  30. info := t.sendServerUsage()
  31. t.SendMsgToTgbotAdmins(info)
  32. t.sendExhaustedToAdmins()
  33. t.notifyExhausted()
  34. backupEnable, err := t.settingService.GetTgBotBackup()
  35. if err == nil && backupEnable {
  36. t.SendBackupToAdmins()
  37. }
  38. }
  39. // SendBackupToAdmins sends a database backup to admin chats.
  40. func (t *Tgbot) SendBackupToAdmins() {
  41. if !t.IsRunning() {
  42. return
  43. }
  44. for i, adminId := range adminIds {
  45. t.sendBackup(int64(adminId))
  46. // Add delay between sends to avoid Telegram rate limits
  47. if i < len(adminIds)-1 {
  48. time.Sleep(1 * time.Second)
  49. }
  50. }
  51. }
  52. // sendExhaustedToAdmins sends notifications about exhausted clients to admins.
  53. func (t *Tgbot) sendExhaustedToAdmins() {
  54. if !t.IsRunning() {
  55. return
  56. }
  57. for _, adminId := range adminIds {
  58. t.getExhausted(int64(adminId))
  59. }
  60. }
  61. // getServerUsage retrieves and formats server usage information.
  62. func (t *Tgbot) getServerUsage(chatId int64, messageID ...int) string {
  63. info := t.prepareServerUsageInfo()
  64. keyboard := tu.InlineKeyboard(tu.InlineKeyboardRow(
  65. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("usage_refresh"))))
  66. if len(messageID) > 0 {
  67. t.editMessageTgBot(chatId, messageID[0], info, keyboard)
  68. } else {
  69. t.SendMsgToTgbot(chatId, info, keyboard)
  70. }
  71. return info
  72. }
  73. // Send server usage without an inline keyboard
  74. func (t *Tgbot) sendServerUsage() string {
  75. info := t.prepareServerUsageInfo()
  76. return info
  77. }
  78. // prepareServerUsageInfo prepares the server usage information string.
  79. func (t *Tgbot) prepareServerUsageInfo() string {
  80. // Check if we have cached data first
  81. if cachedStats, found := t.getCachedServerStats(); found {
  82. return cachedStats
  83. }
  84. info, ipv4, ipv6 := "", "", ""
  85. // get latest status of server with caching
  86. if cachedStatus, found := t.getCachedStatus(); found {
  87. t.lastStatus = cachedStatus
  88. } else {
  89. t.lastStatus = t.serverService.GetStatus(t.lastStatus)
  90. t.setCachedStatus(t.lastStatus)
  91. }
  92. onlines := service.XrayProcess().GetOnlineClients()
  93. info += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname)
  94. info += t.I18nBot("tgbot.messages.version", "Version=="+config.GetVersion())
  95. info += t.I18nBot("tgbot.messages.xrayVersion", "XrayVersion=="+fmt.Sprint(t.lastStatus.Xray.Version))
  96. // get ip address
  97. netInterfaces, err := net.Interfaces()
  98. if err != nil {
  99. logger.Error("net.Interfaces failed, err: ", err.Error())
  100. info += t.I18nBot("tgbot.messages.ip", "IP=="+t.I18nBot("tgbot.unknown"))
  101. info += "\r\n"
  102. } else {
  103. for i := range netInterfaces {
  104. if (netInterfaces[i].Flags & net.FlagUp) != 0 {
  105. addrs, _ := netInterfaces[i].Addrs()
  106. for _, address := range addrs {
  107. if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  108. if ipnet.IP.To4() != nil {
  109. ipv4 += ipnet.IP.String() + " "
  110. } else if ipnet.IP.To16() != nil && !ipnet.IP.IsLinkLocalUnicast() {
  111. ipv6 += ipnet.IP.String() + " "
  112. }
  113. }
  114. }
  115. }
  116. }
  117. info += t.I18nBot("tgbot.messages.ipv4", "IPv4=="+ipv4)
  118. info += t.I18nBot("tgbot.messages.ipv6", "IPv6=="+ipv6)
  119. }
  120. info += t.I18nBot("tgbot.messages.serverUpTime", "UpTime=="+strconv.FormatUint(t.lastStatus.Uptime/86400, 10), "Unit=="+t.I18nBot("tgbot.days"))
  121. info += t.I18nBot("tgbot.messages.serverLoad", "Load1=="+strconv.FormatFloat(t.lastStatus.Loads[0], 'f', 2, 64), "Load2=="+strconv.FormatFloat(t.lastStatus.Loads[1], 'f', 2, 64), "Load3=="+strconv.FormatFloat(t.lastStatus.Loads[2], 'f', 2, 64))
  122. info += t.I18nBot("tgbot.messages.serverMemory", "Current=="+common.FormatTraffic(int64(t.lastStatus.Mem.Current)), "Total=="+common.FormatTraffic(int64(t.lastStatus.Mem.Total)))
  123. info += t.I18nBot("tgbot.messages.onlinesCount", "Count=="+fmt.Sprint(len(onlines)))
  124. info += t.I18nBot("tgbot.messages.tcpCount", "Count=="+strconv.Itoa(t.lastStatus.TcpCount))
  125. info += t.I18nBot("tgbot.messages.udpCount", "Count=="+strconv.Itoa(t.lastStatus.UdpCount))
  126. info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent+t.lastStatus.NetTraffic.Recv)), "Upload=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent)), "Download=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Recv)))
  127. info += t.I18nBot("tgbot.messages.xrayStatus", "State=="+fmt.Sprint(t.lastStatus.Xray.State))
  128. // Cache the complete server stats
  129. t.setCachedServerStats(info)
  130. return info
  131. }
  132. // UserLoginNotify publishes a login event to the event bus.
  133. func (t *Tgbot) UserLoginNotify(attempt LoginAttempt) {
  134. if attempt.Username == "" || attempt.IP == "" || attempt.Time == "" {
  135. logger.Warning("UserLoginNotify failed, invalid info!")
  136. return
  137. }
  138. if EventBus == nil {
  139. return
  140. }
  141. status := "fail"
  142. if attempt.Status == LoginSuccess {
  143. status = "success"
  144. }
  145. EventBus.Publish(eventbus.Event{
  146. Type: eventbus.EventLoginAttempt,
  147. Source: attempt.IP,
  148. Data: &eventbus.LoginEventData{
  149. Username: attempt.Username,
  150. IP: attempt.IP,
  151. Time: attempt.Time,
  152. Status: status,
  153. Reason: attempt.Reason,
  154. },
  155. })
  156. }
  157. // getExhausted retrieves and sends information about exhausted clients.
  158. func (t *Tgbot) getExhausted(chatId int64) {
  159. trDiff := int64(0)
  160. exDiff := int64(0)
  161. now := time.Now().Unix() * 1000
  162. var exhaustedInbounds []model.Inbound
  163. var exhaustedClients []xray.ClientTraffic
  164. var disabledInbounds []model.Inbound
  165. var disabledClients []xray.ClientTraffic
  166. TrafficThreshold, err := t.settingService.GetTrafficDiff()
  167. if err == nil && TrafficThreshold > 0 {
  168. trDiff = int64(TrafficThreshold) * 1073741824
  169. }
  170. ExpireThreshold, err := t.settingService.GetExpireDiff()
  171. if err == nil && ExpireThreshold > 0 {
  172. exDiff = int64(ExpireThreshold) * 86400000
  173. }
  174. inbounds, err := t.inboundService.GetAllInbounds()
  175. if err != nil {
  176. logger.Warning("Unable to load Inbounds", err)
  177. }
  178. for _, inbound := range inbounds {
  179. if inbound.Enable {
  180. if (inbound.ExpiryTime > 0 && (inbound.ExpiryTime-now < exDiff)) ||
  181. (inbound.Total > 0 && (inbound.Total-(inbound.Up+inbound.Down) < trDiff)) {
  182. exhaustedInbounds = append(exhaustedInbounds, *inbound)
  183. }
  184. if len(inbound.ClientStats) > 0 {
  185. for _, client := range inbound.ClientStats {
  186. if client.Enable {
  187. if (client.ExpiryTime > 0 && (client.ExpiryTime-now < exDiff)) ||
  188. (client.Total > 0 && (client.Total-(client.Up+client.Down) < trDiff)) {
  189. exhaustedClients = append(exhaustedClients, client)
  190. }
  191. } else {
  192. disabledClients = append(disabledClients, client)
  193. }
  194. }
  195. }
  196. } else {
  197. disabledInbounds = append(disabledInbounds, *inbound)
  198. }
  199. }
  200. // Inbounds
  201. output := ""
  202. output += t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.inbounds"))
  203. output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledInbounds)))
  204. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(len(exhaustedInbounds)))
  205. if len(exhaustedInbounds) > 0 {
  206. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+t.I18nBot("tgbot.inbounds"))
  207. for _, inbound := range exhaustedInbounds {
  208. output += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark)
  209. output += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port))
  210. output += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down))
  211. if inbound.ExpiryTime == 0 {
  212. output += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited"))
  213. } else {
  214. output += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  215. }
  216. output += "\r\n"
  217. }
  218. }
  219. // Clients
  220. exhaustedCC := len(exhaustedClients)
  221. output += t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.clients"))
  222. output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledClients)))
  223. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(exhaustedCC))
  224. if exhaustedCC > 0 {
  225. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+t.I18nBot("tgbot.clients"))
  226. var buttons []telego.InlineKeyboardButton
  227. for _, traffic := range exhaustedClients {
  228. output += t.clientInfoMsg(&traffic, true, false, false, true, true, false)
  229. output += "\r\n"
  230. buttons = append(buttons, tu.InlineKeyboardButton(traffic.Email).WithCallbackData(t.encodeQuery("client_get_usage "+traffic.Email)))
  231. }
  232. cols := 0
  233. if exhaustedCC < 11 {
  234. cols = 1
  235. } else {
  236. cols = 2
  237. }
  238. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  239. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  240. t.SendMsgToTgbot(chatId, output, keyboard)
  241. } else {
  242. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  243. t.SendMsgToTgbot(chatId, output)
  244. }
  245. }
  246. // notifyExhausted sends notifications for exhausted clients.
  247. func (t *Tgbot) notifyExhausted() {
  248. trDiff := int64(0)
  249. exDiff := int64(0)
  250. now := time.Now().Unix() * 1000
  251. TrafficThreshold, err := t.settingService.GetTrafficDiff()
  252. if err == nil && TrafficThreshold > 0 {
  253. trDiff = int64(TrafficThreshold) * 1073741824
  254. }
  255. ExpireThreshold, err := t.settingService.GetExpireDiff()
  256. if err == nil && ExpireThreshold > 0 {
  257. exDiff = int64(ExpireThreshold) * 86400000
  258. }
  259. inbounds, err := t.inboundService.GetAllInbounds()
  260. if err != nil {
  261. logger.Warning("Unable to load Inbounds", err)
  262. }
  263. var chatIDsDone []int64
  264. for _, inbound := range inbounds {
  265. if inbound.Enable {
  266. if len(inbound.ClientStats) > 0 {
  267. clients, err := t.inboundService.GetClients(inbound)
  268. if err == nil {
  269. for _, client := range clients {
  270. if client.TgID != 0 {
  271. chatID := client.TgID
  272. if !int64Contains(chatIDsDone, chatID) && !checkAdmin(chatID) {
  273. var disabledClients []xray.ClientTraffic
  274. var exhaustedClients []xray.ClientTraffic
  275. traffics, err := t.inboundService.GetClientTrafficTgBot(client.TgID)
  276. if err == nil && len(traffics) > 0 {
  277. var output strings.Builder
  278. output.WriteString(t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.clients")))
  279. for _, traffic := range traffics {
  280. if traffic.Enable {
  281. if (traffic.ExpiryTime > 0 && (traffic.ExpiryTime-now < exDiff)) ||
  282. (traffic.Total > 0 && (traffic.Total-(traffic.Up+traffic.Down) < trDiff)) {
  283. exhaustedClients = append(exhaustedClients, *traffic)
  284. }
  285. } else {
  286. disabledClients = append(disabledClients, *traffic)
  287. }
  288. }
  289. if len(exhaustedClients) > 0 {
  290. output.WriteString(t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledClients))))
  291. if len(disabledClients) > 0 {
  292. output.WriteString(t.I18nBot("tgbot.clients"))
  293. output.WriteString(":\r\n")
  294. for _, traffic := range disabledClients {
  295. output.WriteString(" ")
  296. output.WriteString(traffic.Email)
  297. }
  298. output.WriteString("\r\n")
  299. }
  300. output.WriteString("\r\n")
  301. output.WriteString(t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(len(exhaustedClients))))
  302. for _, traffic := range exhaustedClients {
  303. output.WriteString(t.clientInfoMsg(&traffic, true, false, false, true, true, false))
  304. output.WriteString("\r\n")
  305. }
  306. t.SendMsgToTgbot(chatID, output.String())
  307. }
  308. chatIDsDone = append(chatIDsDone, chatID)
  309. }
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. // onlineClients retrieves and sends information about online clients.
  319. func (t *Tgbot) onlineClients(chatId int64, messageID ...int) {
  320. if !service.XrayProcess().IsRunning() {
  321. return
  322. }
  323. onlines := service.XrayProcess().GetOnlineClients()
  324. onlinesCount := len(onlines)
  325. output := t.I18nBot("tgbot.messages.onlinesCount", "Count=="+fmt.Sprint(onlinesCount))
  326. keyboard := tu.InlineKeyboard(tu.InlineKeyboardRow(
  327. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("onlines_refresh"))))
  328. if onlinesCount > 0 {
  329. var buttons []telego.InlineKeyboardButton
  330. for _, online := range onlines {
  331. buttons = append(buttons, tu.InlineKeyboardButton(online).WithCallbackData(t.encodeQuery("client_get_usage "+online)))
  332. }
  333. cols := 0
  334. if onlinesCount < 21 {
  335. cols = 2
  336. } else if onlinesCount < 61 {
  337. cols = 3
  338. } else {
  339. cols = 4
  340. }
  341. keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, tu.InlineKeyboardCols(cols, buttons...)...)
  342. }
  343. if len(messageID) > 0 {
  344. t.editMessageTgBot(chatId, messageID[0], output, keyboard)
  345. } else {
  346. t.SendMsgToTgbot(chatId, output, keyboard)
  347. }
  348. }
  349. // sendBackup sends a backup of the database and configuration files.
  350. func (t *Tgbot) sendBackup(chatId int64) {
  351. output := t.I18nBot("tgbot.messages.backupTime", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  352. t.SendMsgToTgbot(chatId, output)
  353. // Send database backup (SQLite file, or a pg_dump archive on PostgreSQL)
  354. dbData, err := t.serverService.GetDb()
  355. if err == nil {
  356. dbFilename := "x-ui.db"
  357. if database.IsPostgres() {
  358. dbFilename = "x-ui.dump"
  359. }
  360. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  361. document := tu.Document(
  362. tu.ID(chatId),
  363. tu.FileFromBytes(dbData, dbFilename),
  364. )
  365. _, err = bot.SendDocument(ctx, document)
  366. cancel()
  367. if err != nil {
  368. logger.Error("Error in uploading backup: ", err)
  369. }
  370. } else {
  371. logger.Error("Error in getting db backup: ", err)
  372. }
  373. // Small delay between file sends
  374. time.Sleep(500 * time.Millisecond)
  375. // Send config.json backup
  376. file, err := os.Open(xray.GetConfigPath())
  377. if err == nil {
  378. defer file.Close()
  379. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  380. defer cancel()
  381. document := tu.Document(
  382. tu.ID(chatId),
  383. tu.File(file),
  384. )
  385. _, err = bot.SendDocument(ctx, document)
  386. if err != nil {
  387. logger.Error("Error in uploading config.json: ", err)
  388. }
  389. } else {
  390. logger.Error("Error in opening config.json file for backup: ", err)
  391. }
  392. }
  393. // sendBanLogs sends the ban logs to the specified chat.
  394. func (t *Tgbot) sendBanLogs(chatId int64, dt bool) {
  395. if dt {
  396. output := t.I18nBot("tgbot.messages.datetime", "DateTime=="+time.Now().Format("2006-01-02 15:04:05"))
  397. t.SendMsgToTgbot(chatId, output)
  398. }
  399. file, err := os.Open(xray.GetIPLimitBannedPrevLogPath())
  400. if err == nil {
  401. // Check if the file is non-empty before attempting to upload
  402. fileInfo, _ := file.Stat()
  403. if fileInfo.Size() > 0 {
  404. document := tu.Document(
  405. tu.ID(chatId),
  406. tu.File(file),
  407. )
  408. _, err = bot.SendDocument(context.Background(), document)
  409. if err != nil {
  410. logger.Error("Error in uploading IPLimitBannedPrevLog: ", err)
  411. }
  412. } else {
  413. logger.Warning("IPLimitBannedPrevLog file is empty, not uploading.")
  414. }
  415. file.Close()
  416. } else {
  417. logger.Error("Error in opening IPLimitBannedPrevLog file for backup: ", err)
  418. }
  419. file, err = os.Open(xray.GetIPLimitBannedLogPath())
  420. if err == nil {
  421. // Check if the file is non-empty before attempting to upload
  422. fileInfo, _ := file.Stat()
  423. if fileInfo.Size() > 0 {
  424. document := tu.Document(
  425. tu.ID(chatId),
  426. tu.File(file),
  427. )
  428. _, err = bot.SendDocument(context.Background(), document)
  429. if err != nil {
  430. logger.Error("Error in uploading IPLimitBannedLog: ", err)
  431. }
  432. } else {
  433. logger.Warning("IPLimitBannedLog file is empty, not uploading.")
  434. }
  435. file.Close()
  436. } else {
  437. logger.Error("Error in opening IPLimitBannedLog file for backup: ", err)
  438. }
  439. }