Module: Rex::IO::StreamServer

Defined in:
lib/rex/io/stream_server.rb

Overview

This mixin provides the framework and interface for implementing a streaming server that can listen for and accept stream client connections. Stream servers extend this class and are required to implement the following methods:

accept
fd

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#client_waiterObject

:nodoc: # :nodoc:



131
132
133
# File 'lib/rex/io/stream_server.rb', line 131

def client_waiter
  @client_waiter
end

#clientsObject

:nodoc: # :nodoc:



131
132
133
# File 'lib/rex/io/stream_server.rb', line 131

def clients
  @clients
end

#clients_threadObject

:nodoc: # :nodoc:



131
132
133
# File 'lib/rex/io/stream_server.rb', line 131

def clients_thread
  @clients_thread
end

#listener_threadObject

:nodoc: # :nodoc:



131
132
133
# File 'lib/rex/io/stream_server.rb', line 131

def listener_thread
  @listener_thread
end

#on_client_close_procObject

This callback procedure can be set and will be called when a client disconnects from the server.



129
130
131
# File 'lib/rex/io/stream_server.rb', line 129

def on_client_close_proc
  @on_client_close_proc
end

#on_client_connect_procObject

This callback procedure can be set and will be called when new clients connect.



119
120
121
# File 'lib/rex/io/stream_server.rb', line 119

def on_client_connect_proc
  @on_client_connect_proc
end

#on_client_data_procObject

This callback procedure can be set and will be called when clients have data to be processed.



124
125
126
# File 'lib/rex/io/stream_server.rb', line 124

def on_client_data_proc
  @on_client_data_proc
end

Instance Method Details

#close_client(client) ⇒ Object

This method closes a client connection and cleans up the resources associated with it.



84
85
86
87
88
89
90
91
92
93
# File 'lib/rex/io/stream_server.rb', line 84

def close_client(client)
  if client
    detach_client(client)

    begin
      client.close
    rescue IOError
    end
  end
end

#detach_client(client) ⇒ Object

Detach a client. You are now responsible for it, not us



98
99
100
# File 'lib/rex/io/stream_server.rb', line 98

def detach_client(client)
  clients.delete(client)
end

#on_client_close(client) ⇒ Object

This callback is notified when a client connection has closed.



48
49
50
# File 'lib/rex/io/stream_server.rb', line 48

def on_client_close(client)
  on_client_close_proc.call(client) if on_client_close_proc
end

#on_client_connect(client) ⇒ Object

This callback is notified when a client connects.



33
34
35
# File 'lib/rex/io/stream_server.rb', line 33

def on_client_connect(client)
  on_client_connect_proc.call(client) if on_client_connect_proc
end

#on_client_data(client) ⇒ Object

This callback is notified when a client connection has data that needs to be processed.



41
42
43
# File 'lib/rex/io/stream_server.rb', line 41

def on_client_data(client)
  on_client_data_proc.call(client) if on_client_data_proc
end

#startObject

Start monitoring the listener socket for connections and keep track of all client connections.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rex/io/stream_server.rb', line 56

def start
  self.clients = []
  self.client_waiter = ::Queue.new

  self.listener_thread = Rex::ThreadFactory.spawn('StreamServerListener', false) do
    monitor_listener
  end
  self.clients_thread = Rex::ThreadFactory.spawn('StreamServerClientMonitor', false) do
    monitor_clients
  end
end

#stopObject

Terminates the listener monitoring threads and closes all active clients.



71
72
73
74
75
76
77
78
# File 'lib/rex/io/stream_server.rb', line 71

def stop
  listener_thread.kill
  clients_thread.kill

  clients.each do |cli|
    close_client(cli)
  end
end

#waitObject

This method waits on the server listener thread



105
106
107
# File 'lib/rex/io/stream_server.rb', line 105

def wait
  listener_thread.join if listener_thread
end