feat: optimize the SSH connection method for the host (#7126)
Some checks failed
sync2gitee / repo-sync (push) Has been cancelled

This commit is contained in:
ssongliu 2024-11-19 18:24:05 +08:00 committed by GitHub
parent 7067da7a8a
commit b6641b2776
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 24 deletions

View File

@ -277,12 +277,12 @@ func (b *BaseApi) WsSsh(c *gin.Context) {
connInfo.PassPhrase = []byte(host.PassPhrase) connInfo.PassPhrase = []byte(host.PassPhrase)
} }
client, err := connInfo.NewClient() client, err := ssh.NewClient(connInfo)
if wshandleError(wsConn, errors.WithMessage(err, "failed to set up the connection. Please check the host information")) { if wshandleError(wsConn, errors.WithMessage(err, "failed to set up the connection. Please check the host information")) {
return return
} }
defer client.Close() defer client.Close()
sws, err := terminal.NewLogicSshWsSession(cols, rows, true, connInfo.Client, wsConn) sws, err := terminal.NewLogicSshWsSession(cols, rows, true, client.Client, wsConn)
if wshandleError(wsConn, err) { if wshandleError(wsConn, err) {
return return
} }

View File

@ -65,7 +65,7 @@ func (u *HostService) TestByInfo(req dto.HostConnTest) bool {
if len(req.PassPhrase) != 0 { if len(req.PassPhrase) != 0 {
connInfo.PassPhrase = []byte(req.PassPhrase) connInfo.PassPhrase = []byte(req.PassPhrase)
} }
client, err := connInfo.NewClient() client, err := ssh.NewClient(connInfo)
if err != nil { if err != nil {
return false return false
} }
@ -114,7 +114,7 @@ func (u *HostService) TestLocalConn(id uint) bool {
} }
connInfo.PassPhrase = []byte(host.PassPhrase) connInfo.PassPhrase = []byte(host.PassPhrase)
} }
client, err := connInfo.NewClient() client, err := ssh.NewClient(connInfo)
if err != nil { if err != nil {
return false return false
} }

View File

@ -1,8 +1,8 @@
package constant package constant
const ( const (
StatusSuccess = "Success" StatusSuccess = "success"
StatusFailed = "Failed" StatusFailed = "failed"
// node // node
StatusWaiting = "waiting" StatusWaiting = "waiting"

View File

@ -20,13 +20,13 @@ type ConnInfo struct {
PrivateKey []byte `json:"privateKey"` PrivateKey []byte `json:"privateKey"`
PassPhrase []byte `json:"passPhrase"` PassPhrase []byte `json:"passPhrase"`
DialTimeOut time.Duration `json:"dialTimeOut"` DialTimeOut time.Duration `json:"dialTimeOut"`
Client *gossh.Client `json:"client"`
Session *gossh.Session `json:"session"`
LastResult string `json:"lastResult"`
} }
func (c *ConnInfo) NewClient() (*ConnInfo, error) { type SSHClient struct {
Client *gossh.Client `json:"client"`
}
func NewClient(c ConnInfo) (*SSHClient, error) {
if strings.Contains(c.Addr, ":") { if strings.Contains(c.Addr, ":") {
c.Addr = fmt.Sprintf("[%s]", c.Addr) c.Addr = fmt.Sprintf("[%s]", c.Addr)
} }
@ -55,18 +55,12 @@ func (c *ConnInfo) NewClient() (*ConnInfo, error) {
} }
client, err := gossh.Dial(proto, addr, config) client, err := gossh.Dial(proto, addr, config)
if nil != err { if nil != err {
return c, err return nil, err
} }
c.Client = client return &SSHClient{Client: client}, nil
return c, nil
} }
func (c *ConnInfo) Run(shell string) (string, error) { func (c *SSHClient) Run(shell string) (string, error) {
if c.Client == nil {
if _, err := c.NewClient(); err != nil {
return "", err
}
}
session, err := c.Client.NewSession() session, err := c.Client.NewSession()
if err != nil { if err != nil {
return "", err return "", err
@ -74,11 +68,10 @@ func (c *ConnInfo) Run(shell string) (string, error) {
defer session.Close() defer session.Close()
buf, err := session.CombinedOutput(shell) buf, err := session.CombinedOutput(shell)
c.LastResult = string(buf) return string(buf), err
return c.LastResult, err
} }
func (c *ConnInfo) Close() { func (c *SSHClient) Close() {
_ = c.Client.Close() _ = c.Client.Close()
} }
@ -88,7 +81,7 @@ type SshConn struct {
Session *gossh.Session Session *gossh.Session
} }
func (c *ConnInfo) NewSshConn(cols, rows int) (*SshConn, error) { func (c *SSHClient) NewSshConn(cols, rows int) (*SshConn, error) {
sshSession, err := c.Client.NewSession() sshSession, err := c.Client.NewSession()
if err != nil { if err != nil {
return nil, err return nil, err