2012-03-20 73 views
5

供應商我抓取的文件正在通過SSL從FTP更改爲FTP。通過TLS/SSL通過FTP連接到未經認證的主機

我想更新我的代碼net/ftpnet/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 

回答

2

這可能是世界上最慢的答案,但我跑過你的問題,它幫助我要自己解決,所以我想張貼後人。

你非常接近,你只需要註釋掉#post_connection_check。

我做了什麼,而不是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

2

我知道這可能是太晚了波爾,但我發現double-bag-ftps gem是足夠的和容易,當我不得不做類似的事情使用。