使用Ruby-FTPS进行加密传输

使用Ruby-FTPS进行加密传输,ruby,ftp,openssl,jruby,ftps,Ruby,Ftp,Openssl,Jruby,Ftps,我正在尝试使用FTPS从服务器获取文件。我能够进行身份验证,但当我尝试列出/获取文件时,我得到一个“521数据连接必须加密”。Net::FTP模块能够做到这一点吗?我将如何实现它 我将Net::FTPTLS修改到自己的类中,因为我需要存储一个自签名证书 require 'socket' require 'openssl' require 'net/ftp' module MP class FTPS < Net::FTP def connect(host, port=FTP_P

我正在尝试使用FTPS从服务器获取文件。我能够进行身份验证,但当我尝试列出/获取文件时,我得到一个“521数据连接必须加密”。Net::FTP模块能够做到这一点吗?我将如何实现它

我将Net::FTPTLS修改到自己的类中,因为我需要存储一个自签名证书

require 'socket'
require 'openssl'
require 'net/ftp'

module MP
  class FTPS < Net::FTP
    def connect(host, port=FTP_PORT)
      @hostname = host
      super
    end

    def login(user = "anonymous", passwd = nil, cert_file = nil, acct = nil)
      store = OpenSSL::X509::Store.new
      if cert_file == nil
        store.set_default_paths
      else
        certraw = File.read(cert_file)
        cert = OpenSSL::X509::Certificate.new(certraw)
        store.add_cert(cert)
      end
      ctx = OpenSSL::SSL::SSLContext.new('SSLv23')
      ctx.cert_store = store
      ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
      ctx.key = nil
      ctx.cert = cert
      voidcmd("AUTH TLS")
      @sock = OpenSSL::SSL::SSLSocket.new(@sock, ctx)
      @sock.connect
      #@sock.post_connection_check(@hostname)
      super(user, passwd, acct)
      voidcmd("PBSZ 0")
    end
  end
end
它在nlst上失败

编辑:我尝试将voidcmd(“PROT p”)添加到登录函数的末尾,但它只是挂起了一段时间,然后我最终得到:

IOError: Unsupported record version Unknown-48.48
___BEGIN BACKTRACE___
org/jruby/ext/openssl/SSLSocket.java:564:in `sysread'
/opt/jruby/lib/ruby/gems/1.8/gems/jruby-openssl-0.7.6.1/lib/1.8/openssl/buffering.rb:36:in `fill_rbuff'
/opt/jruby/lib/ruby/gems/1.8/gems/jruby-openssl-0.7.6.1/lib/1.8/openssl/buffering.rb:159:in `eof?'
/opt/jruby/lib/ruby/gems/1.8/gems/jruby-openssl-0.7.6.1/lib/1.8/openssl/buffering.rb:134:in `readline'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:211:in `getline'
    /opt/jruby/lib/ruby/1.8/net/ftp.rb:221:in `getmultiline'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:235:in `getresp'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:251:in `voidresp'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:436:in `retrlines'
/opt/jruby/lib/ruby/1.8/monitor.rb:191:in `mon_synchronize'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:422:in `retrlines'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:612:in `nlst'
... etc

我意识到这是一个老问题,但我在研究FTPS ruby gems时偶然发现了这个问题

不,FTP本身不支持FTP

我极力推荐

提供Net::FTP的子类以支持隐式和显式FTP


在过去的一年里,0.1.1版对我来说每天都运行得很好。

请注意,双包ftps是Ruby 2.4,它引入了FTP over SSL支持
IOError: Unsupported record version Unknown-48.48
___BEGIN BACKTRACE___
org/jruby/ext/openssl/SSLSocket.java:564:in `sysread'
/opt/jruby/lib/ruby/gems/1.8/gems/jruby-openssl-0.7.6.1/lib/1.8/openssl/buffering.rb:36:in `fill_rbuff'
/opt/jruby/lib/ruby/gems/1.8/gems/jruby-openssl-0.7.6.1/lib/1.8/openssl/buffering.rb:159:in `eof?'
/opt/jruby/lib/ruby/gems/1.8/gems/jruby-openssl-0.7.6.1/lib/1.8/openssl/buffering.rb:134:in `readline'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:211:in `getline'
    /opt/jruby/lib/ruby/1.8/net/ftp.rb:221:in `getmultiline'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:235:in `getresp'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:251:in `voidresp'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:436:in `retrlines'
/opt/jruby/lib/ruby/1.8/monitor.rb:191:in `mon_synchronize'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:422:in `retrlines'
/opt/jruby/lib/ruby/1.8/net/ftp.rb:612:in `nlst'
... etc