From fa01896d676e87d27e0a6a0e0570899f46aa917d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20Gul=C3=A1csi?= Date: Fri, 29 Jul 2022 09:09:44 +0200 Subject: [PATCH] Fix PASV address like lftp does --- ftp.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ftp.go b/ftp.go index abe944f..146a8fc 100644 --- a/ftp.go +++ b/ftp.go @@ -10,6 +10,7 @@ import ( "errors" "io" "net" + "net/netip" "net/textproto" "strconv" "strings" @@ -497,6 +498,19 @@ func (c *ServerConn) pasv() (host string, port int, err error) { // Make the IP address to connect to host = strings.Join(pasvData[0:4], ".") + + if c.host != host { + if cmdIP, err := netip.ParseAddr(c.host); err == nil { + if dataIP, err := netip.ParseAddr(host); err == nil { + // Logic stolen from lftp (https://github.com/lavv17/lftp/blob/d67fc14d085849a6b0418bb3e912fea2e94c18d1/src/ftpclass.cc#L769) + if dataIP.IsMulticast() || + cmdIP.IsPrivate() != dataIP.IsPrivate() || + cmdIP.IsLoopback() != dataIP.IsLoopback() { + return c.host, port, nil + } + } + } + } return host, port, nil }