DialWithNetConn is a special case of DialWithDialFunc

This commit is contained in:
Julien Laffaye 2022-08-17 18:41:24 -04:00
parent 11536801d1
commit 560423fa8a
No known key found for this signature in database
GPG Key ID: 890C3E5C169AE841

31
ftp.go
View File

@ -70,7 +70,6 @@ type dialOptions struct {
dialer net.Dialer dialer net.Dialer
tlsConfig *tls.Config tlsConfig *tls.Config
explicitTLS bool explicitTLS bool
conn net.Conn
disableEPSV bool disableEPSV bool
disableUTF8 bool disableUTF8 bool
disableMLSD bool disableMLSD bool
@ -108,14 +107,13 @@ func Dial(addr string, options ...DialOption) (*ServerConn, error) {
do.location = time.UTC do.location = time.UTC
} }
tconn := do.conn dialFunc := do.dialFunc
if tconn == nil {
var err error
if do.dialFunc != nil { if dialFunc == nil {
tconn, err = do.dialFunc("tcp", addr) if do.tlsConfig != nil && !do.explicitTLS {
} else if do.tlsConfig != nil && !do.explicitTLS { dialFunc = func(network, address string) (net.Conn, error) {
tconn, err = tls.DialWithDialer(&do.dialer, "tcp", addr, do.tlsConfig) return tls.DialWithDialer(&do.dialer, network, addr, do.tlsConfig)
}
} else { } else {
ctx := do.context ctx := do.context
@ -123,13 +121,16 @@ func Dial(addr string, options ...DialOption) (*ServerConn, error) {
ctx = context.Background() ctx = context.Background()
} }
tconn, err = do.dialer.DialContext(ctx, "tcp", addr) dialFunc = func(network, address string) (net.Conn, error) {
return do.dialer.DialContext(ctx, network, addr)
}
}
} }
tconn, err := dialFunc("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
// Use the resolved IP address in case addr contains a domain name // Use the resolved IP address in case addr contains a domain name
// If we use the domain name, we might not resolve to the same IP. // If we use the domain name, we might not resolve to the same IP.
@ -143,7 +144,7 @@ func Dial(addr string, options ...DialOption) (*ServerConn, error) {
host: remoteAddr.IP.String(), host: remoteAddr.IP.String(),
} }
_, _, err := c.conn.ReadResponse(StatusReady) _, _, err = c.conn.ReadResponse(StatusReady)
if err != nil { if err != nil {
_ = c.Quit() _ = c.Quit()
return nil, err return nil, err
@ -185,10 +186,12 @@ func DialWithDialer(dialer net.Dialer) DialOption {
} }
// DialWithNetConn returns a DialOption that configures the ServerConn with the underlying net.Conn // DialWithNetConn returns a DialOption that configures the ServerConn with the underlying net.Conn
//
// Deprecated: Use [DialWithDialFunc] instead
func DialWithNetConn(conn net.Conn) DialOption { func DialWithNetConn(conn net.Conn) DialOption {
return DialOption{func(do *dialOptions) { return DialWithDialFunc(func(network, address string) (net.Conn, error) {
do.conn = conn return conn, nil
}} })
} }
// DialWithDisabledEPSV returns a DialOption that configures the ServerConn with EPSV disabled // DialWithDisabledEPSV returns a DialOption that configures the ServerConn with EPSV disabled