From 1edcd4b3ca1f294ab60e0e2cbabcebd0b1448f83 Mon Sep 17 00:00:00 2001 From: Cytown Date: Fri, 7 Jun 2024 16:35:45 +0800 Subject: [PATCH] fix dial stuck when connect to tls ftp without tls configuration Signed-off-by: Cytown --- ftp.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/ftp.go b/ftp.go index 02136ae..05c5096 100644 --- a/ftp.go +++ b/ftp.go @@ -161,11 +161,30 @@ func Dial(addr string, options ...DialOption) (*ServerConn, error) { host: remoteAddr.IP.String(), } - _, _, err = c.conn.ReadResponse(StatusReady) - if err != nil { - _ = c.Quit() - return nil, err + readyChan := make(chan error, 1) + go func(c *ServerConn) { + _, _, err = c.conn.ReadResponse(StatusReady) + if err != nil { + _ = c.Quit() + readyChan <- err + return + } + readyChan <- nil + }(c) + + readyTimeer := time.NewTimer(do.dialer.Timeout) + select { + case err = <-readyChan:{ + if err != nil { + return nil, err + } + break + } + case <-readyTimeer.C:{ + return nil, errors.New("Timeout waiting for server to be ready, maybe need TLS?") + } } + close(readyChan) if do.explicitTLS { if err := c.authTLS(); err != nil {