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

第一次失败。

编辑:我尝试将 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 时偶然发现了它。

没有。 net::FTP 本身不支持 FTPS。

我强烈推荐double-bag-ftps .

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

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

关于ruby - 使用 Ruby FTPS 进行加密传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16446442/

相关文章:

javascript - Rails 在 html 公共(public)文件中包含 js

php - Ruby 命令未在 PHP shell_exec 函数中执行

ruby - 如何在 eventmachine 中启动多个工作进程?

java - JSch SFTP 详细日志记录

ruby-on-rails - 使用客户端证书和 TLS_RSA_WITH_AES_256_CBC_SHA 和其他密码套件

ruby - 设计模式/关于构建规则引擎的建议

java - 寻求帮助诊断使用 apache FTP 客户端进行 FTP 传输的服务器套接字问题

shell - 在 shell 脚本中连接到 FTPS 服务器

python - openssl -aes-128-ecb 加密与 python Crypto.Cipher AES 加密不匹配

php - 使用客户端证书进行身份验证