inbound.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. data := &model.Inbound{}
  147. err := c.ShouldBind(data)
  148. if err != nil {
  149. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  150. return
  151. }
  152. needRestart := true
  153. needRestart, err = a.inboundService.AddInboundClient(data)
  154. if err != nil {
  155. jsonMsg(c, "Something went wrong!", err)
  156. return
  157. }
  158. jsonMsg(c, "Client(s) added", nil)
  159. if err == nil && needRestart {
  160. a.xrayService.SetToNeedRestart()
  161. }
  162. }
  163. func (a *InboundController) addGroupInboundClient(c *gin.Context) {
  164. var requestData []model.Inbound
  165. err := c.ShouldBindJSON(&requestData)
  166. if err != nil {
  167. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  168. return
  169. }
  170. needRestart := true
  171. for _, data := range requestData {
  172. needRestart, err = a.inboundService.AddInboundClient(&data)
  173. if err != nil {
  174. jsonMsg(c, "Something went wrong!", err)
  175. return
  176. }
  177. }
  178. jsonMsg(c, "Client(s) added", nil)
  179. if err == nil && needRestart {
  180. a.xrayService.SetToNeedRestart()
  181. }
  182. }
  183. func (a *InboundController) delInboundClient(c *gin.Context) {
  184. id, err := strconv.Atoi(c.Param("id"))
  185. if err != nil {
  186. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  187. return
  188. }
  189. clientId := c.Param("clientId")
  190. needRestart := true
  191. needRestart, err = a.inboundService.DelInboundClient(id, clientId)
  192. if err != nil {
  193. jsonMsg(c, "Something went wrong!", err)
  194. return
  195. }
  196. jsonMsg(c, "Client deleted", nil)
  197. if err == nil && needRestart {
  198. a.xrayService.SetToNeedRestart()
  199. }
  200. }
  201. func (a *InboundController) updateInboundClient(c *gin.Context) {
  202. clientId := c.Param("clientId")
  203. inbound := &model.Inbound{}
  204. err := c.ShouldBind(inbound)
  205. if err != nil {
  206. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  207. return
  208. }
  209. needRestart := true
  210. needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
  211. if err != nil {
  212. jsonMsg(c, "Something went wrong!", err)
  213. return
  214. }
  215. jsonMsg(c, "Client updated", nil)
  216. if err == nil && needRestart {
  217. a.xrayService.SetToNeedRestart()
  218. }
  219. }
  220. func (a *InboundController) updateGroupInboundClient(c *gin.Context) {
  221. var requestData []map[string]interface{}
  222. if err := c.ShouldBindJSON(&requestData); err != nil {
  223. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  224. return
  225. }
  226. needRestart := false
  227. for _, item := range requestData {
  228. inboundMap, ok := item["inbound"].(map[string]interface{})
  229. if !ok {
  230. jsonMsg(c, "Something went wrong!", errors.New("Failed to convert 'inbound' to map"))
  231. return
  232. }
  233. clientId, ok := item["clientId"].(string)
  234. if !ok {
  235. jsonMsg(c, "Something went wrong!", errors.New("Failed to convert 'clientId' to string"))
  236. return
  237. }
  238. inboundJSON, err := json.Marshal(inboundMap)
  239. if err != nil {
  240. jsonMsg(c, "Something went wrong!", err)
  241. return
  242. }
  243. var inboundModel model.Inbound
  244. if err := json.Unmarshal(inboundJSON, &inboundModel); err != nil {
  245. jsonMsg(c, "Something went wrong!", err)
  246. return
  247. }
  248. if restart, err := a.inboundService.UpdateInboundClient(&inboundModel, clientId); err != nil {
  249. jsonMsg(c, "Something went wrong!", err)
  250. return
  251. } else {
  252. needRestart = needRestart || restart
  253. }
  254. }
  255. jsonMsg(c, "Client updated", nil)
  256. if needRestart {
  257. a.xrayService.SetToNeedRestart()
  258. }
  259. }
  260. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  261. id, err := strconv.Atoi(c.Param("id"))
  262. if err != nil {
  263. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  264. return
  265. }
  266. email := c.Param("email")
  267. needRestart := true
  268. needRestart, err = a.inboundService.ResetClientTraffic(id, email)
  269. if err != nil {
  270. jsonMsg(c, "Something went wrong!", err)
  271. return
  272. }
  273. jsonMsg(c, "traffic reseted", nil)
  274. if err == nil && needRestart {
  275. a.xrayService.SetToNeedRestart()
  276. }
  277. }
  278. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  279. err := a.inboundService.ResetAllTraffics()
  280. if err != nil {
  281. jsonMsg(c, "Something went wrong!", err)
  282. return
  283. } else {
  284. a.xrayService.SetToNeedRestart()
  285. }
  286. jsonMsg(c, "All traffics reseted", nil)
  287. }
  288. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  289. id, err := strconv.Atoi(c.Param("id"))
  290. if err != nil {
  291. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  292. return
  293. }
  294. err = a.inboundService.ResetAllClientTraffics(id)
  295. if err != nil {
  296. jsonMsg(c, "Something went wrong!", err)
  297. return
  298. } else {
  299. a.xrayService.SetToNeedRestart()
  300. }
  301. jsonMsg(c, "All traffics of client reseted", nil)
  302. }
  303. func (a *InboundController) importInbound(c *gin.Context) {
  304. inbound := &model.Inbound{}
  305. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  306. if err != nil {
  307. jsonMsg(c, "Something went wrong!", err)
  308. return
  309. }
  310. user := session.GetLoginUser(c)
  311. inbound.Id = 0
  312. inbound.UserId = user.Id
  313. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  314. inbound.Tag = fmt.Sprintf("inbound-0.0.0.0:%v", inbound.Port)
  315. } else {
  316. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  317. }
  318. for index := range inbound.ClientStats {
  319. inbound.ClientStats[index].Id = 0
  320. inbound.ClientStats[index].Enable = true
  321. }
  322. needRestart := false
  323. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  324. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
  325. if err == nil && needRestart {
  326. a.xrayService.SetToNeedRestart()
  327. }
  328. }
  329. func (a *InboundController) delDepletedClients(c *gin.Context) {
  330. id, err := strconv.Atoi(c.Param("id"))
  331. if err != nil {
  332. jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
  333. return
  334. }
  335. err = a.inboundService.DelDepletedClients(id)
  336. if err != nil {
  337. jsonMsg(c, "Something went wrong!", err)
  338. return
  339. }
  340. jsonMsg(c, "All delpeted clients are deleted", nil)
  341. }
  342. func (a *InboundController) onlines(c *gin.Context) {
  343. jsonObj(c, a.inboundService.GetOnlineClinets(), nil)
  344. }