2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2006-09-10 02:21:23 +00:00
|
|
|
module Rex
|
|
|
|
module IO
|
|
|
|
|
|
|
|
require 'rex/ui/text/output'
|
|
|
|
require 'rex/ui/text/output/buffer'
|
|
|
|
require 'rex/ui/text/input/buffer'
|
|
|
|
|
2007-01-19 08:46:06 +00:00
|
|
|
class BidirectionalPipe < Rex::Ui::Text::Input
|
2006-09-10 02:21:23 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def initialize
|
|
|
|
@subscribers_out = {}
|
|
|
|
@subscribers_ref = {}
|
|
|
|
@subscribers_idx = 0
|
|
|
|
@pipe_input = Rex::Ui::Text::Input::Buffer.new
|
|
|
|
|
|
|
|
# We are the shell, the input, and the output
|
|
|
|
self.output = self
|
|
|
|
self.input = self
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipe_input
|
|
|
|
@pipe_input
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@pipe_input.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_subscriber?(id)
|
|
|
|
@subscribers_out.has_key?(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_subscriber(id=nil)
|
|
|
|
id ||= (@subscribers_idx += 1).to_s
|
|
|
|
@subscribers_out[id] = Rex::Ui::Text::Output::Buffer.new
|
|
|
|
return id
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_subscriber_proc(id=nil, &block)
|
|
|
|
id = create_subscriber(id)
|
|
|
|
@subscribers_ref[id] = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_subscriber(id)
|
|
|
|
@subscribers_out.delete(id)
|
|
|
|
@subscribers_ref.delete(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_input(buf)
|
|
|
|
@pipe_input.put(buf)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_subscriber(id)
|
|
|
|
output = @subscribers_out[id]
|
|
|
|
|
|
|
|
return '' if output.nil?
|
|
|
|
|
|
|
|
buf = output.buf
|
|
|
|
|
|
|
|
output.reset
|
|
|
|
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
|
|
|
def print(msg='')
|
|
|
|
@subscribers_out.each_pair { |id, buf|
|
|
|
|
begin
|
|
|
|
@subscribers_ref[id] ? @subscribers_ref[id].call(msg) : buf.print(msg)
|
|
|
|
rescue ::Exception => e
|
|
|
|
# $stderr.puts "Error handling subscriber #{id}: #{e} #{e.backtrace.inspect}"
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
}
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_error(msg='')
|
|
|
|
print_line('[-] ' + msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_line(msg='')
|
|
|
|
print(msg + "\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_good(msg='')
|
|
|
|
print_line('[+] ' + msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def flush
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_status(msg='')
|
|
|
|
print_line('[*] ' + msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_warning(msg='')
|
|
|
|
print_line('[!] ' + msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Wrappers for the pipe_input methods
|
|
|
|
#
|
|
|
|
|
|
|
|
def close
|
|
|
|
@pipe_input.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def sysread(len = 1)
|
|
|
|
@pipe_input.sysread(len)
|
|
|
|
end
|
2007-02-11 09:22:06 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def put(msg)
|
|
|
|
@pipe_input.put(msg)
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def gets
|
|
|
|
@pipe_input.gets
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def eof?
|
|
|
|
@pipe_input.eof?
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def fd
|
|
|
|
@pipe_input.fd
|
|
|
|
end
|
2007-02-11 09:22:06 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# Wrappers for shell methods
|
|
|
|
#
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
attr_accessor :output, :prompt, :input
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def intrinsic_shell?
|
|
|
|
true
|
|
|
|
end
|
2007-02-11 09:22:06 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def supports_readline
|
|
|
|
false
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def supports_color?
|
|
|
|
false
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def pgets
|
|
|
|
gets
|
|
|
|
end
|
2007-02-11 09:22:06 +00:00
|
|
|
|
|
|
|
|
2006-09-10 02:21:23 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
end
|