Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/ui/text/bidirectional_pipe.rb
Views: 11655
# -*- coding: binary -*-1module Rex2module Ui3module Text456class BidirectionalPipe < Rex::Ui::Text::Input78def initialize9@subscribers_out = {}10@subscribers_ref = {}11@subscribers_idx = 012@pipe_input = Rex::Ui::Text::Input::Buffer.new1314# We are the shell, the input, and the output15self.output = self16self.input = self17end1819def pipe_input20@pipe_input21end2223def close24@pipe_input.close25end2627def has_subscriber?(id)28@subscribers_out.has_key?(id)29end3031def create_subscriber(id=nil)32id ||= (@subscribers_idx += 1).to_s33@subscribers_out[id] = Rex::Ui::Text::Output::Buffer.new34return id35end3637def create_subscriber_proc(id=nil, &block)38id = create_subscriber(id)39@subscribers_ref[id] = block40end4142def remove_subscriber(id)43@subscribers_out.delete(id)44@subscribers_ref.delete(id)45end4647def write_input(buf)48@pipe_input.put(buf)49end5051def read_subscriber(id)52output = @subscribers_out[id]5354return '' if output.nil?5556buf = output.buf5758output.reset5960buf61end6263def print(msg='')64@subscribers_out.each_pair { |id, buf|65begin66@subscribers_ref[id] ? @subscribers_ref[id].call(msg) : buf.print(msg)67rescue ::Exception => e68# $stderr.puts "Error handling subscriber #{id}: #{e} #{e.backtrace.inspect}"69raise e70end71}72msg73end7475def print_error(msg='')76print_line('[-] ' + msg)77end7879alias_method :print_bad, :print_error8081def print_line(msg='')82print(msg + "\n")83end8485def print_good(msg='')86print_line('[+] ' + msg)87end8889def flush90end9192def print_status(msg='')93print_line('[*] ' + msg)94end9596def print_warning(msg='')97print_line('[!] ' + msg)98end99100#101# Wrappers for the pipe_input methods102#103104def close105@pipe_input.close106end107108def sysread(len = 1)109@pipe_input.sysread(len)110end111112def put(msg)113@pipe_input.put(msg)114end115116def gets117@pipe_input.gets118end119120def eof?121@pipe_input.eof?122end123124def fd125@pipe_input.fd126end127128#129# Wrappers for shell methods130#131132attr_accessor :output, :prompt, :input133134def intrinsic_shell?135true136end137138def supports_readline139false140end141142def supports_color?143false144end145146def pgets147gets148end149150151protected152153end154155end156end157end158159160