mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-01-07 15:05:49 +08:00
feat: 新增统一文件模块,统一文件操作
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"mayfly-go/internal/common/consts"
|
||||
"mayfly-go/internal/event"
|
||||
@@ -9,7 +8,6 @@ import (
|
||||
"mayfly-go/internal/machine/api/vo"
|
||||
"mayfly-go/internal/machine/application"
|
||||
"mayfly-go/internal/machine/application/dto"
|
||||
"mayfly-go/internal/machine/config"
|
||||
"mayfly-go/internal/machine/domain/entity"
|
||||
"mayfly-go/internal/machine/guac"
|
||||
"mayfly-go/internal/machine/mcm"
|
||||
@@ -25,8 +23,6 @@ import (
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"mayfly-go/pkg/ws"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -243,15 +239,6 @@ func (m *Machine) MachineTermOpRecords(rc *req.Ctx) {
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (m *Machine) MachineTermOpRecord(rc *req.Ctx) {
|
||||
termOp, err := m.MachineTermOpApp.GetById(uint64(rc.PathParamInt("recId")))
|
||||
biz.ErrIsNil(err)
|
||||
|
||||
bytes, err := os.ReadFile(path.Join(config.GetMachine().TerminalRecPath, termOp.RecordFilePath))
|
||||
biz.ErrIsNilAppendErr(err, "读取终端操作记录失败: %s")
|
||||
rc.ResData = base64.StdEncoding.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
const (
|
||||
SocketTimeout = 15 * time.Second
|
||||
MaxGuacMessage = 8192
|
||||
|
||||
@@ -3,6 +3,7 @@ package application
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
fileapp "mayfly-go/internal/file/application"
|
||||
"mayfly-go/internal/machine/config"
|
||||
"mayfly-go/internal/machine/domain/entity"
|
||||
"mayfly-go/internal/machine/domain/repository"
|
||||
@@ -15,8 +16,7 @@ import (
|
||||
"mayfly-go/pkg/scheduler"
|
||||
"mayfly-go/pkg/utils/jsonx"
|
||||
"mayfly-go/pkg/utils/stringx"
|
||||
"os"
|
||||
"path"
|
||||
"mayfly-go/pkg/utils/timex"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -38,6 +38,7 @@ type machineTermOpAppImpl struct {
|
||||
base.AppImpl[*entity.MachineTermOp, repository.MachineTermOp]
|
||||
|
||||
machineCmdConfApp MachineCmdConf `inject:"MachineCmdConfApp"`
|
||||
fileApp fileapp.File `inject:"FileApp"`
|
||||
}
|
||||
|
||||
// 注入MachineTermOpRepo
|
||||
@@ -63,20 +64,14 @@ func (m *machineTermOpAppImpl) TermConn(ctx context.Context, cli *mcm.Cli, wsCon
|
||||
termOpRecord.MachineId = cli.Info.Id
|
||||
termOpRecord.Username = cli.Info.Username
|
||||
|
||||
// 回放文件路径为: 基础配置路径/机器编号/操作日期(202301)/day/hour/randstr.cast
|
||||
recRelPath := path.Join(cli.Info.Code, now.Format("200601"), fmt.Sprintf("%d", now.Day()), fmt.Sprintf("%d", now.Hour()))
|
||||
// 文件绝对路径
|
||||
recAbsPath := path.Join(config.GetMachine().TerminalRecPath, recRelPath)
|
||||
os.MkdirAll(recAbsPath, 0766)
|
||||
filename := fmt.Sprintf("%s.cast", stringx.RandByChars(18, stringx.LowerChars))
|
||||
f, err := os.OpenFile(path.Join(recAbsPath, filename), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0766)
|
||||
fileKey, wc, saveFileFunc, err := m.fileApp.NewWriter(ctx, "", fmt.Sprintf("mto_%d_%s.cast", termOpRecord.MachineId, timex.TimeNo()))
|
||||
if err != nil {
|
||||
return errorx.NewBiz("创建终端回放记录文件失败: %s", err.Error())
|
||||
}
|
||||
defer f.Close()
|
||||
defer saveFileFunc()
|
||||
|
||||
termOpRecord.RecordFilePath = path.Join(recRelPath, filename)
|
||||
recorder = mcm.NewRecorder(f)
|
||||
termOpRecord.FileKey = fileKey
|
||||
recorder = mcm.NewRecorder(wc)
|
||||
}
|
||||
|
||||
createTsParam := &mcm.CreateTerminalSessionParam{
|
||||
@@ -134,9 +129,8 @@ func (m *machineTermOpAppImpl) TimerDeleteTermOp() {
|
||||
return
|
||||
}
|
||||
|
||||
basePath := config.GetMachine().TerminalRecPath
|
||||
for _, termOp := range termOps {
|
||||
if err := m.DeleteTermOp(basePath, termOp); err != nil {
|
||||
if err := m.DeleteTermOp(termOp); err != nil {
|
||||
logx.Warnf("删除终端操作记录失败: %s", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -144,10 +138,10 @@ func (m *machineTermOpAppImpl) TimerDeleteTermOp() {
|
||||
}
|
||||
|
||||
// 删除终端记录即对应文件
|
||||
func (m *machineTermOpAppImpl) DeleteTermOp(basePath string, termOp *entity.MachineTermOp) error {
|
||||
func (m *machineTermOpAppImpl) DeleteTermOp(termOp *entity.MachineTermOp) error {
|
||||
if err := m.DeleteById(context.Background(), termOp.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Remove(path.Join(basePath, termOp.RecordFilePath))
|
||||
return m.fileApp.Remove(context.TODO(), termOp.FileKey)
|
||||
}
|
||||
|
||||
@@ -13,13 +13,11 @@ const (
|
||||
)
|
||||
|
||||
type Machine struct {
|
||||
TerminalRecPath string // 终端操作记录存储位置
|
||||
UploadMaxFileSize int64 // 允许上传的最大文件size
|
||||
TermOpSaveDays int // 终端记录保存天数
|
||||
GuacdHost string // guacd服务地址 默认 127.0.0.1
|
||||
GuacdPort int // guacd服务端口 默认 4822
|
||||
GuacdFilePath string // guacd服务文件存储位置,用于挂载RDP文件夹
|
||||
GuacdRecPath string // guacd服务记录存储位置,用于记录rdp操作记录
|
||||
}
|
||||
|
||||
// 获取机器相关配置
|
||||
@@ -29,12 +27,6 @@ func GetMachine() *Machine {
|
||||
|
||||
mc := new(Machine)
|
||||
|
||||
terminalRecPath := jm["terminalRecPath"]
|
||||
if terminalRecPath == "" {
|
||||
terminalRecPath = "./rec"
|
||||
}
|
||||
mc.TerminalRecPath = terminalRecPath
|
||||
|
||||
// 将1GB等字符串转为int64的byte
|
||||
uploadMaxFileSizeStr := jm["uploadMaxFileSize"]
|
||||
var uploadMaxFileSize int64 = 1 * bytex.GB
|
||||
@@ -51,7 +43,6 @@ func GetMachine() *Machine {
|
||||
mc.GuacdHost = cast.ToString(jm["guacdHost"])
|
||||
mc.GuacdPort = cast.ToIntD(jm["guacdPort"], 4822)
|
||||
mc.GuacdFilePath = cast.ToStringD(jm["guacdFilePath"], "")
|
||||
mc.GuacdRecPath = cast.ToStringD(jm["guacdRecPath"], "")
|
||||
|
||||
return mc
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
type MachineTermOp struct {
|
||||
model.DeletedModel
|
||||
|
||||
MachineId uint64 `json:"machineId"`
|
||||
Username string `json:"username"`
|
||||
RecordFilePath string `json:"recordFilePath"` // 回放文件路径
|
||||
ExecCmds string `json:"execCmds"` // 执行的命令
|
||||
MachineId uint64 `json:"machineId"`
|
||||
Username string `json:"username"`
|
||||
FileKey string `json:"fileKey"` // 文件key
|
||||
ExecCmds string `json:"execCmds"` // 执行的命令
|
||||
|
||||
CreateTime *time.Time `json:"createTime"`
|
||||
CreatorId uint64 `json:"creatorId"`
|
||||
|
||||
@@ -47,9 +47,6 @@ func InitMachineRouter(router *gin.RouterGroup) {
|
||||
|
||||
// 获取机器终端回放记录列表,目前具有保存机器信息的权限标识才有权限查看终端回放
|
||||
req.NewGet(":machineId/term-recs", m.MachineTermOpRecords).RequiredPermission(saveMachineP),
|
||||
|
||||
// 获取机器终端回放记录
|
||||
req.NewGet(":machineId/term-recs/:recId", m.MachineTermOpRecord).RequiredPermission(saveMachineP),
|
||||
}
|
||||
|
||||
req.BatchSetGroup(machines, reqs[:])
|
||||
|
||||
Reference in New Issue
Block a user