mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-13 19:54:34 +08:00
1
This commit is contained in:
@@ -21,7 +21,7 @@ func (c *configApi) GetSystemConfiguration(ctx *gin.Context, _ any) error {
|
|||||||
func (c *configApi) SetSystemConfiguration(ctx *gin.Context, _ any) (err error) {
|
func (c *configApi) SetSystemConfiguration(ctx *gin.Context, _ any) (err error) {
|
||||||
req := map[string]string{}
|
req := map[string]string{}
|
||||||
if err = ctx.BindJSON(&req); err != nil {
|
if err = ctx.BindJSON(&req); err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
err = logic.ConfigLogic.SetSystemConfiguration(req)
|
err = logic.ConfigLogic.SetSystemConfiguration(req)
|
||||||
return
|
return
|
||||||
|
@@ -20,12 +20,12 @@ var ProcApi = new(procApi)
|
|||||||
func (p *procApi) CreateNewProcess(ctx *gin.Context, req model.Process) (err error) {
|
func (p *procApi) CreateNewProcess(ctx *gin.Context, req model.Process) (err error) {
|
||||||
index, err := repository.ProcessRepository.AddProcessConfig(req)
|
index, err := repository.ProcessRepository.AddProcessConfig(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
req.Uuid = index
|
req.Uuid = index
|
||||||
proc, err := logic.ProcessCtlLogic.NewProcess(req)
|
proc, err := logic.ProcessCtlLogic.NewProcess(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
logic.ProcessCtlLogic.AddProcess(req.Uuid, proc)
|
logic.ProcessCtlLogic.AddProcess(req.Uuid, proc)
|
||||||
rOk(ctx, "Operation successful!", gin.H{
|
rOk(ctx, "Operation successful!", gin.H{
|
||||||
@@ -109,7 +109,7 @@ func (p *procApi) ProcessControl(ctx *gin.Context, req model.ProcessUuidReq) (er
|
|||||||
user := getUserName(ctx)
|
user := getUserName(ctx)
|
||||||
proc, err := logic.ProcessCtlLogic.GetProcess(req.Uuid)
|
proc, err := logic.ProcessCtlLogic.GetProcess(req.Uuid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
proc.ProcessControl(user)
|
proc.ProcessControl(user)
|
||||||
return
|
return
|
||||||
@@ -124,7 +124,7 @@ func (p *procApi) ProcessCreateShare(ctx *gin.Context, req model.ProcessShare) (
|
|||||||
Pid: req.Pid,
|
Pid: req.Pid,
|
||||||
CreateBy: getUserName(ctx),
|
CreateBy: getUserName(ctx),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
rOk(ctx, "Operation successful!", gin.H{
|
rOk(ctx, "Operation successful!", gin.H{
|
||||||
"token": token,
|
"token": token,
|
||||||
|
@@ -21,7 +21,7 @@ func (t *taskApi) CreateTask(ctx *gin.Context, req model.Task) (err error) {
|
|||||||
func (t *taskApi) GetTaskById(ctx *gin.Context, req model.TaskIdReq) (err error) {
|
func (t *taskApi) GetTaskById(ctx *gin.Context, req model.TaskIdReq) (err error) {
|
||||||
result, err := repository.TaskRepository.GetTaskById(req.Id)
|
result, err := repository.TaskRepository.GetTaskById(req.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
rOk(ctx, "Operation successful!", result)
|
rOk(ctx, "Operation successful!", result)
|
||||||
return
|
return
|
||||||
|
@@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/lzh-1625/go_process_manager/internal/app/model"
|
"github.com/lzh-1625/go_process_manager/internal/app/model"
|
||||||
"github.com/lzh-1625/go_process_manager/internal/app/repository"
|
"github.com/lzh-1625/go_process_manager/internal/app/repository"
|
||||||
"github.com/lzh-1625/go_process_manager/log"
|
"github.com/lzh-1625/go_process_manager/log"
|
||||||
"github.com/lzh-1625/go_process_manager/utils"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
@@ -53,17 +52,17 @@ func (w *wsApi) WebsocketHandle(ctx *gin.Context, req model.WebsocketHandleReq)
|
|||||||
reqUser := getUserName(ctx)
|
reqUser := getUserName(ctx)
|
||||||
proc, err := logic.ProcessCtlLogic.GetProcess(req.Uuid)
|
proc, err := logic.ProcessCtlLogic.GetProcess(req.Uuid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
if !proc.HasWsConn(reqUser) {
|
if proc.HasWsConn(reqUser) {
|
||||||
return errors.New("connection already exists; unable to establish a new one")
|
return errors.New("connection already exists")
|
||||||
}
|
}
|
||||||
if proc.Control.Controller == reqUser || proc.VerifyControl() {
|
if proc.Control.Controller != reqUser && !proc.VerifyControl() {
|
||||||
return errors.New("insufficient permissions; please check your access rights")
|
return errors.New("insufficient permissions")
|
||||||
}
|
}
|
||||||
conn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
|
conn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Logger.Infow("ws连接成功")
|
log.Logger.Infow("ws连接成功")
|
||||||
@@ -76,7 +75,7 @@ func (w *wsApi) WebsocketHandle(ctx *gin.Context, req model.WebsocketHandleReq)
|
|||||||
}
|
}
|
||||||
proc.ReadCache(wci)
|
proc.ReadCache(wci)
|
||||||
if proc.State.State == 1 {
|
if proc.State.State == 1 {
|
||||||
proc.SetTerminalSize(utils.GetIntByString(ctx.Query("cols")), utils.GetIntByString(ctx.Query("rows")))
|
proc.SetTerminalSize(req.Cols, req.Rows)
|
||||||
w.startWsConnect(wci, cancel, proc, hasOprPermission(ctx, req.Uuid, constants.OPERATION_TERMINAL_WRITE))
|
w.startWsConnect(wci, cancel, proc, hasOprPermission(ctx, req.Uuid, constants.OPERATION_TERMINAL_WRITE))
|
||||||
proc.AddConn(reqUser, wci)
|
proc.AddConn(reqUser, wci)
|
||||||
defer proc.DeleteConn(reqUser)
|
defer proc.DeleteConn(reqUser)
|
||||||
@@ -100,14 +99,14 @@ func (w *wsApi) WebsocketHandle(ctx *gin.Context, req model.WebsocketHandleReq)
|
|||||||
func (w *wsApi) WebsocketShareHandle(ctx *gin.Context, req model.WebsocketHandleReq) (err error) {
|
func (w *wsApi) WebsocketShareHandle(ctx *gin.Context, req model.WebsocketHandleReq) (err error) {
|
||||||
data, err := repository.WsShare.GetWsShareDataByToken(req.Token)
|
data, err := repository.WsShare.GetWsShareDataByToken(req.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
if data.ExpireTime.Unix() <= time.Now().Unix() {
|
if data.ExpireTime.Unix() <= time.Now().Unix() {
|
||||||
return errors.New("share expired")
|
return errors.New("share expired")
|
||||||
}
|
}
|
||||||
proc, err := logic.ProcessCtlLogic.GetProcess(data.Pid)
|
proc, err := logic.ProcessCtlLogic.GetProcess(data.Pid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
guestName := "guest-" + strconv.Itoa(int(data.ID)) // 构造访客用户名
|
guestName := "guest-" + strconv.Itoa(int(data.ID)) // 构造访客用户名
|
||||||
if proc.HasWsConn(guestName) {
|
if proc.HasWsConn(guestName) {
|
||||||
@@ -121,14 +120,14 @@ func (w *wsApi) WebsocketShareHandle(ctx *gin.Context, req model.WebsocketHandle
|
|||||||
}
|
}
|
||||||
conn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
|
conn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Logger.Infow("ws连接成功")
|
log.Logger.Infow("ws连接成功")
|
||||||
data.UpdatedAt = time.Now()
|
data.UpdatedAt = time.Now()
|
||||||
repository.WsShare.Edit(data)
|
repository.WsShare.Edit(data)
|
||||||
|
|
||||||
proc.SetTerminalSize(utils.GetIntByString(ctx.Query("cols")), utils.GetIntByString(ctx.Query("rows")))
|
proc.SetTerminalSize(req.Cols, req.Rows)
|
||||||
wsCtx, cancel := context.WithCancel(context.Background())
|
wsCtx, cancel := context.WithCancel(context.Background())
|
||||||
wci := &WsConnetInstance{
|
wci := &WsConnetInstance{
|
||||||
WsConnect: conn,
|
WsConnect: conn,
|
||||||
|
@@ -120,7 +120,6 @@ func (p *processCtlLogic) getProcessInfoList(processConfiglist []model.Process)
|
|||||||
pi.State.State = process.State.State
|
pi.State.State = process.State.State
|
||||||
pi.StartTime = process.GetStartTimeFormat()
|
pi.StartTime = process.GetStartTimeFormat()
|
||||||
pi.User = process.GetUserString()
|
pi.User = process.GetUserString()
|
||||||
|
|
||||||
pi.Usage.Cpu = process.performanceStatus.cpu
|
pi.Usage.Cpu = process.performanceStatus.cpu
|
||||||
pi.Usage.Mem = process.performanceStatus.mem
|
pi.Usage.Mem = process.performanceStatus.mem
|
||||||
if config.CF.PerformanceCapacityDisplay {
|
if config.CF.PerformanceCapacityDisplay {
|
||||||
@@ -163,7 +162,6 @@ func (p *processCtlLogic) RunPrcessById(id int) (*ProcessBase, error) {
|
|||||||
func (p *processCtlLogic) ProcessInit() {
|
func (p *processCtlLogic) ProcessInit() {
|
||||||
config := repository.ProcessRepository.GetAllProcessConfig()
|
config := repository.ProcessRepository.GetAllProcessConfig()
|
||||||
for _, v := range config {
|
for _, v := range config {
|
||||||
|
|
||||||
proc, err := p.NewProcess(v)
|
proc, err := p.NewProcess(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Logger.Warnw("初始化启动进程失败", v.Name, "name", "err", err)
|
log.Logger.Warnw("初始化启动进程失败", v.Name, "name", "err", err)
|
||||||
|
@@ -28,7 +28,14 @@ func Logger() gin.HandlerFunc {
|
|||||||
if user, ok := ctx.Get(constants.CTXFLG_USER_NAME); ok {
|
if user, ok := ctx.Get(constants.CTXFLG_USER_NAME); ok {
|
||||||
logKv = append(logKv, "user", user)
|
logKv = append(logKv, "user", user)
|
||||||
}
|
}
|
||||||
log.Logger.Infow("GIN", logKv...)
|
switch {
|
||||||
|
case ctx.Writer.Status() >= 200 && ctx.Writer.Status() < 300:
|
||||||
|
log.Logger.Infow("\033[102mGIN\033[0m", logKv...)
|
||||||
|
case ctx.Writer.Status() >= 500:
|
||||||
|
log.Logger.Infow("\033[101mGIN\033[0m", logKv...)
|
||||||
|
default:
|
||||||
|
log.Logger.Infow("\033[103mGIN\033[0m", logKv...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,26 +0,0 @@
|
|||||||
package middle
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/lzh-1625/go_process_manager/internal/app/constants"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
func PanicMiddle() gin.HandlerFunc {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
defer func() {
|
|
||||||
if err := recover(); err == 0 {
|
|
||||||
if err, ok := c.Get(constants.CTXFLG_ERR); ok {
|
|
||||||
rErr(c, -1, err.(string), nil)
|
|
||||||
} else {
|
|
||||||
rErr(c, -1, "Internal error!", nil)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
c.Next()
|
|
||||||
}
|
|
||||||
}
|
|
@@ -20,8 +20,8 @@ func (WsShare) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type WebsocketHandleReq struct {
|
type WebsocketHandleReq struct {
|
||||||
Uuid int `json:"uuid"`
|
Uuid int `form:"uuid"`
|
||||||
Cols int `json:"cols"`
|
Cols int `form:"cols"`
|
||||||
Rows int `json:"rows"`
|
Rows int `form:"rows"`
|
||||||
Token string `json:"token"`
|
Token string `form:"token"`
|
||||||
}
|
}
|
||||||
|
@@ -54,7 +54,6 @@ func routePathInit(r *gin.Engine) {
|
|||||||
|
|
||||||
apiGroup := r.Group("/api")
|
apiGroup := r.Group("/api")
|
||||||
apiGroup.Use(middle.CheckToken())
|
apiGroup.Use(middle.CheckToken())
|
||||||
apiGroup.Use(middle.PanicMiddle())
|
|
||||||
// apiGroup.Use(middle.DemoMiddle())
|
// apiGroup.Use(middle.DemoMiddle())
|
||||||
{
|
{
|
||||||
wsGroup := apiGroup.Group("/ws")
|
wsGroup := apiGroup.Group("/ws")
|
||||||
@@ -155,19 +154,19 @@ func bind[T any](fn func(*gin.Context, T) error, bindOption int) func(*gin.Conte
|
|||||||
return func(ctx *gin.Context) {
|
return func(ctx *gin.Context) {
|
||||||
var req T
|
var req T
|
||||||
if bindOption&Body != 0 {
|
if bindOption&Body != 0 {
|
||||||
if err := ctx.BindJSON(req); err != nil {
|
if err := ctx.BindJSON(&req); err != nil {
|
||||||
rErr(ctx, err)
|
rErr(ctx, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if bindOption&Header != 0 {
|
if bindOption&Header != 0 {
|
||||||
if err := ctx.BindHeader(req); err != nil {
|
if err := ctx.BindHeader(&req); err != nil {
|
||||||
rErr(ctx, err)
|
rErr(ctx, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if bindOption&Query != 0 {
|
if bindOption&Query != 0 {
|
||||||
if err := ctx.BindQuery(req); err != nil {
|
if err := ctx.BindQuery(&req); err != nil {
|
||||||
rErr(ctx, err)
|
rErr(ctx, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -187,8 +186,9 @@ func bind[T any](fn func(*gin.Context, T) error, bindOption int) func(*gin.Conte
|
|||||||
}
|
}
|
||||||
|
|
||||||
func rErr(ctx *gin.Context, err error) {
|
func rErr(ctx *gin.Context, err error) {
|
||||||
|
log.Logger.Warn(err)
|
||||||
ctx.JSON(500, gin.H{
|
ctx.JSON(500, gin.H{
|
||||||
"code": -1,
|
"code": -1,
|
||||||
"message": err,
|
"message": err.Error(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user