mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-04 00:10:25 +08:00
feat: 新增机器授权凭证管理与其他优化
This commit is contained in:
@@ -14,7 +14,10 @@ type Machine interface {
|
||||
// 根据条件获取账号信息
|
||||
GetMachine(condition *entity.Machine, cols ...string) error
|
||||
|
||||
Save(entity *entity.Machine)
|
||||
Save(*entity.Machine)
|
||||
|
||||
// 测试机器连接
|
||||
TestConn(me *entity.Machine)
|
||||
|
||||
// 调整机器状态
|
||||
ChangeStatus(id uint64, status int8)
|
||||
@@ -33,16 +36,19 @@ type Machine interface {
|
||||
GetCli(id uint64) *machine.Cli
|
||||
|
||||
// 获取ssh隧道机器连接
|
||||
GetSshTunnelMachine(id uint64) *machine.SshTunnelMachine
|
||||
GetSshTunnelMachine(id int) *machine.SshTunnelMachine
|
||||
}
|
||||
|
||||
func newMachineApp(machineRepo repository.Machine) Machine {
|
||||
return &machineAppImpl{machineRepo: machineRepo}
|
||||
|
||||
func newMachineApp(machineRepo repository.Machine, authCertApp AuthCert) Machine {
|
||||
return &machineAppImpl{
|
||||
machineRepo: machineRepo,
|
||||
authCertApp: authCertApp,
|
||||
}
|
||||
}
|
||||
|
||||
type machineAppImpl struct {
|
||||
machineRepo repository.Machine
|
||||
authCertApp AuthCert
|
||||
}
|
||||
|
||||
// 分页获取机器信息列表
|
||||
@@ -54,36 +60,35 @@ func (m *machineAppImpl) Count(condition *entity.MachineQuery) int64 {
|
||||
return m.machineRepo.Count(condition)
|
||||
}
|
||||
|
||||
// 根据条件获取机器信息
|
||||
func (m *machineAppImpl) Save(me *entity.Machine) {
|
||||
// ’修改机器信息且密码不为空‘ or ‘新增’需要测试是否可连接
|
||||
if (me.Id != 0 && me.Password != "") || me.Id == 0 {
|
||||
biz.ErrIsNilAppendErr(machine.TestConn(*me, func(u uint64) *entity.Machine {
|
||||
me := m.GetById(u)
|
||||
me.PwdDecrypt()
|
||||
return me
|
||||
}), "该机器无法连接: %s")
|
||||
}
|
||||
|
||||
oldMachine := &entity.Machine{Ip: me.Ip, Port: me.Port, Username: me.Username}
|
||||
err := m.GetMachine(oldMachine)
|
||||
|
||||
if me.Id != 0 {
|
||||
// 如果存在该库,则校验修改的库是否为该库
|
||||
if err == nil {
|
||||
biz.IsTrue(oldMachine.Id == me.Id, "该机器信息已存在")
|
||||
}
|
||||
// 关闭连接
|
||||
machine.DeleteCli(me.Id)
|
||||
me.PwdEncrypt()
|
||||
m.machineRepo.UpdateById(me)
|
||||
} else {
|
||||
me.PwdEncrypt()
|
||||
if me.Id == 0 {
|
||||
biz.IsTrue(err != nil, "该机器信息已存在")
|
||||
// 新增机器,默认启用状态
|
||||
me.Status = entity.MachineStatusEnable
|
||||
me.PwdEncrypt()
|
||||
m.machineRepo.Create(me)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果存在该库,则校验修改的库是否为该库
|
||||
if err == nil {
|
||||
biz.IsTrue(oldMachine.Id == me.Id, "该机器信息已存在")
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
machine.DeleteCli(me.Id)
|
||||
m.machineRepo.UpdateById(me)
|
||||
}
|
||||
|
||||
func (m *machineAppImpl) TestConn(me *entity.Machine) {
|
||||
me.Id = 0
|
||||
// 测试连接
|
||||
biz.ErrIsNilAppendErr(machine.TestConn(*m.toMachineInfo(me), func(u uint64) *machine.Info {
|
||||
return m.toMachineInfoById(u)
|
||||
}), "该机器无法连接: %s")
|
||||
}
|
||||
|
||||
func (m *machineAppImpl) ChangeStatus(id uint64, status int8) {
|
||||
@@ -128,24 +133,55 @@ func (m *machineAppImpl) GetById(id uint64, cols ...string) *entity.Machine {
|
||||
return m.machineRepo.GetById(id, cols...)
|
||||
}
|
||||
|
||||
func (m *machineAppImpl) GetCli(id uint64) *machine.Cli {
|
||||
cli, err := machine.GetCli(id, func(machineId uint64) *entity.Machine {
|
||||
machine := m.GetById(machineId)
|
||||
machine.PwdDecrypt()
|
||||
biz.IsTrue(machine.Status == entity.MachineStatusEnable, "该机器已被停用")
|
||||
return machine
|
||||
func (m *machineAppImpl) GetCli(machineId uint64) *machine.Cli {
|
||||
cli, err := machine.GetCli(machineId, func(mid uint64) *machine.Info {
|
||||
return m.toMachineInfoById(mid)
|
||||
})
|
||||
biz.ErrIsNilAppendErr(err, "获取客户端错误: %s")
|
||||
return cli
|
||||
}
|
||||
|
||||
func (m *machineAppImpl) GetSshTunnelMachine(id uint64) *machine.SshTunnelMachine {
|
||||
sshTunnel, err := machine.GetSshTunnelMachine(id, func(machineId uint64) *entity.Machine {
|
||||
machine := m.GetById(machineId)
|
||||
machine.PwdDecrypt()
|
||||
biz.IsTrue(machine.Status == entity.MachineStatusEnable, "该机器已被停用")
|
||||
return machine
|
||||
func (m *machineAppImpl) GetSshTunnelMachine(machineId int) *machine.SshTunnelMachine {
|
||||
sshTunnel, err := machine.GetSshTunnelMachine(machineId, func(mid uint64) *machine.Info {
|
||||
return m.toMachineInfoById(mid)
|
||||
})
|
||||
biz.ErrIsNilAppendErr(err, "获取ssh隧道连接失败: %s")
|
||||
return sshTunnel
|
||||
}
|
||||
|
||||
// 生成机器信息,根据授权凭证id填充用户密码等
|
||||
func (m *machineAppImpl) toMachineInfoById(machineId uint64) *machine.Info {
|
||||
me := m.GetById(machineId)
|
||||
biz.IsTrue(me.Status == entity.MachineStatusEnable, "该机器已被停用")
|
||||
|
||||
return m.toMachineInfo(me)
|
||||
}
|
||||
|
||||
func (m *machineAppImpl) toMachineInfo(me *entity.Machine) *machine.Info {
|
||||
mi := new(machine.Info)
|
||||
mi.Id = me.Id
|
||||
mi.Name = me.Name
|
||||
mi.Ip = me.Ip
|
||||
mi.Port = me.Port
|
||||
mi.Username = me.Username
|
||||
mi.TagId = me.TagId
|
||||
mi.TagPath = me.TagPath
|
||||
mi.EnableRecorder = me.EnableRecorder
|
||||
mi.SshTunnelMachineId = me.SshTunnelMachineId
|
||||
|
||||
if me.UseAuthCert() {
|
||||
ac := m.authCertApp.GetById(uint64(me.AuthCertId))
|
||||
biz.NotNil(ac, "授权凭证信息已不存在,请重新关联")
|
||||
mi.AuthMethod = ac.AuthMethod
|
||||
ac.PwdDecrypt()
|
||||
mi.Password = ac.Password
|
||||
mi.Passphrase = ac.Passphrase
|
||||
} else {
|
||||
mi.AuthMethod = entity.AuthCertAuthMethodPassword
|
||||
if me.Id != 0 {
|
||||
me.PwdDecrypt()
|
||||
}
|
||||
mi.Password = me.Password
|
||||
}
|
||||
return mi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user