5

我从中获取文件的供应商正在从 FTP 更改为通过 SSL 的 FTP。

我正在尝试将我的代码更新net/ftpnet/ftptls

我需要连接的新主机没有经过认证,我的脚本报告了这个错误。

主机名与服务器证书不匹配

供应商不会解决这个问题。

看着/usr/lib/ruby/1.8/net/ftptls.rb 我认为猴子修补 FTPTLS 以忽略不受信任的主机不会太难。

我尝试更改verify_modeOpenSSL::SSL::VERIFY_NONE注释掉 post_connection_check` 行。

都没有工作。

关于如何做到这一点的任何想法?

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

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

    def login(user = "anonymous", passwd = nil, acct = nil)
       store = OpenSSL::X509::Store.new
       store.set_default_paths
       ctx = OpenSSL::SSL::SSLContext.new('SSLv23')
       ctx.cert_store = store
       ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
       ctx.key = nil
       ctx.cert = nil
       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
4

2 回答 2

2

这可能是世界上最慢的答案,但我遇到了你的问题,它帮助我自己解决了这个问题,所以我想为后代发布。

你非常接近,你只需要注释掉#post_connection_check。

我所做的,而不是monkeypatching ruby​​ 本身,是将它的副本带入我的项目的/lib 中。

module Net

  class FTPTLS < FTP
    def connect(host, port=FTP_PORT)
      @hostname = host
      super
    end

    def login(user = "anonymous", params = {:password => nil, :acct => nil, :ignore_cert => false})
      store = OpenSSL::X509::Store.new
      store.set_default_paths
      ctx = OpenSSL::SSL::SSLContext.new('SSLv23')
      ctx.cert_store = store
      ctx.verify_mode = params[:ignore_cert] ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
      ctx.key = nil
      ctx.cert = nil
      voidcmd("AUTH TLS")
      @sock = OpenSSL::SSL::SSLSocket.new(@sock, ctx)
      @sock.connect
      @sock.post_connection_check(@hostname) unless params[:ignore_cert]
      super(user, params[:password], params[:acct])
      voidcmd("PBSZ 0")
    end
  end
end

我还清理了传递的参数。你会这样使用它:

  require 'ftptls'  # Use my local version, not net/ftptls
  @ftp_connection = Net::FTPTLS.new()
  @ftp_connection.passive = true
  @ftp_connection.connect(host, 21)
  @ftp_connection.login('user', :password => 'pass', :ignore_cert => true)

高温高压

于 2013-04-08T17:31:15.913 回答
2

我知道这对 Poul 来说可能为时已晚,但当我不得不做类似的事情时,我发现double-bag-ftps gem已经足够且易于使用。

于 2013-12-22T18:03:02.470 回答