From 0cb38f6329d1735aee9fe034499a8c1497172f9f Mon Sep 17 00:00:00 2001 From: Miroslav Date: Fri, 19 Jan 2018 02:07:01 +0200 Subject: [PATCH 1/3] Fix #2777 --- mitmproxy/tools/console/commander/commander.py | 10 +++++++++- test/mitmproxy/tools/console/test_commander.py | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index e2088e71f..bd54fdde9 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -47,9 +47,12 @@ CompletionState = typing.NamedTuple( ) -class CommandBuffer(): +class CommandBuffer: def __init__(self, master: mitmproxy.master.Master, start: str = "") -> None: self.master = master + option_setting = start.startswith("set") and start.endswith("=") + if option_setting: + start += self.get_option_value(start) self.text = self.flatten(start) # Cursor is always within the range [0:len(buffer)]. self._cursor = len(self.text) @@ -94,6 +97,11 @@ class CommandBuffer(): ret.append(("commander_hint", "%s " % v)) return ret + def get_option_value(self, txt): + option = txt.rstrip("=").split()[1] + option_value = getattr(self.master.options, option, None) + return option_value if option_value else "" + def flatten(self, txt): parts, _ = self.master.commands.parse_partial(txt) return " ".join([x.value for x in parts]) diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py index 2a96995d5..cc2f3f355 100644 --- a/test/mitmproxy/tools/console/test_commander.py +++ b/test/mitmproxy/tools/console/test_commander.py @@ -1,4 +1,4 @@ - +from mitmproxy import options from mitmproxy.tools.console.commander import commander from mitmproxy.test import taddons @@ -96,3 +96,11 @@ class TestCommandBuffer: with taddons.context() as tctx: cb = commander.CommandBuffer(tctx.master) assert cb.flatten("foo bar") == "foo bar" + + def test_get_option_value(self): + opts = options.Options(view_filter="value") + with taddons.context(options=opts) as tctx: + cb = commander.CommandBuffer(tctx.master) + assert cb.get_option_value("set unknown_option=") == "" + assert cb.get_option_value("set intercept=") == "" + assert cb.get_option_value("set view_filter=") == "value" From 9f78c36ec447913dde9a0acd69683e856a843db4 Mon Sep 17 00:00:00 2001 From: Miroslav Date: Thu, 8 Feb 2018 20:11:14 +0200 Subject: [PATCH 2/3] New command --- mitmproxy/tools/console/commander/commander.py | 8 -------- mitmproxy/tools/console/consoleaddons.py | 9 +++++++++ mitmproxy/tools/console/defaultkeys.py | 6 +++--- test/mitmproxy/tools/console/test_commander.py | 10 +--------- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index bd54fdde9..566c42e6c 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -50,9 +50,6 @@ CompletionState = typing.NamedTuple( class CommandBuffer: def __init__(self, master: mitmproxy.master.Master, start: str = "") -> None: self.master = master - option_setting = start.startswith("set") and start.endswith("=") - if option_setting: - start += self.get_option_value(start) self.text = self.flatten(start) # Cursor is always within the range [0:len(buffer)]. self._cursor = len(self.text) @@ -97,11 +94,6 @@ class CommandBuffer: ret.append(("commander_hint", "%s " % v)) return ret - def get_option_value(self, txt): - option = txt.rstrip("=").split()[1] - option_value = getattr(self.master.options, option, None) - return option_value if option_value else "" - def flatten(self, txt): parts, _ = self.master.commands.parse_partial(txt) return " ".join([x.value for x in parts]) diff --git a/mitmproxy/tools/console/consoleaddons.py b/mitmproxy/tools/console/consoleaddons.py index b10d27e45..affbf4d39 100644 --- a/mitmproxy/tools/console/consoleaddons.py +++ b/mitmproxy/tools/console/consoleaddons.py @@ -277,6 +277,15 @@ class ConsoleAddon: """ signals.status_prompt_command.send(partial=" ".join(partial)) # type: ignore + @command.command("console.command.set") + def console_command_set(self, option: str) -> None: + """Doc""" + option_value = getattr(self.master.options, option, None) + current_value = option_value if option_value else "" + self.master.commands.call( + "console.command set {}={}".format(option, current_value) + ) + @command.command("console.view.keybindings") def view_keybindings(self) -> None: """View the commands list.""" diff --git a/mitmproxy/tools/console/defaultkeys.py b/mitmproxy/tools/console/defaultkeys.py index d01d9b7e2..084ef2622 100644 --- a/mitmproxy/tools/console/defaultkeys.py +++ b/mitmproxy/tools/console/defaultkeys.py @@ -26,8 +26,8 @@ def map(km): km.add("ctrl b", "console.nav.pageup", ["global"], "Page up") km.add("I", "console.intercept.toggle", ["global"], "Toggle intercept") - km.add("i", "console.command set intercept=", ["global"], "Set intercept") - km.add("W", "console.command set save_stream_file=", ["global"], "Stream to file") + km.add("i", "console.command.set intercept", ["global"], "Set intercept") + km.add("W", "console.command.set save_stream_file", ["global"], "Stream to file") km.add("A", "flow.resume @all", ["flowlist", "flowview"], "Resume all intercepted flows") km.add("a", "flow.resume @focus", ["flowlist", "flowview"], "Resume this intercepted flow") km.add( @@ -46,7 +46,7 @@ def map(km): ["flowlist", "flowview"], "Export this flow to file" ) - km.add("f", "console.command set view_filter=", ["flowlist"], "Set view filter") + km.add("f", "console.command.set view_filter", ["flowlist"], "Set view filter") km.add("F", "set console_focus_follow=toggle", ["flowlist"], "Set focus follow") km.add( "ctrl l", diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py index cc2f3f355..2a96995d5 100644 --- a/test/mitmproxy/tools/console/test_commander.py +++ b/test/mitmproxy/tools/console/test_commander.py @@ -1,4 +1,4 @@ -from mitmproxy import options + from mitmproxy.tools.console.commander import commander from mitmproxy.test import taddons @@ -96,11 +96,3 @@ class TestCommandBuffer: with taddons.context() as tctx: cb = commander.CommandBuffer(tctx.master) assert cb.flatten("foo bar") == "foo bar" - - def test_get_option_value(self): - opts = options.Options(view_filter="value") - with taddons.context(options=opts) as tctx: - cb = commander.CommandBuffer(tctx.master) - assert cb.get_option_value("set unknown_option=") == "" - assert cb.get_option_value("set intercept=") == "" - assert cb.get_option_value("set view_filter=") == "value" From e64947d3bc00375fcb80898f48add3593b787d24 Mon Sep 17 00:00:00 2001 From: Miroslav Date: Thu, 8 Feb 2018 21:14:10 +0200 Subject: [PATCH 3/3] Docs --- mitmproxy/tools/console/consoleaddons.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mitmproxy/tools/console/consoleaddons.py b/mitmproxy/tools/console/consoleaddons.py index affbf4d39..e4a7503cb 100644 --- a/mitmproxy/tools/console/consoleaddons.py +++ b/mitmproxy/tools/console/consoleaddons.py @@ -279,11 +279,13 @@ class ConsoleAddon: @command.command("console.command.set") def console_command_set(self, option: str) -> None: - """Doc""" + """ + Prompt the user to set an option of the form "key[=value]". + """ option_value = getattr(self.master.options, option, None) current_value = option_value if option_value else "" self.master.commands.call( - "console.command set {}={}".format(option, current_value) + "console.command set %s=%s" % (option, current_value) ) @command.command("console.view.keybindings")