Skip to content

Instantly share code, notes, and snippets.

@gregawoods
Created January 24, 2013 14:43
Show Gist options
  • Save gregawoods/4622460 to your computer and use it in GitHub Desktop.
Save gregawoods/4622460 to your computer and use it in GitHub Desktop.
FTP servers are supposed to respond to a PASV command by sending their respective passive port and IP address. Sometimes a misconfigured server that resides behind a NAT will respond with its private IP rather than a public one, which means your passive connection will fail. This little hack saved the day for me.
class IPAddr
def is_private?
return between?('172.16.0.0','172.31.255.255') || between?('10.0.0.0','10.255.255.255') || between?('192.168.0.0','192.168.255.255')
end
end
class Net::FTP
alias_method :parse227_original, :parse227
alias_method :connect_original, :connect
def connect(host, port = FTP_PORT)
@_true_hostname = host
connect_original(host, port)
end
private
def parse227(resp)
host, port = parse227_original(resp)
begin
ip = IPAddr.new(host)
if ip.is_private?
puts "Fixing bogus PASV data address from #{host} to #{@_true_hostname}" if @debug_mode
host = @hostname
end
end
return host, port
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment