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/meterpreter/packet_response_waiter.rb
Views: 11784
# -*- coding: binary -*-12require 'timeout'3require 'thread'45module Rex6module Post7module Meterpreter89###10#11# This class handles waiting for a response to a given request12# and the subsequent response association.13#14###15class PacketResponseWaiter1617# Arbitrary argument to {#completion_routine}18#19# @return [Object,nil]20attr_accessor :completion_param2122# A callback to be called when this waiter is notified of a packet's23# arrival. If not nil, this will be called with the response packet as first24# parameter and {#completion_param} as the second.25#26# @return [Proc,nil]27attr_accessor :completion_routine2829# @return [ConditionVariable]30attr_accessor :cond3132# @return [Mutex]33attr_accessor :mutex3435# @return [Packet]36attr_accessor :response3738# @return [Integer] request ID to wait for39attr_accessor :rid4041#42# Initializes a response waiter instance for the supplied request43# identifier.44#45def initialize(rid, completion_routine = nil, completion_param = nil)46self.rid = rid.dup47self.response = nil4849if (completion_routine)50self.completion_routine = completion_routine51self.completion_param = completion_param52else53self.mutex = Mutex.new54self.cond = ConditionVariable.new55end56end5758#59# Checks to see if this waiter instance is waiting for the supplied60# packet based on its request identifier.61#62def waiting_for?(packet)63return (packet.rid == rid)64end6566#67# Notifies the waiter that the supplied response packet has arrived.68#69# @param response [Packet]70# @return [void]71def notify(response)72if (self.completion_routine)73self.response = response74self.completion_routine.call(response, self.completion_param)75else76self.mutex.synchronize do77self.response = response78self.cond.signal79end80end81end8283#84# Wait for a given time interval for the response packet to arrive.85#86# @param interval [Integer,nil] number of seconds to wait, or nil to wait87# forever88# @return [Packet,nil] the response, or nil if the interval elapsed before89# receiving one90def wait(interval)91interval = nil if interval and interval == -192self.mutex.synchronize do93if self.response.nil?94self.cond.wait(self.mutex, interval)95end96end97return self.response98end99100end101102end; end; end103104105106