我从中获取文件的供应商正在从 FTP 更改为基于 SSL 的 FTP。
我正在尝试更新我的代码net/ftp
to net/ftptls
我需要连接的新主机未经认证,我的脚本报告此错误。
主机名与服务器证书不匹配
供应商不会修复此问题。
看着/usr/lib/ruby/1.8/net/ftptls.rb
我认为对 FTPTLS 进行猴子修补以忽略不受信任的主机不会太难。
我尝试改变verify_mode
to 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
这可能是世界上最慢的答案,但我遇到了你的问题,它帮助我自己解决了这个问题,所以我想为后代发帖。
你已经非常接近了,你只需要注释掉#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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)