1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-14 01:34:47 +08:00

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 var connInfo ssh.ConnInfo
if err := copier.Copy(&connInfo, &req); err != nil { _ = copier.Copy(&connInfo, &req)
helper.SuccessWithData(c, false) connInfo.PrivateKey = []byte(req.PrivateKey)
}
client, err := connInfo.NewClient() client, err := connInfo.NewClient()
if err != nil { if err != nil {
helper.SuccessWithData(c, false) helper.SuccessWithData(c, false)
return
} }
defer client.Close() defer client.Close()
helper.SuccessWithData(c, true) helper.SuccessWithData(c, true)
} }

View File

@ -40,10 +40,8 @@ func (b *BaseApi) WsSsh(c *gin.Context) {
return return
} }
var connInfo ssh.ConnInfo var connInfo ssh.ConnInfo
if err := copier.Copy(&connInfo, &host); err != nil { _ = copier.Copy(&connInfo, &host)
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, constant.ErrStructTransform) connInfo.PrivateKey = []byte(host.PrivateKey)
return
}
wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil) wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil { if err != nil {

View File

@ -8,7 +8,7 @@ type HostOperate struct {
ID uint `json:"id"` ID uint `json:"id"`
GroupID uint `json:"groupID"` GroupID uint `json:"groupID"`
Name string `json:"name"` 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"` Port uint `json:"port" validate:"required,number,max=65535,min=1"`
User string `json:"user" validate:"required"` User string `json:"user" validate:"required"`
AuthMode string `json:"authMode" validate:"oneof=password key"` AuthMode string `json:"authMode" validate:"oneof=password key"`
@ -19,7 +19,7 @@ type HostOperate struct {
} }
type HostConnTest 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"` Port uint `json:"port" validate:"required,number,max=65535,min=1"`
User string `json:"user" validate:"required"` User string `json:"user" validate:"required"`
AuthMode string `json:"authMode" validate:"oneof=password key"` AuthMode string `json:"authMode" validate:"oneof=password key"`

View File

@ -4,11 +4,9 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"net"
"sync" "sync"
"time" "time"
"github.com/1Panel-dev/1Panel/backend/global"
gossh "golang.org/x/crypto/ssh" gossh "golang.org/x/crypto/ssh"
) )
@ -46,7 +44,7 @@ func (c *ConnInfo) NewClient() (*ConnInfo, error) {
} }
config.Timeout = c.DialTimeOut 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) client, err := gossh.Dial("tcp", addr, config)
if nil != err { if nil != err {
return c, err return c, err
@ -73,9 +71,7 @@ func (c *ConnInfo) Run(shell string) (string, error) {
} }
func (c *ConnInfo) Close() { func (c *ConnInfo) Close() {
if err := c.Client.Close(); err != nil { _ = c.Client.Close()
global.LOG.Errorf("close ssh client failed, err: %v", err)
}
} }
type SshConn struct { type SshConn struct {