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

#clientsObject

:nodoc:



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

def clients
  @clients
end

#clients_threadObject

:nodoc:



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

def clients_thread
  @clients_thread
end

#listener_threadObject

:nodoc:



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

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.



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

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.



117
118
119
# File 'lib/rex/io/stream_server.rb', line 117

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.



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

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.



89
90
91
92
93
94
95
96
97
98
# File 'lib/rex/io/stream_server.rb', line 89

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.



52
53
54
55
56
# File 'lib/rex/io/stream_server.rb', line 52

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.



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

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.



43
44
45
46
47
# File 'lib/rex/io/stream_server.rb', line 43

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.



62
63
64
65
66
67
68
69
70
71
# File 'lib/rex/io/stream_server.rb', line 62

def start
	self.clients = []

	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.



76
77
78
79
80
81
82
83
# File 'lib/rex/io/stream_server.rb', line 76

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



103
104
105
# File 'lib/rex/io/stream_server.rb', line 103

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