diff options
author | Henrique <typoon@gmail.com> | 2019-11-23 15:31:00 -0500 |
---|---|---|
committer | Henrique <typoon@gmail.com> | 2019-11-23 15:31:00 -0500 |
commit | a866b424fe60928fb5336f1fa146326424763ca5 (patch) | |
tree | c2cdbebf181c3f15601daf472929210a59449f52 /mitmproxy/addons | |
parent | 16b55f9476373347a3c2553e070497b383288360 (diff) | |
download | mitmproxy-a866b424fe60928fb5336f1fa146326424763ca5.tar.gz mitmproxy-a866b424fe60928fb5336f1fa146326424763ca5.tar.bz2 mitmproxy-a866b424fe60928fb5336f1fa146326424763ca5.zip |
Moved command history to an addon and added a new feature:
* If you start typing a command and press "up" only commands starting
with that string will be returned
Diffstat (limited to 'mitmproxy/addons')
-rw-r--r-- | mitmproxy/addons/__init__.py | 2 | ||||
-rw-r--r-- | mitmproxy/addons/command_history.py | 152 |
2 files changed, 154 insertions, 0 deletions
diff --git a/mitmproxy/addons/__init__.py b/mitmproxy/addons/__init__.py index 838fba9b..ee238938 100644 --- a/mitmproxy/addons/__init__.py +++ b/mitmproxy/addons/__init__.py @@ -4,6 +4,7 @@ from mitmproxy.addons import block from mitmproxy.addons import browser from mitmproxy.addons import check_ca from mitmproxy.addons import clientplayback +from mitmproxy.addons import command_history from mitmproxy.addons import core from mitmproxy.addons import cut from mitmproxy.addons import disable_h2c @@ -30,6 +31,7 @@ def default_addons(): anticomp.AntiComp(), check_ca.CheckCA(), clientplayback.ClientPlayback(), + command_history.CommandHistory(), cut.Cut(), disable_h2c.DisableH2C(), export.Export(), diff --git a/mitmproxy/addons/command_history.py b/mitmproxy/addons/command_history.py new file mode 100644 index 00000000..2c348d1e --- /dev/null +++ b/mitmproxy/addons/command_history.py @@ -0,0 +1,152 @@ +import collections +import copy +import os +import typing + +import mitmproxy.options +import mitmproxy.types + +from mitmproxy import command +from mitmproxy.tools.console.commander.commander import CommandBuffer + + +class CommandHistory: + def __init__(self, size: int = 300) -> None: + self.saved_commands: typing.Deque[str] = collections.deque( + maxlen=size + ) + self.index: int = 0 + + self.filter: str = '' + self.filtered_index: int = 0 + self.filtered_commands: typing.Deque[str] = collections.deque() + self.filter_active: bool = True + + _command_history_path = os.path.join(os.path.expanduser(mitmproxy.options.CONF_DIR), 'command_history') + _history_lines = open(_command_history_path, 'r').readlines() + + self.command_history_file = open(_command_history_path, 'w') + + for l in _history_lines: + self.add_command(l.strip(), True) + + @property + def last_index(self): + return len(self.saved_commands) - 1 + + @property + def last_filtered_index(self): + return len(self.filtered_commands) - 1 + + @command.command("command_history.clear") + def clear_history(self): + self.saved_commands.clear() + self.index = 0 + self.command_history_file.truncate(0) + self.command_history_file.seek(0) + self.command_history_file.flush() + self.filter = '' + self.filtered_index = 0 + self.filtered_commands.clear() + self.filter_active = True + + @command.command("command_history.next") + def get_next(self) -> str: + if self.last_index == -1: + return '' + + if self.filter != '': + if self.filtered_index < self.last_filtered_index: + self.filtered_index = self.filtered_index + 1 + ret = self.filtered_commands[self.filtered_index] + else: + if self.index == -1: + ret = '' + elif self.index < self.last_index: + self.index = self.index + 1 + ret = self.saved_commands[self.index] + else: + self.index = -1 + ret = '' + + + return ret + + @command.command("command_history.prev") + def get_prev(self) -> str: + if self.last_index == -1: + return '' + + if self.filter != '': + if self.filtered_index > 0: + self.filtered_index = self.filtered_index - 1 + ret = self.filtered_commands[self.filtered_index] + else: + if self.index == -1: + self.index = self.last_index + elif self.index > 0: + self.index = self.index - 1 + + ret = self.saved_commands[self.index] + + return ret + + @command.command("command_history.filter") + def set_filter(self, command: str) -> None: + """ + This is used when the user starts typing part of a command + and then press the "up" arrow. This way, the results returned are + only for the command that the user started typing + """ + if command.strip() == '': + return + + if self.filter != '': + last_filtered_command = self.filtered_commands[-1] + if command == last_filtered_command: + self.filter = '' + self.filtered_commands = [] + self.filtered_index = 0 + else: + self.filter = command + _filtered_commands = [c for c in self.saved_commands if c.startswith(command)] + self.filtered_commands = collections.deque(_filtered_commands) + + if command not in self.filtered_commands: + self.filtered_commands.append(command) + + self.filtered_index = self.last_filtered_index + + # No commands found, so act like no filter was added + if len(self.filtered_commands) == 1: + self.add_command(command) + self.filter = '' + + @command.command("command_history.cancel") + def restart(self) -> None: + self.index = -1 + self.filter = '' + self.filtered_commands = [] + self.filtered_index = 0 + + @command.command("command_history.add") + def add_command(self, command: str, execution: bool = False) -> None: + if command.strip() == '': + return + + if execution: + if command in self.saved_commands: + self.saved_commands.remove(command) + + self.saved_commands.append(command) + + _history_str = "\n".join(self.saved_commands) + self.command_history_file.truncate(0) + self.command_history_file.seek(0) + self.command_history_file.write(_history_str) + self.command_history_file.flush() + + self.restart() + else: + if command not in self.saved_commands: + self.saved_commands.append(command) |