2011-05-14 18:34:06 +00:00
|
|
|
require 'thread'
|
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
module Rex
|
|
|
|
module IO
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This mixin provides the framework and interface for implementing a streaming
|
2005-09-27 05:31:48 +00:00
|
|
|
# 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
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
module StreamServer
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Abstract methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-08-22 02:36:09 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Default server monitoring and client management implementation follows
|
|
|
|
# below.
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# This callback is notified when a client connects.
|
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
def on_client_connect(client)
|
|
|
|
if (on_client_connect_proc)
|
|
|
|
on_client_connect_proc.call(client)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# This callback is notified when a client connection has data that needs to
|
|
|
|
# be processed.
|
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
def on_client_data(client)
|
|
|
|
if (on_client_data_proc)
|
|
|
|
on_client_data_proc.call(client)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# This callback is notified when a client connection has closed.
|
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
def on_client_close(client)
|
|
|
|
if (on_client_close_proc)
|
|
|
|
on_client_close_proc.call(client)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Start monitoring the listener socket for connections and keep track of
|
|
|
|
# all client connections.
|
|
|
|
#
|
|
|
|
def start
|
|
|
|
self.clients = []
|
2011-05-14 18:34:06 +00:00
|
|
|
self.client_waiter = ::Queue.new
|
2005-08-22 02:36:09 +00:00
|
|
|
|
2010-11-12 06:19:49 +00:00
|
|
|
self.listener_thread = Rex::ThreadFactory.spawn("StreamServerListener", false) {
|
2005-08-22 02:36:09 +00:00
|
|
|
monitor_listener
|
|
|
|
}
|
2010-11-12 06:19:49 +00:00
|
|
|
self.clients_thread = Rex::ThreadFactory.spawn("StreamServerClientMonitor", false) {
|
2005-08-22 02:36:09 +00:00
|
|
|
monitor_clients
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Terminates the listener monitoring threads and closes all active clients.
|
|
|
|
#
|
|
|
|
def stop
|
|
|
|
self.listener_thread.kill
|
|
|
|
self.clients_thread.kill
|
|
|
|
|
|
|
|
self.clients.each { |cli|
|
|
|
|
close_client(cli)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method closes a client connection and cleans up the resources
|
|
|
|
# associated with it.
|
2005-08-22 02:36:09 +00:00
|
|
|
#
|
|
|
|
def close_client(client)
|
|
|
|
if (client)
|
|
|
|
clients.delete(client)
|
|
|
|
|
2006-08-08 03:29:26 +00:00
|
|
|
begin
|
|
|
|
client.close
|
|
|
|
rescue IOError
|
|
|
|
end
|
2005-08-22 02:36:09 +00:00
|
|
|
end
|
|
|
|
end
|
2010-04-03 06:05:06 +00:00
|
|
|
|
2008-11-16 20:54:41 +00:00
|
|
|
#
|
|
|
|
# This method waits on the server listener thread
|
|
|
|
#
|
|
|
|
def wait
|
|
|
|
self.listener_thread.join if self.listener_thread
|
2010-04-03 06:05:06 +00:00
|
|
|
end
|
2005-08-22 02:36:09 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
##
|
2005-08-22 02:36:09 +00:00
|
|
|
#
|
|
|
|
# Callback procedures.
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
##
|
2010-04-03 06:05:06 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# This callback procedure can be set and will be called when new clients
|
|
|
|
# connect.
|
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
attr_accessor :on_client_connect_proc
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# This callback procedure can be set and will be called when clients
|
|
|
|
# have data to be processed.
|
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
attr_accessor :on_client_data_proc
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# This callback procedure can be set and will be called when a client
|
|
|
|
# disconnects from the server.
|
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
attr_accessor :on_client_close_proc
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_accessor :clients # :nodoc:
|
|
|
|
attr_accessor :listener_thread, :clients_thread # :nodoc:
|
2011-05-14 18:34:06 +00:00
|
|
|
attr_accessor :client_waiter
|
2005-08-22 02:36:09 +00:00
|
|
|
|
2010-04-03 06:05:06 +00:00
|
|
|
protected
|
|
|
|
|
2005-08-22 02:36:09 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method monitors the listener socket for new connections and calls
|
|
|
|
# the +on_client_connect+ callback routine.
|
2005-08-22 02:36:09 +00:00
|
|
|
#
|
|
|
|
def monitor_listener
|
2010-04-03 06:05:06 +00:00
|
|
|
|
2010-04-27 14:39:09 +00:00
|
|
|
while true
|
|
|
|
begin
|
|
|
|
cli = accept
|
|
|
|
if not cli
|
|
|
|
elog("The accept() returned nil in stream server listener monitor: #{fd.inspect}")
|
2010-05-20 20:42:17 +00:00
|
|
|
::IO.select(nil, nil, nil, 0.10)
|
2010-04-27 14:39:09 +00:00
|
|
|
next
|
|
|
|
end
|
2005-08-22 02:36:09 +00:00
|
|
|
|
2010-04-27 14:39:09 +00:00
|
|
|
# Append to the list of clients
|
|
|
|
self.clients << cli
|
2005-08-22 02:36:09 +00:00
|
|
|
|
2010-04-27 14:39:09 +00:00
|
|
|
# Initialize the connection processing
|
|
|
|
on_client_connect(cli)
|
2011-05-14 18:34:06 +00:00
|
|
|
|
|
|
|
# Notify the client monitor
|
|
|
|
self.client_waiter.push(cli)
|
2010-04-03 06:05:06 +00:00
|
|
|
|
2010-04-27 14:39:09 +00:00
|
|
|
# Skip exceptions caused by accept() [ SSL ]
|
|
|
|
rescue ::EOFError, ::Errno::ECONNRESET, ::Errno::ENOTCONN, ::Errno::ECONNABORTED
|
|
|
|
rescue ::Interrupt
|
|
|
|
raise $!
|
|
|
|
rescue ::Exception
|
|
|
|
elog("Error in stream server server monitor: #{$!}")
|
|
|
|
rlog(ExceptionCallStack)
|
|
|
|
break
|
2008-11-11 05:11:40 +00:00
|
|
|
end
|
2010-04-27 14:39:09 +00:00
|
|
|
end
|
2005-08-22 02:36:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method monitors client connections for data and calls the
|
|
|
|
# +on_client_data+ routine when new data arrives.
|
2005-08-22 02:36:09 +00:00
|
|
|
#
|
|
|
|
def monitor_clients
|
|
|
|
begin
|
2011-05-14 18:34:06 +00:00
|
|
|
|
|
|
|
# Wait for a notify if our client list is empty
|
2005-08-22 02:36:09 +00:00
|
|
|
if (clients.length == 0)
|
2011-05-14 18:34:06 +00:00
|
|
|
self.client_waiter.pop
|
2005-08-22 02:36:09 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2011-05-14 18:34:06 +00:00
|
|
|
sd = Rex::ThreadSafe.select(clients, nil, nil, nil)
|
2005-08-22 02:36:09 +00:00
|
|
|
|
2010-04-27 14:39:09 +00:00
|
|
|
sd[0].each { |cfd|
|
2006-02-03 19:54:08 +00:00
|
|
|
begin
|
2010-04-27 14:39:09 +00:00
|
|
|
on_client_data(cfd)
|
2009-03-25 16:41:10 +00:00
|
|
|
rescue ::EOFError, ::Errno::ECONNRESET, ::Errno::ENOTCONN, ::Errno::ECONNABORTED
|
2010-04-27 14:39:09 +00:00
|
|
|
on_client_close(cfd)
|
|
|
|
close_client(cfd)
|
2008-11-10 21:30:35 +00:00
|
|
|
rescue ::Interrupt
|
2010-04-03 06:05:06 +00:00
|
|
|
raise $!
|
2008-11-10 21:30:35 +00:00
|
|
|
rescue ::Exception
|
2010-04-27 14:39:09 +00:00
|
|
|
close_client(cfd)
|
2006-02-03 19:54:08 +00:00
|
|
|
elog("Error in stream server client monitor: #{$!}")
|
|
|
|
rlog(ExceptionCallStack)
|
2010-04-03 06:05:06 +00:00
|
|
|
|
2006-02-03 19:54:08 +00:00
|
|
|
end
|
2005-08-22 02:36:09 +00:00
|
|
|
}
|
2010-04-03 06:05:06 +00:00
|
|
|
|
2008-04-18 01:33:09 +00:00
|
|
|
rescue ::Rex::StreamClosedError => e
|
|
|
|
# Remove the closed stream from the list
|
|
|
|
clients.delete(e.stream)
|
2008-11-10 21:30:35 +00:00
|
|
|
rescue ::Interrupt
|
|
|
|
raise $!
|
|
|
|
rescue ::Exception
|
2005-08-22 02:36:09 +00:00
|
|
|
elog("Error in stream server client monitor: #{$!}")
|
2010-04-03 06:05:06 +00:00
|
|
|
rlog(ExceptionCallStack)
|
2005-08-22 02:36:09 +00:00
|
|
|
end while true
|
|
|
|
end
|
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
2008-11-10 21:30:35 +00:00
|
|
|
end
|
2010-04-03 06:05:06 +00:00
|
|
|
end
|
|
|
|
|