Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ce5ad8

Browse files
fix: Fix SSH SDK timeout and disconnection issues (1Panel-dev#9776)
1 parent 50dcb6c commit 2ce5ad8

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

‎core/utils/ssh/ssh.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ssh
22

33
import (
44
"fmt"
5+
"net"
56
"strings"
67
"time"
78

@@ -51,7 +52,7 @@ func NewClient(c ConnInfo) (*SSHClient, error) {
5152
if strings.Contains(c.Addr, ":") {
5253
proto = "tcp6"
5354
}
54-
client, err := gossh.Dial(proto, addr, config)
55+
client, err := DialWithTimeout(proto, addr, config)
5556
if nil != err {
5657
return nil, err
5758
}
@@ -207,3 +208,20 @@ func (c *SSHClient) RunWithStreamOutput(command string, outputCallback func(stri
207208

208209
return err
209210
}
211+
212+
func DialWithTimeout(network, addr string, config *gossh.ClientConfig) (*gossh.Client, error) {
213+
conn, err := net.DialTimeout(network, addr, config.Timeout)
214+
if err != nil {
215+
return nil, err
216+
}
217+
_ = conn.SetDeadline(time.Now().Add(config.Timeout))
218+
c, chans, reqs, err := gossh.NewClientConn(conn, addr, config)
219+
if err != nil {
220+
return nil, err
221+
}
222+
if err := conn.SetDeadline(time.Time{}); err != nil {
223+
conn.Close()
224+
return nil, fmt.Errorf("clear deadline failed: %v", err)
225+
}
226+
return gossh.NewClient(c, chans, reqs), nil
227+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /