简体   繁体   中英

Make FTPS connection in Ruby with double-bag-ftps gem

I am trying to get a native FTP connection work to an odd FTP server in ruby. It requires TLS and implicit SSL. I have a FileZilla client configured and working. Here's my code:

require 'double_bag_ftps'

DoubleBagFTPS.open(ftp_host, ftp_user, passwd, nil, DoubleBagFTPS::IMPLICIT, :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |ftp|
    ...
  files = ftp.list(file_path)
  STDOUT.write files
end

I get the following runtime error when I run the above:

  bunches of traceback lines
  <path_to_gems>/double-bag-ftps-0.1.4/lib/double_bag_ftps.rb:160:in `initialize': wrong argument type nil (expected OpenSSL/SSL/CTX) (TypeError)

I can't seem to get anything out of the server with Ruby and the traditional net/ftp gem (various errors related to TLS/SSL problems). DoubleBagFTPS seems to be the most promising gem, but I still get an error. It may be the case that I am not calling the open function correctly. The only nil is the fourth parameter, but that's clearly spelled out in the DooubleBagFTPS example.

Can someone help?

Update

Per the suggestion, here's my new code

 class MyFTP < Net::FTP
   FTP_PORT = 990

   def connect(host, port = FTP_PORT)
     synchronize do
       @host = host
       @bare_sock = open_socket(host, port)
       begin
         ssl_sock = start_tls_session(Socket.tcp(host, port))
         @sock = BufferedSSLSocket.new(ssl_sock, read_timeout: @read_timeout)
         voidresp
         if @private_data_connection
           voidcmd("PBSZ 0")
           voidcmd("PROT P")
         end
       rescue OpenSSL::SSL::SSLError, Net::OpenTimeout
         @sock.close
         raise
      end
    end
   end
 end

   def ftp_options
     {
       username: 'user',
       password: 'password',
       ssl: true,
       passive: true
     }
   end


MyFTP.open(ftp_host, ftp_options) do |ftp|
  ftp.login
  files = ftp.chdir(file_path)
  files = ftp.list
  STDOUT.write files
end

I'm still getting an error as follows:

 ---stack-trace---
 <path_to_gem>/ruby/2.5.0/net/protocol.rb:52:in `connect': SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol (OpenSSL::SSL::SSLError)

So I got it working with regular old Net::FTP as follows:

 def ftp_options
   {
      username: '<username>',
      password: '<password>',
      ssl: {
        verify_mode: OpenSSL::SSL::VERIFY_NONE
      }
    }
 end

 Net::FTP.open(ftp_host, ftp_options) do |ftp|
   ftp.login(ftp_options[:username], ftp_options[:password])
   files = ftp.list
   STDOUT.write files
   puts "\n"
 end

The one thing I don't understand is why I am forced to pass the username and password to the ftp.login method, since it's already defined in ftp_options, which was passed to Net::FTP.open(). As far as I can tell everything is set up correctly in ftp_options. For the particular server I'm connecting to, TLS/SSL is required, and that's working, so that parameter variable is being picked up... why not user/password?

Anyway, case closed for me at least. I can confirm that regular Net::FTP seems to work with at least one of these non-vanilla FTP servers requiring TLS and implicit SSL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM