2023-02-09 22:18:06 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"x-ui/database/model"
|
|
|
|
"x-ui/logger"
|
|
|
|
"x-ui/web/global"
|
|
|
|
"x-ui/web/service"
|
|
|
|
"x-ui/web/session"
|
2023-02-18 15:37:32 +03:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-02-09 22:18:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type InboundController struct {
|
|
|
|
inboundService service.InboundService
|
|
|
|
xrayService service.XrayService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInboundController(g *gin.RouterGroup) *InboundController {
|
|
|
|
a := &InboundController{}
|
|
|
|
a.initRouter(g)
|
|
|
|
a.startTask()
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *InboundController) initRouter(g *gin.RouterGroup) {
|
|
|
|
g = g.Group("/inbound")
|
|
|
|
|
|
|
|
g.POST("/list", a.getInbounds)
|
|
|
|
g.POST("/add", a.addInbound)
|
|
|
|
g.POST("/del/:id", a.delInbound)
|
|
|
|
g.POST("/update/:id", a.updateInbound)
|
2023-02-28 22:54:29 +03:00
|
|
|
g.POST("/clientIps/:email", a.getClientIps)
|
|
|
|
g.POST("/clearClientIps/:email", a.clearClientIps)
|
2023-04-18 21:04:06 +03:00
|
|
|
g.POST("/addClient", a.addInboundClient)
|
2023-04-24 14:37:11 +03:00
|
|
|
g.POST("/:id/delClient/:clientId", a.delInboundClient)
|
2023-04-25 14:08:35 +03:00
|
|
|
g.POST("/updateClient/:clientId", a.updateInboundClient)
|
2023-03-17 19:07:49 +03:00
|
|
|
g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
|
2023-04-09 22:43:18 +03:00
|
|
|
g.POST("/resetAllTraffics", a.resetAllTraffics)
|
|
|
|
g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
|
2023-04-25 18:13:37 +03:00
|
|
|
g.POST("/delDepletedClients/:id", a.delDepletedClients)
|
2023-02-09 22:18:06 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *InboundController) startTask() {
|
|
|
|
webServer := global.GetWebServer()
|
|
|
|
c := webServer.GetCron()
|
|
|
|
c.AddFunc("@every 10s", func() {
|
|
|
|
if a.xrayService.IsNeedRestartAndSetFalse() {
|
|
|
|
err := a.xrayService.RestartXray(false)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("restart xray failed:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *InboundController) getInbounds(c *gin.Context) {
|
|
|
|
user := session.GetLoginUser(c)
|
|
|
|
inbounds, err := a.inboundService.GetInbounds(user.Id)
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonObj(c, inbounds, nil)
|
|
|
|
}
|
|
|
|
func (a *InboundController) getInbound(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "get"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
inbound, err := a.inboundService.GetInbound(id)
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.obtain"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonObj(c, inbound, nil)
|
|
|
|
}
|
|
|
|
|
2023-04-21 18:36:59 +03:00
|
|
|
func (a *InboundController) getClientTraffics(c *gin.Context) {
|
|
|
|
email := c.Param("email")
|
|
|
|
clientTraffics, err := a.inboundService.GetClientTrafficByEmail(email)
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, "Error getting traffics", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonObj(c, clientTraffics, nil)
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:18:06 +03:00
|
|
|
func (a *InboundController) addInbound(c *gin.Context) {
|
|
|
|
inbound := &model.Inbound{}
|
|
|
|
err := c.ShouldBind(inbound)
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.create"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user := session.GetLoginUser(c)
|
|
|
|
inbound.UserId = user.Id
|
|
|
|
inbound.Enable = true
|
|
|
|
inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
|
|
|
|
inbound, err = a.inboundService.AddInbound(inbound)
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
|
2023-02-09 22:18:06 +03:00
|
|
|
if err == nil {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *InboundController) delInbound(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "delete"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = a.inboundService.DelInbound(id)
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsgObj(c, I18nWeb(c, "delete"), id, err)
|
2023-02-09 22:18:06 +03:00
|
|
|
if err == nil {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *InboundController) updateInbound(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
inbound := &model.Inbound{
|
|
|
|
Id: id,
|
|
|
|
}
|
|
|
|
err = c.ShouldBind(inbound)
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
inbound, err = a.inboundService.UpdateInbound(inbound)
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsgObj(c, I18nWeb(c, "pages.inbounds.update"), inbound, err)
|
2023-02-09 22:18:06 +03:00
|
|
|
if err == nil {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 19:07:49 +03:00
|
|
|
|
2023-02-28 22:54:29 +03:00
|
|
|
func (a *InboundController) getClientIps(c *gin.Context) {
|
|
|
|
email := c.Param("email")
|
2023-02-09 22:18:06 +03:00
|
|
|
|
2023-04-09 22:43:18 +03:00
|
|
|
ips, err := a.inboundService.GetInboundClientIps(email)
|
2023-02-28 22:54:29 +03:00
|
|
|
if err != nil {
|
2023-05-22 17:01:41 +03:00
|
|
|
jsonObj(c, "Failed to get client IPs", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ips == "" {
|
2023-02-28 22:54:29 +03:00
|
|
|
jsonObj(c, "No IP Record", nil)
|
|
|
|
return
|
|
|
|
}
|
2023-05-22 17:01:41 +03:00
|
|
|
|
2023-02-28 22:54:29 +03:00
|
|
|
jsonObj(c, ips, nil)
|
|
|
|
}
|
2023-05-22 17:01:41 +03:00
|
|
|
|
2023-02-28 22:54:29 +03:00
|
|
|
func (a *InboundController) clearClientIps(c *gin.Context) {
|
|
|
|
email := c.Param("email")
|
|
|
|
|
|
|
|
err := a.inboundService.ClearClientIps(email)
|
|
|
|
if err != nil {
|
2023-05-04 20:52:54 +03:00
|
|
|
jsonMsg(c, "Update", err)
|
2023-02-28 22:54:29 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonMsg(c, "Log Cleared", nil)
|
|
|
|
}
|
2023-03-17 19:07:49 +03:00
|
|
|
func (a *InboundController) addInboundClient(c *gin.Context) {
|
2023-04-18 21:04:06 +03:00
|
|
|
data := &model.Inbound{}
|
|
|
|
err := c.ShouldBind(data)
|
2023-03-17 19:07:49 +03:00
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
|
2023-03-17 19:07:49 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-18 21:04:06 +03:00
|
|
|
err = a.inboundService.AddInboundClient(data)
|
2023-03-17 19:07:49 +03:00
|
|
|
if err != nil {
|
2023-04-25 18:13:37 +03:00
|
|
|
jsonMsg(c, "Something went wrong!", err)
|
2023-03-17 19:07:49 +03:00
|
|
|
return
|
|
|
|
}
|
2023-04-18 21:04:06 +03:00
|
|
|
jsonMsg(c, "Client(s) added", nil)
|
2023-03-17 19:07:49 +03:00
|
|
|
if err == nil {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *InboundController) delInboundClient(c *gin.Context) {
|
2023-04-24 14:37:11 +03:00
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
2023-03-17 19:07:49 +03:00
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
|
2023-03-17 19:07:49 +03:00
|
|
|
return
|
|
|
|
}
|
2023-04-24 14:37:11 +03:00
|
|
|
clientId := c.Param("clientId")
|
2023-03-17 19:07:49 +03:00
|
|
|
|
2023-04-24 14:37:11 +03:00
|
|
|
err = a.inboundService.DelInboundClient(id, clientId)
|
2023-03-17 19:07:49 +03:00
|
|
|
if err != nil {
|
2023-04-25 18:13:37 +03:00
|
|
|
jsonMsg(c, "Something went wrong!", err)
|
2023-03-17 19:07:49 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonMsg(c, "Client deleted", nil)
|
|
|
|
if err == nil {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *InboundController) updateInboundClient(c *gin.Context) {
|
2023-04-25 14:08:35 +03:00
|
|
|
clientId := c.Param("clientId")
|
2023-03-17 19:07:49 +03:00
|
|
|
|
|
|
|
inbound := &model.Inbound{}
|
2023-04-25 14:08:35 +03:00
|
|
|
err := c.ShouldBind(inbound)
|
2023-03-17 19:07:49 +03:00
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
|
2023-03-17 19:07:49 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 14:08:35 +03:00
|
|
|
err = a.inboundService.UpdateInboundClient(inbound, clientId)
|
2023-03-17 19:07:49 +03:00
|
|
|
if err != nil {
|
2023-04-25 18:13:37 +03:00
|
|
|
jsonMsg(c, "Something went wrong!", err)
|
2023-03-17 19:07:49 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonMsg(c, "Client updated", nil)
|
|
|
|
if err == nil {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:18:06 +03:00
|
|
|
func (a *InboundController) resetClientTraffic(c *gin.Context) {
|
2023-03-17 19:07:49 +03:00
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
|
2023-03-17 19:07:49 +03:00
|
|
|
return
|
|
|
|
}
|
2023-02-09 22:18:06 +03:00
|
|
|
email := c.Param("email")
|
|
|
|
|
2023-03-17 19:07:49 +03:00
|
|
|
err = a.inboundService.ResetClientTraffic(id, email)
|
2023-02-09 22:18:06 +03:00
|
|
|
if err != nil {
|
2023-04-25 18:13:37 +03:00
|
|
|
jsonMsg(c, "Something went wrong!", err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonMsg(c, "traffic reseted", nil)
|
2023-03-17 19:07:49 +03:00
|
|
|
if err == nil {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
|
|
|
}
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
2023-04-09 22:43:18 +03:00
|
|
|
|
|
|
|
func (a *InboundController) resetAllTraffics(c *gin.Context) {
|
|
|
|
err := a.inboundService.ResetAllTraffics()
|
|
|
|
if err != nil {
|
2023-04-25 18:13:37 +03:00
|
|
|
jsonMsg(c, "Something went wrong!", err)
|
2023-04-09 22:43:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonMsg(c, "All traffics reseted", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *InboundController) resetAllClientTraffics(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
|
2023-04-09 22:43:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = a.inboundService.ResetAllClientTraffics(id)
|
|
|
|
if err != nil {
|
2023-04-25 18:13:37 +03:00
|
|
|
jsonMsg(c, "Something went wrong!", err)
|
2023-04-09 22:43:18 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonMsg(c, "All traffics of client reseted", nil)
|
|
|
|
}
|
2023-04-25 18:13:37 +03:00
|
|
|
|
|
|
|
func (a *InboundController) delDepletedClients(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
|
2023-04-25 18:13:37 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = a.inboundService.DelDepletedClients(id)
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, "Something went wrong!", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonMsg(c, "All delpeted clients are deleted", nil)
|
|
|
|
}
|