How to connect to FTP via SOCKS5 proxy with Ruby?

1.1k views Asked by At

I'm trying to connect to FTP via SOCKS5 proxy using ruby's library Net::FTP. Documentation says to set env variable SOCKS_SERVER in order to connect through proxy (http://ruby-doc.org/stdlib-2.0.0/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-connect), but it seems like it does not work.

Code I'm running is this:

irb(main):054:0> ftp = Net::FTP.new
=> #<Net::FTP:0x007efd08c73768 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x007efd08c73718>, @binary=true, @passive=true, @debug_mode=false, @resume=false, @sock=#<Net::FTP::NullSocket:0x007efd08c736f0>, @logged_in=false, @open_timeout=nil, @read_timeout=60>
irb(main):056:0> ENV['SOCKS_SERVER'] = 'host:port'
=> "host:port"
irb(main):055:0> ftp.connect('test.rebex.net')
=> nil
irb(main):057:0> ftp.login('demo', 'password')
=> true
irb(main):058:0> ftp.ls
=> ["10-27-15  03:46PM       <DIR>          pub", "04-08-14  03:09PM                  403 readme.txt"]

When I look to proxy logs I can not see any requests going through.

What I'm doing wrong or does anybody have an example how to achieve that?

1

There are 1 answers

0
Nathan On

If your on Windows computer you'll need to use dress_socks gem and Monkeypath:

$socks_server = '127.0.0.1'
$socks_port   = '9090'
require 'dress_socks' 

class Net::FTP 
  def open_socket(host, port) # :nodoc:
    # puts "opening socket #{@host}:#{port}"
    return DressSocks::Socket.new(@host, port, 
        socks_server: $socks_server, socks_port: $socks_port)
  end
end