2021-09-11 14:04:09 +08:00
|
|
|
|
package api
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2022-08-29 21:43:24 +08:00
|
|
|
|
"encoding/base64"
|
2022-01-16 21:45:00 +08:00
|
|
|
|
"fmt"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
"mayfly-go/internal/common/consts"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/machine/api/form"
|
|
|
|
|
|
"mayfly-go/internal/machine/api/vo"
|
|
|
|
|
|
"mayfly-go/internal/machine/application"
|
2023-11-12 20:14:44 +08:00
|
|
|
|
"mayfly-go/internal/machine/config"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/machine/domain/entity"
|
2023-10-30 17:34:56 +08:00
|
|
|
|
"mayfly-go/internal/machine/mcm"
|
2022-10-26 20:49:29 +08:00
|
|
|
|
tagapp "mayfly-go/internal/tag/application"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/biz"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/errorx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/ginx"
|
2022-10-26 20:49:29 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-01-14 16:29:52 +08:00
|
|
|
|
"mayfly-go/pkg/req"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/utils/anyx"
|
2023-10-12 12:14:56 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/ws"
|
2022-08-29 21:43:24 +08:00
|
|
|
|
"os"
|
|
|
|
|
|
"path"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
"strconv"
|
2023-07-01 14:34:42 +08:00
|
|
|
|
"strings"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Machine struct {
|
2023-12-05 23:03:51 +08:00
|
|
|
|
MachineApp application.Machine
|
|
|
|
|
|
MachineTermOpApp application.MachineTermOp
|
|
|
|
|
|
TagApp tagapp.TagTree
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Machine) Machines(rc *req.Ctx) {
|
2023-07-08 20:05:55 +08:00
|
|
|
|
condition, pageParam := ginx.BindQueryAndPage(rc.GinCtx, new(entity.MachineQuery))
|
2022-10-26 20:49:29 +08:00
|
|
|
|
|
|
|
|
|
|
// 不存在可访问标签id,即没有可操作数据
|
2023-12-05 23:03:51 +08:00
|
|
|
|
codes := m.TagApp.GetAccountResourceCodes(rc.GetLoginAccount().Id, consts.TagResourceTypeMachine, condition.TagPath)
|
|
|
|
|
|
if len(codes) == 0 {
|
2023-07-01 14:34:42 +08:00
|
|
|
|
rc.ResData = model.EmptyPageResult[any]()
|
2022-10-26 20:49:29 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
condition.Codes = codes
|
2021-12-11 11:19:47 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
res, err := m.MachineApp.GetMachineList(condition, pageParam, new([]*vo.MachineVO))
|
|
|
|
|
|
biz.ErrIsNil(err)
|
2021-12-11 11:19:47 +08:00
|
|
|
|
if res.Total == 0 {
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-01 14:34:42 +08:00
|
|
|
|
for _, mv := range *res.List {
|
2023-10-30 17:34:56 +08:00
|
|
|
|
mv.HasCli = mcm.HasCli(mv.Id)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
if machineStats, err := m.MachineApp.GetMachineStats(mv.Id); err == nil {
|
|
|
|
|
|
mv.Stat = collx.M{
|
|
|
|
|
|
"cpuIdle": machineStats.CPU.Idle,
|
|
|
|
|
|
"memAvailable": machineStats.MemInfo.Available,
|
|
|
|
|
|
"memTotal": machineStats.MemInfo.Total,
|
|
|
|
|
|
"fsInfos": machineStats.FSInfos,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-08 17:55:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
rc.ResData = res
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Machine) MachineStats(rc *req.Ctx) {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
|
|
|
|
|
|
rc.ResData = cli.GetAllStats()
|
2021-11-25 14:34:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
// 保存机器信息
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Machine) SaveMachine(rc *req.Ctx) {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
machineForm := new(form.MachineForm)
|
2023-07-08 20:05:55 +08:00
|
|
|
|
me := ginx.BindJsonAndCopyTo(rc.GinCtx, machineForm, new(entity.Machine))
|
2022-07-20 23:25:52 +08:00
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
machineForm.Password = "******"
|
2022-07-14 11:39:12 +08:00
|
|
|
|
rc.ReqParam = machineForm
|
|
|
|
|
|
|
2023-12-05 23:03:51 +08:00
|
|
|
|
biz.ErrIsNil(m.MachineApp.Save(rc.MetaCtx, me, machineForm.TagId...))
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
func (m *Machine) TestConn(rc *req.Ctx) {
|
2023-09-09 23:34:26 +08:00
|
|
|
|
me := ginx.BindJsonAndCopyTo(rc.GinCtx, new(form.MachineForm), new(entity.Machine))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 测试连接
|
|
|
|
|
|
biz.ErrIsNilAppendErr(m.MachineApp.TestConn(me), "该机器无法连接: %s")
|
2022-08-02 21:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Machine) ChangeStatus(rc *req.Ctx) {
|
2022-04-27 10:59:02 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
id := uint64(ginx.PathParamInt(g, "machineId"))
|
|
|
|
|
|
status := int8(ginx.PathParamInt(g, "status"))
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("id", id, "status", status)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(m.MachineApp.ChangeStatus(rc.MetaCtx, id, status))
|
2022-04-27 10:59:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Machine) DeleteMachine(rc *req.Ctx) {
|
2023-07-01 14:34:42 +08:00
|
|
|
|
idsStr := ginx.PathParam(rc.GinCtx, "machineId")
|
|
|
|
|
|
rc.ReqParam = idsStr
|
|
|
|
|
|
ids := strings.Split(idsStr, ",")
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range ids {
|
|
|
|
|
|
value, err := strconv.Atoi(v)
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
|
2023-11-07 21:05:21 +08:00
|
|
|
|
m.MachineApp.Delete(rc.MetaCtx, uint64(value))
|
2023-07-01 14:34:42 +08:00
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-08 17:55:57 +08:00
|
|
|
|
// 关闭机器客户端
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Machine) CloseCli(rc *req.Ctx) {
|
2023-10-30 17:34:56 +08:00
|
|
|
|
mcm.DeleteCli(GetMachineId(rc.GinCtx))
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-16 21:45:00 +08:00
|
|
|
|
// 获取进程列表信息
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Machine) GetProcess(rc *req.Ctx) {
|
2022-01-16 21:45:00 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
cmd := "ps -aux "
|
|
|
|
|
|
sortType := g.Query("sortType")
|
|
|
|
|
|
if sortType == "2" {
|
|
|
|
|
|
cmd += "--sort -pmem "
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cmd += "--sort -pcpu "
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pname := g.Query("name")
|
|
|
|
|
|
if pname != "" {
|
|
|
|
|
|
cmd += fmt.Sprintf("| grep %s ", pname)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-05 12:49:12 +08:00
|
|
|
|
count := ginx.QueryInt(g, "count", 10)
|
|
|
|
|
|
cmd += "| head -n " + fmt.Sprintf("%d", count)
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
|
2023-12-05 23:03:51 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.GetLoginAccount().Id, cli.Info.TagPath...), "%s")
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
|
|
|
|
|
res, err := cli.Run(cmd)
|
2022-01-16 21:45:00 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取进程信息失败: %s")
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 终止进程
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Machine) KillProcess(rc *req.Ctx) {
|
2022-01-16 21:45:00 +08:00
|
|
|
|
pid := rc.GinCtx.Query("pid")
|
|
|
|
|
|
biz.NotEmpty(pid, "进程id不能为空")
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
|
2023-12-05 23:03:51 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.GetLoginAccount().Id, cli.Info.TagPath...), "%s")
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2023-09-08 22:24:45 +08:00
|
|
|
|
res, err := cli.Run("sudo kill -9 " + pid)
|
|
|
|
|
|
biz.ErrIsNil(err, "终止进程失败: %s", res)
|
2022-01-16 21:45:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
func (m *Machine) WsSSH(g *gin.Context) {
|
2021-11-11 15:56:02 +08:00
|
|
|
|
wsConn, err := ws.Upgrader.Upgrade(g.Writer, g.Request, nil)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
defer func() {
|
2022-08-29 21:43:24 +08:00
|
|
|
|
if wsConn != nil {
|
|
|
|
|
|
if err := recover(); err != nil {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
wsConn.WriteMessage(websocket.TextMessage, []byte(anyx.ToString(err)))
|
2022-08-29 21:43:24 +08:00
|
|
|
|
}
|
2021-05-08 18:00:33 +08:00
|
|
|
|
wsConn.Close()
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2022-08-29 21:43:24 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "升级websocket失败: %s")
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 权限校验
|
2023-01-14 16:29:52 +08:00
|
|
|
|
rc := req.NewCtxWithGin(g).WithRequiredPermission(req.NewPermission("machine:terminal"))
|
|
|
|
|
|
if err = req.PermissionHandler(rc); err != nil {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
panic(errorx.NewBiz("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m"))
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
cli, err := m.MachineApp.GetCli(GetMachineId(g))
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
|
2023-12-05 23:03:51 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.GetLoginAccount().Id, cli.Info.TagPath...), "%s")
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2022-08-13 19:31:16 +08:00
|
|
|
|
cols := ginx.QueryInt(g, "cols", 80)
|
|
|
|
|
|
rows := ginx.QueryInt(g, "rows", 40)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2022-11-18 17:52:30 +08:00
|
|
|
|
// 记录系统操作日志
|
2023-07-08 20:05:55 +08:00
|
|
|
|
rc.WithLog(req.NewLogSave("机器-终端操作"))
|
2023-10-30 17:34:56 +08:00
|
|
|
|
rc.ReqParam = cli.Info
|
2023-01-14 16:29:52 +08:00
|
|
|
|
req.LogHandler(rc)
|
2022-11-18 17:52:30 +08:00
|
|
|
|
|
2023-12-05 23:03:51 +08:00
|
|
|
|
err = m.MachineTermOpApp.TermConn(rc.MetaCtx, cli, wsConn, rows, cols)
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "\033[1;31m连接失败: %s\033[0m")
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-05 23:03:51 +08:00
|
|
|
|
func (m *Machine) MachineTermOpRecords(rc *req.Ctx) {
|
|
|
|
|
|
mid := GetMachineId(rc.GinCtx)
|
|
|
|
|
|
res, err := m.MachineTermOpApp.GetPageList(&entity.MachineTermOp{MachineId: mid}, ginx.GetPageParam(rc.GinCtx), new([]entity.MachineTermOp))
|
|
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
2022-08-29 21:43:24 +08:00
|
|
|
|
|
2023-12-05 23:03:51 +08:00
|
|
|
|
func (m *Machine) MachineTermOpRecord(rc *req.Ctx) {
|
|
|
|
|
|
recId, _ := strconv.Atoi(rc.GinCtx.Param("recId"))
|
|
|
|
|
|
termOp, err := m.MachineTermOpApp.GetById(new(entity.MachineTermOp), uint64(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)
|
2022-08-29 21:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
func GetMachineId(g *gin.Context) uint64 {
|
|
|
|
|
|
machineId, _ := strconv.Atoi(g.Param("machineId"))
|
|
|
|
|
|
biz.IsTrue(machineId != 0, "machineId错误")
|
|
|
|
|
|
return uint64(machineId)
|
|
|
|
|
|
}
|