mirror of https://github.com/1Panel-dev/1Panel
fix: 解决编辑主机信息时,未输入密码或密钥测试连接失败的问题 (#567)
parent
12d010351a
commit
a4cab09b62
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/1Panel-dev/1Panel/backend/constant"
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
||||||
"github.com/1Panel-dev/1Panel/backend/global"
|
"github.com/1Panel-dev/1Panel/backend/global"
|
||||||
"github.com/1Panel-dev/1Panel/backend/utils/copier"
|
"github.com/1Panel-dev/1Panel/backend/utils/copier"
|
||||||
"github.com/1Panel-dev/1Panel/backend/utils/ssh"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -74,33 +73,9 @@ func (b *BaseApi) TestByInfo(c *gin.Context) {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.AuthMode == "password" && len(req.Password) != 0 {
|
|
||||||
password, err := base64.StdEncoding.DecodeString(req.Password)
|
|
||||||
if err != nil {
|
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req.Password = string(password)
|
|
||||||
}
|
|
||||||
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
|
|
||||||
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
|
|
||||||
if err != nil {
|
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req.PrivateKey = string(privateKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
var connInfo ssh.ConnInfo
|
connStatus := hostService.TestByInfo(req)
|
||||||
_ = copier.Copy(&connInfo, &req)
|
helper.SuccessWithData(c, connStatus)
|
||||||
connInfo.PrivateKey = []byte(req.PrivateKey)
|
|
||||||
client, err := connInfo.NewClient()
|
|
||||||
if err != nil {
|
|
||||||
helper.SuccessWithData(c, false)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer client.Close()
|
|
||||||
helper.SuccessWithData(c, true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Tags Host
|
// @Tags Host
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||||
|
@ -15,6 +16,7 @@ type HostService struct{}
|
||||||
|
|
||||||
type IHostService interface {
|
type IHostService interface {
|
||||||
TestLocalConn(id uint) bool
|
TestLocalConn(id uint) bool
|
||||||
|
TestByInfo(req dto.HostConnTest) bool
|
||||||
GetHostInfo(id uint) (*model.Host, error)
|
GetHostInfo(id uint) (*model.Host, error)
|
||||||
SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error)
|
SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error)
|
||||||
SearchWithPage(search dto.SearchHostWithPage) (int64, interface{}, error)
|
SearchWithPage(search dto.SearchHostWithPage) (int64, interface{}, error)
|
||||||
|
@ -27,6 +29,42 @@ func NewIHostService() IHostService {
|
||||||
return &HostService{}
|
return &HostService{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
var connInfo ssh.ConnInfo
|
||||||
|
_ = copier.Copy(&connInfo, &req)
|
||||||
|
connInfo.PrivateKey = []byte(req.PrivateKey)
|
||||||
|
client, err := connInfo.NewClient()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (u *HostService) TestLocalConn(id uint) bool {
|
func (u *HostService) TestLocalConn(id uint) bool {
|
||||||
var (
|
var (
|
||||||
host model.Host
|
host model.Host
|
||||||
|
|
Loading…
Reference in New Issue