Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions persistent_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package goftp
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"net"
"net/textproto"
Expand Down Expand Up @@ -345,9 +346,41 @@ PASV:
port |= portOctet << (byte(1-i) * 8)
}

pconn.debug("check returned IP routable: %s", ip.String())
isRoutable, err := isRoutableIP(ip.String())
if err != nil {
pconn.debug("failed to resolve whether IP %v is routable: %s", ip.String(), err)
}
if !isRoutable {
pconn.debug("returned IP %v is not routable, use server address instead", ip.String())

remoteHost, _, err = net.SplitHostPort(pconn.controlConn.RemoteAddr().String())
if err != nil {
pconn.debug("failed determining remote host: %s", err)

} else {
return net.JoinHostPort(remoteHost, strconv.Itoa(port)), nil
}
}

return net.JoinHostPort(ip.String(), strconv.Itoa(port)), nil
}

func isRoutableIP(ip string) (bool, error) {
var err error
private := false
IP := net.ParseIP(ip)
if IP == nil {
err = errors.New("Invalid IP")
} else {
_, private24BitBlock, _ := net.ParseCIDR("10.0.0.0/8")
_, private20BitBlock, _ := net.ParseCIDR("172.16.0.0/12")
_, private16BitBlock, _ := net.ParseCIDR("192.168.0.0/16")
private = private24BitBlock.Contains(IP) || private20BitBlock.Contains(IP) || private16BitBlock.Contains(IP)
}
return !private, err
}

type dataConn struct {
net.Conn
Timeout time.Duration
Expand Down Expand Up @@ -420,6 +453,11 @@ func (pconn *persistentConn) prepareDataConn() (func() (net.Conn, error), error)
pconn.debug("upgrading data connection to TLS")
dc = tls.Client(dc, pconn.config.TLSConfig)
}
if dc == nil {
pconn.debug("data connection lost after upgrade to TLS")
} else {
pconn.debug("connection available")
}

return func() (net.Conn, error) {
pconn.dataConn = &dataConn{
Expand Down