通过FTP通过TLS/SSL连接到未经认证的主机

Pou*_*oul 5 ruby ftp ftps

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

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

我需要连接的新主机未经过认证,我的脚本会报告此错误.

hostname与服务器证书不匹配

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

看着/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
Run Code Online (Sandbox Code Playgroud)

Dar*_*ryl 2

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

你已经非常接近了,你只需要注释掉#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
Run Code Online (Sandbox Code Playgroud)

我还清理了一些传递的参数。你可以像这样使用它:

  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)
Run Code Online (Sandbox Code Playgroud)

华泰