diff --git a/mitmproxy/tools/console/keymap.py b/mitmproxy/tools/console/keymap.py index 5f7cdb115..6a900527e 100644 --- a/mitmproxy/tools/console/keymap.py +++ b/mitmproxy/tools/console/keymap.py @@ -163,16 +163,25 @@ requiredKeyAttrs = set(["key", "cmd"]) class KeymapConfig: + defaultFile = "keys.yaml" + @command.command("console.keymap.load") def keymap_load_path(self, path: mitmproxy.types.Path) -> None: try: - master = ctx.master # type: mitmproxy.tools.console.master.ConsoleMaster - self.load_path(master.keymap, path) + self.load_path(ctx.master.keymap, path) # type: ignore except (OSError, KeyBindingError) as e: raise exceptions.CommandError( "Could not load key bindings - %s" % e ) from e + def running(self): + p = os.path.join(os.path.expanduser(ctx.options.confdir), self.defaultFile) + if os.path.exists(p): + try: + self.load_path(ctx.master.keymap, p) + except KeyBindingError as e: + ctx.log.error(e) + def load_path(self, km, p): if os.path.exists(p) and os.path.isfile(p): with open(p, "rt", encoding="utf8") as f: @@ -193,7 +202,7 @@ class KeymapConfig: km.add( key = v["key"], command = v["cmd"], - contexts = v.get("ctx", None), + contexts = v.get("ctx", ["global"]), help = v.get("help", None), ) except ValueError as e: diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index c13bff270..1bc79e5cc 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -56,6 +56,7 @@ class ConsoleMaster(master.Master): consoleaddons.UnsupportedLog(), readfile.ReadFile(), consoleaddons.ConsoleAddon(self), + keymap.KeymapConfig(), ) def sigint_handler(*args, **kwargs): diff --git a/test/mitmproxy/tools/console/test_keymap.py b/test/mitmproxy/tools/console/test_keymap.py index 901cd7954..3e6f7c2ee 100644 --- a/test/mitmproxy/tools/console/test_keymap.py +++ b/test/mitmproxy/tools/console/test_keymap.py @@ -78,6 +78,7 @@ def test_load_path(tmpdir): kmc = keymap.KeymapConfig() with taddons.context(kmc) as tctx: km = keymap.Keymap(tctx.master) + tctx.master.keymap = km with open(dst, 'wb') as f: f.write(b"\xff\xff\xff") diff --git a/test/mitmproxy/utils/test_arg_check.py b/test/mitmproxy/utils/test_arg_check.py index 0a5edd51b..8ab140770 100644 --- a/test/mitmproxy/utils/test_arg_check.py +++ b/test/mitmproxy/utils/test_arg_check.py @@ -11,8 +11,8 @@ from mitmproxy.utils import arg_check (["-T"], "-T is deprecated, please use --mode transparent instead"), (["-U"], "-U is deprecated, please use --mode upstream:SPEC instead"), (["--confdir"], "--confdir is deprecated.\n" - "Please use `--set confdir=value` instead.\n" - "To show all options and their default values use --options"), + "Please use `--set confdir=value` instead.\n" + "To show all options and their default values use --options"), (["--palette"], "--palette is deprecated.\n" "Please use `--set console_palette=value` instead.\n" "To show all options and their default values use --options"),