Last active
December 17, 2015 07:48
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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