From a4cab09b62db25a25ccf57dc7b6d0c8502e4e6dc Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 10 Apr 2023 17:56:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E4=B8=BB=E6=9C=BA=E4=BF=A1=E6=81=AF=E6=97=B6=EF=BC=8C=E6=9C=AA?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=AF=86=E7=A0=81=E6=88=96=E5=AF=86=E9=92=A5?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=BF=9E=E6=8E=A5=E5=A4=B1=E8=B4=A5=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#567)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/host.go | 29 ++-------------------------- backend/app/service/host.go | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 27 deletions(-) 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