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/post/hwbridge/ui/console.rb
Views: 11784
# -*- coding: binary -*-1require 'rex/post/hwbridge'23module Rex4module Post5module HWBridge6module Ui78###9#10# This class provides a shell driven interface to the hwbridge client API.11#12###13class Console1415include Rex::Ui::Text::DispatcherShell1617# Dispatchers18require 'rex/post/hwbridge/ui/console/interactive_channel'19require 'rex/post/hwbridge/ui/console/command_dispatcher'20require 'rex/post/hwbridge/ui/console/command_dispatcher/core'2122#23# Initialize the hardware bridge console.24#25def initialize(client)26if (Rex::Compat.is_windows())27super("hwbridge")28else29super("%undhwbridge%clr")30end3132# The hwbridge client context33self.client = client3435# Queued commands array36self.commands = []3738# Point the input/output handles elsewhere39reset_ui4041enstack_dispatcher(Console::CommandDispatcher::Core)4243# Set up logging to whatever logsink 'core' is using44if ! $dispatcher['hwbridge']45$dispatcher['hwbridge'] = $dispatcher['core']46end47end4849#50# Called when someone wants to interact with the hwbridge client. It's51# assumed that init_ui has been called prior.52#53def interact(&block)54# Run queued commands55commands.delete_if { |ent|56run_single(ent)57true58}5960# Run the interactive loop61run { |line|62# Run the command63run_single(line)6465# If a block was supplied, call it, otherwise return false66if (block)67block.call68else69false70end71}72end7374#75# Interacts with the supplied channel.76#77def interact_with_channel(channel, *_)78channel.extend(InteractiveChannel) unless (channel.kind_of?(InteractiveChannel) == true)79channel.on_command_proc = self.on_command_proc if self.on_command_proc80channel.on_print_proc = self.on_print_proc if self.on_print_proc81channel.on_log_proc = method(:log_output) if self.respond_to?(:log_output, true)8283channel.interact(input, output)84channel.reset_ui85end8687#88# Queues a command to be run when the interactive loop is entered.89#90def queue_cmd(cmd)91self.commands << cmd92end9394#95# Runs the specified command wrapper in something to catch hwbridge96# exceptions.97#98def run_command(dispatcher, method, arguments)99begin100super101rescue Timeout::Error102log_error("Operation timed out.")103rescue RequestError => info104log_error(info.to_s)105rescue Rex::InvalidDestination => e106log_error(e.message)107rescue ::Errno::EPIPE, ::OpenSSL::SSL::SSLError, ::IOError108self.client.kill109rescue ::Exception => e110log_error("Error running command #{method}: #{e.class} #{e}")111end112end113114#115# Logs that an error occurred and persists the callstack.116#117def log_error(msg)118print_error(msg)119120elog(msg, 'hwbridge')121122dlog("Call stack:\n#{$@.join("\n")}", 'hwbridge')123end124125attr_reader :client # :nodoc:126127protected128129attr_writer :client # :nodoc:130attr_accessor :commands # :nodoc:131132end133134end135end136end137end138139140