1
0

client.go 22 KB

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