Class: Rex::IO::RelayManager

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/io/relay_manager.rb

Overview

An IO RelayManager which will read data from a socket and write it to a sink using a background thread.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRelayManager

Returns a new instance of RelayManager.



11
12
13
14
# File 'lib/rex/io/relay_manager.rb', line 11

def initialize
  @thread = nil
  @scheduler = FiberScheduler.new
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



9
10
11
# File 'lib/rex/io/relay_manager.rb', line 9

def thread
  @thread
end

Class Method Details

.io_write_all(io, data) ⇒ Object

Write all data to the specified IO. This is intended to be used in scenarios where partial writes are possible but not desirable.

Parameters:

  • io (#write)

    An object to write the data to. It must return a number indicating how many bytes of the provided data were processed.

  • data (String)

    The data that should be written.



42
43
44
45
46
47
48
49
# File 'lib/rex/io/relay_manager.rb', line 42

def self.io_write_all(io, data)
  offset = 0
  while offset < data.bytesize
    written = io.write(data.byteslice(offset..-1))
    offset += written
  end
  data.bytesize
end

Instance Method Details

#add_relay(sock, sink: nil, name: nil, on_exit: nil) ⇒ Object

Add a IO relay to the manager. This will start relaying data from the source socket to the destination sink immediately. An optional “on_exit” callback can be provided which will be called when the socket is closed.

Parameters:

  • sock (::Socket)

    The source socket that data will be read from.

  • sink (#write, #call) (defaults to: nil)

    A data destination where read data will be sent. It is called with one parameter, the data to be transferred. If the object exposes a #write method, it will be called repeatedly all data is processed and the return value will be used to determine how much of the data was written. If the object exposes a #call method, it will be called once and must handle processing all the data it is provided.

  • name (String) (defaults to: nil)

    A human-friendly name for the relay used in debug output.

  • on_exit (#call) (defaults to: nil)

    A callback to be invoked when sink can no longer be read from.



28
29
30
31
32
33
34
# File 'lib/rex/io/relay_manager.rb', line 28

def add_relay(sock, sink: nil, name: nil, on_exit: nil)
  @scheduler.schedule_fiber do
    relay_fiber(sock, sink, name, on_exit: on_exit)
  end

  start unless running?
end