From 4994cc39f1267d16136a2ecded55fbf429d8ccc0 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Tue, 14 Mar 2023 00:15:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E5=AF=86=E9=92=A5=E8=BF=9E=E6=8E=A5=E5=A4=B1=E8=B4=A5=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/host.go | 7 +++---- backend/app/api/v1/terminal.go | 6 ++---- backend/app/dto/host.go | 4 ++-- backend/utils/ssh/ssh.go | 8 ++------ 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/backend/app/api/v1/host.go b/backend/app/api/v1/host.go index d7e69a53c..1c5331875 100644 --- a/backend/app/api/v1/host.go +++ b/backend/app/api/v1/host.go @@ -57,15 +57,14 @@ func (b *BaseApi) TestByInfo(c *gin.Context) { } var connInfo ssh.ConnInfo - if err := copier.Copy(&connInfo, &req); err != nil { - helper.SuccessWithData(c, false) - } + _ = 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) } diff --git a/backend/app/api/v1/terminal.go b/backend/app/api/v1/terminal.go index 43d04341f..7f6e74aec 100644 --- a/backend/app/api/v1/terminal.go +++ b/backend/app/api/v1/terminal.go @@ -40,10 +40,8 @@ func (b *BaseApi) WsSsh(c *gin.Context) { return } var connInfo ssh.ConnInfo - if err := copier.Copy(&connInfo, &host); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, constant.ErrStructTransform) - return - } + _ = copier.Copy(&connInfo, &host) + connInfo.PrivateKey = []byte(host.PrivateKey) wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil) if err != nil { diff --git a/backend/app/dto/host.go b/backend/app/dto/host.go index a9854a7ac..3d510404f 100644 --- a/backend/app/dto/host.go +++ b/backend/app/dto/host.go @@ -8,7 +8,7 @@ type HostOperate struct { ID uint `json:"id"` GroupID uint `json:"groupID"` Name string `json:"name"` - Addr string `json:"addr" validate:"required,ip"` + Addr string `json:"addr" validate:"required"` Port uint `json:"port" validate:"required,number,max=65535,min=1"` User string `json:"user" validate:"required"` AuthMode string `json:"authMode" validate:"oneof=password key"` @@ -19,7 +19,7 @@ type HostOperate struct { } type HostConnTest struct { - Addr string `json:"addr" validate:"required,ip"` + Addr string `json:"addr" validate:"required"` Port uint `json:"port" validate:"required,number,max=65535,min=1"` User string `json:"user" validate:"required"` AuthMode string `json:"authMode" validate:"oneof=password key"` diff --git a/backend/utils/ssh/ssh.go b/backend/utils/ssh/ssh.go index b33b76a5d..a1cd69659 100644 --- a/backend/utils/ssh/ssh.go +++ b/backend/utils/ssh/ssh.go @@ -4,11 +4,9 @@ import ( "bytes" "fmt" "io" - "net" "sync" "time" - "github.com/1Panel-dev/1Panel/backend/global" gossh "golang.org/x/crypto/ssh" ) @@ -46,7 +44,7 @@ func (c *ConnInfo) NewClient() (*ConnInfo, error) { } config.Timeout = c.DialTimeOut - config.HostKeyCallback = func(hostname string, remote net.Addr, key gossh.PublicKey) error { return nil } + config.HostKeyCallback = gossh.InsecureIgnoreHostKey() client, err := gossh.Dial("tcp", addr, config) if nil != err { return c, err @@ -73,9 +71,7 @@ func (c *ConnInfo) Run(shell string) (string, error) { } func (c *ConnInfo) Close() { - if err := c.Client.Close(); err != nil { - global.LOG.Errorf("close ssh client failed, err: %v", err) - } + _ = c.Client.Close() } type SshConn struct {