更新大量功能

This commit is contained in:
sprov
2021-05-28 17:32:51 +08:00
parent 3cd25ce5ea
commit a66dff6959
41 changed files with 1231 additions and 1009 deletions

View File

@@ -2,15 +2,20 @@ package controller
import (
"github.com/gin-gonic/gin"
"net/http"
"x-ui/web/session"
)
type BaseController struct {
}
func (a *BaseController) before(c *gin.Context) {
func (a *BaseController) checkLogin(c *gin.Context) {
if !session.IsLogin(c) {
pureJsonMsg(c, false, "登录时效已过,请重新登录")
if isAjax(c) {
pureJsonMsg(c, false, "登录时效已过,请重新登录")
} else {
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
}
c.Abort()
} else {
c.Next()

View File

@@ -32,6 +32,10 @@ func (a *IndexController) initRouter(g *gin.RouterGroup) {
}
func (a *IndexController) index(c *gin.Context) {
if session.IsLogin(c) {
c.Redirect(http.StatusTemporaryRedirect, "xui/")
return
}
html(c, "login.html", "登录", nil)
}
@@ -68,5 +72,5 @@ func (a *IndexController) logout(c *gin.Context) {
logger.Info("user", user.Id, "logout")
}
session.ClearSession(c)
c.Redirect(http.StatusTemporaryRedirect, "")
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
}

View File

@@ -1,37 +1,17 @@
package controller
import (
"context"
"github.com/gin-gonic/gin"
"runtime"
"time"
"x-ui/web/global"
"x-ui/web/service"
)
func stopServerController(a *ServerController) {
a.stopTask()
}
type ServerController struct {
*serverController
}
func NewServerController(g *gin.RouterGroup) *ServerController {
a := &ServerController{
serverController: newServerController(g),
}
runtime.SetFinalizer(a, stopServerController)
return a
}
type serverController struct {
BaseController
serverService service.ServerService
ctx context.Context
cancel context.CancelFunc
lastStatus *service.Status
lastGetStatusTime time.Time
@@ -39,11 +19,8 @@ type serverController struct {
lastGetVersionsTime time.Time
}
func newServerController(g *gin.RouterGroup) *serverController {
ctx, cancel := context.WithCancel(context.Background())
a := &serverController{
ctx: ctx,
cancel: cancel,
func NewServerController(g *gin.RouterGroup) *ServerController {
a := &ServerController{
lastGetStatusTime: time.Now(),
}
a.initRouter(g)
@@ -51,23 +28,28 @@ func newServerController(g *gin.RouterGroup) *serverController {
return a
}
func (a *serverController) initRouter(g *gin.RouterGroup) {
g.POST("/server/status", a.status)
g.POST("/server/getXrayVersion", a.getXrayVersion)
g.POST("/server/installXray/:version", a.installXray)
func (a *ServerController) initRouter(g *gin.RouterGroup) {
g = g.Group("/server")
g.Use(a.checkLogin)
g.POST("/status", a.status)
g.POST("/getXrayVersion", a.getXrayVersion)
g.POST("/installXray/:version", a.installXray)
}
func (a *serverController) refreshStatus() {
func (a *ServerController) refreshStatus() {
status := a.serverService.GetStatus(a.lastStatus)
a.lastStatus = status
}
func (a *serverController) startTask() {
func (a *ServerController) startTask() {
webServer := global.GetWebServer()
ctx := webServer.GetCtx()
go func() {
for {
select {
case <-a.ctx.Done():
break
case <-ctx.Done():
return
default:
}
now := time.Now()
@@ -80,17 +62,13 @@ func (a *serverController) startTask() {
}()
}
func (a *serverController) stopTask() {
a.cancel()
}
func (a *serverController) status(c *gin.Context) {
func (a *ServerController) status(c *gin.Context) {
a.lastGetStatusTime = time.Now()
jsonObj(c, a.lastStatus, nil)
}
func (a *serverController) getXrayVersion(c *gin.Context) {
func (a *ServerController) getXrayVersion(c *gin.Context) {
now := time.Now()
if now.Sub(a.lastGetVersionsTime) <= time.Minute {
jsonObj(c, a.lastVersions, nil)
@@ -109,7 +87,7 @@ func (a *serverController) getXrayVersion(c *gin.Context) {
jsonObj(c, versions, nil)
}
func (a *serverController) installXray(c *gin.Context) {
func (a *ServerController) installXray(c *gin.Context) {
version := c.Param("version")
err := a.serverService.UpdateXray(version)
jsonMsg(c, "安装 xray", err)

View File

@@ -2,9 +2,12 @@ package controller
import (
"github.com/gin-gonic/gin"
"go.uber.org/atomic"
"log"
"strconv"
"x-ui/database/model"
"x-ui/logger"
"x-ui/web/global"
"x-ui/web/service"
"x-ui/web/session"
)
@@ -13,25 +16,45 @@ type XUIController struct {
BaseController
inboundService service.InboundService
xrayService service.XrayService
isNeedXrayRestart atomic.Bool
}
func NewXUIController(g *gin.RouterGroup) *XUIController {
a := &XUIController{}
a.initRouter(g)
a.startTask()
return a
}
func (a *XUIController) initRouter(g *gin.RouterGroup) {
g = g.Group("/xui")
g.Use(a.checkLogin)
g.GET("/", a.index)
g.GET("/inbounds", a.inbounds)
g.POST("/inbounds", a.postInbounds)
g.POST("/inbounds", a.getInbounds)
g.POST("/inbound/add", a.addInbound)
g.POST("/inbound/del/:id", a.delInbound)
g.POST("/inbound/update/:id", a.updateInbound)
g.GET("/setting", a.setting)
}
func (a *XUIController) startTask() {
webServer := global.GetWebServer()
c := webServer.GetCron()
c.AddFunc("@every 10s", func() {
if a.isNeedXrayRestart.Load() {
err := a.xrayService.RestartXray()
if err != nil {
logger.Error("restart xray failed:", err)
}
a.isNeedXrayRestart.Store(false)
}
})
}
func (a *XUIController) index(c *gin.Context) {
html(c, "index.html", "系统状态", nil)
}
@@ -44,7 +67,7 @@ func (a *XUIController) setting(c *gin.Context) {
html(c, "setting.html", "设置", nil)
}
func (a *XUIController) postInbounds(c *gin.Context) {
func (a *XUIController) getInbounds(c *gin.Context) {
user := session.GetLoginUser(c)
inbounds, err := a.inboundService.GetInbounds(user.Id)
if err != nil {
@@ -67,6 +90,9 @@ func (a *XUIController) addInbound(c *gin.Context) {
log.Println(inbound)
err = a.inboundService.AddInbound(inbound)
jsonMsg(c, "添加", err)
if err == nil {
a.isNeedXrayRestart.Store(true)
}
}
func (a *XUIController) delInbound(c *gin.Context) {
@@ -77,4 +103,28 @@ func (a *XUIController) delInbound(c *gin.Context) {
}
err = a.inboundService.DelInbound(id)
jsonMsg(c, "删除", err)
if err == nil {
a.isNeedXrayRestart.Store(true)
}
}
func (a *XUIController) 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.isNeedXrayRestart.Store(true)
}
}