mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-23 17:40:25 +08:00
feat: 新增终端回放记录&其他小优化
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"mayfly-go/internal/devops/api/form"
|
||||
"mayfly-go/internal/devops/api/vo"
|
||||
@@ -8,11 +9,17 @@ import (
|
||||
"mayfly-go/internal/devops/domain/entity"
|
||||
"mayfly-go/internal/devops/infrastructure/machine"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/config"
|
||||
"mayfly-go/pkg/ctx"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/utils"
|
||||
"mayfly-go/pkg/ws"
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -145,15 +152,15 @@ func (m *Machine) KillProcess(rc *ctx.ReqCtx) {
|
||||
func (m *Machine) WsSSH(g *gin.Context) {
|
||||
wsConn, err := ws.Upgrader.Upgrade(g.Writer, g.Request, nil)
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
wsConn.WriteMessage(websocket.TextMessage, []byte(err.(error).Error()))
|
||||
if wsConn != nil {
|
||||
if err := recover(); err != nil {
|
||||
wsConn.WriteMessage(websocket.TextMessage, []byte(err.(error).Error()))
|
||||
}
|
||||
wsConn.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
panic(biz.NewBizErr("升级websocket失败"))
|
||||
}
|
||||
biz.ErrIsNilAppendErr(err, "升级websocket失败: %s")
|
||||
// 权限校验
|
||||
rc := ctx.NewReqCtxWithGin(g).WithRequiredPermission(ctx.NewPermission("machine:terminal"))
|
||||
if err = ctx.PermissionHandler(rc); err != nil {
|
||||
@@ -166,12 +173,52 @@ func (m *Machine) WsSSH(g *gin.Context) {
|
||||
cols := ginx.QueryInt(g, "cols", 80)
|
||||
rows := ginx.QueryInt(g, "rows", 40)
|
||||
|
||||
mts, err := machine.NewTerminalSession(utils.RandString(16), wsConn, cli, rows, cols)
|
||||
var recorder *machine.Recorder
|
||||
if cli.GetMachine().EnableRecorder == 1 {
|
||||
mask := syscall.Umask(0)
|
||||
defer syscall.Umask(mask)
|
||||
now := time.Now()
|
||||
// 回放文件路径为: 基础配置路径/机器id/操作日期/操作者账号/操作时间.cast
|
||||
recPath := fmt.Sprintf("%s/%d/%s/%s", config.Conf.Server.GetMachineRecPath(), cli.GetMachine().Id, now.Format("20060102"), rc.LoginAccount.Username)
|
||||
os.MkdirAll(recPath, 0766)
|
||||
fileName := path.Join(recPath, fmt.Sprintf("%s.cast", now.Format("20060102_150405")))
|
||||
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0766)
|
||||
biz.ErrIsNilAppendErr(err, "创建终端回放记录文件失败: %s")
|
||||
defer f.Close()
|
||||
recorder = machine.NewRecorder(f)
|
||||
}
|
||||
|
||||
mts, err := machine.NewTerminalSession(utils.RandString(16), wsConn, cli, rows, cols, recorder)
|
||||
biz.ErrIsNilAppendErr(err, "\033[1;31m连接失败: %s\033[0m")
|
||||
mts.Start()
|
||||
defer mts.Stop()
|
||||
}
|
||||
|
||||
// 获取机器终端回放记录的相应文件夹名或文件内容
|
||||
func (m *Machine) MachineRecDirNames(rc *ctx.ReqCtx) {
|
||||
readPath := rc.GinCtx.Query("path")
|
||||
biz.NotEmpty(readPath, "path不能为空")
|
||||
path_ := path.Join(config.Conf.Server.GetMachineRecPath(), readPath)
|
||||
|
||||
// 如果是读取文件内容,则读取对应回放记录文件内容,否则读取文件夹名列表。小小偷懒一会不想再加个接口
|
||||
isFile := rc.GinCtx.Query("isFile")
|
||||
if isFile == "1" {
|
||||
bytes, err := os.ReadFile(path_)
|
||||
biz.ErrIsNilAppendErr(err, "还未有相应终端操作记录: %s")
|
||||
rc.ResData = base64.StdEncoding.EncodeToString(bytes)
|
||||
return
|
||||
}
|
||||
|
||||
files, err := os.ReadDir(path_)
|
||||
biz.ErrIsNilAppendErr(err, "还未有相应终端操作记录: %s")
|
||||
var names []string
|
||||
for _, f := range files {
|
||||
names = append(names, f.Name())
|
||||
}
|
||||
sort.Sort(sort.Reverse(sort.StringSlice(names)))
|
||||
rc.ResData = names
|
||||
}
|
||||
|
||||
func GetMachineId(g *gin.Context) uint64 {
|
||||
machineId, _ := strconv.Atoi(g.Param("machineId"))
|
||||
biz.IsTrue(machineId != 0, "machineId错误")
|
||||
|
||||
Reference in New Issue
Block a user