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"
|
|
|
|
"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)
|
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-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)
|
|
|
|
}
|
2023-07-01 15:26:43 +03:00
|
|
|
|
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
|
2024-01-17 15:51:28 +03:00
|
|
|
if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
|
|
|
|
inbound.Tag = fmt.Sprintf("inbound-0.0.0.0:%v", inbound.Port)
|
|
|
|
} else {
|
|
|
|
inbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
|
|
|
|
}
|
2023-07-18 02:10:22 +03:00
|
|
|
|
|
|
|
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)
|
2023-07-18 02:10:22 +03:00
|
|
|
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
|
|
|
|
}
|
2023-07-18 02:10:22 +03:00
|
|
|
needRestart := true
|
|
|
|
needRestart, err = a.inboundService.DelInbound(id)
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsgObj(c, I18nWeb(c, "delete"), id, err)
|
2023-07-18 02:10:22 +03:00
|
|
|
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
|
|
|
|
}
|
2023-07-18 02:10:22 +03:00
|
|
|
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)
|
2023-07-18 02:10:22 +03:00
|
|
|
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
|
|
|
|
2023-04-09 22:43:18 +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-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-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{}
|
2024-01-26 21:37:15 +03:00
|
|
|
err := c.ShouldBind(data)
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, I18nWeb(c, "pages.inbounds.update"), err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-22 14:36:01 +03:00
|
|
|
|
2024-01-26 21:37:15 +03:00
|
|
|
needRestart := true
|
2024-01-22 14:36:01 +03:00
|
|
|
|
2024-01-26 21:37:15 +03:00
|
|
|
needRestart, err = a.inboundService.AddInboundClient(data)
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, "Something went wrong!", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonMsg(c, "Client(s) added", nil)
|
|
|
|
if err == nil && needRestart {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
|
|
|
}
|
2024-01-22 14:36:01 +03:00
|
|
|
}
|
|
|
|
|
2023-03-17 19:07:49 +03:00
|
|
|
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-07-18 02:10:22 +03:00
|
|
|
needRestart := true
|
2023-06-05 00:02:19 +03:00
|
|
|
|
|
|
|
needRestart, 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)
|
2023-06-05 00:02:19 +03:00
|
|
|
if err == nil && needRestart {
|
2023-03-17 19:07:49 +03:00
|
|
|
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-07-18 02:10:22 +03:00
|
|
|
needRestart := true
|
2023-06-05 00:02:19 +03:00
|
|
|
|
|
|
|
needRestart, 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)
|
2023-06-05 00:02:19 +03:00
|
|
|
if err == nil && 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")
|
|
|
|
|
2023-07-18 02:10:22 +03:00
|
|
|
needRestart := true
|
2023-06-05 00:02:19 +03:00
|
|
|
|
|
|
|
needRestart, 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-06-05 00:02:19 +03:00
|
|
|
if err == nil && needRestart {
|
2023-03-17 19:07:49 +03:00
|
|
|
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
|
2023-06-05 00:02:19 +03:00
|
|
|
} else {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
2023-04-09 22:43:18 +03:00
|
|
|
}
|
|
|
|
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
|
2023-06-05 00:02:19 +03:00
|
|
|
} else {
|
|
|
|
a.xrayService.SetToNeedRestart()
|
2023-04-09 22:43:18 +03:00
|
|
|
}
|
|
|
|
jsonMsg(c, "All traffics of client reseted", nil)
|
|
|
|
}
|
2023-04-25 18:13:37 +03:00
|
|
|
|
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
|
2024-01-17 15:51:28 +03:00
|
|
|
if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
|
|
|
|
inbound.Tag = fmt.Sprintf("inbound-0.0.0.0:%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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2023-12-04 21:13:21 +03:00
|
|
|
|
|
|
|
func (a *InboundController) onlines(c *gin.Context) {
|
|
|
|
jsonObj(c, a.inboundService.GetOnlineClinets(), nil)
|
|
|
|
}
|