client.go 21 KB

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