inbound.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "github.com/mhsanaei/3x-ui/v2/database/model"
  7. "github.com/mhsanaei/3x-ui/v2/web/service"
  8. "github.com/mhsanaei/3x-ui/v2/web/session"
  9. "github.com/gin-gonic/gin"
  10. )
  11. type InboundController struct {
  12. inboundService service.InboundService
  13. xrayService service.XrayService
  14. }
  15. func NewInboundController(g *gin.RouterGroup) *InboundController {
  16. a := &InboundController{}
  17. a.initRouter(g)
  18. return a
  19. }
  20. func (a *InboundController) initRouter(g *gin.RouterGroup) {
  21. g.GET("/list", a.getInbounds)
  22. g.GET("/get/:id", a.getInbound)
  23. g.GET("/getClientTraffics/:email", a.getClientTraffics)
  24. g.GET("/getClientTrafficsById/:id", a.getClientTrafficsById)
  25. g.POST("/add", a.addInbound)
  26. g.POST("/del/:id", a.delInbound)
  27. g.POST("/update/:id", a.updateInbound)
  28. g.POST("/clientIps/:email", a.getClientIps)
  29. g.POST("/clearClientIps/:email", a.clearClientIps)
  30. g.POST("/addClient", a.addInboundClient)
  31. g.POST("/:id/delClient/:clientId", a.delInboundClient)
  32. g.POST("/updateClient/:clientId", a.updateInboundClient)
  33. g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
  34. g.POST("/resetAllTraffics", a.resetAllTraffics)
  35. g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
  36. g.POST("/delDepletedClients/:id", a.delDepletedClients)
  37. g.POST("/import", a.importInbound)
  38. g.POST("/onlines", a.onlines)
  39. g.POST("/lastOnline", a.lastOnline)
  40. g.POST("/updateClientTraffic/:email", a.updateClientTraffic)
  41. g.POST("/:id/delClientByEmail/:email", a.delInboundClientByEmail)
  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, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), 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, I18nWeb(c, "pages.inbounds.toasts.trafficGetError"), 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.toasts.inboundCreateSuccess"), 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. inbound, needRestart, err := a.inboundService.AddInbound(inbound)
  98. if err != nil {
  99. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  100. return
  101. }
  102. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, nil)
  103. if needRestart {
  104. a.xrayService.SetToNeedRestart()
  105. }
  106. }
  107. func (a *InboundController) delInbound(c *gin.Context) {
  108. id, err := strconv.Atoi(c.Param("id"))
  109. if err != nil {
  110. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), err)
  111. return
  112. }
  113. needRestart, err := a.inboundService.DelInbound(id)
  114. if err != nil {
  115. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  116. return
  117. }
  118. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundDeleteSuccess"), id, nil)
  119. if needRestart {
  120. a.xrayService.SetToNeedRestart()
  121. }
  122. }
  123. func (a *InboundController) updateInbound(c *gin.Context) {
  124. id, err := strconv.Atoi(c.Param("id"))
  125. if err != nil {
  126. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  127. return
  128. }
  129. inbound := &model.Inbound{
  130. Id: id,
  131. }
  132. err = c.ShouldBind(inbound)
  133. if err != nil {
  134. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  135. return
  136. }
  137. inbound, needRestart, err := a.inboundService.UpdateInbound(inbound)
  138. if err != nil {
  139. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  140. return
  141. }
  142. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), inbound, nil)
  143. if needRestart {
  144. a.xrayService.SetToNeedRestart()
  145. }
  146. }
  147. func (a *InboundController) getClientIps(c *gin.Context) {
  148. email := c.Param("email")
  149. ips, err := a.inboundService.GetInboundClientIps(email)
  150. if err != nil || ips == "" {
  151. jsonObj(c, "No IP Record", nil)
  152. return
  153. }
  154. jsonObj(c, ips, nil)
  155. }
  156. func (a *InboundController) clearClientIps(c *gin.Context) {
  157. email := c.Param("email")
  158. err := a.inboundService.ClearClientIps(email)
  159. if err != nil {
  160. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.updateSuccess"), err)
  161. return
  162. }
  163. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.logCleanSuccess"), nil)
  164. }
  165. func (a *InboundController) addInboundClient(c *gin.Context) {
  166. data := &model.Inbound{}
  167. err := c.ShouldBind(data)
  168. if err != nil {
  169. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  170. return
  171. }
  172. needRestart, err := a.inboundService.AddInboundClient(data)
  173. if err != nil {
  174. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  175. return
  176. }
  177. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientAddSuccess"), nil)
  178. if needRestart {
  179. a.xrayService.SetToNeedRestart()
  180. }
  181. }
  182. func (a *InboundController) delInboundClient(c *gin.Context) {
  183. id, err := strconv.Atoi(c.Param("id"))
  184. if err != nil {
  185. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  186. return
  187. }
  188. clientId := c.Param("clientId")
  189. needRestart, err := a.inboundService.DelInboundClient(id, clientId)
  190. if err != nil {
  191. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  192. return
  193. }
  194. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientDeleteSuccess"), nil)
  195. if needRestart {
  196. a.xrayService.SetToNeedRestart()
  197. }
  198. }
  199. func (a *InboundController) updateInboundClient(c *gin.Context) {
  200. clientId := c.Param("clientId")
  201. inbound := &model.Inbound{}
  202. err := c.ShouldBind(inbound)
  203. if err != nil {
  204. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  205. return
  206. }
  207. needRestart, err := a.inboundService.UpdateInboundClient(inbound, clientId)
  208. if err != nil {
  209. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  210. return
  211. }
  212. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  213. if needRestart {
  214. a.xrayService.SetToNeedRestart()
  215. }
  216. }
  217. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  218. id, err := strconv.Atoi(c.Param("id"))
  219. if err != nil {
  220. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  221. return
  222. }
  223. email := c.Param("email")
  224. needRestart, err := a.inboundService.ResetClientTraffic(id, email)
  225. if err != nil {
  226. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  227. return
  228. }
  229. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetInboundClientTrafficSuccess"), nil)
  230. if needRestart {
  231. a.xrayService.SetToNeedRestart()
  232. }
  233. }
  234. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  235. err := a.inboundService.ResetAllTraffics()
  236. if err != nil {
  237. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  238. return
  239. } else {
  240. a.xrayService.SetToNeedRestart()
  241. }
  242. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllTrafficSuccess"), nil)
  243. }
  244. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  245. id, err := strconv.Atoi(c.Param("id"))
  246. if err != nil {
  247. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  248. return
  249. }
  250. err = a.inboundService.ResetAllClientTraffics(id)
  251. if err != nil {
  252. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  253. return
  254. } else {
  255. a.xrayService.SetToNeedRestart()
  256. }
  257. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.resetAllClientTrafficSuccess"), nil)
  258. }
  259. func (a *InboundController) importInbound(c *gin.Context) {
  260. inbound := &model.Inbound{}
  261. err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
  262. if err != nil {
  263. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  264. return
  265. }
  266. user := session.GetLoginUser(c)
  267. inbound.Id = 0
  268. inbound.UserId = user.Id
  269. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  270. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  271. } else {
  272. inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  273. }
  274. for index := range inbound.ClientStats {
  275. inbound.ClientStats[index].Id = 0
  276. inbound.ClientStats[index].Enable = true
  277. }
  278. needRestart := false
  279. inbound, needRestart, err = a.inboundService.AddInbound(inbound)
  280. jsonMsgObj(c, I18nWeb(c, "pages.inbounds.toasts.inboundCreateSuccess"), inbound, err)
  281. if err == nil && needRestart {
  282. a.xrayService.SetToNeedRestart()
  283. }
  284. }
  285. func (a *InboundController) delDepletedClients(c *gin.Context) {
  286. id, err := strconv.Atoi(c.Param("id"))
  287. if err != nil {
  288. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  289. return
  290. }
  291. err = a.inboundService.DelDepletedClients(id)
  292. if err != nil {
  293. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  294. return
  295. }
  296. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.delDepletedClientsSuccess"), nil)
  297. }
  298. func (a *InboundController) onlines(c *gin.Context) {
  299. jsonObj(c, a.inboundService.GetOnlineClients(), nil)
  300. }
  301. func (a *InboundController) lastOnline(c *gin.Context) {
  302. data, err := a.inboundService.GetClientsLastOnline()
  303. jsonObj(c, data, err)
  304. }
  305. func (a *InboundController) updateClientTraffic(c *gin.Context) {
  306. email := c.Param("email")
  307. // Define the request structure for traffic update
  308. type TrafficUpdateRequest struct {
  309. Upload int64 `json:"upload"`
  310. Download int64 `json:"download"`
  311. }
  312. var request TrafficUpdateRequest
  313. err := c.ShouldBindJSON(&request)
  314. if err != nil {
  315. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
  316. return
  317. }
  318. err = a.inboundService.UpdateClientTrafficByEmail(email, request.Upload, request.Download)
  319. if err != nil {
  320. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  321. return
  322. }
  323. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
  324. }
  325. func (a *InboundController) delInboundClientByEmail(c *gin.Context) {
  326. inboundId, err := strconv.Atoi(c.Param("id"))
  327. if err != nil {
  328. jsonMsg(c, "Invalid inbound ID", err)
  329. return
  330. }
  331. email := c.Param("email")
  332. needRestart, err := a.inboundService.DelInboundClientByEmail(inboundId, email)
  333. if err != nil {
  334. jsonMsg(c, "Failed to delete client by email", err)
  335. return
  336. }
  337. jsonMsg(c, "Client deleted successfully", nil)
  338. if needRestart {
  339. a.xrayService.SetToNeedRestart()
  340. }
  341. }