完成xray启动

This commit is contained in:
sprov
2021-05-27 23:04:39 +08:00
parent 56ed8f355c
commit 3cd25ce5ea
42 changed files with 3627 additions and 229 deletions

View File

@@ -8,10 +8,6 @@ import (
type BaseController struct {
}
func NewBaseController(g *gin.RouterGroup) *BaseController {
return &BaseController{}
}
func (a *BaseController) before(c *gin.Context) {
if !session.IsLogin(c) {
pureJsonMsg(c, false, "登录时效已过,请重新登录")

116
web/controller/server.go Normal file
View File

@@ -0,0 +1,116 @@
package controller
import (
"context"
"github.com/gin-gonic/gin"
"runtime"
"time"
"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
lastVersions []string
lastGetVersionsTime time.Time
}
func newServerController(g *gin.RouterGroup) *serverController {
ctx, cancel := context.WithCancel(context.Background())
a := &serverController{
ctx: ctx,
cancel: cancel,
lastGetStatusTime: time.Now(),
}
a.initRouter(g)
a.startTask()
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) refreshStatus() {
status := a.serverService.GetStatus(a.lastStatus)
a.lastStatus = status
}
func (a *serverController) startTask() {
go func() {
for {
select {
case <-a.ctx.Done():
break
default:
}
now := time.Now()
if now.Sub(a.lastGetStatusTime) > time.Minute*3 {
time.Sleep(time.Second * 2)
continue
}
a.refreshStatus()
}
}()
}
func (a *serverController) stopTask() {
a.cancel()
}
func (a *serverController) status(c *gin.Context) {
a.lastGetStatusTime = time.Now()
jsonObj(c, a.lastStatus, nil)
}
func (a *serverController) getXrayVersion(c *gin.Context) {
now := time.Now()
if now.Sub(a.lastGetVersionsTime) <= time.Minute {
jsonObj(c, a.lastVersions, nil)
return
}
versions, err := a.serverService.GetXrayVersions()
if err != nil {
jsonMsg(c, "获取版本", err)
return
}
a.lastVersions = versions
a.lastGetVersionsTime = time.Now()
jsonObj(c, versions, nil)
}
func (a *serverController) installXray(c *gin.Context) {
version := c.Param("version")
err := a.serverService.UpdateXray(version)
jsonMsg(c, "安装 xray", err)
}

View File

@@ -1,256 +0,0 @@
package controller
import (
"bytes"
"context"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"net/http"
"runtime"
"time"
"x-ui/logger"
)
type ProcessState string
const (
Running ProcessState = "running"
Stop ProcessState = "stop"
Error ProcessState = "error"
)
type Status struct {
Cpu float64 `json:"cpu"`
Mem struct {
Current uint64 `json:"current"`
Total uint64 `json:"total"`
} `json:"mem"`
Swap struct {
Current uint64 `json:"current"`
Total uint64 `json:"total"`
} `json:"swap"`
Disk struct {
Current uint64 `json:"current"`
Total uint64 `json:"total"`
} `json:"disk"`
Xray struct {
State ProcessState `json:"state"`
ErrorMsg string `json:"errorMsg"`
Version string `json:"version"`
} `json:"xray"`
Uptime uint64 `json:"uptime"`
Loads []float64 `json:"loads"`
TcpCount int `json:"tcpCount"`
UdpCount int `json:"udpCount"`
NetIO struct {
Up uint64 `json:"up"`
Down uint64 `json:"down"`
} `json:"netIO"`
NetTraffic struct {
Sent uint64 `json:"sent"`
Recv uint64 `json:"recv"`
} `json:"netTraffic"`
}
type Release struct {
TagName string `json:"tag_name"`
}
func stopServerController(a *ServerController) {
a.stopTask()
}
type ServerController struct {
BaseController
ctx context.Context
cancel context.CancelFunc
lastStatus *Status
lastRefreshTime time.Time
lastGetStatusTime time.Time
}
func NewServerController(g *gin.RouterGroup) *ServerController {
ctx, cancel := context.WithCancel(context.Background())
a := &ServerController{
ctx: ctx,
cancel: cancel,
lastGetStatusTime: time.Now(),
}
a.initRouter(g)
go a.runTask()
runtime.SetFinalizer(a, stopServerController)
return a
}
func (a *ServerController) initRouter(g *gin.RouterGroup) {
g.POST("/server/status", a.status)
g.POST("/server/getXrayVersion", a.getXrayVersion)
}
func (a *ServerController) refreshStatus() {
status := &Status{}
now := time.Now()
percents, err := cpu.Percent(time.Second*2, false)
if err != nil {
logger.Warning("get cpu percent failed:", err)
} else {
status.Cpu = percents[0]
}
upTime, err := host.Uptime()
if err != nil {
logger.Warning("get uptime failed:", err)
} else {
status.Uptime = upTime
}
memInfo, err := mem.VirtualMemory()
if err != nil {
logger.Warning("get virtual memory failed:", err)
} else {
status.Mem.Current = memInfo.Used
status.Mem.Total = memInfo.Total
}
swapInfo, err := mem.SwapMemory()
if err != nil {
logger.Warning("get swap memory failed:", err)
} else {
status.Swap.Current = swapInfo.Used
status.Swap.Total = swapInfo.Total
}
distInfo, err := disk.Usage("/")
if err != nil {
logger.Warning("get dist usage failed:", err)
} else {
status.Disk.Current = distInfo.Used
status.Disk.Total = distInfo.Total
}
avgState, err := load.Avg()
if err != nil {
logger.Warning("get load avg failed:", err)
} else {
status.Loads = []float64{avgState.Load1, avgState.Load5, avgState.Load15}
}
ioStats, err := net.IOCounters(false)
if err != nil {
logger.Warning("get io counters failed:", err)
} else if len(ioStats) > 0 {
ioStat := ioStats[0]
status.NetTraffic.Sent = ioStat.BytesSent
status.NetTraffic.Recv = ioStat.BytesRecv
if a.lastStatus != nil {
duration := now.Sub(a.lastRefreshTime)
seconds := float64(duration) / float64(time.Second)
up := uint64(float64(status.NetTraffic.Sent-a.lastStatus.NetTraffic.Sent) / seconds)
down := uint64(float64(status.NetTraffic.Recv-a.lastStatus.NetTraffic.Recv) / seconds)
status.NetIO.Up = up
status.NetIO.Down = down
}
} else {
logger.Warning("can not find io counters")
}
tcpConnStats, err := net.Connections("tcp")
if err != nil {
logger.Warning("get connections failed:", err)
} else {
status.TcpCount = len(tcpConnStats)
}
udpConnStats, err := net.Connections("udp")
if err != nil {
logger.Warning("get connections failed:", err)
} else {
status.UdpCount = len(udpConnStats)
}
// TODO 临时
status.Xray.State = Running
status.Xray.ErrorMsg = ""
status.Xray.Version = "1.0.0"
a.lastStatus = status
a.lastRefreshTime = now
}
func (a *ServerController) runTask() {
for {
select {
case <-a.ctx.Done():
break
default:
}
now := time.Now()
if now.Sub(a.lastGetStatusTime) > time.Minute*3 {
time.Sleep(time.Second * 2)
continue
}
a.refreshStatus()
}
}
func (a *ServerController) stopTask() {
a.cancel()
}
func (a *ServerController) status(c *gin.Context) {
a.lastGetStatusTime = time.Now()
jsonMsgObj(c, "", a.lastStatus, nil)
}
var lastVersions []string
var lastGetReleaseTime time.Time
func (a *ServerController) getXrayVersion(c *gin.Context) {
now := time.Now()
if now.Sub(lastGetReleaseTime) <= time.Minute {
jsonMsgObj(c, "", lastVersions, nil)
return
}
url := "https://api.github.com/repos/XTLS/Xray-core/releases"
resp, err := http.Get(url)
if err != nil {
jsonMsg(c, "获取版本失败,请稍后尝试", err)
return
}
defer resp.Body.Close()
buffer := bytes.NewBuffer(make([]byte, 8192))
buffer.Reset()
_, err = buffer.ReadFrom(resp.Body)
if err != nil {
jsonMsg(c, "获取版本失败,请稍后尝试", err)
return
}
releases := make([]Release, 0)
err = json.Unmarshal(buffer.Bytes(), &releases)
if err != nil {
jsonMsg(c, "获取版本失败,请向作者反馈此问题", err)
return
}
versions := make([]string, 0, len(releases))
for _, release := range releases {
versions = append(versions, release.TagName)
}
lastVersions = versions
lastGetReleaseTime = time.Now()
jsonMsgObj(c, "", versions, nil)
}

View File

@@ -51,7 +51,7 @@ func jsonMsgObj(c *gin.Context, msg string, obj interface{}, err error) {
} else {
m.Success = false
m.Msg = msg + "失败: " + err.Error()
logger.Warning(msg, err)
logger.Warning(msg+"失败: ", err)
}
c.JSON(http.StatusOK, m)
}
@@ -76,7 +76,7 @@ func html(c *gin.Context, name string, title string, data gin.H) {
}
data["title"] = title
data["request_uri"] = c.Request.RequestURI
data["base_path"] = config.GetBasePath()
data["base_path"] = c.GetString("base_path")
c.HTML(http.StatusOK, name, getContext(data))
}

80
web/controller/xui.go Normal file
View File

@@ -0,0 +1,80 @@
package controller
import (
"github.com/gin-gonic/gin"
"log"
"strconv"
"x-ui/database/model"
"x-ui/web/service"
"x-ui/web/session"
)
type XUIController struct {
BaseController
inboundService service.InboundService
}
func NewXUIController(g *gin.RouterGroup) *XUIController {
a := &XUIController{}
a.initRouter(g)
return a
}
func (a *XUIController) initRouter(g *gin.RouterGroup) {
g = g.Group("/xui")
g.GET("/", a.index)
g.GET("/inbounds", a.inbounds)
g.POST("/inbounds", a.postInbounds)
g.POST("/inbound/add", a.addInbound)
g.POST("/inbound/del/:id", a.delInbound)
g.GET("/setting", a.setting)
}
func (a *XUIController) index(c *gin.Context) {
html(c, "index.html", "系统状态", nil)
}
func (a *XUIController) inbounds(c *gin.Context) {
html(c, "inbounds.html", "入站列表", nil)
}
func (a *XUIController) setting(c *gin.Context) {
html(c, "setting.html", "设置", nil)
}
func (a *XUIController) postInbounds(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 *XUIController) 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
log.Println(inbound)
err = a.inboundService.AddInbound(inbound)
jsonMsg(c, "添加", err)
}
func (a *XUIController) 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)
}

View File

@@ -1,31 +0,0 @@
package controller
import (
"github.com/gin-gonic/gin"
)
type XUIController struct {
BaseController
}
func NewXUIController(g *gin.RouterGroup) *XUIController {
a := &XUIController{}
a.initRouter(g)
return a
}
func (a *XUIController) initRouter(g *gin.RouterGroup) {
g = g.Group("/xui")
g.GET("/", a.index)
g.GET("/accounts", a.index)
g.GET("/setting", a.setting)
}
func (a *XUIController) index(c *gin.Context) {
html(c, "index.html", "系统状态", nil)
}
func (a *XUIController) setting(c *gin.Context) {
}