diff options
author | Aldo Cortesi <aldo@corte.si> | 2017-04-28 12:10:02 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-28 12:10:02 +1200 |
commit | cbb35cee51a6fa89d248cd8808af6e03f3dbfe2a (patch) | |
tree | 9dde488216525ac70d63d6729a8fd712381ebe13 | |
parent | 29b3e787cab7e9bf9ca74453c894fc47882cc32d (diff) | |
parent | ce01cb9c097614c12113df15e1f35839c1926e4d (diff) | |
download | mitmproxy-cbb35cee51a6fa89d248cd8808af6e03f3dbfe2a.tar.gz mitmproxy-cbb35cee51a6fa89d248cd8808af6e03f3dbfe2a.tar.bz2 mitmproxy-cbb35cee51a6fa89d248cd8808af6e03f3dbfe2a.zip |
Merge pull request #2280 from cortesi/coreset
commands: add the core command addon, and the command "set"
-rw-r--r-- | mitmproxy/addonmanager.py | 2 | ||||
-rw-r--r-- | mitmproxy/addons/__init__.py | 2 | ||||
-rw-r--r-- | mitmproxy/addons/core.py | 18 | ||||
-rw-r--r-- | mitmproxy/tools/console/master.py | 7 | ||||
-rw-r--r-- | mitmproxy/tools/console/window.py | 13 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_core.py | 17 |
6 files changed, 43 insertions, 16 deletions
diff --git a/mitmproxy/addonmanager.py b/mitmproxy/addonmanager.py index ea23b6ff..9779a31a 100644 --- a/mitmproxy/addonmanager.py +++ b/mitmproxy/addonmanager.py @@ -56,7 +56,7 @@ def safecall(): try: with contextlib.redirect_stdout(stdout_replacement): yield - except exceptions.AddonHalt: + except (exceptions.AddonHalt, exceptions.OptionsError): raise except Exception as e: etype, value, tb = sys.exc_info() diff --git a/mitmproxy/addons/__init__.py b/mitmproxy/addons/__init__.py index e87f2cbd..fa194fc1 100644 --- a/mitmproxy/addons/__init__.py +++ b/mitmproxy/addons/__init__.py @@ -4,6 +4,7 @@ from mitmproxy.addons import check_alpn from mitmproxy.addons import check_ca from mitmproxy.addons import clientplayback from mitmproxy.addons import core_option_validation +from mitmproxy.addons import core from mitmproxy.addons import disable_h2c from mitmproxy.addons import onboarding from mitmproxy.addons import proxyauth @@ -20,6 +21,7 @@ from mitmproxy.addons import upstream_auth def default_addons(): return [ + core.Core(), core_option_validation.CoreOptionValidation(), anticache.AntiCache(), anticomp.AntiComp(), diff --git a/mitmproxy/addons/core.py b/mitmproxy/addons/core.py new file mode 100644 index 00000000..7b648403 --- /dev/null +++ b/mitmproxy/addons/core.py @@ -0,0 +1,18 @@ +from mitmproxy import ctx +from mitmproxy import exceptions + + +class Core: + def set(self, spec: str) -> None: + """ + Set an option of the form "key[=value]". When the value is omitted, + booleans are set to true, strings and integers are set to None (if + permitted), and sequences are emptied. + """ + try: + ctx.options.set(spec) + except exceptions.OptionsError as e: + raise exceptions.CommandError(e) from e + + def load(self, l): + l.add_command("set", self.set) diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index d65562de..74413f4c 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -83,9 +83,9 @@ class ConsoleCommands: def __init__(self, master): self.master = master - def command(self) -> None: + def command(self, partial: str) -> None: """Prompt for a command.""" - signals.status_prompt_command.send() + signals.status_prompt_command.send(partial=partial) def view_commands(self) -> None: """View the commands list.""" @@ -120,12 +120,13 @@ class ConsoleCommands: def default_keymap(km): - km.add(":", "console.command") + km.add(":", "console.command ''") km.add("?", "console.view.help") km.add("C", "console.view.commands") km.add("O", "console.view.options") km.add("Q", "console.exit") km.add("q", "console.view.pop") + km.add("i", "console.command 'set intercept='") class ConsoleMaster(master.Master): diff --git a/mitmproxy/tools/console/window.py b/mitmproxy/tools/console/window.py index 0e41fb2a..b7fc8efa 100644 --- a/mitmproxy/tools/console/window.py +++ b/mitmproxy/tools/console/window.py @@ -82,15 +82,4 @@ class Window(urwid.Frame): def keypress(self, size, k): k = super().keypress(size, k) - k = self.master.keymap.handle("", k) - if not k: - return - elif k == "i": - signals.status_prompt.send( - self, - prompt = "Intercept filter", - text = self.master.options.intercept, - callback = self.master.options.setter("intercept") - ) - else: - return k + return self.master.keymap.handle("", k) diff --git a/test/mitmproxy/addons/test_core.py b/test/mitmproxy/addons/test_core.py new file mode 100644 index 00000000..6ebf4ba9 --- /dev/null +++ b/test/mitmproxy/addons/test_core.py @@ -0,0 +1,17 @@ +from mitmproxy.addons import core +from mitmproxy.test import taddons +from mitmproxy import exceptions +import pytest + + +def test_set(): + sa = core.Core() + with taddons.context() as tctx: + tctx.master.addons.add(sa) + + assert not tctx.master.options.anticomp + tctx.command(sa.set, "anticomp") + assert tctx.master.options.anticomp + + with pytest.raises(exceptions.CommandError): + tctx.command(sa.set, "nonexistent") |