Skip to content

Commit

Permalink
Merge pull request #19571 from sjanusz-r7/fix-readline-unresponsive-o…
Browse files Browse the repository at this point in the history
…n-windows-11

Monkey-patch Readline to fix unresponsiveness on Windows 11
  • Loading branch information
adfoster-r7 authored Oct 18, 2024
2 parents b60a70b + 7dc918f commit 27fa707
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/msf/ui/console/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,46 @@ def choose_readline(opts)

begin
require 'readline'

# Only Windows requires a monkey-patched RbReadline
return unless Rex::Compat.is_windows

if defined?(::RbReadline) && !defined?(RbReadline.refresh_console_handle)
::RbReadline.instance_eval do
class << self
alias_method :old_rl_move_cursor_relative, :_rl_move_cursor_relative
alias_method :old_rl_get_screen_size, :_rl_get_screen_size
alias_method :old_space_to_eol, :space_to_eol
alias_method :old_insert_some_chars, :insert_some_chars
end

def self.refresh_console_handle
# hConsoleHandle gets set only when RbReadline detects it is running on Windows.
# Therefore, we don't need to check Rex::Compat.is_windows, we can simply check if hConsoleHandle is nil or not.
@hConsoleHandle = @GetStdHandle.Call(::Readline::STD_OUTPUT_HANDLE) if @hConsoleHandle
end

def self._rl_move_cursor_relative(*args)
refresh_console_handle
old_rl_move_cursor_relative(*args)
end

def self._rl_get_screen_size(*args)
refresh_console_handle
old_rl_get_screen_size(*args)
end

def self.space_to_eol(*args)
refresh_console_handle
old_space_to_eol(*args)
end

def self.insert_some_chars(*args)
refresh_console_handle
old_insert_some_chars(*args)
end
end
end
rescue ::LoadError => e
if @rl_err.nil? && index
# Then this is the first time the require failed and we have an index
Expand Down

0 comments on commit 27fa707

Please sign in to comment.