Module: Rex::IO::StreamServer

Included in:
Socket::TcpServer
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

Returns the value of attribute client_waiter.



135
136
137
# File 'lib/rex/io/stream_server.rb', line 135

def client_waiter
  @client_waiter
end

#clientsObject

:nodoc:



133
134
135
# File 'lib/rex/io/stream_server.rb', line 133

def clients
  @clients
end

#clients_threadObject

:nodoc:



134
135
136
# File 'lib/rex/io/stream_server.rb', line 134

def clients_thread
  @clients_thread
end

#listener_threadObject

:nodoc:



134
135
136
# File 'lib/rex/io/stream_server.rb', line 134

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.



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

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.



121
122
123
# File 'lib/rex/io/stream_server.rb', line 121

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.



126
127
128
# File 'lib/rex/io/stream_server.rb', line 126

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.



93
94
95
96
97
98
99
100
101
102
# File 'lib/rex/io/stream_server.rb', line 93

def close_client(client)
  if (client)
    clients.delete(client)

    begin
      client.close
    rescue IOError
    end
  end
end

#on_client_close(client) ⇒ Object

This callback is notified when a client connection has closed.



55
56
57
58
59
# File 'lib/rex/io/stream_server.rb', line 55

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

#on_client_connect(client) ⇒ Object

This callback is notified when a client connects.



36
37
38
39
40
# File 'lib/rex/io/stream_server.rb', line 36

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

#on_client_data(client) ⇒ Object

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



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

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

#startObject

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



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rex/io/stream_server.rb', line 65

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

  self.listener_thread = Rex::ThreadFactory.spawn("StreamServerListener", false) {
    monitor_listener
  }
  self.clients_thread = Rex::ThreadFactory.spawn("StreamServerClientMonitor", false) {
    monitor_clients
  }
end

#stopObject

Terminates the listener monitoring threads and closes all active clients.



80
81
82
83
84
85
86
87
# File 'lib/rex/io/stream_server.rb', line 80

def stop
  self.listener_thread.kill
  self.clients_thread.kill

  self.clients.each { |cli|
    close_client(cli)
  }
end

#waitObject

This method waits on the server listener thread



107
108
109
# File 'lib/rex/io/stream_server.rb', line 107

def wait
  self.listener_thread.join if self.listener_thread
end