Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use proxy server with Ruby Net::FTP?

I am using the Net::FTP ruby library to connect to an FTP server and download files. It all works well, but now I need to use an outbound proxy since their firewall whitelists IP addresses, and I am using Heroku to host the site. I'm trying out the new Proximo add-on which looks promising, but I can't get Net::FTP to use it.

I see the following in the Net::FTP docs:

connect(host, port = FTP_PORT)

Establishes an FTP connection to host, optionally overriding the default port. If the environment variable SOCKS_SERVER is set, sets up the connection through a SOCKS proxy. Raises an exception (typically Errno::ECONNREFUSED) if the connection cannot be established.

But trying to set this environment variable, or setting ENV['SOCKS_SERVER'] = proximo_url doesn't work either. Does anyone know how to properly do this? It doesn't have to be a SOCKS proxy, an HTTP proxy will do, but I'm not sure that the Net::FTP library supports this.

... after some research ...

I found that Net::FTP looks for a class called SOCKSSocket, for which I found these docs. But I can't include it. include 'socket' returns false which I'm pretty sure means it's already loaded.

like image 706
Erik J Avatar asked Jul 27 '12 01:07

Erik J


1 Answers

You can use the socksify gem as in combination with eg. Quotaguard. Make sure you use one of the provided static ip's though (and not the DNS hostname) as proxy server though since FTP and loadbalancing can cause troubles.

Socksify::debug = true
proxy_uri = URI(ENV['QUOTAGUARDSTATIC_URL'])
proxy_hostname = proxy_uri.hostname
proxy_port = 1080
TCPSocket::socks_username = proxy_uri.user
TCPSocket::socks_password = proxy_uri.password
Socksify::proxy(proxy_hostname, proxy_port) do |soc| 
       Net::FTP.open(server) do |ftp|
            ftp.passive = true
            ftp.debug_mode = true 
            ftp.login user, password
            ftp.get(export, storelocation)
            ftp.close
       end
end
like image 124
tvgriek Avatar answered Oct 20 '22 01:10

tvgriek