client.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/mhsanaei/3x-ui/v3/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/web/service"
  8. "github.com/mhsanaei/3x-ui/v3/web/websocket"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func notifyClientsChanged() {
  12. websocket.BroadcastInvalidate(websocket.MessageTypeClients)
  13. }
  14. type ClientController struct {
  15. clientService service.ClientService
  16. inboundService service.InboundService
  17. xrayService service.XrayService
  18. settingService service.SettingService
  19. }
  20. func NewClientController(g *gin.RouterGroup) *ClientController {
  21. a := &ClientController{}
  22. a.initRouter(g)
  23. return a
  24. }
  25. func (a *ClientController) initRouter(g *gin.RouterGroup) {
  26. g.GET("/list", a.list)
  27. g.GET("/list/paged", a.listPaged)
  28. g.GET("/get/:email", a.get)
  29. g.GET("/traffic/:email", a.getTrafficByEmail)
  30. g.GET("/subLinks/:subId", a.getSubLinks)
  31. g.GET("/links/:email", a.getClientLinks)
  32. g.POST("/add", a.create)
  33. g.POST("/update/:email", a.update)
  34. g.POST("/del/:email", a.delete)
  35. g.POST("/:email/attach", a.attach)
  36. g.POST("/:email/detach", a.detach)
  37. g.POST("/resetAllTraffics", a.resetAllTraffics)
  38. g.POST("/delDepleted", a.delDepleted)
  39. g.POST("/bulkAdjust", a.bulkAdjust)
  40. g.POST("/bulkDel", a.bulkDelete)
  41. g.POST("/bulkCreate", a.bulkCreate)
  42. g.POST("/resetTraffic/:email", a.resetTrafficByEmail)
  43. g.POST("/updateTraffic/:email", a.updateTrafficByEmail)
  44. g.POST("/ips/:email", a.getIps)
  45. g.POST("/clearIps/:email", a.clearIps)
  46. g.POST("/onlines", a.onlines)
  47. g.POST("/lastOnline", a.lastOnline)
  48. }
  49. func (a *ClientController) list(c *gin.Context) {
  50. rows, err := a.clientService.List()
  51. if err != nil {
  52. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  53. return
  54. }
  55. jsonObj(c, rows, nil)
  56. }
  57. func (a *ClientController) listPaged(c *gin.Context) {
  58. var params service.ClientPageParams
  59. if err := c.ShouldBindQuery(&params); err != nil {
  60. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  61. return
  62. }
  63. resp, err := a.clientService.ListPaged(&a.inboundService, &a.settingService, params)
  64. if err != nil {
  65. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  66. return
  67. }
  68. jsonObj(c, resp, nil)
  69. }
  70. func (a *ClientController) get(c *gin.Context) {
  71. email := c.Param("email")
  72. rec, err := a.clientService.GetRecordByEmail(nil, email)
  73. if err != nil {
  74. jsonMsg(c, I18nWeb(c, "get"), err)
  75. return
  76. }
  77. inboundIds, err := a.clientService.GetInboundIdsForRecord(rec.Id)
  78. if err != nil {
  79. jsonMsg(c, I18nWeb(c, "get"), err)
  80. return
  81. }
  82. jsonObj(c, gin.H{"client": rec, "inboundIds": inboundIds}, nil)
  83. }
  84. func (a *ClientController) create(c *gin.Context) {
  85. var payload service.ClientCreatePayload
  86. if err := c.ShouldBindJSON(&payload); err != nil {
  87. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  88. return
  89. }
  90. needRestart, err := a.clientService.Create(&a.inboundService, &payload)
  91. if err != nil {
  92. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  93. return
  94. }
  95. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), nil)
  96. if needRestart {
  97. a.xrayService.SetToNeedRestart()
  98. }
  99. notifyClientsChanged()
  100. }
  101. func (a *ClientController) update(c *gin.Context) {
  102. email := c.Param("email")
  103. var updated model.Client
  104. if err := c.ShouldBindJSON(&updated); err != nil {
  105. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  106. return
  107. }
  108. needRestart, err := a.clientService.UpdateByEmail(&a.inboundService, email, updated)
  109. if err != nil {
  110. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  111. return
  112. }
  113. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  114. if needRestart {
  115. a.xrayService.SetToNeedRestart()
  116. }
  117. notifyClientsChanged()
  118. }
  119. func (a *ClientController) delete(c *gin.Context) {
  120. email := c.Param("email")
  121. keepTraffic := c.Query("keepTraffic") == "1"
  122. needRestart, err := a.clientService.DeleteByEmail(&a.inboundService, email, keepTraffic)
  123. if err != nil {
  124. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  125. return
  126. }
  127. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), nil)
  128. if needRestart {
  129. a.xrayService.SetToNeedRestart()
  130. }
  131. notifyClientsChanged()
  132. }
  133. type attachDetachBody struct {
  134. InboundIds []int `json:"inboundIds"`
  135. }
  136. func (a *ClientController) attach(c *gin.Context) {
  137. email := c.Param("email")
  138. var body attachDetachBody
  139. if err := c.ShouldBindJSON(&body); err != nil {
  140. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  141. return
  142. }
  143. needRestart, err := a.clientService.AttachByEmail(&a.inboundService, email, body.InboundIds)
  144. if err != nil {
  145. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  146. return
  147. }
  148. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), nil)
  149. if needRestart {
  150. a.xrayService.SetToNeedRestart()
  151. }
  152. notifyClientsChanged()
  153. }
  154. func (a *ClientController) resetAllTraffics(c *gin.Context) {
  155. needRestart, err := a.clientService.ResetAllTraffics()
  156. if err != nil {
  157. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  158. return
  159. }
  160. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllClientTrafficSuccess"), nil)
  161. if needRestart {
  162. a.xrayService.SetToNeedRestart()
  163. }
  164. notifyClientsChanged()
  165. }
  166. type bulkAdjustRequest struct {
  167. Emails []string `json:"emails"`
  168. AddDays int `json:"addDays"`
  169. AddBytes int64 `json:"addBytes"`
  170. }
  171. func (a *ClientController) bulkAdjust(c *gin.Context) {
  172. var req bulkAdjustRequest
  173. if err := c.ShouldBindJSON(&req); err != nil {
  174. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  175. return
  176. }
  177. result, needRestart, err := a.clientService.BulkAdjust(&a.inboundService, req.Emails, req.AddDays, req.AddBytes)
  178. if err != nil {
  179. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  180. return
  181. }
  182. jsonObj(c, result, nil)
  183. if needRestart {
  184. a.xrayService.SetToNeedRestart()
  185. }
  186. notifyClientsChanged()
  187. }
  188. type bulkDeleteRequest struct {
  189. Emails []string `json:"emails"`
  190. KeepTraffic bool `json:"keepTraffic"`
  191. }
  192. func (a *ClientController) bulkDelete(c *gin.Context) {
  193. var req bulkDeleteRequest
  194. if err := c.ShouldBindJSON(&req); err != nil {
  195. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  196. return
  197. }
  198. result, needRestart, err := a.clientService.BulkDelete(&a.inboundService, req.Emails, req.KeepTraffic)
  199. if err != nil {
  200. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  201. return
  202. }
  203. jsonObj(c, result, nil)
  204. if needRestart {
  205. a.xrayService.SetToNeedRestart()
  206. }
  207. notifyClientsChanged()
  208. }
  209. func (a *ClientController) bulkCreate(c *gin.Context) {
  210. var payloads []service.ClientCreatePayload
  211. if err := c.ShouldBindJSON(&payloads); err != nil {
  212. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  213. return
  214. }
  215. result, needRestart, err := a.clientService.BulkCreate(&a.inboundService, payloads)
  216. if err != nil {
  217. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  218. return
  219. }
  220. jsonObj(c, result, nil)
  221. if needRestart {
  222. a.xrayService.SetToNeedRestart()
  223. }
  224. notifyClientsChanged()
  225. }
  226. func (a *ClientController) delDepleted(c *gin.Context) {
  227. deleted, needRestart, err := a.clientService.DelDepleted(&a.inboundService)
  228. if err != nil {
  229. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  230. return
  231. }
  232. jsonObj(c, gin.H{"deleted": deleted}, nil)
  233. if needRestart {
  234. a.xrayService.SetToNeedRestart()
  235. }
  236. notifyClientsChanged()
  237. }
  238. func (a *ClientController) resetTrafficByEmail(c *gin.Context) {
  239. email := c.Param("email")
  240. needRestart, err := a.clientService.ResetTrafficByEmail(&a.inboundService, email)
  241. if err != nil {
  242. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  243. return
  244. }
  245. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundClientTrafficSuccess"), nil)
  246. if needRestart {
  247. a.xrayService.SetToNeedRestart()
  248. }
  249. notifyClientsChanged()
  250. }
  251. type trafficUpdateRequest struct {
  252. Upload int64 `json:"upload"`
  253. Download int64 `json:"download"`
  254. }
  255. func (a *ClientController) updateTrafficByEmail(c *gin.Context) {
  256. email := c.Param("email")
  257. var req trafficUpdateRequest
  258. if err := c.ShouldBindJSON(&req); err != nil {
  259. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  260. return
  261. }
  262. if err := a.inboundService.UpdateClientTrafficByEmail(email, req.Upload, req.Download); err != nil {
  263. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  264. return
  265. }
  266. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  267. notifyClientsChanged()
  268. }
  269. func (a *ClientController) getIps(c *gin.Context) {
  270. email := c.Param("email")
  271. ips, err := a.inboundService.GetInboundClientIps(email)
  272. if err != nil || ips == "" {
  273. jsonObj(c, "No IP Record", nil)
  274. return
  275. }
  276. type ipWithTimestamp struct {
  277. IP string `json:"ip"`
  278. Timestamp int64 `json:"timestamp"`
  279. }
  280. var ipsWithTime []ipWithTimestamp
  281. if err := json.Unmarshal([]byte(ips), &ipsWithTime); err == nil && len(ipsWithTime) > 0 {
  282. formatted := make([]string, 0, len(ipsWithTime))
  283. for _, item := range ipsWithTime {
  284. if item.IP == "" {
  285. continue
  286. }
  287. if item.Timestamp > 0 {
  288. ts := time.Unix(item.Timestamp, 0).Local().Format("2006-01-02 15:04:05")
  289. formatted = append(formatted, fmt.Sprintf("%s (%s)", item.IP, ts))
  290. continue
  291. }
  292. formatted = append(formatted, item.IP)
  293. }
  294. jsonObj(c, formatted, nil)
  295. return
  296. }
  297. var oldIps []string
  298. if err := json.Unmarshal([]byte(ips), &oldIps); err == nil && len(oldIps) > 0 {
  299. jsonObj(c, oldIps, nil)
  300. return
  301. }
  302. jsonObj(c, ips, nil)
  303. }
  304. func (a *ClientController) clearIps(c *gin.Context) {
  305. email := c.Param("email")
  306. if err := a.inboundService.ClearClientIps(email); err != nil {
  307. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  308. return
  309. }
  310. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  311. }
  312. func (a *ClientController) onlines(c *gin.Context) {
  313. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  314. }
  315. func (a *ClientController) lastOnline(c *gin.Context) {
  316. data, err := a.inboundService.GetClientsLastOnline()
  317. jsonObj(c, data, err)
  318. }
  319. func (a *ClientController) getTrafficByEmail(c *gin.Context) {
  320. email := c.Param("email")
  321. traffic, err := a.inboundService.GetClientTrafficByEmail(email)
  322. if err != nil {
  323. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), err)
  324. return
  325. }
  326. jsonObj(c, traffic, nil)
  327. }
  328. func (a *ClientController) getSubLinks(c *gin.Context) {
  329. links, err := a.inboundService.GetSubLinks(resolveHost(c), c.Param("subId"))
  330. if err != nil {
  331. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  332. return
  333. }
  334. jsonObj(c, links, nil)
  335. }
  336. func (a *ClientController) getClientLinks(c *gin.Context) {
  337. links, err := a.inboundService.GetAllClientLinks(resolveHost(c), c.Param("email"))
  338. if err != nil {
  339. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  340. return
  341. }
  342. jsonObj(c, links, nil)
  343. }
  344. func (a *ClientController) detach(c *gin.Context) {
  345. email := c.Param("email")
  346. var body attachDetachBody
  347. if err := c.ShouldBindJSON(&body); err != nil {
  348. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  349. return
  350. }
  351. needRestart, err := a.clientService.DetachByEmailMany(&a.inboundService, email, body.InboundIds)
  352. if err != nil {
  353. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  354. return
  355. }
  356. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), nil)
  357. if needRestart {
  358. a.xrayService.SetToNeedRestart()
  359. }
  360. notifyClientsChanged()
  361. }