使用Ruby 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_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

以下是尝试获取文件的代码段:

def get_ftpclient(host)
  FTPS::new(host)
end

def check_for_files
  @ftp = get_ftpclient(@host)
  @ftp.passive = true
  @ftp.login(@user_name, @password, @cert_file)
  @ftp.chdir(@remote_dir)
  files = @ftp.nlst
  files
end

它在nlst上失败了.

编辑:我尝试将void cmd(“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

最佳答案 我意识到这是一个老问题,但我在研究FTPSruby宝石时偶然发现了它.

不.net :: FTP本身不支持FTPS.

我强烈推荐double-bag-ftps.

Provides a child class of Net::FTP to support implicit and explicit FTPS.

对于我来说,0.1.1版本在过去的一年中每天都在运行.

点赞