diff --git a/backend/app/api/v1/host.go b/backend/app/api/v1/host.go index 4755bade9..7bd794138 100644 --- a/backend/app/api/v1/host.go +++ b/backend/app/api/v1/host.go @@ -8,7 +8,6 @@ import ( "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/copier" - "github.com/1Panel-dev/1Panel/backend/utils/ssh" "github.com/gin-gonic/gin" ) @@ -74,33 +73,9 @@ func (b *BaseApi) TestByInfo(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) 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 - _ = copier.Copy(&connInfo, &req) - connInfo.PrivateKey = []byte(req.PrivateKey) - client, err := connInfo.NewClient() - if err != nil { - helper.SuccessWithData(c, false) - return - } - defer client.Close() - helper.SuccessWithData(c, true) + connStatus := hostService.TestByInfo(req) + helper.SuccessWithData(c, connStatus) } // @Tags Host diff --git a/backend/app/service/host.go b/backend/app/service/host.go index 6ad56e7be..5286c5312 100644 --- a/backend/app/service/host.go +++ b/backend/app/service/host.go @@ -1,6 +1,7 @@ package service import ( + "encoding/base64" "fmt" "github.com/1Panel-dev/1Panel/backend/app/dto" @@ -15,6 +16,7 @@ type HostService struct{} type IHostService interface { TestLocalConn(id uint) bool + TestByInfo(req dto.HostConnTest) bool GetHostInfo(id uint) (*model.Host, error) SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error) SearchWithPage(search dto.SearchHostWithPage) (int64, interface{}, error) @@ -27,6 +29,42 @@ func NewIHostService() IHostService { 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 { var ( host model.Host