@@ -26,6 +26,7 @@ const defaultDialTimeout = 10 * time.Second
2626type VerifyConfig struct {
2727 Host string
2828 Port int
29+ DialTimeout time.Duration
2930 User string
3031 Password []byte
3132 KeyPath string
@@ -58,6 +59,7 @@ func (e *UnknownHostError) Error() string {
5859type Session struct {
5960 Host string
6061 Port int
62+ DialTimeout time.Duration
6163 User string
6264 Password []byte
6365 KeyPath string
@@ -83,7 +85,7 @@ func (s *Session) RunWithContext(ctx context.Context) error {
8385 if ctx == nil {
8486 ctx = context .Background ()
8587 }
86- dialCtx , cancel := withDefaultTimeout (ctx )
88+ dialCtx , cancel := withDefaultTimeout (ctx , s . DialTimeout )
8789 defer cancel ()
8890
8991 defer s .zeroPassword ()
@@ -454,7 +456,7 @@ func VerifyWithContext(ctx context.Context, cfg VerifyConfig) error {
454456 if ctx == nil {
455457 ctx = context .Background ()
456458 }
457- dialCtx , cancel := withDefaultTimeout (ctx )
459+ dialCtx , cancel := withDefaultTimeout (ctx , cfg . DialTimeout )
458460 defer cancel ()
459461
460462 authMethods , err := buildAuthMethods (cfg .Password , cfg .KeyPath , cfg .KeyData , cfg .KeyPassphrase )
@@ -545,11 +547,14 @@ func ensureKnownHostsFile() (string, error) {
545547 return khPath , nil
546548}
547549
548- func withDefaultTimeout (ctx context.Context ) (context.Context , context.CancelFunc ) {
550+ func withDefaultTimeout (ctx context.Context , timeout time. Duration ) (context.Context , context.CancelFunc ) {
549551 if _ , ok := ctx .Deadline (); ok {
550552 return ctx , func () {}
551553 }
552- return context .WithTimeout (ctx , defaultDialTimeout )
554+ if timeout <= 0 {
555+ timeout = defaultDialTimeout
556+ }
557+ return context .WithTimeout (ctx , timeout )
553558}
554559
555560func dialSSHClientWithContext (ctx context.Context , addr string , config * ssh.ClientConfig ) (* ssh.Client , error ) {
0 commit comments