【问题标题】:Connect to uncertified host via FTP over TLS/SSL通过 FTP over TLS/SSL 连接到未经认证的主机
【发布时间】:2012-03-20 20:35:04
【问题描述】:

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

我正在尝试将我的代码从 net/ftp 更新为 net/ftptls

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

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

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

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

我尝试将 verify_mode 更改为 OpenSSL::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

【问题讨论】:

    标签: ruby ftp ftps


    【解决方案1】:

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

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

    我所做的不是猴子修补 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)
    

    HTH

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-18
        • 1970-01-01
        • 1970-01-01
        • 2018-05-27
        • 2014-03-21
        • 1970-01-01
        • 2022-08-18
        相关资源
        最近更新 更多