tgbot_report.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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/model"
  12. "github.com/mhsanaei/3x-ui/v3/internal/eventbus"
  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 publishes a login event to the event bus.
  132. func (t *Tgbot) UserLoginNotify(attempt LoginAttempt) {
  133. if attempt.Username == "" || attempt.IP == "" || attempt.Time == "" {
  134. logger.Warning("UserLoginNotify failed, invalid info!")
  135. return
  136. }
  137. if EventBus == nil {
  138. return
  139. }
  140. status := "fail"
  141. if attempt.Status == LoginSuccess {
  142. status = "success"
  143. }
  144. EventBus.Publish(eventbus.Event{
  145. Type: eventbus.EventLoginAttempt,
  146. Source: attempt.IP,
  147. Data: &eventbus.LoginEventData{
  148. Username: attempt.Username,
  149. IP: attempt.IP,
  150. Time: attempt.Time,
  151. Status: status,
  152. Reason: attempt.Reason,
  153. },
  154. })
  155. }
  156. // getExhausted retrieves and sends information about exhausted clients.
  157. func (t *Tgbot) getExhausted(chatId int64) {
  158. trDiff := int64(0)
  159. exDiff := int64(0)
  160. now := time.Now().Unix() * 1000
  161. var exhaustedInbounds []model.Inbound
  162. var exhaustedClients []xray.ClientTraffic
  163. var disabledInbounds []model.Inbound
  164. var disabledClients []xray.ClientTraffic
  165. TrafficThreshold, err := t.settingService.GetTrafficDiff()
  166. if err == nil && TrafficThreshold > 0 {
  167. trDiff = int64(TrafficThreshold) * 1073741824
  168. }
  169. ExpireThreshold, err := t.settingService.GetExpireDiff()
  170. if err == nil && ExpireThreshold > 0 {
  171. exDiff = int64(ExpireThreshold) * 86400000
  172. }
  173. inbounds, err := t.inboundService.GetAllInbounds()
  174. if err != nil {
  175. logger.Warning("Unable to load Inbounds", err)
  176. }
  177. seenClients := make(map[string]bool)
  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 seenClients[client.Email] {
  187. continue
  188. }
  189. seenClients[client.Email] = true
  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 := t.serverService.BackupFilename("")
  361. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  362. document := tu.Document(
  363. tu.ID(chatId),
  364. tu.FileFromBytes(dbData, dbFilename),
  365. )
  366. _, err = bot.SendDocument(ctx, document)
  367. cancel()
  368. if err != nil {
  369. logger.Error("Error in uploading backup: ", err)
  370. }
  371. } else {
  372. logger.Error("Error in getting db backup: ", err)
  373. }
  374. // Small delay between file sends
  375. time.Sleep(500 * time.Millisecond)
  376. // Send config.json backup
  377. file, err := os.Open(xray.GetConfigPath())
  378. if err == nil {
  379. defer file.Close()
  380. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  381. defer cancel()
  382. document := tu.Document(
  383. tu.ID(chatId),
  384. tu.File(file),
  385. )
  386. _, err = bot.SendDocument(ctx, document)
  387. if err != nil {
  388. logger.Error("Error in uploading config.json: ", err)
  389. }
  390. } else {
  391. logger.Error("Error in opening config.json file for backup: ", err)
  392. }
  393. }
  394. // sendBanLogs sends the ban logs to the specified chat.
  395. func (t *Tgbot) sendBanLogs(chatId int64, dt bool) {
  396. if dt {
  397. output := t.I18nBot("tgbot.messages.datetime", "DateTime=="+time.Now().Format("2006-01-02 15:04:05"))
  398. t.SendMsgToTgbot(chatId, output)
  399. }
  400. file, err := os.Open(xray.GetIPLimitBannedPrevLogPath())
  401. if err == nil {
  402. // Check if the file is non-empty before attempting to upload
  403. fileInfo, _ := file.Stat()
  404. if fileInfo.Size() > 0 {
  405. document := tu.Document(
  406. tu.ID(chatId),
  407. tu.File(file),
  408. )
  409. _, err = bot.SendDocument(context.Background(), document)
  410. if err != nil {
  411. logger.Error("Error in uploading IPLimitBannedPrevLog: ", err)
  412. }
  413. } else {
  414. logger.Warning("IPLimitBannedPrevLog file is empty, not uploading.")
  415. }
  416. file.Close()
  417. } else {
  418. logger.Error("Error in opening IPLimitBannedPrevLog file for backup: ", err)
  419. }
  420. file, err = os.Open(xray.GetIPLimitBannedLogPath())
  421. if err == nil {
  422. // Check if the file is non-empty before attempting to upload
  423. fileInfo, _ := file.Stat()
  424. if fileInfo.Size() > 0 {
  425. document := tu.Document(
  426. tu.ID(chatId),
  427. tu.File(file),
  428. )
  429. _, err = bot.SendDocument(context.Background(), document)
  430. if err != nil {
  431. logger.Error("Error in uploading IPLimitBannedLog: ", err)
  432. }
  433. } else {
  434. logger.Warning("IPLimitBannedLog file is empty, not uploading.")
  435. }
  436. file.Close()
  437. } else {
  438. logger.Error("Error in opening IPLimitBannedLog file for backup: ", err)
  439. }
  440. }