client.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. tunnelAllowedIPs, err := a.clientService.TunnelAllowedIPsByInbound(&a.inboundService, rec.Email, inboundIds)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return gin.H{
  123. "client": rec,
  124. "inboundIds": inboundIds,
  125. "externalLinks": externalLinks,
  126. "usedTraffic": usedTraffic,
  127. "tunnelAllowedIPs": tunnelAllowedIPs,
  128. }, nil
  129. }
  130. func (a *ClientController) get(c *gin.Context) {
  131. email := c.Param("email")
  132. rec, err := a.clientService.GetRecordByEmail(nil, email)
  133. if err != nil {
  134. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  135. return
  136. }
  137. payload, err := a.buildClientPayload(rec)
  138. if err != nil {
  139. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  140. return
  141. }
  142. jsonObj(c, payload, nil)
  143. }
  144. func (a *ClientController) getByTgId(c *gin.Context) {
  145. tgIdStr := c.Param("tgId")
  146. tgId, err := strconv.ParseInt(tgIdStr, 10, 64)
  147. if err != nil {
  148. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  149. return
  150. }
  151. records, err := a.clientService.GetRecordsByTgID(tgId)
  152. if err != nil {
  153. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  154. return
  155. }
  156. results := make([]gin.H, 0, len(records))
  157. for _, rec := range records {
  158. payload, err := a.buildClientPayload(rec)
  159. if err != nil {
  160. jsonMsg(c, I18nWeb(c, "get"), err)
  161. return
  162. }
  163. results = append(results, payload)
  164. }
  165. jsonObj(c, results, nil)
  166. }
  167. func (a *ClientController) create(c *gin.Context) {
  168. var payload service.ClientCreatePayload
  169. if err := c.ShouldBindJSON(&payload); err != nil {
  170. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  171. return
  172. }
  173. needRestart, err := a.clientService.Create(&a.inboundService, &payload)
  174. // Flagged before the error check: a partly-applied create leaves clients
  175. // committed on the inbounds that succeeded, and those still need the restart.
  176. if needRestart {
  177. a.xrayService.SetToNeedRestart()
  178. }
  179. // A partly-applied call committed real clients; a rejected one touched
  180. // nothing, and broadcasting those would refetch every panel for nothing.
  181. if needRestart || err == nil {
  182. notifyClientsChanged()
  183. }
  184. if err != nil {
  185. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  186. return
  187. }
  188. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), pendingNodeObj(a.inboundService.AnyNodePending(payload.InboundIds)), nil)
  189. }
  190. func (a *ClientController) update(c *gin.Context) {
  191. email := c.Param("email")
  192. var req struct {
  193. model.Client
  194. LimitHwid int `json:"limitHwid"`
  195. }
  196. if err := c.ShouldBindJSON(&req); err != nil {
  197. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  198. return
  199. }
  200. inboundFilter := parseInboundIdsQuery(c.Query("inboundIds"))
  201. needRestart, err := a.clientService.UpdateByEmail(&a.inboundService, email, req.Client, req.LimitHwid, inboundFilter...)
  202. // Flagged before the error check: a partly-applied edit leaves the change
  203. // committed on the inbounds that succeeded, and those still need the restart.
  204. if needRestart {
  205. a.xrayService.SetToNeedRestart()
  206. }
  207. // A partly-applied call committed real changes; a rejected one touched
  208. // nothing, and broadcasting those would refetch every panel for nothing.
  209. if needRestart || err == nil {
  210. notifyClientsChanged()
  211. }
  212. if err != nil {
  213. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  214. return
  215. }
  216. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), pendingNodeObj(a.clientService.HasPendingNode(&a.inboundService, email)), nil)
  217. }
  218. func (a *ClientController) delete(c *gin.Context) {
  219. email := c.Param("email")
  220. keepTraffic := c.Query("keepTraffic") == "1"
  221. needRestart, err := a.clientService.DeleteByEmail(&a.inboundService, email, keepTraffic)
  222. // Flagged before the error check: a partly-applied delete already removed
  223. // the client from the inbounds that succeeded, and those need the restart.
  224. if needRestart {
  225. a.xrayService.SetToNeedRestart()
  226. }
  227. // A partly-applied call committed real removals; a rejected one touched
  228. // nothing, and broadcasting those would refetch every panel for nothing.
  229. if needRestart || err == nil {
  230. notifyClientsChanged()
  231. }
  232. if err != nil {
  233. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  234. return
  235. }
  236. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), nil)
  237. }
  238. type attachDetachBody struct {
  239. InboundIds []int `json:"inboundIds"`
  240. }
  241. type externalLinksBody struct {
  242. ExternalLinks []service.ExternalLinkInput `json:"externalLinks"`
  243. }
  244. func (a *ClientController) attach(c *gin.Context) {
  245. email := c.Param("email")
  246. var body attachDetachBody
  247. if err := c.ShouldBindJSON(&body); err != nil {
  248. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  249. return
  250. }
  251. needRestart, err := a.clientService.AttachByEmail(&a.inboundService, email, body.InboundIds)
  252. if needRestart {
  253. a.xrayService.SetToNeedRestart()
  254. }
  255. // A partly-applied call committed real clients; a rejected one touched
  256. // nothing, and broadcasting those would refetch every panel for nothing.
  257. if needRestart || err == nil {
  258. notifyClientsChanged()
  259. }
  260. if err != nil {
  261. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  262. return
  263. }
  264. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), pendingNodeObj(a.inboundService.AnyNodePending(body.InboundIds)), nil)
  265. }
  266. func (a *ClientController) setExternalLinks(c *gin.Context) {
  267. email := c.Param("email")
  268. var body externalLinksBody
  269. if err := c.ShouldBindJSON(&body); err != nil {
  270. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  271. return
  272. }
  273. if err := a.clientService.SetExternalLinksByEmail(email, body.ExternalLinks); err != nil {
  274. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  275. return
  276. }
  277. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  278. notifyClientsChanged()
  279. }
  280. func (a *ClientController) resetAllTraffics(c *gin.Context) {
  281. needRestart, err := a.clientService.ResetAllTraffics()
  282. if err != nil {
  283. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  284. return
  285. }
  286. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllClientTrafficSuccess"), nil)
  287. if needRestart {
  288. a.xrayService.SetToNeedRestart()
  289. }
  290. notifyClientsChanged()
  291. }
  292. type bulkAdjustRequest struct {
  293. Emails []string `json:"emails"`
  294. AddDays int `json:"addDays"`
  295. AddBytes int64 `json:"addBytes"`
  296. Flow string `json:"flow"`
  297. LimitHwid *int `json:"limitHwid"`
  298. AdTag string `json:"adTag"`
  299. }
  300. func (a *ClientController) bulkAdjust(c *gin.Context) {
  301. var req bulkAdjustRequest
  302. if err := c.ShouldBindJSON(&req); err != nil {
  303. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  304. return
  305. }
  306. result, needRestart, err := a.clientService.BulkAdjust(&a.inboundService, req.Emails, req.AddDays, req.AddBytes, req.Flow, req.LimitHwid, req.AdTag)
  307. if err != nil {
  308. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  309. return
  310. }
  311. jsonObj(c, result, nil)
  312. if needRestart {
  313. a.xrayService.SetToNeedRestart()
  314. }
  315. notifyClientsChanged()
  316. }
  317. type bulkDeleteRequest struct {
  318. Emails []string `json:"emails"`
  319. KeepTraffic bool `json:"keepTraffic"`
  320. }
  321. type bulkAttachRequest struct {
  322. Emails []string `json:"emails"`
  323. InboundIds []int `json:"inboundIds"`
  324. }
  325. func (a *ClientController) bulkAttach(c *gin.Context) {
  326. var req bulkAttachRequest
  327. if err := c.ShouldBindJSON(&req); err != nil {
  328. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  329. return
  330. }
  331. result, needRestart, err := a.clientService.BulkAttach(&a.inboundService, req.Emails, req.InboundIds)
  332. if err != nil {
  333. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  334. return
  335. }
  336. jsonObj(c, result, nil)
  337. if needRestart {
  338. a.xrayService.SetToNeedRestart()
  339. }
  340. notifyClientsChanged()
  341. }
  342. type bulkDetachRequest struct {
  343. Emails []string `json:"emails"`
  344. InboundIds []int `json:"inboundIds"`
  345. }
  346. func (a *ClientController) bulkDetach(c *gin.Context) {
  347. var req bulkDetachRequest
  348. if err := c.ShouldBindJSON(&req); err != nil {
  349. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  350. return
  351. }
  352. result, needRestart, err := a.clientService.BulkDetach(&a.inboundService, req.Emails, req.InboundIds)
  353. if err != nil {
  354. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  355. return
  356. }
  357. jsonObj(c, result, nil)
  358. if needRestart {
  359. a.xrayService.SetToNeedRestart()
  360. }
  361. notifyClientsChanged()
  362. }
  363. func (a *ClientController) bulkDelete(c *gin.Context) {
  364. var req bulkDeleteRequest
  365. if err := c.ShouldBindJSON(&req); err != nil {
  366. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  367. return
  368. }
  369. result, needRestart, err := a.clientService.BulkDelete(&a.inboundService, req.Emails, req.KeepTraffic)
  370. if err != nil {
  371. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  372. return
  373. }
  374. jsonObj(c, result, nil)
  375. if needRestart {
  376. a.xrayService.SetToNeedRestart()
  377. }
  378. notifyClientsChanged()
  379. }
  380. type bulkEnableRequest struct {
  381. Emails []string `json:"emails"`
  382. }
  383. func (a *ClientController) bulkEnable(c *gin.Context) {
  384. a.bulkSetEnable(c, true)
  385. }
  386. func (a *ClientController) bulkDisable(c *gin.Context) {
  387. a.bulkSetEnable(c, false)
  388. }
  389. func (a *ClientController) bulkSetEnable(c *gin.Context, enable bool) {
  390. var req bulkEnableRequest
  391. if err := c.ShouldBindJSON(&req); err != nil {
  392. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  393. return
  394. }
  395. result, needRestart, err := a.clientService.BulkSetEnable(&a.inboundService, req.Emails, enable)
  396. if err != nil {
  397. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  398. return
  399. }
  400. jsonObj(c, result, nil)
  401. if needRestart {
  402. a.xrayService.SetToNeedRestart()
  403. }
  404. notifyClientsChanged()
  405. }
  406. func (a *ClientController) bulkCreate(c *gin.Context) {
  407. var payloads []service.ClientCreatePayload
  408. if err := c.ShouldBindJSON(&payloads); err != nil {
  409. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  410. return
  411. }
  412. result, needRestart, err := a.clientService.BulkCreate(&a.inboundService, payloads)
  413. if err != nil {
  414. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  415. return
  416. }
  417. jsonObj(c, result, nil)
  418. if needRestart {
  419. a.xrayService.SetToNeedRestart()
  420. }
  421. notifyClientsChanged()
  422. }
  423. func (a *ClientController) delDepleted(c *gin.Context) {
  424. deleted, needRestart, err := a.clientService.DelDepleted(&a.inboundService)
  425. if err != nil {
  426. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  427. return
  428. }
  429. jsonObj(c, gin.H{"deleted": deleted}, nil)
  430. if needRestart {
  431. a.xrayService.SetToNeedRestart()
  432. }
  433. notifyClientsChanged()
  434. }
  435. // export returns every client as a {client, inboundIds} list in the standard
  436. // envelope. The frontend renders it in a read-only CodeMirror viewer (Copy /
  437. // Download), so this hands back data rather than streaming a file attachment.
  438. func (a *ClientController) export(c *gin.Context) {
  439. items, err := a.clientService.ExportAll()
  440. if err != nil {
  441. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  442. return
  443. }
  444. jsonObj(c, items, nil)
  445. }
  446. type importClientsRequest struct {
  447. Data string `json:"data"`
  448. }
  449. // importClients accepts the pasted export text as a JSON body { "data": "..." },
  450. // mirroring the inbound import flow. The data string is itself a JSON-encoded
  451. // []ClientCreatePayload, so it is unmarshalled in a second step.
  452. func (a *ClientController) importClients(c *gin.Context) {
  453. var req importClientsRequest
  454. if err := c.ShouldBindJSON(&req); err != nil {
  455. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  456. return
  457. }
  458. var items []service.ClientCreatePayload
  459. if err := json.Unmarshal([]byte(req.Data), &items); err != nil {
  460. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  461. return
  462. }
  463. result, needRestart, err := a.clientService.ImportClients(&a.inboundService, items)
  464. if err != nil {
  465. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  466. return
  467. }
  468. jsonObj(c, result, nil)
  469. if needRestart {
  470. a.xrayService.SetToNeedRestart()
  471. }
  472. notifyClientsChanged()
  473. }
  474. func (a *ClientController) delOrphans(c *gin.Context) {
  475. deleted, err := a.clientService.DeleteOrphans()
  476. if err != nil {
  477. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  478. return
  479. }
  480. jsonObj(c, gin.H{"deleted": deleted}, nil)
  481. notifyClientsChanged()
  482. }
  483. func (a *ClientController) resetTrafficByEmail(c *gin.Context) {
  484. email := c.Param("email")
  485. needRestart, err := a.clientService.ResetTrafficByEmail(&a.inboundService, email)
  486. if err != nil {
  487. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  488. return
  489. }
  490. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundClientTrafficSuccess"), nil)
  491. if needRestart {
  492. a.xrayService.SetToNeedRestart()
  493. }
  494. notifyClientsChanged()
  495. }
  496. type trafficUpdateRequest struct {
  497. Upload int64 `json:"upload"`
  498. Download int64 `json:"download"`
  499. }
  500. func (a *ClientController) updateTrafficByEmail(c *gin.Context) {
  501. email := c.Param("email")
  502. var req trafficUpdateRequest
  503. if err := c.ShouldBindJSON(&req); err != nil {
  504. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  505. return
  506. }
  507. if err := a.inboundService.UpdateClientTrafficByEmail(email, req.Upload, req.Download); err != nil {
  508. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  509. return
  510. }
  511. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  512. notifyClientsChanged()
  513. }
  514. func (a *ClientController) getIps(c *gin.Context) {
  515. email := c.Param("email")
  516. infos, err := a.inboundService.GetClientIpsWithNodes(email)
  517. jsonObj(c, infos, err)
  518. }
  519. func (a *ClientController) clientIpsByGuid(c *gin.Context) {
  520. data, err := a.inboundService.GetClientIpsByGuid()
  521. jsonObj(c, data, err)
  522. }
  523. func (a *ClientController) clearIps(c *gin.Context) {
  524. email := c.Param("email")
  525. if err := a.inboundService.ClearClientIps(email); err != nil {
  526. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  527. return
  528. }
  529. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  530. }
  531. func (a *ClientController) getHwids(c *gin.Context) {
  532. infos, err := a.clientService.ListClientHwids(c.Param("email"))
  533. jsonObj(c, infos, err)
  534. }
  535. func (a *ClientController) clearHwids(c *gin.Context) {
  536. if err := a.clientService.ClearClientHwids(c.Param("email")); err != nil {
  537. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  538. return
  539. }
  540. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  541. }
  542. func (a *ClientController) deleteHwid(c *gin.Context) {
  543. id, err := strconv.Atoi(c.Param("id"))
  544. if err != nil {
  545. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  546. return
  547. }
  548. if err := a.clientService.DeleteClientHwid(c.Param("email"), id); err != nil {
  549. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  550. return
  551. }
  552. jsonMsg(c, I18nWeb(c, "pages.clients.hwidDeleted"), nil)
  553. }
  554. func (a *ClientController) onlines(c *gin.Context) {
  555. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  556. }
  557. func (a *ClientController) onlinesByGuid(c *gin.Context) {
  558. jsonObj(c, a.inboundService.GetOnlineClientsByGuid(), nil)
  559. }
  560. func (a *ClientController) activeInbounds(c *gin.Context) {
  561. jsonObj(c, a.inboundService.GetActiveInboundsByGuid(), nil)
  562. }
  563. func (a *ClientController) lastOnline(c *gin.Context) {
  564. data, err := a.inboundService.GetClientsLastOnline()
  565. jsonObj(c, data, err)
  566. }
  567. func (a *ClientController) getTrafficByEmail(c *gin.Context) {
  568. email := c.Param("email")
  569. traffic, err := a.inboundService.GetClientTrafficByEmail(email)
  570. if err != nil {
  571. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  572. return
  573. }
  574. jsonObj(c, traffic, nil)
  575. }
  576. func (a *ClientController) getSubLinks(c *gin.Context) {
  577. links, err := a.inboundService.GetSubLinks(resolveHost(c), c.Param("subId"))
  578. if err != nil {
  579. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  580. return
  581. }
  582. jsonObj(c, links, nil)
  583. }
  584. func (a *ClientController) getClientLinks(c *gin.Context) {
  585. links, err := a.inboundService.GetAllClientLinks(resolveHost(c), c.Param("email"))
  586. if err != nil {
  587. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  588. return
  589. }
  590. jsonObj(c, links, nil)
  591. }
  592. func (a *ClientController) detach(c *gin.Context) {
  593. email := c.Param("email")
  594. var body attachDetachBody
  595. if err := c.ShouldBindJSON(&body); err != nil {
  596. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  597. return
  598. }
  599. needRestart, err := a.clientService.DetachByEmailMany(&a.inboundService, email, body.InboundIds)
  600. // Flagged before the error check: a partly-applied detach already removed
  601. // the client from the inbounds that succeeded, and those need the restart.
  602. if needRestart {
  603. a.xrayService.SetToNeedRestart()
  604. }
  605. // A partly-applied call committed real removals; a rejected one touched
  606. // nothing, and broadcasting those would refetch every panel for nothing.
  607. if needRestart || err == nil {
  608. notifyClientsChanged()
  609. }
  610. if err != nil {
  611. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  612. return
  613. }
  614. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), pendingNodeObj(a.inboundService.AnyNodePending(body.InboundIds)), nil)
  615. }
  616. type bulkResetRequest struct {
  617. Emails []string `json:"emails"`
  618. }
  619. func (a *ClientController) bulkResetTraffic(c *gin.Context) {
  620. var req bulkResetRequest
  621. if err := c.ShouldBindJSON(&req); err != nil {
  622. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  623. return
  624. }
  625. affected, err := a.clientService.BulkResetTraffic(&a.inboundService, req.Emails)
  626. if err != nil {
  627. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  628. return
  629. }
  630. jsonObj(c, gin.H{"affected": affected}, nil)
  631. a.xrayService.SetToNeedRestart()
  632. notifyClientsChanged()
  633. }