tgbot_inbound.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // addClientExcludedProtocols are the protocols with no per-client model: Tunnel
  120. // has no clients, Mixed/HTTP authenticate at the inbound level, not per-client.
  121. var addClientExcludedProtocols = map[model.Protocol]bool{
  122. model.Tunnel: true,
  123. model.Mixed: true,
  124. model.HTTP: true,
  125. }
  126. // getInboundsAddClient creates an inline keyboard for adding clients to inbounds.
  127. func (t *Tgbot) getInboundsAddClient() (*telego.InlineKeyboardMarkup, error) {
  128. inbounds, err := t.inboundService.GetAllInbounds()
  129. if err != nil {
  130. logger.Warning("GetAllInbounds run failed:", err)
  131. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  132. }
  133. if len(inbounds) == 0 {
  134. logger.Warning("No inbounds found")
  135. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  136. }
  137. var buttons []telego.InlineKeyboardButton
  138. for _, inbound := range inbounds {
  139. if addClientExcludedProtocols[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. if len(buttons) == 0 {
  150. logger.Warning("No inbounds eligible for add-client (all excluded by protocol)")
  151. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  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(draft *clientDraft) (*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. selected := make(map[int]bool, len(draft.receiverInboundIDs))
  175. for _, id := range draft.receiverInboundIDs {
  176. selected[id] = true
  177. }
  178. var buttons []telego.InlineKeyboardButton
  179. for _, ib := range inbounds {
  180. if addClientExcludedProtocols[ib.Protocol] {
  181. continue
  182. }
  183. mark := "☐"
  184. if selected[ib.Id] {
  185. mark = "✅"
  186. }
  187. label := fmt.Sprintf("%s %s (%s)", mark, ib.Remark, ib.Protocol)
  188. callback := t.encodeQuery(fmt.Sprintf("add_client_toggle_attach %d", ib.Id))
  189. buttons = append(buttons, tu.InlineKeyboardButton(label).WithCallbackData(callback))
  190. }
  191. cols := 1
  192. if len(buttons) >= 6 {
  193. cols = 2
  194. }
  195. rows := tu.InlineKeyboardCols(cols, buttons...)
  196. rows = append(rows, tu.InlineKeyboardRow(
  197. tu.InlineKeyboardButton("✅ Done").WithCallbackData(t.encodeQuery("add_client_attach_done")),
  198. ))
  199. return tu.InlineKeyboardGrid(rows), nil
  200. }
  201. // getInboundClients creates an inline keyboard with clients of a specific inbound.
  202. func (t *Tgbot) getInboundClients(id int) (*telego.InlineKeyboardMarkup, error) {
  203. inbound, err := t.inboundService.GetInbound(id)
  204. if err != nil {
  205. logger.Warning("getIboundClients run failed:", err)
  206. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  207. }
  208. clients, err := t.inboundService.GetClients(inbound)
  209. var buttons []telego.InlineKeyboardButton
  210. if err != nil {
  211. logger.Warning("GetInboundClients run failed:", err)
  212. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  213. } else {
  214. if len(clients) > 0 {
  215. for _, client := range clients {
  216. buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery("client_get_usage "+client.Email)))
  217. }
  218. } else {
  219. return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed"))
  220. }
  221. }
  222. cols := 0
  223. if len(buttons) < 6 {
  224. cols = 3
  225. } else {
  226. cols = 2
  227. }
  228. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  229. return keyboard, nil
  230. }
  231. // searchInbound searches for inbounds by remark and sends the results.
  232. func (t *Tgbot) searchInbound(chatId int64, remark string) {
  233. inbounds, err := t.inboundService.SearchInbounds(remark)
  234. if err != nil {
  235. logger.Warning(err)
  236. msg := t.I18nBot("tgbot.wentWrong")
  237. t.SendMsgToTgbot(chatId, msg)
  238. return
  239. }
  240. if len(inbounds) == 0 {
  241. msg := t.I18nBot("tgbot.noInbounds")
  242. t.SendMsgToTgbot(chatId, msg)
  243. return
  244. }
  245. for _, inbound := range inbounds {
  246. info := ""
  247. info += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark)
  248. info += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port))
  249. info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down))
  250. if inbound.ExpiryTime == 0 {
  251. info += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited"))
  252. } else {
  253. info += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  254. }
  255. t.SendMsgToTgbot(chatId, info)
  256. if len(inbound.ClientStats) > 0 {
  257. var output strings.Builder
  258. for _, traffic := range inbound.ClientStats {
  259. output.WriteString(t.clientInfoMsg(&traffic, true, true, true, true, true, true))
  260. }
  261. t.SendMsgToTgbot(chatId, output.String())
  262. }
  263. }
  264. }