2022-08-18 10:54:21 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-04-10 09:56:24 +00:00
|
|
|
"encoding/base64"
|
2022-08-30 10:49:07 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-01-03 06:37:17 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/ssh"
|
2022-08-18 10:54:21 +00:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HostService struct{}
|
|
|
|
|
|
|
|
type IHostService interface {
|
2023-01-03 06:37:17 +00:00
|
|
|
TestLocalConn(id uint) bool
|
2023-04-10 09:56:24 +00:00
|
|
|
TestByInfo(req dto.HostConnTest) bool
|
2022-08-31 15:16:10 +00:00
|
|
|
GetHostInfo(id uint) (*model.Host, error)
|
2022-08-30 10:49:07 +00:00
|
|
|
SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error)
|
2023-03-01 09:43:28 +00:00
|
|
|
SearchWithPage(search dto.SearchHostWithPage) (int64, interface{}, error)
|
2022-08-31 15:16:10 +00:00
|
|
|
Create(hostDto dto.HostOperate) (*dto.HostInfo, error)
|
2022-08-18 10:54:21 +00:00
|
|
|
Update(id uint, upMap map[string]interface{}) error
|
2023-03-01 09:43:28 +00:00
|
|
|
Delete(id []uint) error
|
2022-08-18 10:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIHostService() IHostService {
|
|
|
|
return &HostService{}
|
|
|
|
}
|
|
|
|
|
2023-04-10 09:56:24 +00:00
|
|
|
func (u *HostService) TestByInfo(req dto.HostConnTest) bool {
|
|
|
|
if req.AuthMode == "password" && len(req.Password) != 0 {
|
|
|
|
password, err := base64.StdEncoding.DecodeString(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
req.Password = string(password)
|
|
|
|
}
|
|
|
|
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
|
|
|
|
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
req.PrivateKey = string(privateKey)
|
|
|
|
}
|
|
|
|
if len(req.Password) == 0 && len(req.PrivateKey) == 0 {
|
|
|
|
host, err := hostRepo.Get(hostRepo.WithByAddr(req.Addr))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
req.Password = host.Password
|
|
|
|
req.AuthMode = host.AuthMode
|
|
|
|
req.PrivateKey = host.PrivateKey
|
2023-04-10 13:50:24 +00:00
|
|
|
req.PassPhrase = host.PassPhrase
|
2023-04-10 09:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var connInfo ssh.ConnInfo
|
|
|
|
_ = copier.Copy(&connInfo, &req)
|
|
|
|
connInfo.PrivateKey = []byte(req.PrivateKey)
|
2023-04-10 13:50:24 +00:00
|
|
|
if len(req.PassPhrase) != 0 {
|
|
|
|
connInfo.PassPhrase = []byte(req.PassPhrase)
|
|
|
|
}
|
2023-04-10 09:56:24 +00:00
|
|
|
client, err := connInfo.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:37:17 +00:00
|
|
|
func (u *HostService) TestLocalConn(id uint) bool {
|
|
|
|
var (
|
|
|
|
host model.Host
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if id == 0 {
|
|
|
|
host, err = hostRepo.Get(hostRepo.WithByAddr("127.0.0.1"))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
host, err = hostRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var connInfo ssh.ConnInfo
|
|
|
|
if err := copier.Copy(&connInfo, &host); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-04-10 13:50:24 +00:00
|
|
|
connInfo.PrivateKey = []byte(host.PrivateKey)
|
|
|
|
if len(host.PassPhrase) != 0 {
|
|
|
|
connInfo.PassPhrase = []byte(host.PassPhrase)
|
|
|
|
}
|
2023-01-03 06:37:17 +00:00
|
|
|
client, err := connInfo.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-08-31 15:16:10 +00:00
|
|
|
func (u *HostService) GetHostInfo(id uint) (*model.Host, error) {
|
2022-08-18 10:54:21 +00:00
|
|
|
host, err := hostRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
return &host, err
|
|
|
|
}
|
|
|
|
|
2023-03-01 09:43:28 +00:00
|
|
|
func (u *HostService) SearchWithPage(search dto.SearchHostWithPage) (int64, interface{}, error) {
|
2023-03-06 09:18:13 +00:00
|
|
|
total, hosts, err := hostRepo.Page(search.Page, search.PageSize, hostRepo.WithByInfo(search.Info), commonRepo.WithByGroupID(search.GroupID))
|
2023-03-01 09:43:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
var dtoHosts []dto.HostInfo
|
|
|
|
for _, host := range hosts {
|
|
|
|
var item dto.HostInfo
|
|
|
|
if err := copier.Copy(&item, &host); err != nil {
|
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2023-03-06 09:18:13 +00:00
|
|
|
group, _ := groupRepo.Get(commonRepo.WithByID(host.GroupID))
|
|
|
|
item.GroupBelong = group.Name
|
2023-04-10 13:50:24 +00:00
|
|
|
if !item.RememberPassword {
|
|
|
|
item.Password = ""
|
|
|
|
item.PrivateKey = ""
|
|
|
|
item.PassPhrase = ""
|
|
|
|
}
|
2023-03-01 09:43:28 +00:00
|
|
|
dtoHosts = append(dtoHosts, item)
|
|
|
|
}
|
|
|
|
return total, dtoHosts, err
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:49:07 +00:00
|
|
|
func (u *HostService) SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error) {
|
|
|
|
hosts, err := hostRepo.GetList(hostRepo.WithByInfo(search.Info))
|
2022-08-31 15:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-06 09:18:13 +00:00
|
|
|
groups, err := groupRepo.GetList(commonRepo.WithByType("host"))
|
2022-08-31 15:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-30 10:49:07 +00:00
|
|
|
}
|
2022-08-31 15:16:10 +00:00
|
|
|
var datas []dto.HostTree
|
|
|
|
for _, group := range groups {
|
|
|
|
var data dto.HostTree
|
|
|
|
data.ID = group.ID + 10000
|
|
|
|
data.Label = group.Name
|
|
|
|
for _, host := range hosts {
|
|
|
|
label := fmt.Sprintf("%s@%s:%d", host.User, host.Addr, host.Port)
|
2022-12-30 09:36:19 +00:00
|
|
|
if len(host.Name) != 0 {
|
2023-01-03 06:37:17 +00:00
|
|
|
label = fmt.Sprintf("%s - %s@%s:%d", host.Name, host.User, host.Addr, host.Port)
|
2022-12-30 09:36:19 +00:00
|
|
|
}
|
2023-03-06 09:18:13 +00:00
|
|
|
if host.GroupID == group.ID {
|
2022-08-31 15:16:10 +00:00
|
|
|
data.Children = append(data.Children, dto.TreeChild{ID: host.ID, Label: label})
|
|
|
|
}
|
2022-08-18 10:54:21 +00:00
|
|
|
}
|
2023-03-06 09:18:13 +00:00
|
|
|
if len(data.Children) != 0 {
|
|
|
|
datas = append(datas, data)
|
|
|
|
}
|
2022-08-18 10:54:21 +00:00
|
|
|
}
|
2022-08-31 15:16:10 +00:00
|
|
|
return datas, err
|
2022-08-18 10:54:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 13:32:27 +00:00
|
|
|
func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
|
2023-02-24 10:49:34 +00:00
|
|
|
var host model.Host
|
2022-12-19 13:32:27 +00:00
|
|
|
if err := copier.Copy(&host, &req); err != nil {
|
2022-08-18 10:54:21 +00:00
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2023-03-06 09:18:13 +00:00
|
|
|
if req.GroupID == 0 {
|
|
|
|
group, err := groupRepo.Get(groupRepo.WithByIsDefault(true))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("get default group failed")
|
|
|
|
}
|
|
|
|
host.GroupID = group.ID
|
|
|
|
req.GroupID = group.ID
|
|
|
|
}
|
2022-12-19 13:32:27 +00:00
|
|
|
var sameHostID uint
|
|
|
|
if req.Addr == "127.0.0.1" {
|
|
|
|
hostSame, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr))
|
|
|
|
sameHostID = hostSame.ID
|
|
|
|
} else {
|
|
|
|
hostSame, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr), hostRepo.WithByUser(req.User), hostRepo.WithByPort(req.Port))
|
|
|
|
sameHostID = hostSame.ID
|
|
|
|
}
|
|
|
|
if sameHostID != 0 {
|
|
|
|
host.ID = sameHostID
|
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["name"] = req.Name
|
2023-03-06 09:18:13 +00:00
|
|
|
upMap["group_id"] = req.GroupID
|
2022-12-19 13:32:27 +00:00
|
|
|
upMap["addr"] = req.Addr
|
|
|
|
upMap["port"] = req.Port
|
|
|
|
upMap["user"] = req.User
|
|
|
|
upMap["auth_mode"] = req.AuthMode
|
|
|
|
upMap["password"] = req.Password
|
|
|
|
upMap["private_key"] = req.PrivateKey
|
2023-04-10 13:50:24 +00:00
|
|
|
upMap["pass_phrase"] = req.PassPhrase
|
|
|
|
upMap["remember_password"] = req.RememberPassword
|
2022-12-29 09:06:04 +00:00
|
|
|
upMap["description"] = req.Description
|
2022-12-19 13:32:27 +00:00
|
|
|
if err := hostRepo.Update(sameHostID, upMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var hostinfo dto.HostInfo
|
|
|
|
if err := copier.Copy(&hostinfo, &host); err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
return &hostinfo, nil
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:54:21 +00:00
|
|
|
if err := hostRepo.Create(&host); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var hostinfo dto.HostInfo
|
|
|
|
if err := copier.Copy(&hostinfo, &host); err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
return &hostinfo, nil
|
|
|
|
}
|
|
|
|
|
2023-03-01 09:43:28 +00:00
|
|
|
func (u *HostService) Delete(ids []uint) error {
|
|
|
|
hosts, _ := hostRepo.GetList(commonRepo.WithIdsIn(ids))
|
|
|
|
for _, host := range hosts {
|
|
|
|
if host.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
if host.Addr == "127.0.0.1" {
|
|
|
|
return errors.New("the local connection information cannot be deleted!")
|
|
|
|
}
|
2023-01-03 06:37:17 +00:00
|
|
|
}
|
2023-03-01 09:43:28 +00:00
|
|
|
return hostRepo.Delete(commonRepo.WithIdsIn(ids))
|
2022-08-18 10:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *HostService) Update(id uint, upMap map[string]interface{}) error {
|
|
|
|
return hostRepo.Update(id, upMap)
|
|
|
|
}
|