inbound.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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("/updateClients", a.updateGroupInboundClient)
  33. g.POST("/updateClient/:clientId", a.updateInboundClient)
  34. g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
  35. g.POST("/resetAllTraffics", a.resetAllTraffics)
  36. g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
  37. g.POST("/delDepletedClients/:id", a.delDepletedClients)
  38. g.POST("/import", a.importInbound)
  39. g.POST("/onlines", a.onlines)
  40. }
  41. func (a *InboundController) getInbounds(c *gin.Context) {
  42. user := session.GetLoginUser(c)
  43. inbounds, err := a.inboundService.GetInbounds(user.Id)
  44. if err != nil {
  45. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  46. return
  47. }
  48. jsonObj(c, inbounds, nil)
  49. }
  50. func (a *InboundController) getInbound(c *gin.Context) {
  51. id, err := strconv.Atoi(c.Param("id"))
  52. if err != nil {
  53. jsonMsg(c, I18nWeb(c, "get"), err)
  54. return
  55. }
  56. inbound, err := a.inboundService.GetInbound(id)
  57. if err != nil {
  58. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
  59. return
  60. }
  61. jsonObj(c, inbound, nil)
  62. }
  63. func (a *InboundController) getClientTraffics(c *gin.Context) {
  64. email := c.Param("email")
  65. clientTraffics, err := a.inboundService.GetClientTrafficByEmail(email)
  66. if err != nil {
  67. jsonMsg(c, "Error getting traffics", err)
  68. return
  69. }
  70. jsonObj(c, clientTraffics, nil)
  71. }
  72. func (a *InboundController) addInbound(c *gin.Context) {
  73. inbound := &model.Inbound{}
  74. err := c.ShouldBind(inbound)
  75. if err != nil {
  76. jsonMsg(c, I18nWeb(c, "pages.inbounds.create"), err)
  77. return
  78. }
  79. user := session.GetLoginUser(c)
  80. inbound.UserId = user.Id
  81. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  82. inbound.Tag = fmt.Sprintf("inbound-0.0.0.0:%v", inbound.Port)
  83. } else {
  84. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  85. }
  86. needRestart := false
  87. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  88. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
  89. if err == nil && needRestart {
  90. a.xrayService.SetToNeedRestart()
  91. }
  92. }
  93. func (a *InboundController) delInbound(c *gin.Context) {
  94. id, err := strconv.Atoi(c.Param("id"))
  95. if err != nil {
  96. jsonMsg(c, I18nWeb(c, "delete"), err)
  97. return
  98. }
  99. needRestart := true
  100. needRestart, err = a.inboundService.DelInbound(id)
  101. jsonMsgObj(c, I18nWeb(c, "delete"), id, err)
  102. if err == nil && needRestart {
  103. a.xrayService.SetToNeedRestart()
  104. }
  105. }
  106. func (a *InboundController) updateInbound(c *gin.Context) {
  107. id, err := strconv.Atoi(c.Param("id"))
  108. if err != nil {
  109. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  110. return
  111. }
  112. inbound := &model.Inbound{
  113. Id: id,
  114. }
  115. err = c.ShouldBind(inbound)
  116. if err != nil {
  117. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  118. return
  119. }
  120. needRestart := true
  121. inbound, needRestart, err = a.inboundService.UpdateInbound(inbound)
  122. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.update"), inbound, err)
  123. if err == nil && needRestart {
  124. a.xrayService.SetToNeedRestart()
  125. }
  126. }
  127. func (a *InboundController) getClientIps(c *gin.Context) {
  128. email := c.Param("email")
  129. ips, err := a.inboundService.GetInboundClientIps(email)
  130. if err != nil || ips == "" {
  131. jsonObj(c, "No IP Record", nil)
  132. return
  133. }
  134. jsonObj(c, ips, nil)
  135. }
  136. func (a *InboundController) clearClientIps(c *gin.Context) {
  137. email := c.Param("email")
  138. err := a.inboundService.ClearClientIps(email)
  139. if err != nil {
  140. jsonMsg(c, "Update", err)
  141. return
  142. }
  143. jsonMsg(c, "Log Cleared", nil)
  144. }
  145. func (a *InboundController) addInboundClient(c *gin.Context) {
  146. var requestData []model.Inbound
  147. err := c.ShouldBindJSON(&requestData)
  148. if err != nil {
  149. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  150. return
  151. }
  152. needRestart := true
  153. for _, data := range requestData {
  154. needRestart, err = a.inboundService.AddInboundClient(&data)
  155. if err != nil {
  156. jsonMsg(c, "Something went wrong!", err)
  157. return
  158. }
  159. }
  160. jsonMsg(c, "Client(s) added", nil)
  161. if err == nil && needRestart {
  162. a.xrayService.SetToNeedRestart()
  163. }
  164. }
  165. func (a *InboundController) addGroupInboundClient(c *gin.Context) {
  166. var requestData []model.Inbound
  167. err := c.ShouldBindJSON(&requestData)
  168. if err != nil {
  169. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  170. return
  171. }
  172. needRestart := true
  173. for _, data := range requestData {
  174. needRestart, err = a.inboundService.AddInboundClient(&data)
  175. if err != nil {
  176. jsonMsg(c, "Something went wrong!", err)
  177. return
  178. }
  179. }
  180. jsonMsg(c, "Client(s) added", nil)
  181. if err == nil && needRestart {
  182. a.xrayService.SetToNeedRestart()
  183. }
  184. }
  185. func (a *InboundController) delInboundClient(c *gin.Context) {
  186. id, err := strconv.Atoi(c.Param("id"))
  187. if err != nil {
  188. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  189. return
  190. }
  191. clientId := c.Param("clientId")
  192. needRestart := true
  193. needRestart, err = a.inboundService.DelInboundClient(id, clientId)
  194. if err != nil {
  195. jsonMsg(c, "Something went wrong!", err)
  196. return
  197. }
  198. jsonMsg(c, "Client deleted", nil)
  199. if err == nil && needRestart {
  200. a.xrayService.SetToNeedRestart()
  201. }
  202. }
  203. func (a *InboundController) updateInboundClient(c *gin.Context) {
  204. clientId := c.Param("clientId")
  205. inbound := &model.Inbound{}
  206. err := c.ShouldBind(inbound)
  207. if err != nil {
  208. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  209. return
  210. }
  211. needRestart := true
  212. needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
  213. if err != nil {
  214. jsonMsg(c, "Something went wrong!", err)
  215. return
  216. }
  217. jsonMsg(c, "Client updated", nil)
  218. if err == nil && needRestart {
  219. a.xrayService.SetToNeedRestart()
  220. }
  221. }
  222. func (a *InboundController) updateGroupInboundClient(c *gin.Context) {
  223. var requestData []map[string]interface{}
  224. if err := c.ShouldBindJSON(&requestData); err != nil {
  225. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  226. return
  227. }
  228. needRestart := false
  229. for _, item := range requestData {
  230. inboundMap, ok := item["inbound"].(map[string]interface{})
  231. if !ok {
  232. jsonMsg(c, "Something went wrong!", errors.New("Failed to convert 'inbound' to map"))
  233. return
  234. }
  235. clientId, ok := item["clientId"].(string)
  236. if !ok {
  237. jsonMsg(c, "Something went wrong!", errors.New("Failed to convert 'clientId' to string"))
  238. return
  239. }
  240. inboundJSON, err := json.Marshal(inboundMap)
  241. if err != nil {
  242. jsonMsg(c, "Something went wrong!", err)
  243. return
  244. }
  245. var inboundModel model.Inbound
  246. if err := json.Unmarshal(inboundJSON, &inboundModel); err != nil {
  247. jsonMsg(c, "Something went wrong!", err)
  248. return
  249. }
  250. if restart, err := a.inboundService.UpdateInboundClient(&inboundModel, clientId); err != nil {
  251. jsonMsg(c, "Something went wrong!", err)
  252. return
  253. } else {
  254. needRestart = needRestart || restart
  255. }
  256. }
  257. jsonMsg(c, "Client updated", nil)
  258. if needRestart {
  259. a.xrayService.SetToNeedRestart()
  260. }
  261. }
  262. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  263. id, err := strconv.Atoi(c.Param("id"))
  264. if err != nil {
  265. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  266. return
  267. }
  268. email := c.Param("email")
  269. needRestart := true
  270. needRestart, err = a.inboundService.ResetClientTraffic(id, email)
  271. if err != nil {
  272. jsonMsg(c, "Something went wrong!", err)
  273. return
  274. }
  275. jsonMsg(c, "traffic reseted", nil)
  276. if err == nil && needRestart {
  277. a.xrayService.SetToNeedRestart()
  278. }
  279. }
  280. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  281. err := a.inboundService.ResetAllTraffics()
  282. if err != nil {
  283. jsonMsg(c, "Something went wrong!", err)
  284. return
  285. } else {
  286. a.xrayService.SetToNeedRestart()
  287. }
  288. jsonMsg(c, "All traffics reseted", nil)
  289. }
  290. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  291. id, err := strconv.Atoi(c.Param("id"))
  292. if err != nil {
  293. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  294. return
  295. }
  296. err = a.inboundService.ResetAllClientTraffics(id)
  297. if err != nil {
  298. jsonMsg(c, "Something went wrong!", err)
  299. return
  300. } else {
  301. a.xrayService.SetToNeedRestart()
  302. }
  303. jsonMsg(c, "All traffics of client reseted", nil)
  304. }
  305. func (a *InboundController) importInbound(c *gin.Context) {
  306. inbound := &model.Inbound{}
  307. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  308. if err != nil {
  309. jsonMsg(c, "Something went wrong!", err)
  310. return
  311. }
  312. user := session.GetLoginUser(c)
  313. inbound.Id = 0
  314. inbound.UserId = user.Id
  315. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  316. inbound.Tag = fmt.Sprintf("inbound-0.0.0.0:%v", inbound.Port)
  317. } else {
  318. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  319. }
  320. for index := range inbound.ClientStats {
  321. inbound.ClientStats[index].Id = 0
  322. inbound.ClientStats[index].Enable = true
  323. }
  324. needRestart := false
  325. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  326. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
  327. if err == nil && needRestart {
  328. a.xrayService.SetToNeedRestart()
  329. }
  330. }
  331. func (a *InboundController) delDepletedClients(c *gin.Context) {
  332. id, err := strconv.Atoi(c.Param("id"))
  333. if err != nil {
  334. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  335. return
  336. }
  337. err = a.inboundService.DelDepletedClients(id)
  338. if err != nil {
  339. jsonMsg(c, "Something went wrong!", err)
  340. return
  341. }
  342. jsonMsg(c, "All delpeted clients are deleted", nil)
  343. }
  344. func (a *InboundController) onlines(c *gin.Context) {
  345. jsonObj(c, a.inboundService.GetOnlineClinets(), nil)
  346. }