2013-01-21 134 views
0

我试图通过Net :: FTPTLS连接到基于Microsoft的文件服务器(IIS),该服务器被配置为在端口22上使用FTP并需要SSL。使用Net :: FTPTLS连接到服务器证书不匹配的FTPS服务器

我连接通过:

require 'net/ftptls' 
ftp = Net::FTPTLS.new() 
ftp.connect('host.com', port_number) 
ftp.login('Username', 'Password') 
ftp.puttextfile('somefile.txt', 'where/to/save/somefile.txt') 
ftp.close 

问题是,我得到以下错误:

hostname does not match the server certificate 

看来我必须禁用OpenSSL的对等认证:OpenSSL的:: SSL :: VERIFY_PEER应该成为OpenSSL :: SSL :: VERIFY_NONE。

有关如何修补Net :: FTPTLS类的任何想法?有没有人成功做到这一点?

回答

0

我做了什么,而不是monkeypatching红宝石本身,带来了这个到我的项目/ 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 

我也清理了param传递了一下。你会使用这个像这样:

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

1

而不是使用的Net :: FTPTLS,使用Ruby 2.4及以上的用下面的代码:

require 'net/ftp' 
ftp = Net::FTP.new(nil, ssl: {:verify_mode => OpenSSL::SSL::VERIFY_NONE}) 
ftp.connect('host.com', port_number) 
ftp.login('Username', 'Password') 
ftp.puttextfile('somefile.txt', 'where/to/save/somefile.txt') 
ftp.close 
+2

我建议这个答案添加更多的信息。 –

+0

这不提供问题的答案。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/18602355) –

相关问题