From 8a07059cf43104636f8f6c6f52854da66a9b9ea8 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 28 Apr 2017 10:41:44 +1200 Subject: commands: add the core command addon, and the command "set" The set command sets an option using the same syntax as commandline --set. --- mitmproxy/addons/__init__.py | 2 ++ mitmproxy/addons/core.py | 18 ++++++++++++++++++ test/mitmproxy/addons/test_core.py | 15 +++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 mitmproxy/addons/core.py create mode 100644 test/mitmproxy/addons/test_core.py 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/test/mitmproxy/addons/test_core.py b/test/mitmproxy/addons/test_core.py new file mode 100644 index 00000000..99bf3f40 --- /dev/null +++ b/test/mitmproxy/addons/test_core.py @@ -0,0 +1,15 @@ +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: + 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") -- cgit v1.2.3