client.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. }
  298. func (a *ClientController) bulkAdjust(c *gin.Context) {
  299. var req bulkAdjustRequest
  300. if err := c.ShouldBindJSON(&req); err != nil {
  301. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  302. return
  303. }
  304. result, needRestart, err := a.clientService.BulkAdjust(&a.inboundService, req.Emails, req.AddDays, req.AddBytes, req.Flow)
  305. if err != nil {
  306. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  307. return
  308. }
  309. jsonObj(c, result, nil)
  310. if needRestart {
  311. a.xrayService.SetToNeedRestart()
  312. }
  313. notifyClientsChanged()
  314. }
  315. type bulkDeleteRequest struct {
  316. Emails []string `json:"emails"`
  317. KeepTraffic bool `json:"keepTraffic"`
  318. }
  319. type bulkAttachRequest struct {
  320. Emails []string `json:"emails"`
  321. InboundIds []int `json:"inboundIds"`
  322. }
  323. func (a *ClientController) bulkAttach(c *gin.Context) {
  324. var req bulkAttachRequest
  325. if err := c.ShouldBindJSON(&req); err != nil {
  326. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  327. return
  328. }
  329. result, needRestart, err := a.clientService.BulkAttach(&a.inboundService, req.Emails, req.InboundIds)
  330. if err != nil {
  331. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  332. return
  333. }
  334. jsonObj(c, result, nil)
  335. if needRestart {
  336. a.xrayService.SetToNeedRestart()
  337. }
  338. notifyClientsChanged()
  339. }
  340. type bulkDetachRequest struct {
  341. Emails []string `json:"emails"`
  342. InboundIds []int `json:"inboundIds"`
  343. }
  344. func (a *ClientController) bulkDetach(c *gin.Context) {
  345. var req bulkDetachRequest
  346. if err := c.ShouldBindJSON(&req); err != nil {
  347. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  348. return
  349. }
  350. result, needRestart, err := a.clientService.BulkDetach(&a.inboundService, req.Emails, req.InboundIds)
  351. if err != nil {
  352. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  353. return
  354. }
  355. jsonObj(c, result, nil)
  356. if needRestart {
  357. a.xrayService.SetToNeedRestart()
  358. }
  359. notifyClientsChanged()
  360. }
  361. func (a *ClientController) bulkDelete(c *gin.Context) {
  362. var req bulkDeleteRequest
  363. if err := c.ShouldBindJSON(&req); err != nil {
  364. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  365. return
  366. }
  367. result, needRestart, err := a.clientService.BulkDelete(&a.inboundService, req.Emails, req.KeepTraffic)
  368. if err != nil {
  369. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  370. return
  371. }
  372. jsonObj(c, result, nil)
  373. if needRestart {
  374. a.xrayService.SetToNeedRestart()
  375. }
  376. notifyClientsChanged()
  377. }
  378. type bulkEnableRequest struct {
  379. Emails []string `json:"emails"`
  380. }
  381. func (a *ClientController) bulkEnable(c *gin.Context) {
  382. a.bulkSetEnable(c, true)
  383. }
  384. func (a *ClientController) bulkDisable(c *gin.Context) {
  385. a.bulkSetEnable(c, false)
  386. }
  387. func (a *ClientController) bulkSetEnable(c *gin.Context, enable bool) {
  388. var req bulkEnableRequest
  389. if err := c.ShouldBindJSON(&req); err != nil {
  390. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  391. return
  392. }
  393. result, needRestart, err := a.clientService.BulkSetEnable(&a.inboundService, req.Emails, enable)
  394. if err != nil {
  395. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  396. return
  397. }
  398. jsonObj(c, result, nil)
  399. if needRestart {
  400. a.xrayService.SetToNeedRestart()
  401. }
  402. notifyClientsChanged()
  403. }
  404. func (a *ClientController) bulkCreate(c *gin.Context) {
  405. var payloads []service.ClientCreatePayload
  406. if err := c.ShouldBindJSON(&payloads); err != nil {
  407. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  408. return
  409. }
  410. result, needRestart, err := a.clientService.BulkCreate(&a.inboundService, payloads)
  411. if err != nil {
  412. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  413. return
  414. }
  415. jsonObj(c, result, nil)
  416. if needRestart {
  417. a.xrayService.SetToNeedRestart()
  418. }
  419. notifyClientsChanged()
  420. }
  421. func (a *ClientController) delDepleted(c *gin.Context) {
  422. deleted, needRestart, err := a.clientService.DelDepleted(&a.inboundService)
  423. if err != nil {
  424. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  425. return
  426. }
  427. jsonObj(c, gin.H{"deleted": deleted}, nil)
  428. if needRestart {
  429. a.xrayService.SetToNeedRestart()
  430. }
  431. notifyClientsChanged()
  432. }
  433. // export returns every client as a {client, inboundIds} list in the standard
  434. // envelope. The frontend renders it in a read-only CodeMirror viewer (Copy /
  435. // Download), so this hands back data rather than streaming a file attachment.
  436. func (a *ClientController) export(c *gin.Context) {
  437. items, err := a.clientService.ExportAll()
  438. if err != nil {
  439. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  440. return
  441. }
  442. jsonObj(c, items, nil)
  443. }
  444. type importClientsRequest struct {
  445. Data string `json:"data"`
  446. }
  447. // importClients accepts the pasted export text as a JSON body { "data": "..." },
  448. // mirroring the inbound import flow. The data string is itself a JSON-encoded
  449. // []ClientCreatePayload, so it is unmarshalled in a second step.
  450. func (a *ClientController) importClients(c *gin.Context) {
  451. var req importClientsRequest
  452. if err := c.ShouldBindJSON(&req); err != nil {
  453. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  454. return
  455. }
  456. var items []service.ClientCreatePayload
  457. if err := json.Unmarshal([]byte(req.Data), &items); err != nil {
  458. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  459. return
  460. }
  461. result, needRestart, err := a.clientService.ImportClients(&a.inboundService, items)
  462. if err != nil {
  463. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  464. return
  465. }
  466. jsonObj(c, result, nil)
  467. if needRestart {
  468. a.xrayService.SetToNeedRestart()
  469. }
  470. notifyClientsChanged()
  471. }
  472. func (a *ClientController) delOrphans(c *gin.Context) {
  473. deleted, err := a.clientService.DeleteOrphans()
  474. if err != nil {
  475. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  476. return
  477. }
  478. jsonObj(c, gin.H{"deleted": deleted}, nil)
  479. notifyClientsChanged()
  480. }
  481. func (a *ClientController) resetTrafficByEmail(c *gin.Context) {
  482. email := c.Param("email")
  483. needRestart, err := a.clientService.ResetTrafficByEmail(&a.inboundService, email)
  484. if err != nil {
  485. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  486. return
  487. }
  488. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundClientTrafficSuccess"), nil)
  489. if needRestart {
  490. a.xrayService.SetToNeedRestart()
  491. }
  492. notifyClientsChanged()
  493. }
  494. type trafficUpdateRequest struct {
  495. Upload int64 `json:"upload"`
  496. Download int64 `json:"download"`
  497. }
  498. func (a *ClientController) updateTrafficByEmail(c *gin.Context) {
  499. email := c.Param("email")
  500. var req trafficUpdateRequest
  501. if err := c.ShouldBindJSON(&req); err != nil {
  502. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  503. return
  504. }
  505. if err := a.inboundService.UpdateClientTrafficByEmail(email, req.Upload, req.Download); err != nil {
  506. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  507. return
  508. }
  509. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  510. notifyClientsChanged()
  511. }
  512. func (a *ClientController) getIps(c *gin.Context) {
  513. email := c.Param("email")
  514. infos, err := a.inboundService.GetClientIpsWithNodes(email)
  515. jsonObj(c, infos, err)
  516. }
  517. func (a *ClientController) clientIpsByGuid(c *gin.Context) {
  518. data, err := a.inboundService.GetClientIpsByGuid()
  519. jsonObj(c, data, err)
  520. }
  521. func (a *ClientController) clearIps(c *gin.Context) {
  522. email := c.Param("email")
  523. if err := a.inboundService.ClearClientIps(email); err != nil {
  524. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  525. return
  526. }
  527. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  528. }
  529. func (a *ClientController) getHwids(c *gin.Context) {
  530. infos, err := a.clientService.ListClientHwids(c.Param("email"))
  531. jsonObj(c, infos, err)
  532. }
  533. func (a *ClientController) clearHwids(c *gin.Context) {
  534. if err := a.clientService.ClearClientHwids(c.Param("email")); err != nil {
  535. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  536. return
  537. }
  538. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  539. }
  540. func (a *ClientController) deleteHwid(c *gin.Context) {
  541. id, err := strconv.Atoi(c.Param("id"))
  542. if err != nil {
  543. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  544. return
  545. }
  546. if err := a.clientService.DeleteClientHwid(c.Param("email"), id); err != nil {
  547. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  548. return
  549. }
  550. jsonMsg(c, I18nWeb(c, "pages.clients.hwidDeleted"), nil)
  551. }
  552. func (a *ClientController) onlines(c *gin.Context) {
  553. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  554. }
  555. func (a *ClientController) onlinesByGuid(c *gin.Context) {
  556. jsonObj(c, a.inboundService.GetOnlineClientsByGuid(), nil)
  557. }
  558. func (a *ClientController) activeInbounds(c *gin.Context) {
  559. jsonObj(c, a.inboundService.GetActiveInboundsByGuid(), nil)
  560. }
  561. func (a *ClientController) lastOnline(c *gin.Context) {
  562. data, err := a.inboundService.GetClientsLastOnline()
  563. jsonObj(c, data, err)
  564. }
  565. func (a *ClientController) getTrafficByEmail(c *gin.Context) {
  566. email := c.Param("email")
  567. traffic, err := a.inboundService.GetClientTrafficByEmail(email)
  568. if err != nil {
  569. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  570. return
  571. }
  572. jsonObj(c, traffic, nil)
  573. }
  574. func (a *ClientController) getSubLinks(c *gin.Context) {
  575. links, err := a.inboundService.GetSubLinks(resolveHost(c), c.Param("subId"))
  576. if err != nil {
  577. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  578. return
  579. }
  580. jsonObj(c, links, nil)
  581. }
  582. func (a *ClientController) getClientLinks(c *gin.Context) {
  583. links, err := a.inboundService.GetAllClientLinks(resolveHost(c), c.Param("email"))
  584. if err != nil {
  585. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  586. return
  587. }
  588. jsonObj(c, links, nil)
  589. }
  590. func (a *ClientController) detach(c *gin.Context) {
  591. email := c.Param("email")
  592. var body attachDetachBody
  593. if err := c.ShouldBindJSON(&body); err != nil {
  594. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  595. return
  596. }
  597. needRestart, err := a.clientService.DetachByEmailMany(&a.inboundService, email, body.InboundIds)
  598. // Flagged before the error check: a partly-applied detach already removed
  599. // the client from the inbounds that succeeded, and those need the restart.
  600. if needRestart {
  601. a.xrayService.SetToNeedRestart()
  602. }
  603. // A partly-applied call committed real removals; a rejected one touched
  604. // nothing, and broadcasting those would refetch every panel for nothing.
  605. if needRestart || err == nil {
  606. notifyClientsChanged()
  607. }
  608. if err != nil {
  609. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  610. return
  611. }
  612. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), pendingNodeObj(a.inboundService.AnyNodePending(body.InboundIds)), nil)
  613. }
  614. type bulkResetRequest struct {
  615. Emails []string `json:"emails"`
  616. }
  617. func (a *ClientController) bulkResetTraffic(c *gin.Context) {
  618. var req bulkResetRequest
  619. if err := c.ShouldBindJSON(&req); err != nil {
  620. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  621. return
  622. }
  623. affected, err := a.clientService.BulkResetTraffic(&a.inboundService, req.Emails)
  624. if err != nil {
  625. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  626. return
  627. }
  628. jsonObj(c, gin.H{"affected": affected}, nil)
  629. a.xrayService.SetToNeedRestart()
  630. notifyClientsChanged()
  631. }