tgbot_report.go 17 KB

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