From a3bb5b871464c64feefdf3c87d4db2ce85ec0152 Mon Sep 17 00:00:00 2001 From: Arnaud Ysmal Date: Wed, 4 Dec 2013 23:42:17 +0100 Subject: [PATCH] Use net.JoinHostPort and net.SplitHostPort --- ftp.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ftp.go b/ftp.go index 542565a..949a7b6 100644 --- a/ftp.go +++ b/ftp.go @@ -4,7 +4,6 @@ package ftp import ( "bufio" "errors" - "fmt" "io" "net" "net/textproto" @@ -53,10 +52,14 @@ func Connect(addr string) (*ServerConn, error) { return nil, err } - a := addr[:strings.LastIndex(addr, ":")] + host, _, err := net.SplitHostPort(addr) + if err != nil { + conn.Close() + return nil, err + } c := &ServerConn{ conn: conn, - host: a, + host: host, features: make(map[string]string), } @@ -198,7 +201,7 @@ func (c *ServerConn) openDataConn() (net.Conn, error) { _, nat6Supported := c.features["nat6"] _, epsvSupported := c.features["EPSV"] // If host is IPv6 => EPSV - if c.host[0] == '[' { + if strings.ContainsAny(c.host, ":%") { epsvSupported = true } if nat6Supported || epsvSupported { @@ -214,7 +217,7 @@ func (c *ServerConn) openDataConn() (net.Conn, error) { } // Build the new net address string - addr := fmt.Sprintf("%s:%d", c.host, port) + addr := net.JoinHostPort(c.host, strconv.Itoa(port)) conn, err := net.Dial("tcp", addr) if err != nil {