tgbot_inbound.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. fmt.Fprintf(&info, "👥 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(inbound *model.Inbound, action string) (*telego.InlineKeyboardMarkup, error) {
  96. clients, err := t.inboundService.GetClients(inbound)
  97. var buttons []telego.InlineKeyboardButton
  98. if err != nil {
  99. logger.Warning("GetInboundClients run failed:", err)
  100. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  101. } else {
  102. if len(clients) > 0 {
  103. for _, client := range clients {
  104. buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery(action+" "+client.Email)))
  105. }
  106. } else {
  107. return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed"))
  108. }
  109. }
  110. cols := 0
  111. if len(buttons) < 6 {
  112. cols = 3
  113. } else {
  114. cols = 2
  115. }
  116. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  117. return keyboard, nil
  118. }
  119. // getInboundsAddClient creates an inline keyboard for adding clients to inbounds.
  120. func (t *Tgbot) getInboundsAddClient() (*telego.InlineKeyboardMarkup, error) {
  121. inbounds, err := t.inboundService.GetAllInbounds()
  122. if err != nil {
  123. logger.Warning("GetAllInbounds run failed:", err)
  124. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  125. }
  126. if len(inbounds) == 0 {
  127. logger.Warning("No inbounds found")
  128. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  129. }
  130. excludedProtocols := map[model.Protocol]bool{
  131. model.Tunnel: true,
  132. model.Mixed: true,
  133. model.WireGuard: true,
  134. model.AmneziaWG: true,
  135. model.HTTP: true,
  136. }
  137. var buttons []telego.InlineKeyboardButton
  138. for _, inbound := range inbounds {
  139. if excludedProtocols[inbound.Protocol] {
  140. continue
  141. }
  142. status := "❌"
  143. if inbound.Enable {
  144. status = "✅"
  145. }
  146. callbackData := t.encodeQuery(fmt.Sprintf("%s %d", "add_client_to", inbound.Id))
  147. buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(callbackData))
  148. }
  149. cols := 1
  150. if len(buttons) >= 6 {
  151. cols = 2
  152. }
  153. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  154. return keyboard, nil
  155. }
  156. // getInboundsAttachPicker builds a toggle picker over multi-client inbounds
  157. // for the "attach more inbounds to the new client" step. Each row shows the
  158. // current selection state for the inbound; tapping fires
  159. // add_client_toggle_attach <id> which flips it and re-renders. A final
  160. // "Done" button (add_client_attach_done) returns to the field-edit screen.
  161. func (t *Tgbot) getInboundsAttachPicker() (*telego.InlineKeyboardMarkup, error) {
  162. inbounds, err := t.inboundService.GetAllInbounds()
  163. if err != nil {
  164. logger.Warning("GetAllInbounds run failed:", err)
  165. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  166. }
  167. if len(inbounds) == 0 {
  168. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  169. }
  170. excludedProtocols := map[model.Protocol]bool{
  171. model.Tunnel: true,
  172. model.Mixed: true,
  173. model.WireGuard: true,
  174. model.AmneziaWG: true,
  175. model.HTTP: true,
  176. }
  177. selected := make(map[int]bool, len(receiver_inbound_IDs))
  178. for _, id := range receiver_inbound_IDs {
  179. selected[id] = true
  180. }
  181. var buttons []telego.InlineKeyboardButton
  182. for _, ib := range inbounds {
  183. if excludedProtocols[ib.Protocol] {
  184. continue
  185. }
  186. mark := "☐"
  187. if selected[ib.Id] {
  188. mark = "✅"
  189. }
  190. label := fmt.Sprintf("%s %s (%s)", mark, ib.Remark, ib.Protocol)
  191. callback := t.encodeQuery(fmt.Sprintf("add_client_toggle_attach %d", ib.Id))
  192. buttons = append(buttons, tu.InlineKeyboardButton(label).WithCallbackData(callback))
  193. }
  194. cols := 1
  195. if len(buttons) >= 6 {
  196. cols = 2
  197. }
  198. rows := tu.InlineKeyboardCols(cols, buttons...)
  199. rows = append(rows, tu.InlineKeyboardRow(
  200. tu.InlineKeyboardButton("✅ Done").WithCallbackData(t.encodeQuery("add_client_attach_done")),
  201. ))
  202. return tu.InlineKeyboardGrid(rows), nil
  203. }
  204. // getInboundClients creates an inline keyboard with clients of a specific inbound.
  205. func (t *Tgbot) getInboundClients(id int) (*telego.InlineKeyboardMarkup, error) {
  206. inbound, err := t.inboundService.GetInbound(id)
  207. if err != nil {
  208. logger.Warning("getIboundClients run failed:", err)
  209. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  210. }
  211. clients, err := t.inboundService.GetClients(inbound)
  212. var buttons []telego.InlineKeyboardButton
  213. if err != nil {
  214. logger.Warning("GetInboundClients run failed:", err)
  215. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  216. } else {
  217. if len(clients) > 0 {
  218. for _, client := range clients {
  219. buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery("client_get_usage "+client.Email)))
  220. }
  221. } else {
  222. return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed"))
  223. }
  224. }
  225. cols := 0
  226. if len(buttons) < 6 {
  227. cols = 3
  228. } else {
  229. cols = 2
  230. }
  231. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  232. return keyboard, nil
  233. }
  234. // searchInbound searches for inbounds by remark and sends the results.
  235. func (t *Tgbot) searchInbound(chatId int64, remark string) {
  236. inbounds, err := t.inboundService.SearchInbounds(remark)
  237. if err != nil {
  238. logger.Warning(err)
  239. msg := t.I18nBot("tgbot.wentWrong")
  240. t.SendMsgToTgbot(chatId, msg)
  241. return
  242. }
  243. if len(inbounds) == 0 {
  244. msg := t.I18nBot("tgbot.noInbounds")
  245. t.SendMsgToTgbot(chatId, msg)
  246. return
  247. }
  248. for _, inbound := range inbounds {
  249. info := ""
  250. info += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark)
  251. info += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port))
  252. info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down))
  253. if inbound.ExpiryTime == 0 {
  254. info += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited"))
  255. } else {
  256. info += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  257. }
  258. t.SendMsgToTgbot(chatId, info)
  259. if len(inbound.ClientStats) > 0 {
  260. var output strings.Builder
  261. for _, traffic := range inbound.ClientStats {
  262. output.WriteString(t.clientInfoMsg(&traffic, true, true, true, true, true, true))
  263. }
  264. t.SendMsgToTgbot(chatId, output.String())
  265. }
  266. }
  267. }