inbound.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package controller
  2. import (
  3. "fmt"
  4. "strconv"
  5. "x-ui/database/model"
  6. "x-ui/logger"
  7. "x-ui/web/global"
  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. a.startTask()
  20. return a
  21. }
  22. func (a *InboundController) initRouter(g *gin.RouterGroup) {
  23. g = g.Group("/inbound")
  24. g.POST("/list", a.getInbounds)
  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("/delClient/:email", a.delInboundClient)
  32. g.POST("/updateClient/:index", a.updateInboundClient)
  33. g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
  34. g.POST("/resetAllTraffics", a.resetAllTraffics)
  35. g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
  36. }
  37. func (a *InboundController) startTask() {
  38. webServer := global.GetWebServer()
  39. c := webServer.GetCron()
  40. c.AddFunc("@every 10s", func() {
  41. if a.xrayService.IsNeedRestartAndSetFalse() {
  42. err := a.xrayService.RestartXray(false)
  43. if err != nil {
  44. logger.Error("restart xray failed:", err)
  45. }
  46. }
  47. })
  48. }
  49. func (a *InboundController) getInbounds(c *gin.Context) {
  50. user := session.GetLoginUser(c)
  51. inbounds, err := a.inboundService.GetInbounds(user.Id)
  52. if err != nil {
  53. jsonMsg(c, I18n(c, "pages.inbounds.toasts.obtain"), err)
  54. return
  55. }
  56. jsonObj(c, inbounds, nil)
  57. }
  58. func (a *InboundController) getInbound(c *gin.Context) {
  59. id, err := strconv.Atoi(c.Param("id"))
  60. if err != nil {
  61. jsonMsg(c, I18n(c, "get"), err)
  62. return
  63. }
  64. inbound, err := a.inboundService.GetInbound(id)
  65. if err != nil {
  66. jsonMsg(c, I18n(c, "pages.inbounds.toasts.obtain"), err)
  67. return
  68. }
  69. jsonObj(c, inbound, nil)
  70. }
  71. func (a *InboundController) addInbound(c *gin.Context) {
  72. inbound := &model.Inbound{}
  73. err := c.ShouldBind(inbound)
  74. if err != nil {
  75. jsonMsg(c, I18n(c, "pages.inbounds.addTo"), err)
  76. return
  77. }
  78. user := session.GetLoginUser(c)
  79. inbound.UserId = user.Id
  80. inbound.Enable = true
  81. inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  82. inbound, err = a.inboundService.AddInbound(inbound)
  83. jsonMsgObj(c, I18n(c, "pages.inbounds.addTo"), inbound, err)
  84. if err == nil {
  85. a.xrayService.SetToNeedRestart()
  86. }
  87. }
  88. func (a *InboundController) delInbound(c *gin.Context) {
  89. id, err := strconv.Atoi(c.Param("id"))
  90. if err != nil {
  91. jsonMsg(c, I18n(c, "delete"), err)
  92. return
  93. }
  94. err = a.inboundService.DelInbound(id)
  95. jsonMsgObj(c, I18n(c, "delete"), id, err)
  96. if err == nil {
  97. a.xrayService.SetToNeedRestart()
  98. }
  99. }
  100. func (a *InboundController) updateInbound(c *gin.Context) {
  101. id, err := strconv.Atoi(c.Param("id"))
  102. if err != nil {
  103. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  104. return
  105. }
  106. inbound := &model.Inbound{
  107. Id: id,
  108. }
  109. err = c.ShouldBind(inbound)
  110. if err != nil {
  111. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  112. return
  113. }
  114. inbound, err = a.inboundService.UpdateInbound(inbound)
  115. jsonMsgObj(c, I18n(c, "pages.inbounds.revise"), inbound, err)
  116. if err == nil {
  117. a.xrayService.SetToNeedRestart()
  118. }
  119. }
  120. func (a *InboundController) getClientIps(c *gin.Context) {
  121. email := c.Param("email")
  122. ips, err := a.inboundService.GetInboundClientIps(email)
  123. if err != nil {
  124. jsonObj(c, "No IP Record", nil)
  125. return
  126. }
  127. jsonObj(c, ips, nil)
  128. }
  129. func (a *InboundController) clearClientIps(c *gin.Context) {
  130. email := c.Param("email")
  131. err := a.inboundService.ClearClientIps(email)
  132. if err != nil {
  133. jsonMsg(c, "修改", err)
  134. return
  135. }
  136. jsonMsg(c, "Log Cleared", nil)
  137. }
  138. func (a *InboundController) addInboundClient(c *gin.Context) {
  139. inbound := &model.Inbound{}
  140. err := c.ShouldBind(inbound)
  141. if err != nil {
  142. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  143. return
  144. }
  145. err = a.inboundService.AddInboundClient(inbound)
  146. if err != nil {
  147. jsonMsg(c, "something worng!", err)
  148. return
  149. }
  150. jsonMsg(c, "Client added", nil)
  151. if err == nil {
  152. a.xrayService.SetToNeedRestart()
  153. }
  154. }
  155. func (a *InboundController) delInboundClient(c *gin.Context) {
  156. email := c.Param("email")
  157. inbound := &model.Inbound{}
  158. err := c.ShouldBind(inbound)
  159. if err != nil {
  160. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  161. return
  162. }
  163. err = a.inboundService.DelInboundClient(inbound, email)
  164. if err != nil {
  165. jsonMsg(c, "something worng!", err)
  166. return
  167. }
  168. jsonMsg(c, "Client deleted", nil)
  169. if err == nil {
  170. a.xrayService.SetToNeedRestart()
  171. }
  172. }
  173. func (a *InboundController) updateInboundClient(c *gin.Context) {
  174. index, err := strconv.Atoi(c.Param("index"))
  175. if err != nil {
  176. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  177. return
  178. }
  179. inbound := &model.Inbound{}
  180. err = c.ShouldBind(inbound)
  181. if err != nil {
  182. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  183. return
  184. }
  185. err = a.inboundService.UpdateInboundClient(inbound, index)
  186. if err != nil {
  187. jsonMsg(c, "something worng!", err)
  188. return
  189. }
  190. jsonMsg(c, "Client updated", nil)
  191. if err == nil {
  192. a.xrayService.SetToNeedRestart()
  193. }
  194. }
  195. func (a *InboundController) resetClientTraffic(c *gin.Context) {
  196. id, err := strconv.Atoi(c.Param("id"))
  197. if err != nil {
  198. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  199. return
  200. }
  201. email := c.Param("email")
  202. err = a.inboundService.ResetClientTraffic(id, email)
  203. if err != nil {
  204. jsonMsg(c, "something worng!", err)
  205. return
  206. }
  207. jsonMsg(c, "traffic reseted", nil)
  208. if err == nil {
  209. a.xrayService.SetToNeedRestart()
  210. }
  211. }
  212. func (a *InboundController) resetAllTraffics(c *gin.Context) {
  213. err := a.inboundService.ResetAllTraffics()
  214. if err != nil {
  215. jsonMsg(c, "something worng!", err)
  216. return
  217. }
  218. jsonMsg(c, "All traffics reseted", nil)
  219. }
  220. func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
  221. id, err := strconv.Atoi(c.Param("id"))
  222. if err != nil {
  223. jsonMsg(c, I18n(c, "pages.inbounds.revise"), err)
  224. return
  225. }
  226. err = a.inboundService.ResetAllClientTraffics(id)
  227. if err != nil {
  228. jsonMsg(c, "something worng!", err)
  229. return
  230. }
  231. jsonMsg(c, "All traffics of client reseted", nil)
  232. }