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

Returns the value of attribute client_waiter.



142
143
144
# File 'lib/rex/io/stream_server.rb', line 142

def client_waiter
  @client_waiter
end

#clientsObject

:nodoc:



140
141
142
# File 'lib/rex/io/stream_server.rb', line 140

def clients
  @clients
end

#clients_threadObject

:nodoc:



141
142
143
# File 'lib/rex/io/stream_server.rb', line 141

def clients_thread
  @clients_thread
end

#listener_threadObject

:nodoc:



141
142
143
# File 'lib/rex/io/stream_server.rb', line 141

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.



138
139
140
# File 'lib/rex/io/stream_server.rb', line 138

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.



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

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.



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

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)
    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



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

def detach_client(client)
  self.clients.delete(client)
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



114
115
116
# File 'lib/rex/io/stream_server.rb', line 114

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