这是我对这个问题的看法。锅炉板:
# 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: '[email protected]' ,
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
现在,在实际的应用程序,你可以这样做:
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
感谢tvgriek为指向正确的方向。
来源
2017-02-05 20:04:36
nus
你有没有想过这个? – 2013-06-26 15:20:48
@JohnWright不幸的是:( – 2013-07-02 19:08:14