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

I am using the ruby ​​Net :: FTP library to connect to an FTP server and upload files. Everything works fine, but now I need to use an outgoing proxy, as their firewall assigns white IP addresses, and I use Heroku to host the site. I am testing a new Proximo add-on that looks promising, but I cannot use Net :: FTP to use it.

I see the following in Net :: FTP docs :

connect (host, port = FTP_PORT)

Establishes an FTP connection with the host, optionally overriding the default port. If the SOCKS_SERVER environment variable is set, a connection is established through the SOCKS proxy server. Throws an exception (usually Errno :: ECONNREFUSED) if the connection cannot be established.

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

... after some research ...

I found that Net :: FTP is looking for a class called SOCKSSocket , for which I found these documents . But I can’t turn it on. include 'socket' returns false, which, I am sure, means that it is already loaded.

+6
source share
4 answers

You can use socksify gem , as in combination with, for example. Quotaguard. Make sure that you use one of the provided static IPs, although (not the DNS name), as the proxy server, though, since FTP and loadbalancing can cause problems.

 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 
+1
source

Maybe this link can be useful to you?

https://github.com/plevel/ruby-ftp-proxy

0
source

This is my solution to the problem. Boiler Plate:

 # Connects to ftp through a socks proxy # # TODO: testing, use ssl, capture debugging output, recovery on network failure... # # @param ftpHost [String ] Domain name or ip address of the ftp server # @param proxyHost [String ] Host name or ip address of the socks proxy # @param proxyPort [FixNum ] Port number of the socks proxy # @param proxyUser [String ] User name for connecting to the proxy if needed # @param proxyPass [String ] Password for connecting to the proxy if needed # @param ftpPort [FixNum ] Ftp port, defaults to 21 # @param ftpUser [String ] Ftp username # @param ftpPass [String ] Ftp password # @param debug [Boolean] Whether to turn on debug output of both socksify and Net::FTP, will be sent to STDOUT # @param passive [Boolean] Whether to use passive FTP mode # def ftp( ftpHost: , proxyHost: , proxyPort: , proxyUser: nil , proxyPass: nil , ftpPort: Net::FTP::FTP_PORT , ftpUser: 'anonymous' , ftpPass: ' anonymous@ ' , debug: false , passive: true ) Socksify::debug = debug TCPSocket::socks_username = proxyUser TCPSocket::socks_password = proxyPass Socksify::proxy( proxyHost, proxyPort ) do |_| begin # Check whether we got an ip address or a domain name. # If needed we'll do dns through the socket to avoid dns leaks. # Regexp.new( URI::RFC2396_Parser.new.pattern[ :IPV4ADDR ] ) =~ ftpHost or ftpHost = Socksify::resolve( ftpHost ) ftp = Net::FTP.new ftp.debug_mode = debug ftp.passive = passive ftp.connect ftpHost, ftpPort ftp.login ftpUser, ftpPass yield ftp ensure ftp.close end end end 

Now in your real application you can simply:

 def listRemoteFiles connect do |ftp| puts ftp.list.inspect # or other cool functions from Net::FTP end end def connect &block ftp({ debug: true , proxyHost: your.proxy.host , proxyPort: your.proxy.port , ftpHost: your.ftpHost }, &block ) end 

Thanks tvgriek for pointing in the right direction.

0
source

You can use the Proximo shell to forward traffic through Proximo. Here are the official instructions: https://devcenter.heroku.com/articles/proximo#forwarding-traffic-through-proximo

-1
source

Source: https://habr.com/ru/post/921445/


All Articles