Skip to content

Instantly share code, notes, and snippets.

@icebreaker
Last active December 17, 2015 07:48
Show Gist options
  • Select an option

  • Save icebreaker/5575100 to your computer and use it in GitHub Desktop.

Select an option

Save icebreaker/5575100 to your computer and use it in GitHub Desktop.
Fallback to the server's external ip address if the IP returned by requesting passive mode is not routable.
require 'net/ftp'
module Net
class FTP
alias :old_makepasv :makepasv
def makepasv # :nodoc:
host, port = old_makepasv
host = @sock.peeraddr[3] unless is_routeable?(host)
return host, port
end
def is_routeable?(host) # :nodoc:
if @sock.peeraddr[0] == "AF_INET"
# http://snipplr.com/view/31058/ - this is good enough :)
host.match(/(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)|(^127\.0\.0\.1)/).nil?
else
# TODO: IPv6
true
end
end
end
end
if $0 == __FILE__
ftp = Net::FTP.new('yourhost')
ftp.login('user', 'pass')
ftp.passive = true
puts ftp.list
ftp.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment