Skip to content

Commit 640a302

Browse files
committed
Switch to a Queue for the dispatcher's packet queue
The select() based sleep can be replaced by a blocking pop(). The thread will be suspended until data is pushed onto the queue.
1 parent b2d6458 commit 640a302

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

lib/rex/post/meterpreter/packet_dispatcher.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def monitor_socket
253253

254254
self.waiters = []
255255

256-
@pqueue = []
256+
@pqueue = ::Queue.new
257257
@finish = false
258258
@last_recvd = Time.now
259259
@ping_sent = false
@@ -318,16 +318,12 @@ def monitor_socket
318318
# Whether we're finished or not is determined by the receiver
319319
# thread above.
320320
while(not @finish)
321-
if(@pqueue.empty?)
322-
::IO.select(nil, nil, nil, 0.10)
323-
next
324-
end
325-
326321
incomplete = []
327322
backlog = []
328323

324+
backlog << @pqueue.pop
329325
while(@pqueue.length > 0)
330-
backlog << @pqueue.shift
326+
backlog << @pqueue.pop
331327
end
332328

333329
#
@@ -393,11 +389,16 @@ def monitor_socket
393389
::IO.select(nil, nil, nil, 0.10)
394390
end
395391

396-
@pqueue.unshift(*incomplete)
392+
while incomplete.length > 0
393+
@pqueue << incomplete.shift
394+
end
397395

398396
if(@pqueue.length > 100)
399-
dlog("Backlog has grown to over 100 in monitor_socket, dropping older packets: #{@pqueue[0 .. 25].map{|x| x.inspect}.join(" - ")}", 'meterpreter', LEV_1)
400-
@pqueue = @pqueue[25 .. 100]
397+
removed = []
398+
(1..25).each {
399+
removed << @pqueue.pop
400+
}
401+
dlog("Backlog has grown to over 100 in monitor_socket, dropping older packets: #{removed.map{|x| x.inspect}.join(" - ")}", 'meterpreter', LEV_1)
401402
end
402403
end
403404
rescue ::Exception => e

0 commit comments

Comments
 (0)