如何在Ruby Net :: FTP中使用代理服务器? [英] How to use proxy server with Ruby Net::FTP?

查看:174
本文介绍了如何在Ruby Net :: FTP中使用代理服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Net :: FTP ruby​​库连接到FTP服务器并下载文件。它一切正常,但现在我需要使用出站代理,因为它们的防火墙将IP地址列入白名单,并且我使用Heroku来托管该站点。我正在尝试新的Proximo插件,它看上去很有前途,但我无法使用Net :: FTP来使用它。



我在 Net :: FTP文档
$ b


connect(host,port = FTP_PORT)



与主机的FTP连接,可选地覆盖默认端口。如果设置了环境变量SOCKS_SERVER,则通过SOCKS代理设置连接。如果无法建立连接,则引发异常(通常为Errno :: ECONNREFUSED)。


但尝试设置此环境变量或设置 ENV ['SOCKS_SERVER'] = proximo_url 也不起作用。有谁知道如何正确地做到这一点?它不一定是SOCKS代理,HTTP代理会做,但我不确定Net :: FTP库是否支持这个。



..经过一番研究......我发现Net :: FTP寻找一个名为 SOCKSSocket 的类,用于我找到了这些文档。但我不能包括它。 include'socket'返回false,我很确定它意味着它已经被加载了。 div>

您可以使用 socksify gem作为例如。 Quotaguard。请确保您使用提供的静态IP的一个(虽然不是DNS主机名)作为代理服务器,但由于FTP和负载平衡可能导致麻烦。

  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用户,密码
ftp.get(export,storelocation)
ftp.close
结束
结束


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.

解决方案

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

这篇关于如何在Ruby Net :: FTP中使用代理服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆