Module: Ftpd::DataConnectionHelper
- Included in:
- CommandHandler
- Defined in:
- lib/ftpd/data_connection_helper.rb
Instance Method Summary collapse
- #close_data_server_socket_when_done ⇒ Object
- #data_connection_description ⇒ Object
- #encrypt_data? ⇒ Boolean
- #handle_data_disconnect ⇒ Object
- #make_tls_connection(data_socket) ⇒ Object
- #open_active_data_connection ⇒ Object
- #open_active_tls_data_connection ⇒ Object
- #open_data_connection(path_to_advertise = nil, &block) ⇒ Object
- #open_passive_data_connection ⇒ Object
- #open_passive_tls_data_connection ⇒ Object
- #receive_file(path_to_advertise = nil, &block) ⇒ Object
- #send_start_of_data_connection_reply(path) ⇒ Object
- #transmit_file(io, data_type = session.data_type) ⇒ Object
Instance Method Details
#close_data_server_socket_when_done ⇒ Object
113 114 115 116 117 |
# File 'lib/ftpd/data_connection_helper.rb', line 113 def close_data_server_socket_when_done yield ensure close_data_server_socket end |
#data_connection_description ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/ftpd/data_connection_helper.rb', line 95 def data_connection_description [ Session::DATA_TYPES[data_type][0], "mode data connection", ("(TLS)" if encrypt_data?) ].compact.join(' ') end |
#encrypt_data? ⇒ Boolean
119 120 121 |
# File 'lib/ftpd/data_connection_helper.rb', line 119 def encrypt_data? data_channel_protection_level != :clear end |
#handle_data_disconnect ⇒ Object
81 82 83 84 85 |
# File 'lib/ftpd/data_connection_helper.rb', line 81 def handle_data_disconnect return yield rescue Errno::ECONNRESET, Errno::EPIPE reply "426 Connection closed; transfer aborted." end |
#make_tls_connection(data_socket) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/ftpd/data_connection_helper.rb', line 103 def make_tls_connection(data_socket) ssl_socket = OpenSSL::SSL::SSLSocket.new(data_socket, socket.ssl_context) ssl_socket.accept begin yield(ssl_socket) ensure ssl_socket.close end end |
#open_active_data_connection ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/ftpd/data_connection_helper.rb', line 63 def open_active_data_connection data_socket = TCPSocket.new(data_hostname, data_port) begin yield(data_socket) ensure data_socket.close end end |
#open_active_tls_data_connection ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/ftpd/data_connection_helper.rb', line 55 def open_active_tls_data_connection open_active_data_connection do |socket| make_tls_connection(socket) do |ssl_socket| yield(ssl_socket) end end end |
#open_data_connection(path_to_advertise = nil, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ftpd/data_connection_helper.rb', line 30 def open_data_connection(path_to_advertise = nil, &block) send_start_of_data_connection_reply(path_to_advertise) if data_server if encrypt_data? open_passive_tls_data_connection(&block) else open_passive_data_connection(&block) end else if encrypt_data? open_active_tls_data_connection(&block) else open_active_data_connection(&block) end end end |
#open_passive_data_connection ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/ftpd/data_connection_helper.rb', line 72 def open_passive_data_connection data_socket = data_server.accept begin yield(data_socket) ensure data_socket.close end end |
#open_passive_tls_data_connection ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/ftpd/data_connection_helper.rb', line 47 def open_passive_tls_data_connection open_passive_data_connection do |socket| make_tls_connection(socket) do |ssl_socket| yield(ssl_socket) end end end |
#receive_file(path_to_advertise = nil, &block) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/ftpd/data_connection_helper.rb', line 20 def receive_file(path_to_advertise = nil, &block) open_data_connection(path_to_advertise) do |data_socket| socket = Ftpd::Stream.new(data_socket, data_type) handle_data_disconnect do yield socket end config.log.debug "Received #{socket.byte_count} bytes" end end |
#send_start_of_data_connection_reply(path) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/ftpd/data_connection_helper.rb', line 87 def send_start_of_data_connection_reply(path) if path reply "150 FILE: #{path}" else reply "150 Opening #{data_connection_description}" end end |
#transmit_file(io, data_type = session.data_type) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ftpd/data_connection_helper.rb', line 9 def transmit_file(io, data_type = session.data_type) open_data_connection do |data_socket| socket = Ftpd::Stream.new(data_socket, data_type) handle_data_disconnect do socket.write(io) end config.log.debug "Sent #{socket.byte_count} bytes" reply "226 Transfer complete" end end |