Skip to content

Commit

Permalink
Disable EPSV for tests.
Browse files Browse the repository at this point in the history
Also disable it for next attempts when it failed.
  • Loading branch information
jlaffaye committed Nov 24, 2016
1 parent 72154df commit 988909a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
5 changes: 3 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestConnEPSV(t *testing.T) {
testConn(t, false)
}

func testConn(t *testing.T, passive bool) {
func testConn(t *testing.T, disableEPSV bool) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
Expand All @@ -31,8 +31,9 @@ func testConn(t *testing.T, passive bool) {
t.Fatal(err)
}

if passive {
if disableEPSV {
delete(c.features, "EPSV")
c.disableEPSV = true
}

err = c.Login("anonymous", "anonymous")
Expand Down
36 changes: 23 additions & 13 deletions ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ const (

// ServerConn represents the connection to a remote FTP server.
type ServerConn struct {
conn *textproto.Conn
host string
timeout time.Duration
features map[string]string
conn *textproto.Conn
host string
timeout time.Duration
features map[string]string
disableEPSV bool
}

// Entry describes a file and is returned by List().
Expand Down Expand Up @@ -219,17 +220,26 @@ func (c *ServerConn) pasv() (port int, err error) {
return
}

// getDataConnPort returns a port for a new data connection
// it uses the best available method to do so
func (c *ServerConn) getDataConnPort() (int, error) {
if !c.disableEPSV {
if port, err := c.epsv(); err == nil {
return port, nil
}

// if there is an error, disable EPSV for the next attempts
c.disableEPSV = true
}

return c.pasv()
}

// openDataConn creates a new FTP data connection.
func (c *ServerConn) openDataConn() (net.Conn, error) {
var (
port int
err error
)

if port, err = c.epsv(); err != nil {
if port, err = c.pasv(); err != nil {
return nil, err
}
port, err := c.getDataConnPort()
if err != nil {
return nil, err
}

return net.DialTimeout("tcp", net.JoinHostPort(c.host, strconv.Itoa(port)), c.timeout)
Expand Down

0 comments on commit 988909a

Please sign in to comment.