3x-ui/web/controller/inbound.go

330 lines
8.2 KiB
Go
Raw Normal View History

2023-02-09 22:18:06 +03:00
package controller
import (
2023-12-08 22:08:44 +03:00
"encoding/json"
2023-02-09 22:18:06 +03:00
"fmt"
"strconv"
2023-02-09 22:18:06 +03:00
"x-ui/database/model"
"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)
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)
g.POST("/addClient", a.addInboundClient)
g.POST("/:id/delClient/:clientId", a.delInboundClient)
g.POST("/updateClient/:clientId", a.updateInboundClient)
2023-03-17 19:07:49 +03:00
g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
g.POST("/resetAllTraffics", a.resetAllTraffics)
g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
g.POST("/delDepletedClients/:id", a.delDepletedClients)
2023-12-08 22:08:44 +03:00
g.POST("/import", a.importInbound)
2023-12-04 21:13:21 +03:00
g.POST("/onlines", a.onlines)
2023-02-09 22:18:06 +03:00
}
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)
}
2023-05-30 23:51:14 +03:00
2023-02-09 22:18:06 +03:00
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)
}
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)
}
func (a *InboundController) getClientTrafficsById(c *gin.Context) {
id := c.Param("id")
clientTraffics, err := a.inboundService.GetClientTrafficByID(id)
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
if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
2024-02-21 21:50:51 +03:00
inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
} else {
inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
}
needRestart := false
inbound, needRestart, err = a.inboundService.AddInbound(inbound)
2023-05-21 01:59:27 +03:00
jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
if err == nil && needRestart {
2023-02-09 22:18:06 +03:00
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
}
needRestart := true
needRestart, err = a.inboundService.DelInbound(id)
2023-05-21 01:59:27 +03:00
jsonMsgObj(c, I18nWeb(c, "delete"), id, err)
if err == nil && needRestart {
2023-02-09 22:18:06 +03:00
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
}
needRestart := true
inbound, needRestart, err = a.inboundService.UpdateInbound(inbound)
2023-05-21 01:59:27 +03:00
jsonMsgObj(c, I18nWeb(c, "pages.inbounds.update"), inbound, err)
if err == nil && needRestart {
2023-02-09 22:18:06 +03:00
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
ips, err := a.inboundService.GetInboundClientIps(email)
2023-06-14 19:20:19 +03:00
if err != nil || ips == "" {
2023-02-28 22:54:29 +03:00
jsonObj(c, "No IP Record", nil)
return
}
2023-02-28 22:54:29 +03:00
jsonObj(c, ips, nil)
}
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-05-30 23:51:14 +03:00
2023-03-17 19:07:49 +03:00
func (a *InboundController) addInboundClient(c *gin.Context) {
2024-01-23 13:00:21 +03:00
data := &model.Inbound{}
err := c.ShouldBind(data)
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
return
}
needRestart := true
needRestart, err = a.inboundService.AddInboundClient(data)
if err != nil {
jsonMsg(c, "Something went wrong!", err)
return
}
jsonMsg(c, "Client(s) added", nil)
if needRestart {
a.xrayService.SetToNeedRestart()
}
}
2023-03-17 19:07:49 +03:00
func (a *InboundController) delInboundClient(c *gin.Context) {
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
}
clientId := c.Param("clientId")
2023-03-17 19:07:49 +03:00
needRestart := true
needRestart, err = a.inboundService.DelInboundClient(id, clientId)
2023-03-17 19:07:49 +03:00
if err != nil {
jsonMsg(c, "Something went wrong!", err)
2023-03-17 19:07:49 +03:00
return
}
jsonMsg(c, "Client deleted", nil)
if needRestart {
2023-03-17 19:07:49 +03:00
a.xrayService.SetToNeedRestart()
}
}
func (a *InboundController) updateInboundClient(c *gin.Context) {
clientId := c.Param("clientId")
2023-03-17 19:07:49 +03:00
inbound := &model.Inbound{}
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
}
needRestart := true
needRestart, err = a.inboundService.UpdateInboundClient(inbound, clientId)
2023-03-17 19:07:49 +03:00
if err != nil {
jsonMsg(c, "Something went wrong!", err)
2023-03-17 19:07:49 +03:00
return
}
jsonMsg(c, "Client updated", nil)
if needRestart {
2023-03-17 19:07:49 +03:00
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")
2024-07-07 12:55:59 +03:00
needRestart, err := a.inboundService.ResetClientTraffic(id, email)
2023-02-09 22:18:06 +03:00
if err != nil {
jsonMsg(c, "Something went wrong!", err)
2023-02-09 22:18:06 +03:00
return
}
2024-07-07 12:55:59 +03:00
jsonMsg(c, "Traffic has been reset", nil)
if needRestart {
2023-03-17 19:07:49 +03:00
a.xrayService.SetToNeedRestart()
}
2023-02-09 22:18:06 +03:00
}
func (a *InboundController) resetAllTraffics(c *gin.Context) {
err := a.inboundService.ResetAllTraffics()
if err != nil {
jsonMsg(c, "Something went wrong!", err)
return
} else {
a.xrayService.SetToNeedRestart()
}
2024-07-07 12:55:59 +03:00
jsonMsg(c, "all traffic has been reset", 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)
return
}
err = a.inboundService.ResetAllClientTraffics(id)
if err != nil {
jsonMsg(c, "Something went wrong!", err)
return
} else {
a.xrayService.SetToNeedRestart()
}
2024-07-07 12:55:59 +03:00
jsonMsg(c, "All traffic from the client has been reset.", nil)
}
2023-12-08 22:08:44 +03:00
func (a *InboundController) importInbound(c *gin.Context) {
inbound := &model.Inbound{}
err := json.Unmarshal([]byte(c.PostForm("data")), inbound)
if err != nil {
jsonMsg(c, "Something went wrong!", err)
return
}
user := session.GetLoginUser(c)
inbound.Id = 0
inbound.UserId = user.Id
if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
2024-02-21 21:50:51 +03:00
inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
} else {
inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
}
2023-12-08 22:08:44 +03:00
for index := range inbound.ClientStats {
inbound.ClientStats[index].Id = 0
inbound.ClientStats[index].Enable = true
}
needRestart := false
inbound, needRestart, err = a.inboundService.AddInbound(inbound)
jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err)
if err == nil && needRestart {
a.xrayService.SetToNeedRestart()
}
}
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)
return
}
err = a.inboundService.DelDepletedClients(id)
if err != nil {
jsonMsg(c, "Something went wrong!", err)
return
}
2024-07-07 12:55:59 +03:00
jsonMsg(c, "All depleted clients are deleted", nil)
}
2023-12-04 21:13:21 +03:00
func (a *InboundController) onlines(c *gin.Context) {
2024-07-07 12:55:59 +03:00
jsonObj(c, a.inboundService.GetOnlineClients(), nil)
2023-12-04 21:13:21 +03:00
}