1
0

client.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. package controller
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  8. "github.com/mhsanaei/3x-ui/v3/internal/web/websocket"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func notifyClientsChanged() {
  12. websocket.BroadcastInvalidate(websocket.MessageTypeClients)
  13. }
  14. func parseInboundIdsQuery(raw string) []int {
  15. raw = strings.TrimSpace(raw)
  16. if raw == "" {
  17. return nil
  18. }
  19. parts := strings.Split(raw, ",")
  20. ids := make([]int, 0, len(parts))
  21. for _, p := range parts {
  22. if id, err := strconv.Atoi(strings.TrimSpace(p)); err == nil {
  23. ids = append(ids, id)
  24. }
  25. }
  26. return ids
  27. }
  28. type ClientController struct {
  29. clientService service.ClientService
  30. inboundService service.InboundService
  31. xrayService service.XrayService
  32. settingService service.SettingService
  33. }
  34. func NewClientController(g *gin.RouterGroup) *ClientController {
  35. a := &ClientController{}
  36. a.initRouter(g)
  37. return a
  38. }
  39. func (a *ClientController) initRouter(g *gin.RouterGroup) {
  40. g.GET("/list", a.list)
  41. g.GET("/list/paged", a.listPaged)
  42. g.GET("/get/:email", a.get)
  43. g.GET("/get/tgId/:tgId", a.getByTgId)
  44. g.GET("/traffic/:email", a.getTrafficByEmail)
  45. g.GET("/subLinks/:subId", a.getSubLinks)
  46. g.GET("/links/:email", a.getClientLinks)
  47. g.POST("/add", a.create)
  48. g.POST("/update/:email", a.update)
  49. g.POST("/del/:email", a.delete)
  50. g.POST("/:email/attach", a.attach)
  51. g.POST("/:email/detach", a.detach)
  52. g.POST("/:email/externalLinks", a.setExternalLinks)
  53. g.GET("/export", a.export)
  54. g.POST("/import", a.importClients)
  55. g.POST("/delOrphans", a.delOrphans)
  56. g.POST("/resetAllTraffics", a.resetAllTraffics)
  57. g.POST("/delDepleted", a.delDepleted)
  58. g.POST("/bulkAdjust", a.bulkAdjust)
  59. g.POST("/bulkEnable", a.bulkEnable)
  60. g.POST("/bulkDisable", a.bulkDisable)
  61. g.POST("/bulkDel", a.bulkDelete)
  62. g.POST("/bulkCreate", a.bulkCreate)
  63. g.POST("/bulkAttach", a.bulkAttach)
  64. g.POST("/bulkDetach", a.bulkDetach)
  65. g.POST("/bulkResetTraffic", a.bulkResetTraffic)
  66. g.POST("/resetTraffic/:email", a.resetTrafficByEmail)
  67. g.POST("/updateTraffic/:email", a.updateTrafficByEmail)
  68. g.POST("/ips/:email", a.getIps)
  69. g.POST("/clearIps/:email", a.clearIps)
  70. g.POST("/hwids/:email", a.getHwids)
  71. g.DELETE("/hwids/:email", a.clearHwids)
  72. g.DELETE("/hwids/:email/:id", a.deleteHwid)
  73. g.POST("/onlines", a.onlines)
  74. g.POST("/onlinesByGuid", a.onlinesByGuid)
  75. g.POST("/clientIpsByGuid", a.clientIpsByGuid)
  76. g.POST("/activeInbounds", a.activeInbounds)
  77. g.POST("/lastOnline", a.lastOnline)
  78. }
  79. func (a *ClientController) list(c *gin.Context) {
  80. rows, err := a.clientService.List()
  81. if err != nil {
  82. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  83. return
  84. }
  85. jsonObj(c, rows, nil)
  86. }
  87. func (a *ClientController) listPaged(c *gin.Context) {
  88. var params service.ClientPageParams
  89. if err := c.ShouldBindQuery(&params); err != nil {
  90. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  91. return
  92. }
  93. resp, err := a.clientService.ListPaged(&a.inboundService, &a.settingService, params)
  94. if err != nil {
  95. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  96. return
  97. }
  98. jsonObj(c, resp, nil)
  99. }
  100. func (a *ClientController) buildClientPayload(rec *model.ClientRecord) (gin.H, error) {
  101. inboundIds, err := a.clientService.GetInboundIdsForRecord(rec.Id)
  102. if err != nil {
  103. return nil, err
  104. }
  105. externalLinks, err := a.clientService.GetExternalLinksForRecord(rec.Id)
  106. if err != nil {
  107. return nil, err
  108. }
  109. flow, err := a.clientService.EffectiveFlow(nil, rec.Id)
  110. if err != nil {
  111. return nil, err
  112. }
  113. rec.Flow = flow
  114. var usedTraffic int64
  115. if t, tErr := a.inboundService.GetClientTrafficByEmail(rec.Email); tErr == nil && t != nil {
  116. usedTraffic = t.Up + t.Down
  117. }
  118. return gin.H{
  119. "client": rec,
  120. "inboundIds": inboundIds,
  121. "externalLinks": externalLinks,
  122. "usedTraffic": usedTraffic,
  123. }, nil
  124. }
  125. func (a *ClientController) get(c *gin.Context) {
  126. email := c.Param("email")
  127. rec, err := a.clientService.GetRecordByEmail(nil, email)
  128. if err != nil {
  129. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  130. return
  131. }
  132. payload, err := a.buildClientPayload(rec)
  133. if err != nil {
  134. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  135. return
  136. }
  137. jsonObj(c, payload, nil)
  138. }
  139. func (a *ClientController) getByTgId(c *gin.Context) {
  140. tgIdStr := c.Param("tgId")
  141. tgId, err := strconv.ParseInt(tgIdStr, 10, 64)
  142. if err != nil {
  143. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  144. return
  145. }
  146. records, err := a.clientService.GetRecordsByTgID(tgId)
  147. if err != nil {
  148. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  149. return
  150. }
  151. results := make([]gin.H, 0, len(records))
  152. for _, rec := range records {
  153. payload, err := a.buildClientPayload(rec)
  154. if err != nil {
  155. jsonMsg(c, I18nWeb(c, "get"), err)
  156. return
  157. }
  158. results = append(results, payload)
  159. }
  160. jsonObj(c, results, nil)
  161. }
  162. func (a *ClientController) create(c *gin.Context) {
  163. var payload service.ClientCreatePayload
  164. if err := c.ShouldBindJSON(&payload); err != nil {
  165. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  166. return
  167. }
  168. needRestart, err := a.clientService.Create(&a.inboundService, &payload)
  169. if err != nil {
  170. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  171. return
  172. }
  173. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), pendingNodeObj(a.inboundService.AnyNodePending(payload.InboundIds)), nil)
  174. if needRestart {
  175. a.xrayService.SetToNeedRestart()
  176. }
  177. notifyClientsChanged()
  178. }
  179. func (a *ClientController) update(c *gin.Context) {
  180. email := c.Param("email")
  181. var req struct {
  182. model.Client
  183. LimitHwid int `json:"limitHwid"`
  184. }
  185. if err := c.ShouldBindJSON(&req); err != nil {
  186. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  187. return
  188. }
  189. inboundFilter := parseInboundIdsQuery(c.Query("inboundIds"))
  190. needRestart, err := a.clientService.UpdateByEmail(&a.inboundService, email, req.Client, req.LimitHwid, inboundFilter...)
  191. if err != nil {
  192. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  193. return
  194. }
  195. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), pendingNodeObj(a.clientService.HasPendingNode(&a.inboundService, email)), nil)
  196. if needRestart {
  197. a.xrayService.SetToNeedRestart()
  198. }
  199. notifyClientsChanged()
  200. }
  201. func (a *ClientController) delete(c *gin.Context) {
  202. email := c.Param("email")
  203. keepTraffic := c.Query("keepTraffic") == "1"
  204. needRestart, err := a.clientService.DeleteByEmail(&a.inboundService, email, keepTraffic)
  205. if err != nil {
  206. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  207. return
  208. }
  209. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), nil)
  210. if needRestart {
  211. a.xrayService.SetToNeedRestart()
  212. }
  213. notifyClientsChanged()
  214. }
  215. type attachDetachBody struct {
  216. InboundIds []int `json:"inboundIds"`
  217. }
  218. type externalLinksBody struct {
  219. ExternalLinks []service.ExternalLinkInput `json:"externalLinks"`
  220. }
  221. func (a *ClientController) attach(c *gin.Context) {
  222. email := c.Param("email")
  223. var body attachDetachBody
  224. if err := c.ShouldBindJSON(&body); err != nil {
  225. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  226. return
  227. }
  228. needRestart, err := a.clientService.AttachByEmail(&a.inboundService, email, body.InboundIds)
  229. if err != nil {
  230. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  231. return
  232. }
  233. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), pendingNodeObj(a.inboundService.AnyNodePending(body.InboundIds)), nil)
  234. if needRestart {
  235. a.xrayService.SetToNeedRestart()
  236. }
  237. notifyClientsChanged()
  238. }
  239. func (a *ClientController) setExternalLinks(c *gin.Context) {
  240. email := c.Param("email")
  241. var body externalLinksBody
  242. if err := c.ShouldBindJSON(&body); err != nil {
  243. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  244. return
  245. }
  246. if err := a.clientService.SetExternalLinksByEmail(email, body.ExternalLinks); err != nil {
  247. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  248. return
  249. }
  250. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  251. notifyClientsChanged()
  252. }
  253. func (a *ClientController) resetAllTraffics(c *gin.Context) {
  254. needRestart, err := a.clientService.ResetAllTraffics()
  255. if err != nil {
  256. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  257. return
  258. }
  259. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllClientTrafficSuccess"), nil)
  260. if needRestart {
  261. a.xrayService.SetToNeedRestart()
  262. }
  263. notifyClientsChanged()
  264. }
  265. type bulkAdjustRequest struct {
  266. Emails []string `json:"emails"`
  267. AddDays int `json:"addDays"`
  268. AddBytes int64 `json:"addBytes"`
  269. Flow string `json:"flow"`
  270. }
  271. func (a *ClientController) bulkAdjust(c *gin.Context) {
  272. var req bulkAdjustRequest
  273. if err := c.ShouldBindJSON(&req); err != nil {
  274. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  275. return
  276. }
  277. result, needRestart, err := a.clientService.BulkAdjust(&a.inboundService, req.Emails, req.AddDays, req.AddBytes, req.Flow)
  278. if err != nil {
  279. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  280. return
  281. }
  282. jsonObj(c, result, nil)
  283. if needRestart {
  284. a.xrayService.SetToNeedRestart()
  285. }
  286. notifyClientsChanged()
  287. }
  288. type bulkDeleteRequest struct {
  289. Emails []string `json:"emails"`
  290. KeepTraffic bool `json:"keepTraffic"`
  291. }
  292. type bulkAttachRequest struct {
  293. Emails []string `json:"emails"`
  294. InboundIds []int `json:"inboundIds"`
  295. }
  296. func (a *ClientController) bulkAttach(c *gin.Context) {
  297. var req bulkAttachRequest
  298. if err := c.ShouldBindJSON(&req); err != nil {
  299. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  300. return
  301. }
  302. result, needRestart, err := a.clientService.BulkAttach(&a.inboundService, req.Emails, req.InboundIds)
  303. if err != nil {
  304. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  305. return
  306. }
  307. jsonObj(c, result, nil)
  308. if needRestart {
  309. a.xrayService.SetToNeedRestart()
  310. }
  311. notifyClientsChanged()
  312. }
  313. type bulkDetachRequest struct {
  314. Emails []string `json:"emails"`
  315. InboundIds []int `json:"inboundIds"`
  316. }
  317. func (a *ClientController) bulkDetach(c *gin.Context) {
  318. var req bulkDetachRequest
  319. if err := c.ShouldBindJSON(&req); err != nil {
  320. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  321. return
  322. }
  323. result, needRestart, err := a.clientService.BulkDetach(&a.inboundService, req.Emails, req.InboundIds)
  324. if err != nil {
  325. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  326. return
  327. }
  328. jsonObj(c, result, nil)
  329. if needRestart {
  330. a.xrayService.SetToNeedRestart()
  331. }
  332. notifyClientsChanged()
  333. }
  334. func (a *ClientController) bulkDelete(c *gin.Context) {
  335. var req bulkDeleteRequest
  336. if err := c.ShouldBindJSON(&req); err != nil {
  337. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  338. return
  339. }
  340. result, needRestart, err := a.clientService.BulkDelete(&a.inboundService, req.Emails, req.KeepTraffic)
  341. if err != nil {
  342. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  343. return
  344. }
  345. jsonObj(c, result, nil)
  346. if needRestart {
  347. a.xrayService.SetToNeedRestart()
  348. }
  349. notifyClientsChanged()
  350. }
  351. type bulkEnableRequest struct {
  352. Emails []string `json:"emails"`
  353. }
  354. func (a *ClientController) bulkEnable(c *gin.Context) {
  355. a.bulkSetEnable(c, true)
  356. }
  357. func (a *ClientController) bulkDisable(c *gin.Context) {
  358. a.bulkSetEnable(c, false)
  359. }
  360. func (a *ClientController) bulkSetEnable(c *gin.Context, enable bool) {
  361. var req bulkEnableRequest
  362. if err := c.ShouldBindJSON(&req); err != nil {
  363. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  364. return
  365. }
  366. result, needRestart, err := a.clientService.BulkSetEnable(&a.inboundService, req.Emails, enable)
  367. if err != nil {
  368. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  369. return
  370. }
  371. jsonObj(c, result, nil)
  372. if needRestart {
  373. a.xrayService.SetToNeedRestart()
  374. }
  375. notifyClientsChanged()
  376. }
  377. func (a *ClientController) bulkCreate(c *gin.Context) {
  378. var payloads []service.ClientCreatePayload
  379. if err := c.ShouldBindJSON(&payloads); err != nil {
  380. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  381. return
  382. }
  383. result, needRestart, err := a.clientService.BulkCreate(&a.inboundService, payloads)
  384. if err != nil {
  385. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  386. return
  387. }
  388. jsonObj(c, result, nil)
  389. if needRestart {
  390. a.xrayService.SetToNeedRestart()
  391. }
  392. notifyClientsChanged()
  393. }
  394. func (a *ClientController) delDepleted(c *gin.Context) {
  395. deleted, needRestart, err := a.clientService.DelDepleted(&a.inboundService)
  396. if err != nil {
  397. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  398. return
  399. }
  400. jsonObj(c, gin.H{"deleted": deleted}, nil)
  401. if needRestart {
  402. a.xrayService.SetToNeedRestart()
  403. }
  404. notifyClientsChanged()
  405. }
  406. // export returns every client as a {client, inboundIds} list in the standard
  407. // envelope. The frontend renders it in a read-only CodeMirror viewer (Copy /
  408. // Download), so this hands back data rather than streaming a file attachment.
  409. func (a *ClientController) export(c *gin.Context) {
  410. items, err := a.clientService.ExportAll()
  411. if err != nil {
  412. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  413. return
  414. }
  415. jsonObj(c, items, nil)
  416. }
  417. type importClientsRequest struct {
  418. Data string `json:"data"`
  419. }
  420. // importClients accepts the pasted export text as a JSON body { "data": "..." },
  421. // mirroring the inbound import flow. The data string is itself a JSON-encoded
  422. // []ClientCreatePayload, so it is unmarshalled in a second step.
  423. func (a *ClientController) importClients(c *gin.Context) {
  424. var req importClientsRequest
  425. if err := c.ShouldBindJSON(&req); err != nil {
  426. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  427. return
  428. }
  429. var items []service.ClientCreatePayload
  430. if err := json.Unmarshal([]byte(req.Data), &items); err != nil {
  431. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  432. return
  433. }
  434. result, needRestart, err := a.clientService.ImportClients(&a.inboundService, items)
  435. if err != nil {
  436. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  437. return
  438. }
  439. jsonObj(c, result, nil)
  440. if needRestart {
  441. a.xrayService.SetToNeedRestart()
  442. }
  443. notifyClientsChanged()
  444. }
  445. func (a *ClientController) delOrphans(c *gin.Context) {
  446. deleted, err := a.clientService.DeleteOrphans()
  447. if err != nil {
  448. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  449. return
  450. }
  451. jsonObj(c, gin.H{"deleted": deleted}, nil)
  452. notifyClientsChanged()
  453. }
  454. func (a *ClientController) resetTrafficByEmail(c *gin.Context) {
  455. email := c.Param("email")
  456. needRestart, err := a.clientService.ResetTrafficByEmail(&a.inboundService, email)
  457. if err != nil {
  458. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  459. return
  460. }
  461. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundClientTrafficSuccess"), nil)
  462. if needRestart {
  463. a.xrayService.SetToNeedRestart()
  464. }
  465. notifyClientsChanged()
  466. }
  467. type trafficUpdateRequest struct {
  468. Upload int64 `json:"upload"`
  469. Download int64 `json:"download"`
  470. }
  471. func (a *ClientController) updateTrafficByEmail(c *gin.Context) {
  472. email := c.Param("email")
  473. var req trafficUpdateRequest
  474. if err := c.ShouldBindJSON(&req); err != nil {
  475. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  476. return
  477. }
  478. if err := a.inboundService.UpdateClientTrafficByEmail(email, req.Upload, req.Download); err != nil {
  479. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  480. return
  481. }
  482. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  483. notifyClientsChanged()
  484. }
  485. func (a *ClientController) getIps(c *gin.Context) {
  486. email := c.Param("email")
  487. infos, err := a.inboundService.GetClientIpsWithNodes(email)
  488. jsonObj(c, infos, err)
  489. }
  490. func (a *ClientController) clientIpsByGuid(c *gin.Context) {
  491. data, err := a.inboundService.GetClientIpsByGuid()
  492. jsonObj(c, data, err)
  493. }
  494. func (a *ClientController) clearIps(c *gin.Context) {
  495. email := c.Param("email")
  496. if err := a.inboundService.ClearClientIps(email); err != nil {
  497. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  498. return
  499. }
  500. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  501. }
  502. func (a *ClientController) getHwids(c *gin.Context) {
  503. infos, err := a.clientService.ListClientHwids(c.Param("email"))
  504. jsonObj(c, infos, err)
  505. }
  506. func (a *ClientController) clearHwids(c *gin.Context) {
  507. if err := a.clientService.ClearClientHwids(c.Param("email")); err != nil {
  508. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  509. return
  510. }
  511. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  512. }
  513. func (a *ClientController) deleteHwid(c *gin.Context) {
  514. id, err := strconv.Atoi(c.Param("id"))
  515. if err != nil {
  516. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  517. return
  518. }
  519. if err := a.clientService.DeleteClientHwid(c.Param("email"), id); err != nil {
  520. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  521. return
  522. }
  523. jsonMsg(c, I18nWeb(c, "pages.clients.hwidDeleted"), nil)
  524. }
  525. func (a *ClientController) onlines(c *gin.Context) {
  526. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  527. }
  528. func (a *ClientController) onlinesByGuid(c *gin.Context) {
  529. jsonObj(c, a.inboundService.GetOnlineClientsByGuid(), nil)
  530. }
  531. func (a *ClientController) activeInbounds(c *gin.Context) {
  532. jsonObj(c, a.inboundService.GetActiveInboundsByGuid(), nil)
  533. }
  534. func (a *ClientController) lastOnline(c *gin.Context) {
  535. data, err := a.inboundService.GetClientsLastOnline()
  536. jsonObj(c, data, err)
  537. }
  538. func (a *ClientController) getTrafficByEmail(c *gin.Context) {
  539. email := c.Param("email")
  540. traffic, err := a.inboundService.GetClientTrafficByEmail(email)
  541. if err != nil {
  542. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  543. return
  544. }
  545. jsonObj(c, traffic, nil)
  546. }
  547. func (a *ClientController) getSubLinks(c *gin.Context) {
  548. links, err := a.inboundService.GetSubLinks(resolveHost(c), c.Param("subId"))
  549. if err != nil {
  550. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  551. return
  552. }
  553. jsonObj(c, links, nil)
  554. }
  555. func (a *ClientController) getClientLinks(c *gin.Context) {
  556. links, err := a.inboundService.GetAllClientLinks(resolveHost(c), c.Param("email"))
  557. if err != nil {
  558. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  559. return
  560. }
  561. jsonObj(c, links, nil)
  562. }
  563. func (a *ClientController) detach(c *gin.Context) {
  564. email := c.Param("email")
  565. var body attachDetachBody
  566. if err := c.ShouldBindJSON(&body); err != nil {
  567. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  568. return
  569. }
  570. needRestart, err := a.clientService.DetachByEmailMany(&a.inboundService, email, body.InboundIds)
  571. if err != nil {
  572. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  573. return
  574. }
  575. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), pendingNodeObj(a.inboundService.AnyNodePending(body.InboundIds)), nil)
  576. if needRestart {
  577. a.xrayService.SetToNeedRestart()
  578. }
  579. notifyClientsChanged()
  580. }
  581. type bulkResetRequest struct {
  582. Emails []string `json:"emails"`
  583. }
  584. func (a *ClientController) bulkResetTraffic(c *gin.Context) {
  585. var req bulkResetRequest
  586. if err := c.ShouldBindJSON(&req); err != nil {
  587. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  588. return
  589. }
  590. affected, err := a.clientService.BulkResetTraffic(&a.inboundService, req.Emails)
  591. if err != nil {
  592. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  593. return
  594. }
  595. jsonObj(c, gin.H{"affected": affected}, nil)
  596. a.xrayService.SetToNeedRestart()
  597. notifyClientsChanged()
  598. }