Files
x-ui/web/controller/inbound.go
sprov 292d5b89d4 0.3.0
- 增加到期时间限制
 - 新增配置面板 https 访问后,http 自动跳转 https(同端口)
 - 降低获取系统连接数的 cpu 使用率
 - 优化界面
 - VMess 协议 alterId 默认改为 0
 - 修复旧版本 iOS 系统白屏问题
 - 修复重启面板后 xray 没有启动的问题
2021-07-26 13:29:29 +08:00

109 lines
2.3 KiB
Go

package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"strconv"
"x-ui/database/model"
"x-ui/logger"
"x-ui/web/global"
"x-ui/web/service"
"x-ui/web/session"
)
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)
}
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 {
jsonMsg(c, "获取", err)
return
}
jsonObj(c, inbounds, nil)
}
func (a *InboundController) addInbound(c *gin.Context) {
inbound := &model.Inbound{}
err := c.ShouldBind(inbound)
if err != nil {
jsonMsg(c, "添加", err)
return
}
user := session.GetLoginUser(c)
inbound.UserId = user.Id
inbound.Enable = true
inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
err = a.inboundService.AddInbound(inbound)
jsonMsg(c, "添加", err)
if err == nil {
a.xrayService.SetToNeedRestart()
}
}
func (a *InboundController) delInbound(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
jsonMsg(c, "删除", err)
return
}
err = a.inboundService.DelInbound(id)
jsonMsg(c, "删除", err)
if err == nil {
a.xrayService.SetToNeedRestart()
}
}
func (a *InboundController) updateInbound(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
jsonMsg(c, "修改", err)
return
}
inbound := &model.Inbound{
Id: id,
}
err = c.ShouldBind(inbound)
if err != nil {
jsonMsg(c, "修改", err)
return
}
err = a.inboundService.UpdateInbound(inbound)
jsonMsg(c, "修改", err)
if err == nil {
a.xrayService.SetToNeedRestart()
}
}