fix: 解决主机密钥连接失败的问题

This commit is contained in:
ssongliu 2023-03-14 00:15:51 +08:00 committed by ssongliu
parent 2dec0bfb3c
commit 4994cc39f1
4 changed files with 9 additions and 16 deletions

View File

@ -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)
}

View File

@ -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 {

View File

@ -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"`

View File

@ -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 {