我正在尝试通过Net :: FTPTLS连接到基于Microsoft的文件服务器(IIS),该服务器配置为在端口22上使用FTP并且需要SSL。
我通过以下方式连接:
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 :(得分: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
答案 1 :(得分:0)
我做了什么,而不是monkeypatching 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 :(得分:0)
这对我来说很好用。 #ROR
ftp = Net::FTP.new("ftps.host.com", ftp_options)
open("where/is/your/file/somefile.txt") do |file_data|
ftp.putbinaryfile(file_data, 'where/to/save/somefile.txt')
end
ftp.puttextfile('somefile.txt', 'where/to/save/somefile.txt')
def ftp_options
{
port: FTP_PORT,
username: 'ftp_user',
password: 'password',
passive: true,
ssl: { verify_mode: 0 }
}
end
请记住,您必须提供 ftps.hostname.com。