diff --git a/mayfly_go_web/src/components/terminal-rdp/MachineRdp.vue b/mayfly_go_web/src/components/terminal-rdp/MachineRdp.vue index 8a5428e04e293cdf713127a1709db88293beb0f8..53a9f453531868effe7ce440175f39f36f685c31 100644 --- a/mayfly_go_web/src/components/terminal-rdp/MachineRdp.vue +++ b/mayfly_go_web/src/components/terminal-rdp/MachineRdp.vue @@ -52,6 +52,7 @@ import { useEventListener } from '@vueuse/core'; import { debounce } from 'lodash'; import { ClientState, TunnelState } from '@/components/terminal-rdp/guac/states'; import { ElMessage } from 'element-plus'; +import { joinClientParams } from '@/common/request'; const viewportRef = ref({} as any); const displayRef = ref({} as any); @@ -180,7 +181,7 @@ const installDisplay = () => { } e.returnValue = false; }); - state.client.connect('width=' + width + '&height=' + height + '&force=' + force); + state.client.connect('width=' + width + '&height=' + height + '&force=' + force + '&' + joinClientParams()); window.onunload = () => state.client.disconnect(); // allows focusing on the display div so that keyboard doesn't always go to session diff --git a/server/internal/db/dbm/dm/metadata.go b/server/internal/db/dbm/dm/metadata.go index b9e876078463f38610371366b699429c9e934818..18e2e63b3f0af88f5fddc762fb88745570f35593 100644 --- a/server/internal/db/dbm/dm/metadata.go +++ b/server/internal/db/dbm/dm/metadata.go @@ -228,7 +228,7 @@ func (dd *DMMetaData) GenerateIndexDDL(indexs []dbi.Index, tableInfo dbi.Table) colNames[i] = meta.QuoteIdentifier(name) } - sqls = append(sqls, fmt.Sprintf("create %s index %s on %s(%s)", unique, index.IndexName, meta.QuoteIdentifier(tableInfo.TableName), strings.Join(colNames, ","))) + sqls = append(sqls, fmt.Sprintf("create %s index %s on %s(%s)", unique, meta.QuoteIdentifier(index.IndexName), meta.QuoteIdentifier(tableInfo.TableName), strings.Join(colNames, ","))) } return sqls } diff --git a/server/internal/db/dbm/mssql/metadata.go b/server/internal/db/dbm/mssql/metadata.go index 11e50872688760a2ae6980d5c100fe1d8ffabdce..af2f5fe1c4a1651737bb6674577e1ff06c952e60 100644 --- a/server/internal/db/dbm/mssql/metadata.go +++ b/server/internal/db/dbm/mssql/metadata.go @@ -248,7 +248,7 @@ func (md *MssqlMetaData) GenerateIndexDDL(indexs []dbi.Index, tableInfo dbi.Tabl colNames[i] = meta.QuoteIdentifier(name) } - sqls = append(sqls, fmt.Sprintf("create %s NONCLUSTERED index %s on %s.%s(%s)", unique, index.IndexName, meta.QuoteIdentifier(md.dc.Info.CurrentSchema()), meta.QuoteIdentifier(tbName), strings.Join(colNames, ","))) + sqls = append(sqls, fmt.Sprintf("create %s NONCLUSTERED index %s on %s.%s(%s)", unique, meta.QuoteIdentifier(index.IndexName), meta.QuoteIdentifier(md.dc.Info.CurrentSchema()), meta.QuoteIdentifier(tbName), strings.Join(colNames, ","))) if index.IndexComment != "" { comment := meta.QuoteEscape(index.IndexComment) comments = append(comments, fmt.Sprintf("EXECUTE sp_addextendedproperty N'MS_Description', N'%s', N'SCHEMA', N'%s', N'TABLE', N'%s', N'INDEX', N'%s'", comment, md.dc.Info.CurrentSchema(), tbName, index.IndexName)) diff --git a/server/internal/db/dbm/mysql/metadata.go b/server/internal/db/dbm/mysql/metadata.go index 4bab9e4f381be0f5f4ad023a271b125938c43db1..e3a5cc7134ada1fec5d863de53aa8aa877392119 100644 --- a/server/internal/db/dbm/mysql/metadata.go +++ b/server/internal/db/dbm/mysql/metadata.go @@ -194,7 +194,7 @@ func (md *MysqlMetaData) GenerateIndexDDL(indexs []dbi.Index, tableInfo dbi.Tabl colNames[i] = meta.QuoteIdentifier(name) } sqlTmp := "ALTER TABLE %s ADD %s INDEX %s(%s) USING BTREE" - sqlStr := fmt.Sprintf(sqlTmp, meta.QuoteIdentifier(tableInfo.TableName), unique, index.IndexName, strings.Join(colNames, ",")) + sqlStr := fmt.Sprintf(sqlTmp, meta.QuoteIdentifier(tableInfo.TableName), unique, meta.QuoteIdentifier(index.IndexName), strings.Join(colNames, ",")) comment := meta.QuoteEscape(index.IndexComment) if comment != "" { sqlStr += fmt.Sprintf(" COMMENT '%s'", comment) diff --git a/server/internal/db/dbm/oracle/metadata.go b/server/internal/db/dbm/oracle/metadata.go index 4bc1663eacb2bd09bb398c6f700762dca522c209..0dba591cd7930b4b48f895aef68ec9b02955f200 100644 --- a/server/internal/db/dbm/oracle/metadata.go +++ b/server/internal/db/dbm/oracle/metadata.go @@ -213,7 +213,7 @@ func (od *OracleMetaData) GenerateIndexDDL(indexs []dbi.Index, tableInfo dbi.Tab colNames[i] = meta.QuoteIdentifier(name) } - sqls = append(sqls, fmt.Sprintf("CREATE %s INDEX %s ON %s(%s)", unique, index.IndexName, meta.QuoteIdentifier(tableInfo.TableName), strings.Join(colNames, ","))) + sqls = append(sqls, fmt.Sprintf("CREATE %s INDEX %s ON %s(%s)", unique, meta.QuoteIdentifier(index.IndexName), meta.QuoteIdentifier(tableInfo.TableName), strings.Join(colNames, ","))) } sqlArr := make([]string, 0) diff --git a/server/internal/db/dbm/postgres/metadata.go b/server/internal/db/dbm/postgres/metadata.go index efb82ec1fa198f7990c15bb8f7fe0c54bb69a3cf..17abd11e0ddcaf1761c0562b408994ffa71b2842 100644 --- a/server/internal/db/dbm/postgres/metadata.go +++ b/server/internal/db/dbm/postgres/metadata.go @@ -195,7 +195,7 @@ func (pd *PgsqlMetaData) GenerateIndexDDL(indexs []dbi.Index, tableInfo dbi.Tabl colNames[i] = meta.QuoteIdentifier(name) } // 创建索引 - creates = append(creates, fmt.Sprintf("CREATE %s INDEX %s on %s.%s(%s)", unique, index.IndexName, pd.dc.Info.CurrentSchema(), tableInfo.TableName, strings.Join(colNames, ","))) + creates = append(creates, fmt.Sprintf("CREATE %s INDEX %s on %s.%s(%s)", unique, meta.QuoteIdentifier(index.IndexName), meta.QuoteIdentifier(pd.dc.Info.CurrentSchema()), meta.QuoteIdentifier(tableInfo.TableName), strings.Join(colNames, ","))) if index.IndexComment != "" { comment := meta.QuoteEscape(index.IndexComment) comments = append(comments, fmt.Sprintf("COMMENT ON INDEX %s.%s IS '%s'", pd.dc.Info.CurrentSchema(), index.IndexName, comment)) diff --git a/server/internal/db/dbm/sqlite/metadata.go b/server/internal/db/dbm/sqlite/metadata.go index d70a82ccca6609c2152949b8961d556191cfb953..36e0105eaa0c986ba0ac69e506b54e9676eda3bf 100644 --- a/server/internal/db/dbm/sqlite/metadata.go +++ b/server/internal/db/dbm/sqlite/metadata.go @@ -216,8 +216,8 @@ func (sd *SqliteMetaData) GenerateIndexDDL(indexs []dbi.Index, tableInfo dbi.Tab // 创建前尝试删除 sqls = append(sqls, fmt.Sprintf("DROP INDEX IF EXISTS \"%s\"", index.IndexName)) - sqlTmp := "CREATE %s INDEX %s ON \"%s\" (%s) " - sqls = append(sqls, fmt.Sprintf(sqlTmp, unique, index.IndexName, tableInfo.TableName, strings.Join(colNames, ","))) + sqlTmp := "CREATE %s INDEX %s ON %s (%s) " + sqls = append(sqls, fmt.Sprintf(sqlTmp, unique, meta.QuoteIdentifier(index.IndexName), meta.QuoteIdentifier(tableInfo.TableName), strings.Join(colNames, ","))) } return sqls } diff --git a/server/internal/machine/api/machine.go b/server/internal/machine/api/machine.go index 45d3d1ce4c9d8494385b28f44fc166766270ee66..ff5a762343cebfd3d166871f7513cd0ef3e7a175 100644 --- a/server/internal/machine/api/machine.go +++ b/server/internal/machine/api/machine.go @@ -244,6 +244,10 @@ func (m *Machine) WsGuacamole(g *gin.Context) { biz.ErrIsNil(err) rc := req.NewCtxWithGin(g).WithRequiredPermission(req.NewPermission("machine:terminal")) + if err = req.PermissionHandler(rc); err != nil { + panic(errorx.NewBiz("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m")) + } + ac := GetMachineAc(rc) mi, err := m.MachineApp.ToMachineInfoByAc(ac) @@ -293,7 +297,7 @@ func (m *Machine) WsGuacamole(g *gin.Context) { } } - tunnel, err := guac.DoConnect(query, params, ac) + tunnel, err := guac.DoConnect(query, params, rc.GetLoginAccount().Username) if err != nil { return } diff --git a/server/internal/machine/api/machine_file.go b/server/internal/machine/api/machine_file.go index baba59d370260eeaf5fc46d84d4fa0d1b9bcfc5b..559cee74170271e368e688c85c976ca9d69dfa3b 100644 --- a/server/internal/machine/api/machine_file.go +++ b/server/internal/machine/api/machine_file.go @@ -71,10 +71,10 @@ func (m *MachineFile) CreateFile(rc *req.Ctx) { var err error if opForm.Type == dir { attrs["type"] = "目录" - mi, err = m.MachineFileApp.MkDir(opForm.MachineFileOpParam) + mi, err = m.MachineFileApp.MkDir(rc.MetaCtx, opForm.MachineFileOpParam) } else { attrs["type"] = "文件" - mi, err = m.MachineFileApp.CreateFile(opForm.MachineFileOpParam) + mi, err = m.MachineFileApp.CreateFile(rc.MetaCtx, opForm.MachineFileOpParam) } attrs["machine"] = mi rc.ReqParam = attrs @@ -86,7 +86,7 @@ func (m *MachineFile) ReadFileContent(rc *req.Ctx) { readPath := opForm.Path // 特殊处理rdp文件 if opForm.Protocol == entity.MachineProtocolRdp { - path := m.MachineFileApp.GetRdpFilePath(opForm.MachineId, opForm.Path) + path := m.MachineFileApp.GetRdpFilePath(rc.GetLoginAccount(), opForm.Path) fi, err := os.Stat(path) biz.ErrIsNilAppendErr(err, "读取文件内容失败: %s") biz.IsTrue(fi.Size() < max_read_size, "文件超过1m,请使用下载查看") @@ -96,7 +96,7 @@ func (m *MachineFile) ReadFileContent(rc *req.Ctx) { return } - sftpFile, mi, err := m.MachineFileApp.ReadFile(opForm) + sftpFile, mi, err := m.MachineFileApp.ReadFile(rc.MetaCtx, opForm) rc.ReqParam = collx.Kvs("machine", mi, "path", readPath) biz.ErrIsNilAppendErr(err, "打开文件失败: %s") defer sftpFile.Close() @@ -121,7 +121,7 @@ func (m *MachineFile) DownloadFile(rc *req.Ctx) { fileName := path[len(path)-1] if opForm.Protocol == entity.MachineProtocolRdp { - path := m.MachineFileApp.GetRdpFilePath(opForm.MachineId, opForm.Path) + path := m.MachineFileApp.GetRdpFilePath(rc.GetLoginAccount(), opForm.Path) file, err := os.Open(path) if err != nil { return @@ -131,7 +131,7 @@ func (m *MachineFile) DownloadFile(rc *req.Ctx) { return } - sftpFile, mi, err := m.MachineFileApp.ReadFile(opForm) + sftpFile, mi, err := m.MachineFileApp.ReadFile(rc.MetaCtx, opForm) rc.ReqParam = collx.Kvs("machine", mi, "path", readPath) biz.ErrIsNilAppendErr(err, "打开文件失败: %s") defer sftpFile.Close() @@ -144,7 +144,7 @@ func (m *MachineFile) GetDirEntry(rc *req.Ctx) { readPath := opForm.Path rc.ReqParam = fmt.Sprintf("path: %s", readPath) - fis, err := m.MachineFileApp.ReadDir(opForm) + fis, err := m.MachineFileApp.ReadDir(rc.MetaCtx, opForm) biz.ErrIsNilAppendErr(err, "读取目录失败: %s") fisVO := make([]vo.MachineFileInfo, 0) @@ -175,14 +175,14 @@ func (m *MachineFile) GetDirEntry(rc *req.Ctx) { func (m *MachineFile) GetDirSize(rc *req.Ctx) { opForm := req.BindQuery(rc, new(application.MachineFileOpParam)) - size, err := m.MachineFileApp.GetDirSize(opForm) + size, err := m.MachineFileApp.GetDirSize(rc.MetaCtx, opForm) biz.ErrIsNil(err) rc.ResData = size } func (m *MachineFile) GetFileStat(rc *req.Ctx) { opForm := req.BindQuery(rc, new(application.MachineFileOpParam)) - res, err := m.MachineFileApp.FileStat(opForm) + res, err := m.MachineFileApp.FileStat(rc.MetaCtx, opForm) biz.ErrIsNil(err, res) rc.ResData = res } @@ -191,7 +191,7 @@ func (m *MachineFile) WriteFileContent(rc *req.Ctx) { opForm := req.BindJsonAndValid(rc, new(form.WriteFileContentForm)) path := opForm.Path - mi, err := m.MachineFileApp.WriteFileContent(opForm.MachineFileOpParam, []byte(opForm.Content)) + mi, err := m.MachineFileApp.WriteFileContent(rc.MetaCtx, opForm.MachineFileOpParam, []byte(opForm.Content)) rc.ReqParam = collx.Kvs("machine", mi, "path", path) biz.ErrIsNilAppendErr(err, "打开文件失败: %s") } @@ -226,7 +226,7 @@ func (m *MachineFile) UploadFile(rc *req.Ctx) { Path: path, } - mi, err := m.MachineFileApp.UploadFile(opForm, fileheader.Filename, file) + mi, err := m.MachineFileApp.UploadFile(rc.MetaCtx, opForm, fileheader.Filename, file) rc.ReqParam = collx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", path, fileheader.Filename)) biz.ErrIsNilAppendErr(err, "创建文件失败: %s") // 保存消息并发送文件上传成功通知 @@ -266,7 +266,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) { } if protocol == entity.MachineProtocolRdp { - m.MachineFileApp.UploadFiles(opForm, basePath, fileheaders, paths) + m.MachineFileApp.UploadFiles(rc.MetaCtx, opForm, basePath, fileheaders, paths) return } @@ -347,28 +347,28 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) { func (m *MachineFile) RemoveFile(rc *req.Ctx) { opForm := req.BindJsonAndValid(rc, new(form.RemoveFileForm)) - mi, err := m.MachineFileApp.RemoveFile(opForm.MachineFileOpParam, opForm.Paths...) + mi, err := m.MachineFileApp.RemoveFile(rc.MetaCtx, opForm.MachineFileOpParam, opForm.Paths...) rc.ReqParam = collx.Kvs("machine", mi, "path", opForm) biz.ErrIsNilAppendErr(err, "删除文件失败: %s") } func (m *MachineFile) CopyFile(rc *req.Ctx) { opForm := req.BindJsonAndValid(rc, new(form.CopyFileForm)) - mi, err := m.MachineFileApp.Copy(opForm.MachineFileOpParam, opForm.ToPath, opForm.Paths...) + mi, err := m.MachineFileApp.Copy(rc.MetaCtx, opForm.MachineFileOpParam, opForm.ToPath, opForm.Paths...) biz.ErrIsNilAppendErr(err, "文件拷贝失败: %s") rc.ReqParam = collx.Kvs("machine", mi, "cp", opForm) } func (m *MachineFile) MvFile(rc *req.Ctx) { opForm := req.BindJsonAndValid(rc, new(form.CopyFileForm)) - mi, err := m.MachineFileApp.Mv(opForm.MachineFileOpParam, opForm.ToPath, opForm.Paths...) + mi, err := m.MachineFileApp.Mv(rc.MetaCtx, opForm.MachineFileOpParam, opForm.ToPath, opForm.Paths...) rc.ReqParam = collx.Kvs("machine", mi, "mv", opForm) biz.ErrIsNilAppendErr(err, "文件移动失败: %s") } func (m *MachineFile) Rename(rc *req.Ctx) { renameForm := req.BindJsonAndValid(rc, new(form.RenameForm)) - mi, err := m.MachineFileApp.Rename(renameForm.MachineFileOpParam, renameForm.Newname) + mi, err := m.MachineFileApp.Rename(rc.MetaCtx, renameForm.MachineFileOpParam, renameForm.Newname) rc.ReqParam = collx.Kvs("machine", mi, "rename", renameForm) biz.ErrIsNilAppendErr(err, "文件重命名失败: %s") } diff --git a/server/internal/machine/application/machine_file.go b/server/internal/machine/application/machine_file.go index ce938909145ebde75a007438da40a8c9ccc1523c..796c49c8671d93428396bae7f7acc913301aefa0 100644 --- a/server/internal/machine/application/machine_file.go +++ b/server/internal/machine/application/machine_file.go @@ -11,6 +11,7 @@ import ( "mayfly-go/internal/machine/domain/repository" "mayfly-go/internal/machine/mcm" "mayfly-go/pkg/base" + "mayfly-go/pkg/contextx" "mayfly-go/pkg/errorx" "mayfly-go/pkg/logx" "mayfly-go/pkg/model" @@ -25,6 +26,7 @@ import ( ) type MachineFileOpParam struct { + Ua *model.LoginAccount MachineId uint64 `json:"machineId" binding:"required" form:"machineId"` Protocol int `json:"protocol" binding:"required" form:"protocol"` AuthCertName string `json:"authCertName" binding:"required" form:"authCertName"` // 授权凭证 @@ -45,44 +47,44 @@ type MachineFile interface { // 获取机器cli GetMachineCli(authCertName string) (*mcm.Cli, error) - GetRdpFilePath(MachineId uint64, path string) string + GetRdpFilePath(ua *model.LoginAccount, path string) string /** sftp 相关操作 **/ // 创建目录 - MkDir(opParam *MachineFileOpParam) (*mcm.MachineInfo, error) + MkDir(ctx context.Context, opParam *MachineFileOpParam) (*mcm.MachineInfo, error) // 创建文件 - CreateFile(opParam *MachineFileOpParam) (*mcm.MachineInfo, error) + CreateFile(ctx context.Context, opParam *MachineFileOpParam) (*mcm.MachineInfo, error) // 读取目录 - ReadDir(opParam *MachineFileOpParam) ([]fs.FileInfo, error) + ReadDir(ctx context.Context, opParam *MachineFileOpParam) ([]fs.FileInfo, error) // 获取指定目录内容大小 - GetDirSize(opParam *MachineFileOpParam) (string, error) + GetDirSize(ctx context.Context, opParam *MachineFileOpParam) (string, error) // 获取文件stat - FileStat(opParam *MachineFileOpParam) (string, error) + FileStat(ctx context.Context, opParam *MachineFileOpParam) (string, error) // 读取文件内容 - ReadFile(opParam *MachineFileOpParam) (*sftp.File, *mcm.MachineInfo, error) + ReadFile(ctx context.Context, opParam *MachineFileOpParam) (*sftp.File, *mcm.MachineInfo, error) // 写文件 - WriteFileContent(opParam *MachineFileOpParam, content []byte) (*mcm.MachineInfo, error) + WriteFileContent(ctx context.Context, opParam *MachineFileOpParam, content []byte) (*mcm.MachineInfo, error) // 文件上传 - UploadFile(opParam *MachineFileOpParam, filename string, reader io.Reader) (*mcm.MachineInfo, error) + UploadFile(ctx context.Context, opParam *MachineFileOpParam, filename string, reader io.Reader) (*mcm.MachineInfo, error) - UploadFiles(opParam *MachineFileOpParam, basePath string, fileHeaders []*multipart.FileHeader, paths []string) (*mcm.MachineInfo, error) + UploadFiles(ctx context.Context, opParam *MachineFileOpParam, basePath string, fileHeaders []*multipart.FileHeader, paths []string) (*mcm.MachineInfo, error) // 移除文件 - RemoveFile(opParam *MachineFileOpParam, path ...string) (*mcm.MachineInfo, error) + RemoveFile(ctx context.Context, opParam *MachineFileOpParam, path ...string) (*mcm.MachineInfo, error) - Copy(opParam *MachineFileOpParam, toPath string, path ...string) (*mcm.MachineInfo, error) + Copy(ctx context.Context, opParam *MachineFileOpParam, toPath string, path ...string) (*mcm.MachineInfo, error) - Mv(opParam *MachineFileOpParam, toPath string, path ...string) (*mcm.MachineInfo, error) + Mv(ctx context.Context, opParam *MachineFileOpParam, toPath string, path ...string) (*mcm.MachineInfo, error) - Rename(opParam *MachineFileOpParam, newname string) (*mcm.MachineInfo, error) + Rename(ctx context.Context, opParam *MachineFileOpParam, newname string) (*mcm.MachineInfo, error) } type machineFileAppImpl struct { @@ -120,7 +122,7 @@ func (m *machineFileAppImpl) Save(ctx context.Context, mf *entity.MachineFile) e return m.Insert(ctx, mf) } -func (m *machineFileAppImpl) ReadDir(opParam *MachineFileOpParam) ([]fs.FileInfo, error) { +func (m *machineFileAppImpl) ReadDir(ctx context.Context, opParam *MachineFileOpParam) ([]fs.FileInfo, error) { path := opParam.Path if !strings.HasSuffix(path, "/") { path = path + "/" @@ -128,7 +130,7 @@ func (m *machineFileAppImpl) ReadDir(opParam *MachineFileOpParam) ([]fs.FileInfo // 如果是rdp,则直接读取本地文件 if opParam.Protocol == entity.MachineProtocolRdp { - path = m.GetRdpFilePath(opParam.MachineId, path) + path = m.GetRdpFilePath(contextx.GetLoginAccount(ctx), path) dirs, err := os.ReadDir(path) if err != nil { return nil, err @@ -146,11 +148,11 @@ func (m *machineFileAppImpl) ReadDir(opParam *MachineFileOpParam) ([]fs.FileInfo return sftpCli.ReadDir(path) } -func (m *machineFileAppImpl) GetDirSize(opParam *MachineFileOpParam) (string, error) { +func (m *machineFileAppImpl) GetDirSize(ctx context.Context, opParam *MachineFileOpParam) (string, error) { path := opParam.Path if opParam.Protocol == entity.MachineProtocolRdp { - dirPath := m.GetRdpFilePath(opParam.MachineId, path) + dirPath := m.GetRdpFilePath(contextx.GetLoginAccount(ctx), path) // 递归计算目录下文件大小 var totalSize int64 @@ -195,10 +197,10 @@ func (m *machineFileAppImpl) GetDirSize(opParam *MachineFileOpParam) (string, er return strings.Split(res, "\t")[0], nil } -func (m *machineFileAppImpl) FileStat(opParam *MachineFileOpParam) (string, error) { +func (m *machineFileAppImpl) FileStat(ctx context.Context, opParam *MachineFileOpParam) (string, error) { path := opParam.Path if opParam.Protocol == entity.MachineProtocolRdp { - path = m.GetRdpFilePath(opParam.MachineId, path) + path = m.GetRdpFilePath(contextx.GetLoginAccount(ctx), path) stat, err := os.Stat(path) return fmt.Sprintf("%v", stat), err } @@ -210,14 +212,14 @@ func (m *machineFileAppImpl) FileStat(opParam *MachineFileOpParam) (string, erro return mcli.Run(fmt.Sprintf("stat -L %s", path)) } -func (m *machineFileAppImpl) MkDir(opParam *MachineFileOpParam) (*mcm.MachineInfo, error) { +func (m *machineFileAppImpl) MkDir(ctx context.Context, opParam *MachineFileOpParam) (*mcm.MachineInfo, error) { path := opParam.Path if !strings.HasSuffix(path, "/") { path = path + "/" } if opParam.Protocol == entity.MachineProtocolRdp { - path = m.GetRdpFilePath(opParam.MachineId, path) + path = m.GetRdpFilePath(contextx.GetLoginAccount(ctx), path) os.MkdirAll(path, os.ModePerm) return nil, nil } @@ -231,10 +233,10 @@ func (m *machineFileAppImpl) MkDir(opParam *MachineFileOpParam) (*mcm.MachineInf return mi, err } -func (m *machineFileAppImpl) CreateFile(opParam *MachineFileOpParam) (*mcm.MachineInfo, error) { +func (m *machineFileAppImpl) CreateFile(ctx context.Context, opParam *MachineFileOpParam) (*mcm.MachineInfo, error) { path := opParam.Path if opParam.Protocol == entity.MachineProtocolRdp { - path = m.GetRdpFilePath(opParam.MachineId, path) + path = m.GetRdpFilePath(contextx.GetLoginAccount(ctx), path) file, err := os.Create(path) defer file.Close() return nil, err @@ -252,7 +254,7 @@ func (m *machineFileAppImpl) CreateFile(opParam *MachineFileOpParam) (*mcm.Machi return mi, err } -func (m *machineFileAppImpl) ReadFile(opParam *MachineFileOpParam) (*sftp.File, *mcm.MachineInfo, error) { +func (m *machineFileAppImpl) ReadFile(ctx context.Context, opParam *MachineFileOpParam) (*sftp.File, *mcm.MachineInfo, error) { mi, sftpCli, err := m.GetMachineSftpCli(opParam) if err != nil { return nil, nil, err @@ -264,10 +266,10 @@ func (m *machineFileAppImpl) ReadFile(opParam *MachineFileOpParam) (*sftp.File, } // 写文件内容 -func (m *machineFileAppImpl) WriteFileContent(opParam *MachineFileOpParam, content []byte) (*mcm.MachineInfo, error) { +func (m *machineFileAppImpl) WriteFileContent(ctx context.Context, opParam *MachineFileOpParam, content []byte) (*mcm.MachineInfo, error) { path := opParam.Path if opParam.Protocol == entity.MachineProtocolRdp { - path = m.GetRdpFilePath(opParam.MachineId, path) + path = m.GetRdpFilePath(contextx.GetLoginAccount(ctx), path) file, err := os.Create(path) defer file.Close() if err != nil { @@ -292,14 +294,14 @@ func (m *machineFileAppImpl) WriteFileContent(opParam *MachineFileOpParam, conte } // 上传文件 -func (m *machineFileAppImpl) UploadFile(opParam *MachineFileOpParam, filename string, reader io.Reader) (*mcm.MachineInfo, error) { +func (m *machineFileAppImpl) UploadFile(ctx context.Context, opParam *MachineFileOpParam, filename string, reader io.Reader) (*mcm.MachineInfo, error) { path := opParam.Path if !strings.HasSuffix(path, "/") { path = path + "/" } if opParam.Protocol == entity.MachineProtocolRdp { - path = m.GetRdpFilePath(opParam.MachineId, path) + path = m.GetRdpFilePath(contextx.GetLoginAccount(ctx), path) file, err := os.Create(path + filename) defer file.Close() if err != nil { @@ -323,9 +325,9 @@ func (m *machineFileAppImpl) UploadFile(opParam *MachineFileOpParam, filename st return mi, err } -func (m *machineFileAppImpl) UploadFiles(opParam *MachineFileOpParam, basePath string, fileHeaders []*multipart.FileHeader, paths []string) (*mcm.MachineInfo, error) { +func (m *machineFileAppImpl) UploadFiles(ctx context.Context, opParam *MachineFileOpParam, basePath string, fileHeaders []*multipart.FileHeader, paths []string) (*mcm.MachineInfo, error) { if opParam.Protocol == entity.MachineProtocolRdp { - baseFolder := m.GetRdpFilePath(opParam.MachineId, basePath) + baseFolder := m.GetRdpFilePath(contextx.GetLoginAccount(ctx), basePath) for i, fileHeader := range fileHeaders { file, err := fileHeader.Open() @@ -340,7 +342,7 @@ func (m *machineFileAppImpl) UploadFiles(opParam *MachineFileOpParam, basePath s rdpBaseDir = rdpBaseDir + "/" } rdpDir := filepath.Dir(rdpBaseDir + paths[i]) - m.MkDir(&MachineFileOpParam{ + m.MkDir(ctx, &MachineFileOpParam{ MachineId: opParam.MachineId, Protocol: opParam.Protocol, Path: rdpDir, @@ -366,10 +368,10 @@ func (m *machineFileAppImpl) UploadFiles(opParam *MachineFileOpParam, basePath s } // 删除文件 -func (m *machineFileAppImpl) RemoveFile(opParam *MachineFileOpParam, path ...string) (*mcm.MachineInfo, error) { +func (m *machineFileAppImpl) RemoveFile(ctx context.Context, opParam *MachineFileOpParam, path ...string) (*mcm.MachineInfo, error) { if opParam.Protocol == entity.MachineProtocolRdp { for _, pt := range path { - pt = m.GetRdpFilePath(opParam.MachineId, pt) + pt = m.GetRdpFilePath(contextx.GetLoginAccount(ctx), pt) os.RemoveAll(pt) } return nil, nil @@ -402,11 +404,11 @@ func (m *machineFileAppImpl) RemoveFile(opParam *MachineFileOpParam, path ...str return minfo, err } -func (m *machineFileAppImpl) Copy(opParam *MachineFileOpParam, toPath string, path ...string) (*mcm.MachineInfo, error) { +func (m *machineFileAppImpl) Copy(ctx context.Context, opParam *MachineFileOpParam, toPath string, path ...string) (*mcm.MachineInfo, error) { if opParam.Protocol == entity.MachineProtocolRdp { for _, pt := range path { - srcPath := m.GetRdpFilePath(opParam.MachineId, pt) - targetPath := m.GetRdpFilePath(opParam.MachineId, toPath+pt) + srcPath := m.GetRdpFilePath(contextx.GetLoginAccount(ctx), pt) + targetPath := m.GetRdpFilePath(contextx.GetLoginAccount(ctx), toPath+pt) // 打开源文件 srcFile, err := os.Open(srcPath) @@ -438,7 +440,7 @@ func (m *machineFileAppImpl) Copy(opParam *MachineFileOpParam, toPath string, pa return mi, err } -func (m *machineFileAppImpl) Mv(opParam *MachineFileOpParam, toPath string, path ...string) (*mcm.MachineInfo, error) { +func (m *machineFileAppImpl) Mv(ctx context.Context, opParam *MachineFileOpParam, toPath string, path ...string) (*mcm.MachineInfo, error) { if opParam.Protocol == entity.MachineProtocolRdp { for _, pt := range path { // 获取文件名 @@ -447,8 +449,8 @@ func (m *machineFileAppImpl) Mv(opParam *MachineFileOpParam, toPath string, path toPath += "/" } - srcPath := m.GetRdpFilePath(opParam.MachineId, pt) - targetPath := m.GetRdpFilePath(opParam.MachineId, toPath+filename) + srcPath := m.GetRdpFilePath(contextx.GetLoginAccount(ctx), pt) + targetPath := m.GetRdpFilePath(contextx.GetLoginAccount(ctx), toPath+filename) os.Rename(srcPath, targetPath) } return nil, nil @@ -467,11 +469,11 @@ func (m *machineFileAppImpl) Mv(opParam *MachineFileOpParam, toPath string, path return mi, err } -func (m *machineFileAppImpl) Rename(opParam *MachineFileOpParam, newname string) (*mcm.MachineInfo, error) { +func (m *machineFileAppImpl) Rename(ctx context.Context, opParam *MachineFileOpParam, newname string) (*mcm.MachineInfo, error) { oldname := opParam.Path if opParam.Protocol == entity.MachineProtocolRdp { - oldname = m.GetRdpFilePath(opParam.MachineId, oldname) - newname = m.GetRdpFilePath(opParam.MachineId, newname) + oldname = m.GetRdpFilePath(contextx.GetLoginAccount(ctx), oldname) + newname = m.GetRdpFilePath(contextx.GetLoginAccount(ctx), newname) return nil, os.Rename(oldname, newname) } @@ -502,6 +504,6 @@ func (m *machineFileAppImpl) GetMachineSftpCli(opParam *MachineFileOpParam) (*mc return mcli.Info, sftpCli, nil } -func (m *machineFileAppImpl) GetRdpFilePath(machineId uint64, path string) string { - return fmt.Sprintf("%s/%d%s", config.GetMachine().GuacdFilePath, machineId, path) +func (m *machineFileAppImpl) GetRdpFilePath(ua *model.LoginAccount, path string) string { + return fmt.Sprintf("%s/%s%s", config.GetMachine().GuacdFilePath, ua.Username, path) } diff --git a/server/internal/machine/guac/guac.go b/server/internal/machine/guac/guac.go index bba66c69daa580452932025a2e7f2bb1feec116d..4492e11517ae62734239f7fc6efce72567bf5e13 100644 --- a/server/internal/machine/guac/guac.go +++ b/server/internal/machine/guac/guac.go @@ -16,7 +16,7 @@ import ( ) // creates the tunnel to the remote machine (via guacd) -func DoConnect(query url.Values, parameters map[string]string, ac string) (Tunnel, error) { +func DoConnect(query url.Values, parameters map[string]string, username string) (Tunnel, error) { conf := NewGuacamoleConfiguration() parameters["client-name"] = "mayfly" @@ -34,7 +34,7 @@ func DoConnect(query url.Values, parameters map[string]string, ac string) (Tunne parameters["enable-drive"] = "true" parameters["drive-name"] = "Filesystem" parameters["create-drive-path"] = "true" - parameters["drive-path"] = fmt.Sprintf("/rdp-file/%s", ac) + parameters["drive-path"] = fmt.Sprintf("/rdp-file/%s", username) conf.Protocol = parameters["scheme"] conf.Parameters = parameters diff --git a/server/pkg/validatorx/pattern.go b/server/pkg/validatorx/pattern.go index 9178c263242d0133c4e95aaaa6c08315dd7b059b..199ca173b2a4a82b9dfbbe0639f6c198f0caddbd 100644 --- a/server/pkg/validatorx/pattern.go +++ b/server/pkg/validatorx/pattern.go @@ -18,7 +18,7 @@ var ( func RegisterCustomPatterns() { // 账号用户名校验 RegisterPattern("account_username", "^[a-zA-Z0-9_]{5,20}$", "只允许输入5-20位大小写字母、数字、下划线") - RegisterPattern("resource_code", "^[a-zA-Z0-9_-.:]{1,32}$", "只允许输入1-32位大小写字母、数字、_-.:") + RegisterPattern("resource_code", "^[a-zA-Z0-9_\\-.:]{1,32}$", "只允许输入1-32位大小写字母、数字、_-.:") } // 注册自定义正则表达式