mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-11-23 18:49:21 +08:00
feat: optimize the SSH connection method for the host (#7126)
Some checks failed
sync2gitee / repo-sync (push) Has been cancelled
Some checks failed
sync2gitee / repo-sync (push) Has been cancelled
This commit is contained in:
parent
7067da7a8a
commit
b6641b2776
@ -277,12 +277,12 @@ func (b *BaseApi) WsSsh(c *gin.Context) {
|
||||
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")) {
|
||||
return
|
||||
}
|
||||
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) {
|
||||
return
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ func (u *HostService) TestByInfo(req dto.HostConnTest) bool {
|
||||
if len(req.PassPhrase) != 0 {
|
||||
connInfo.PassPhrase = []byte(req.PassPhrase)
|
||||
}
|
||||
client, err := connInfo.NewClient()
|
||||
client, err := ssh.NewClient(connInfo)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@ -114,7 +114,7 @@ func (u *HostService) TestLocalConn(id uint) bool {
|
||||
}
|
||||
connInfo.PassPhrase = []byte(host.PassPhrase)
|
||||
}
|
||||
client, err := connInfo.NewClient()
|
||||
client, err := ssh.NewClient(connInfo)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package constant
|
||||
|
||||
const (
|
||||
StatusSuccess = "Success"
|
||||
StatusFailed = "Failed"
|
||||
StatusSuccess = "success"
|
||||
StatusFailed = "failed"
|
||||
|
||||
// node
|
||||
StatusWaiting = "waiting"
|
||||
|
@ -20,13 +20,13 @@ type ConnInfo struct {
|
||||
PrivateKey []byte `json:"privateKey"`
|
||||
PassPhrase []byte `json:"passPhrase"`
|
||||
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, ":") {
|
||||
c.Addr = fmt.Sprintf("[%s]", c.Addr)
|
||||
}
|
||||
@ -55,18 +55,12 @@ func (c *ConnInfo) NewClient() (*ConnInfo, error) {
|
||||
}
|
||||
client, err := gossh.Dial(proto, addr, config)
|
||||
if nil != err {
|
||||
return c, err
|
||||
return nil, err
|
||||
}
|
||||
c.Client = client
|
||||
return c, nil
|
||||
return &SSHClient{Client: client}, nil
|
||||
}
|
||||
|
||||
func (c *ConnInfo) Run(shell string) (string, error) {
|
||||
if c.Client == nil {
|
||||
if _, err := c.NewClient(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
func (c *SSHClient) Run(shell string) (string, error) {
|
||||
session, err := c.Client.NewSession()
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -74,11 +68,10 @@ func (c *ConnInfo) Run(shell string) (string, error) {
|
||||
defer session.Close()
|
||||
buf, err := session.CombinedOutput(shell)
|
||||
|
||||
c.LastResult = string(buf)
|
||||
return c.LastResult, err
|
||||
return string(buf), err
|
||||
}
|
||||
|
||||
func (c *ConnInfo) Close() {
|
||||
func (c *SSHClient) Close() {
|
||||
_ = c.Client.Close()
|
||||
}
|
||||
|
||||
@ -88,7 +81,7 @@ type SshConn struct {
|
||||
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()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Loading…
Reference in New Issue
Block a user