tgbot_inbound.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package tgbot
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  9. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  10. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  11. "github.com/mymmrac/telego"
  12. tu "github.com/mymmrac/telego/telegoutil"
  13. )
  14. // getInboundUsages retrieves and formats inbound usage information.
  15. func (t *Tgbot) getInboundUsages() string {
  16. var info strings.Builder
  17. inbounds, err := t.inboundService.GetAllInbounds()
  18. if err != nil {
  19. logger.Warning("GetAllInbounds run failed:", err)
  20. info.WriteString(t.I18nBot("tgbot.answers.getInboundsFailed"))
  21. return info.String()
  22. }
  23. for _, inbound := range inbounds {
  24. info.WriteString(t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark))
  25. info.WriteString(t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port)))
  26. info.WriteString(t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down)))
  27. clients, listErr := t.clientService.ListForInbound(nil, inbound.Id)
  28. if listErr == nil {
  29. info.WriteString(fmt.Sprintf("👥 Clients: %d\r\n", len(clients)))
  30. }
  31. if inbound.ExpiryTime == 0 {
  32. info.WriteString(t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited")))
  33. } else {
  34. info.WriteString(t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")))
  35. }
  36. info.WriteString("\r\n")
  37. }
  38. return info.String()
  39. }
  40. // getInbounds creates an inline keyboard with all inbounds.
  41. func (t *Tgbot) getInbounds() (*telego.InlineKeyboardMarkup, error) {
  42. inbounds, err := t.inboundService.GetAllInbounds()
  43. if err != nil {
  44. logger.Warning("GetAllInbounds run failed:", err)
  45. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  46. }
  47. if len(inbounds) == 0 {
  48. logger.Warning("No inbounds found")
  49. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  50. }
  51. var buttons []telego.InlineKeyboardButton
  52. for _, inbound := range inbounds {
  53. status := "❌"
  54. if inbound.Enable {
  55. status = "✅"
  56. }
  57. callbackData := t.encodeQuery(fmt.Sprintf("%s %d", "get_clients", inbound.Id))
  58. buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(callbackData))
  59. }
  60. cols := 1
  61. if len(buttons) >= 6 {
  62. cols = 2
  63. }
  64. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  65. return keyboard, nil
  66. }
  67. // getInboundsFor builds an inline keyboard of inbounds for a custom next action.
  68. func (t *Tgbot) getInboundsFor(nextAction string) (*telego.InlineKeyboardMarkup, error) {
  69. inbounds, err := t.inboundService.GetAllInbounds()
  70. if err != nil {
  71. logger.Warning("GetAllInbounds run failed:", err)
  72. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  73. }
  74. if len(inbounds) == 0 {
  75. logger.Warning("No inbounds found")
  76. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  77. }
  78. var buttons []telego.InlineKeyboardButton
  79. for _, inbound := range inbounds {
  80. status := "❌"
  81. if inbound.Enable {
  82. status = "✅"
  83. }
  84. callbackData := t.encodeQuery(fmt.Sprintf("%s %d", nextAction, inbound.Id))
  85. buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(callbackData))
  86. }
  87. cols := 1
  88. if len(buttons) >= 6 {
  89. cols = 2
  90. }
  91. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  92. return keyboard, nil
  93. }
  94. // getInboundClientsFor lists clients of an inbound with a specific action prefix to be appended with email
  95. func (t *Tgbot) getInboundClientsFor(inboundID int, action string) (*telego.InlineKeyboardMarkup, error) {
  96. inbound, err := t.inboundService.GetInbound(inboundID)
  97. if err != nil {
  98. logger.Warning("getInboundClientsFor run failed:", err)
  99. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  100. }
  101. clients, err := t.inboundService.GetClients(inbound)
  102. var buttons []telego.InlineKeyboardButton
  103. if err != nil {
  104. logger.Warning("GetInboundClients run failed:", err)
  105. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  106. } else {
  107. if len(clients) > 0 {
  108. for _, client := range clients {
  109. buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery(action+" "+client.Email)))
  110. }
  111. } else {
  112. return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed"))
  113. }
  114. }
  115. cols := 0
  116. if len(buttons) < 6 {
  117. cols = 3
  118. } else {
  119. cols = 2
  120. }
  121. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  122. return keyboard, nil
  123. }
  124. // getInboundsAddClient creates an inline keyboard for adding clients to inbounds.
  125. func (t *Tgbot) getInboundsAddClient() (*telego.InlineKeyboardMarkup, error) {
  126. inbounds, err := t.inboundService.GetAllInbounds()
  127. if err != nil {
  128. logger.Warning("GetAllInbounds run failed:", err)
  129. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  130. }
  131. if len(inbounds) == 0 {
  132. logger.Warning("No inbounds found")
  133. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  134. }
  135. excludedProtocols := map[model.Protocol]bool{
  136. model.Tunnel: true,
  137. model.Mixed: true,
  138. model.WireGuard: true,
  139. model.HTTP: true,
  140. }
  141. var buttons []telego.InlineKeyboardButton
  142. for _, inbound := range inbounds {
  143. if excludedProtocols[inbound.Protocol] {
  144. continue
  145. }
  146. status := "❌"
  147. if inbound.Enable {
  148. status = "✅"
  149. }
  150. callbackData := t.encodeQuery(fmt.Sprintf("%s %d", "add_client_to", inbound.Id))
  151. buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(callbackData))
  152. }
  153. cols := 1
  154. if len(buttons) >= 6 {
  155. cols = 2
  156. }
  157. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  158. return keyboard, nil
  159. }
  160. // getInboundsAttachPicker builds a toggle picker over multi-client inbounds
  161. // for the "attach more inbounds to the new client" step. Each row shows the
  162. // current selection state for the inbound; tapping fires
  163. // add_client_toggle_attach <id> which flips it and re-renders. A final
  164. // "Done" button (add_client_attach_done) returns to the field-edit screen.
  165. func (t *Tgbot) getInboundsAttachPicker() (*telego.InlineKeyboardMarkup, error) {
  166. inbounds, err := t.inboundService.GetAllInbounds()
  167. if err != nil {
  168. logger.Warning("GetAllInbounds run failed:", err)
  169. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  170. }
  171. if len(inbounds) == 0 {
  172. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  173. }
  174. excludedProtocols := map[model.Protocol]bool{
  175. model.Tunnel: true,
  176. model.Mixed: true,
  177. model.WireGuard: true,
  178. model.HTTP: true,
  179. }
  180. selected := make(map[int]bool, len(receiver_inbound_IDs))
  181. for _, id := range receiver_inbound_IDs {
  182. selected[id] = true
  183. }
  184. var buttons []telego.InlineKeyboardButton
  185. for _, ib := range inbounds {
  186. if excludedProtocols[ib.Protocol] {
  187. continue
  188. }
  189. mark := "☐"
  190. if selected[ib.Id] {
  191. mark = "✅"
  192. }
  193. label := fmt.Sprintf("%s %s (%s)", mark, ib.Remark, ib.Protocol)
  194. callback := t.encodeQuery(fmt.Sprintf("add_client_toggle_attach %d", ib.Id))
  195. buttons = append(buttons, tu.InlineKeyboardButton(label).WithCallbackData(callback))
  196. }
  197. cols := 1
  198. if len(buttons) >= 6 {
  199. cols = 2
  200. }
  201. rows := tu.InlineKeyboardCols(cols, buttons...)
  202. rows = append(rows, tu.InlineKeyboardRow(
  203. tu.InlineKeyboardButton("✅ Done").WithCallbackData(t.encodeQuery("add_client_attach_done")),
  204. ))
  205. return tu.InlineKeyboardGrid(rows), nil
  206. }
  207. // getInboundClients creates an inline keyboard with clients of a specific inbound.
  208. func (t *Tgbot) getInboundClients(id int) (*telego.InlineKeyboardMarkup, error) {
  209. inbound, err := t.inboundService.GetInbound(id)
  210. if err != nil {
  211. logger.Warning("getIboundClients run failed:", err)
  212. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  213. }
  214. clients, err := t.inboundService.GetClients(inbound)
  215. var buttons []telego.InlineKeyboardButton
  216. if err != nil {
  217. logger.Warning("GetInboundClients run failed:", err)
  218. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  219. } else {
  220. if len(clients) > 0 {
  221. for _, client := range clients {
  222. buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery("client_get_usage "+client.Email)))
  223. }
  224. } else {
  225. return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed"))
  226. }
  227. }
  228. cols := 0
  229. if len(buttons) < 6 {
  230. cols = 3
  231. } else {
  232. cols = 2
  233. }
  234. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  235. return keyboard, nil
  236. }
  237. // searchInbound searches for inbounds by remark and sends the results.
  238. func (t *Tgbot) searchInbound(chatId int64, remark string) {
  239. inbounds, err := t.inboundService.SearchInbounds(remark)
  240. if err != nil {
  241. logger.Warning(err)
  242. msg := t.I18nBot("tgbot.wentWrong")
  243. t.SendMsgToTgbot(chatId, msg)
  244. return
  245. }
  246. if len(inbounds) == 0 {
  247. msg := t.I18nBot("tgbot.noInbounds")
  248. t.SendMsgToTgbot(chatId, msg)
  249. return
  250. }
  251. for _, inbound := range inbounds {
  252. info := ""
  253. info += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark)
  254. info += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port))
  255. info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down))
  256. if inbound.ExpiryTime == 0 {
  257. info += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited"))
  258. } else {
  259. info += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  260. }
  261. t.SendMsgToTgbot(chatId, info)
  262. if len(inbound.ClientStats) > 0 {
  263. var output strings.Builder
  264. for _, traffic := range inbound.ClientStats {
  265. output.WriteString(t.clientInfoMsg(&traffic, true, true, true, true, true, true))
  266. }
  267. t.SendMsgToTgbot(chatId, output.String())
  268. }
  269. }
  270. }