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"
|
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-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 {
|
2024-01-21 22:52:20 +08:00
|
|
|
|
MachineApp application.Machine `inject:""`
|
|
|
|
|
|
MachineTermOpApp application.MachineTermOp `inject:""`
|
|
|
|
|
|
TagApp tagapp.TagTree `inject:"TagTreeApp"`
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *Machine) Machines(rc *req.Ctx) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
condition, pageParam := req.BindQueryAndPage(rc, 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-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) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
cli, err := m.MachineApp.GetCli(GetMachineId(rc))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
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)
|
2024-02-24 16:30:29 +08:00
|
|
|
|
me := req.BindJsonAndCopyTo(rc, 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
|
|
|
|
|
|
|
2024-01-07 21:46:25 +08:00
|
|
|
|
biz.ErrIsNil(m.MachineApp.SaveMachine(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) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
me := req.BindJsonAndCopyTo(rc, 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) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
id := uint64(rc.F.PathParamInt("machineId"))
|
|
|
|
|
|
status := int8(rc.F.PathParamInt("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) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
idsStr := rc.F.PathParam("machineId")
|
2023-07-01 14:34:42 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
cmd := "ps -aux "
|
2024-02-24 16:30:29 +08:00
|
|
|
|
sortType := rc.F.Query("sortType")
|
2022-01-16 21:45:00 +08:00
|
|
|
|
if sortType == "2" {
|
|
|
|
|
|
cmd += "--sort -pmem "
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cmd += "--sort -pcpu "
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
pname := rc.F.Query("name")
|
2022-01-16 21:45:00 +08:00
|
|
|
|
if pname != "" {
|
|
|
|
|
|
cmd += fmt.Sprintf("| grep %s ", pname)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
count := rc.F.QueryIntDefault("count", 10)
|
2023-09-05 12:49:12 +08:00
|
|
|
|
cmd += "| head -n " + fmt.Sprintf("%d", count)
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
cli, err := m.MachineApp.GetCli(GetMachineId(rc))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
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) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
pid := rc.F.Query("pid")
|
2022-01-16 21:45:00 +08:00
|
|
|
|
biz.NotEmpty(pid, "进程id不能为空")
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
cli, err := m.MachineApp.GetCli(GetMachineId(rc))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
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")
|
2024-02-23 22:53:17 +08:00
|
|
|
|
wsConn.WriteMessage(websocket.TextMessage, []byte("Connecting to host..."))
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
cli, err := m.MachineApp.NewCli(GetMachineId(rc))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
|
2024-02-23 22:53:17 +08:00
|
|
|
|
defer cli.Close()
|
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
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
cols := rc.F.QueryIntDefault("cols", 80)
|
|
|
|
|
|
rows := rc.F.QueryIntDefault("rows", 32)
|
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) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
mid := GetMachineId(rc)
|
|
|
|
|
|
res, err := m.MachineTermOpApp.GetPageList(&entity.MachineTermOp{MachineId: mid}, rc.F.GetPageParam(), new([]entity.MachineTermOp))
|
2023-12-05 23:03:51 +08:00
|
|
|
|
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) {
|
2024-02-24 16:30:29 +08:00
|
|
|
|
termOp, err := m.MachineTermOpApp.GetById(new(entity.MachineTermOp), uint64(rc.F.PathParamInt("recId")))
|
2023-12-05 23:03:51 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-24 16:30:29 +08:00
|
|
|
|
func GetMachineId(rc *req.Ctx) uint64 {
|
|
|
|
|
|
machineId, _ := strconv.Atoi(rc.F.PathParam("machineId"))
|
2021-05-08 18:00:33 +08:00
|
|
|
|
biz.IsTrue(machineId != 0, "machineId错误")
|
|
|
|
|
|
return uint64(machineId)
|
|
|
|
|
|
}
|