inbound.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package controller
  2. import (
  3. "errors"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "x-ui/database/model"
  8. "x-ui/web/service"
  9. "x-ui/web/session"
  10. "github.com/gin-gonic/gin"
  11. )
  12. type InboundController struct {
  13. inboundService service.InboundService
  14. xrayService service.XrayService
  15. }
  16. func NewInboundController(g *gin.RouterGroup) *InboundController {
  17. a := &InboundController{}
  18. a.initRouter(g)
  19. return a
  20. }
  21. func (a *InboundController) initRouter(g *gin.RouterGroup) {
  22. g = g.Group("/inbound")
  23. g.POST("/list", a.getInbounds)
  24. g.POST("/add", a.addInbound)
  25. g.POST("/del/:id", a.delInbound)
  26. g.POST("/update/:id", a.updateInbound)
  27. g.POST("/clientIps/:email", a.getClientIps)
  28. g.POST("/clearClientIps/:email", a.clearClientIps)
  29. g.POST("/addClient", a.addInboundClient)
  30. g.POST("/addGroupClient", a.addGroupInboundClient)
  31. g.POST("/:id/delClient/:clientId", a.delInboundClient)
  32. g.POST("/delGroupClients", a.delGroupClients)
  33. g.POST("/updateClient/:clientId", a.updateInboundClient)
  34. g.POST("/updateClients", a.updateGroupInboundClient)
  35. g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
  36. g.POST("/resetGroupClientTraffic", a.resetGroupClientTraffic)
  37. g.POST("/resetAllTraffics", a.resetAllTraffics)
  38. g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
  39. g.POST("/delDepletedClients/:id", a.delDepletedClients)
  40. g.POST("/import", a.importInbound)
  41. g.POST("/onlines", a.onlines)
  42. }
  43. func (a *InboundController) getInbounds(c *gin.Context) {
  44. user := session.GetLoginUser(c)
  45. inbounds, err := a.inboundService.GetInbounds(user.Id)
  46. if err != nil {
  47. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  48. return
  49. }
  50. jsonObj(c, inbounds, nil)
  51. }
  52. func (a *InboundController) getInbound(c *gin.Context) {
  53. id, err := strconv.Atoi(c.Param("id"))
  54. if err != nil {
  55. jsonMsg(c, I18nWeb(c, "get"), err)
  56. return
  57. }
  58. inbound, err := a.inboundService.GetInbound(id)
  59. if err != nil {
  60. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  61. return
  62. }
  63. jsonObj(c, inbound, nil)
  64. }
  65. func (a *InboundController) getClientTraffics(c *gin.Context) {
  66. email := c.Param("email")
  67. clientTraffics, err := a.inboundService.GetClientTrafficByEmail(email)
  68. if err != nil {
  69. jsonMsg(c, "Error getting traffics", err)
  70. return
  71. }
  72. jsonObj(c, clientTraffics, nil)
  73. }
  74. func (a *InboundController) getClientTrafficsById(c *gin.Context) {
  75. id := c.Param("id")
  76. clientTraffics, err := a.inboundService.GetClientTrafficByID(id)
  77. if err != nil {
  78. jsonMsg(c, "Error getting traffics", err)
  79. return
  80. }
  81. jsonObj(c, clientTraffics, nil)
  82. }
  83. func (a *InboundController) addInbound(c *gin.Context) {
  84. inbound := &model.Inbound{}
  85. err := c.ShouldBind(inbound)
  86. if err != nil {
  87. jsonMsg(c, I18nWeb(c, "pages.inbounds.create"), err)
  88. return
  89. }
  90. user := session.GetLoginUser(c)
  91. inbound.UserId = user.Id
  92. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  93. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  94. } else {
  95. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  96. }
  97. needRestart := false
  98. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  99. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
  100. if err == nil && needRestart {
  101. a.xrayService.SetToNeedRestart()
  102. }
  103. }
  104. func (a *InboundController) delInbound(c *gin.Context) {
  105. id, err := strconv.Atoi(c.Param("id"))
  106. if err != nil {
  107. jsonMsg(c, I18nWeb(c, "delete"), err)
  108. return
  109. }
  110. needRestart := true
  111. needRestart, err = a.inboundService.DelInbound(id)
  112. jsonMsgObj(c, I18nWeb(c, "delete"), id, err)
  113. if err == nil && needRestart {
  114. a.xrayService.SetToNeedRestart()
  115. }
  116. }
  117. func (a *InboundController) updateInbound(c *gin.Context) {
  118. id, err := strconv.Atoi(c.Param("id"))
  119. if err != nil {
  120. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  121. return
  122. }
  123. inbound := &model.Inbound{
  124. Id: id,
  125. }
  126. err = c.ShouldBind(inbound)
  127. if err != nil {
  128. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  129. return
  130. }
  131. needRestart := true
  132. inbound, needRestart, err = a.inboundService.UpdateInbound(inbound)
  133. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.update"), inbound, err)
  134. if err == nil && needRestart {
  135. a.xrayService.SetToNeedRestart()
  136. }
  137. }
  138. func (a *InboundController) getClientIps(c *gin.Context) {
  139. email := c.Param("email")
  140. ips, err := a.inboundService.GetInboundClientIps(email)
  141. if err != nil || ips == "" {
  142. jsonObj(c, "No IP Record", nil)
  143. return
  144. }
  145. jsonObj(c, ips, nil)
  146. }
  147. func (a *InboundController) clearClientIps(c *gin.Context) {
  148. email := c.Param("email")
  149. err := a.inboundService.ClearClientIps(email)
  150. if err != nil {
  151. jsonMsg(c, "Update", err)
  152. return
  153. }
  154. jsonMsg(c, "Log Cleared", nil)
  155. }
  156. func (a *InboundController) addInboundClient(c *gin.Context) {
  157. data := &model.Inbound{}
  158. err := c.ShouldBind(data)
  159. if err != nil {
  160. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  161. return
  162. }
  163. needRestart := true
  164. needRestart, err = a.inboundService.AddInboundClient(data)
  165. if err != nil {
  166. jsonMsg(c, "Something went wrong!", err)
  167. return
  168. }
  169. jsonMsg(c, "Client(s) added", nil)
  170. if needRestart {
  171. a.xrayService.SetToNeedRestart()
  172. }
  173. }
  174. func (a *InboundController) addGroupInboundClient(c *gin.Context) {
  175. var requestData []model.Inbound
  176. err := c.ShouldBindJSON(&requestData)
  177. if err != nil {
  178. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  179. return
  180. }
  181. needRestart := true
  182. for _, data := range requestData {
  183. needRestart, err = a.inboundService.AddInboundClient(&data)
  184. if err != nil {
  185. jsonMsg(c, "Something went wrong!", err)
  186. return
  187. }
  188. }
  189. jsonMsg(c, "Client(s) added", nil)
  190. if err == nil && needRestart {
  191. a.xrayService.SetToNeedRestart()
  192. }
  193. }
  194. func (a *InboundController) delInboundClient(c *gin.Context) {
  195. id, err := strconv.Atoi(c.Param("id"))
  196. if err != nil {
  197. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  198. return
  199. }
  200. clientId := c.Param("clientId")
  201. needRestart := true
  202. needRestart, err = a.inboundService.DelInboundClient(id, clientId)
  203. if err != nil {
  204. jsonMsg(c, "Something went wrong!", err)
  205. return
  206. }
  207. jsonMsg(c, "Client deleted", nil)
  208. if needRestart {
  209. a.xrayService.SetToNeedRestart()
  210. }
  211. }
  212. func (a *InboundController) delGroupClients(c *gin.Context) {
  213. var requestData []struct {
  214. InboundID int `json:"inboundId"`
  215. ClientID string `json:"clientId"`
  216. }
  217. if err := c.ShouldBindJSON(&requestData); err != nil {
  218. jsonMsg(c, "Invalid request data", err)
  219. return
  220. }
  221. needRestart := false
  222. for _, req := range requestData {
  223. needRestartTmp, err := a.inboundService.DelInboundClient(req.InboundID, req.ClientID)
  224. if err != nil {
  225. jsonMsg(c, "Failed to delete client", err)
  226. return
  227. }
  228. if needRestartTmp {
  229. needRestart = true
  230. }
  231. }
  232. jsonMsg(c, "Clients deleted successfully", nil)
  233. if needRestart {
  234. a.xrayService.SetToNeedRestart()
  235. }
  236. }
  237. func (a *InboundController) updateInboundClient(c *gin.Context) {
  238. clientId := c.Param("clientId")
  239. inbound := &model.Inbound{}
  240. err := c.ShouldBind(inbound)
  241. if err != nil {
  242. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  243. return
  244. }
  245. needRestart := true
  246. needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
  247. if err != nil {
  248. jsonMsg(c, "Something went wrong!", err)
  249. return
  250. }
  251. jsonMsg(c, "Client updated", nil)
  252. if needRestart {
  253. a.xrayService.SetToNeedRestart()
  254. }
  255. }
  256. func (a *InboundController) updateGroupInboundClient(c *gin.Context) {
  257. var requestData []map[string]interface{}
  258. if err := c.ShouldBindJSON(&requestData); err != nil {
  259. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  260. return
  261. }
  262. needRestart := false
  263. for _, item := range requestData {
  264. inboundMap, ok := item["inbound"].(map[string]interface{})
  265. if !ok {
  266. jsonMsg(c, "Something went wrong!", errors.New("Failed to convert 'inbound' to map"))
  267. return
  268. }
  269. clientId, ok := item["clientId"].(string)
  270. if !ok {
  271. jsonMsg(c, "Something went wrong!", errors.New("Failed to convert 'clientId' to string"))
  272. return
  273. }
  274. inboundJSON, err := json.Marshal(inboundMap)
  275. if err != nil {
  276. jsonMsg(c, "Something went wrong!", err)
  277. return
  278. }
  279. var inboundModel model.Inbound
  280. if err := json.Unmarshal(inboundJSON, &inboundModel); err != nil {
  281. jsonMsg(c, "Something went wrong!", err)
  282. return
  283. }
  284. if restart, err := a.inboundService.UpdateInboundClient(&inboundModel, clientId); err != nil {
  285. jsonMsg(c, "Something went wrong!", err)
  286. return
  287. } else {
  288. needRestart = needRestart || restart
  289. }
  290. }
  291. jsonMsg(c, "Client updated", nil)
  292. if needRestart {
  293. a.xrayService.SetToNeedRestart()
  294. }
  295. }
  296. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  297. id, err := strconv.Atoi(c.Param("id"))
  298. if err != nil {
  299. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  300. return
  301. }
  302. email := c.Param("email")
  303. needRestart, err := a.inboundService.ResetClientTraffic(id, email)
  304. if err != nil {
  305. jsonMsg(c, "Something went wrong!", err)
  306. return
  307. }
  308. jsonMsg(c, "Traffic has been reset", nil)
  309. if needRestart {
  310. a.xrayService.SetToNeedRestart()
  311. }
  312. }
  313. func (a *InboundController) resetGroupClientTraffic(c *gin.Context) {
  314. var requestData []struct {
  315. InboundID int `json:"inboundId"` // Map JSON "inboundId" to struct field "InboundID"
  316. Email string `json:"email"` // Map JSON "email" to struct field "Email"
  317. }
  318. // Parse JSON body directly using ShouldBindJSON
  319. if err := c.ShouldBindJSON(&requestData); err != nil {
  320. jsonMsg(c, "Invalid request data", err)
  321. return
  322. }
  323. needRestart := false
  324. // Process each request data
  325. for _, req := range requestData {
  326. needRestartTmp, err := a.inboundService.ResetClientTraffic(req.InboundID, req.Email)
  327. if err != nil {
  328. jsonMsg(c, "Failed to reset client traffic", err)
  329. return
  330. }
  331. // If any request requires a restart, set needRestart to true
  332. if needRestartTmp {
  333. needRestart = true
  334. }
  335. }
  336. // Send response back to the client
  337. jsonMsg(c, "Traffic reset for all clients", nil)
  338. // Restart the service if required
  339. if needRestart {
  340. a.xrayService.SetToNeedRestart()
  341. }
  342. }
  343. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  344. err := a.inboundService.ResetAllTraffics()
  345. if err != nil {
  346. jsonMsg(c, "Something went wrong!", err)
  347. return
  348. } else {
  349. a.xrayService.SetToNeedRestart()
  350. }
  351. jsonMsg(c, "all traffic has been reset", nil)
  352. }
  353. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  354. id, err := strconv.Atoi(c.Param("id"))
  355. if err != nil {
  356. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  357. return
  358. }
  359. err = a.inboundService.ResetAllClientTraffics(id)
  360. if err != nil {
  361. jsonMsg(c, "Something went wrong!", err)
  362. return
  363. } else {
  364. a.xrayService.SetToNeedRestart()
  365. }
  366. jsonMsg(c, "All traffic from the client has been reset.", nil)
  367. }
  368. func (a *InboundController) importInbound(c *gin.Context) {
  369. inbound := &model.Inbound{}
  370. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  371. if err != nil {
  372. jsonMsg(c, "Something went wrong!", err)
  373. return
  374. }
  375. user := session.GetLoginUser(c)
  376. inbound.Id = 0
  377. inbound.UserId = user.Id
  378. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  379. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  380. } else {
  381. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  382. }
  383. for index := range inbound.ClientStats {
  384. inbound.ClientStats[index].Id = 0
  385. inbound.ClientStats[index].Enable = true
  386. }
  387. needRestart := false
  388. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  389. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
  390. if err == nil && needRestart {
  391. a.xrayService.SetToNeedRestart()
  392. }
  393. }
  394. func (a *InboundController) delDepletedClients(c *gin.Context) {
  395. id, err := strconv.Atoi(c.Param("id"))
  396. if err != nil {
  397. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  398. return
  399. }
  400. err = a.inboundService.DelDepletedClients(id)
  401. if err != nil {
  402. jsonMsg(c, "Something went wrong!", err)
  403. return
  404. }
  405. jsonMsg(c, "All depleted clients are deleted", nil)
  406. }
  407. func (a *InboundController) onlines(c *gin.Context) {
  408. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  409. }