diff options
-rw-r--r-- | mitmproxy/tools/console/master.py | 5 | ||||
-rw-r--r-- | mitmproxy/tools/main.py | 42 | ||||
-rw-r--r-- | test/mitmproxy/tools/console/conftest.py | 9 | ||||
-rw-r--r-- | test/mitmproxy/tools/test_cmdline.py | 8 |
4 files changed, 34 insertions, 30 deletions
diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index b2d4415d..775817c7 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -32,11 +32,6 @@ class ConsoleMaster(master.Master): def __init__(self, opts): super().__init__(opts) - if not sys.stdout.isatty(): - print("Error: mitmproxy's console interface requires a tty. " - "Please run mitmproxy in an interactive shell environment.", file=sys.stderr) - sys.exit(1) - self.view = view.View() # type: view.View self.stream_path = None self.keymap = keymap.Keymap(self) diff --git a/mitmproxy/tools/main.py b/mitmproxy/tools/main.py index 6305d57f..d9be06ff 100644 --- a/mitmproxy/tools/main.py +++ b/mitmproxy/tools/main.py @@ -1,22 +1,25 @@ from __future__ import print_function # this is here for the version check to work on Python 2. + import sys -# This must be at the very top, before importing anything else that might break! -# Keep all other imports below with the 'noqa' magic comment. if sys.version_info < (3, 5): + # This must be before any mitmproxy imports, as they already break! + # Keep all other imports below with the 'noqa' magic comment. print("#" * 49, file=sys.stderr) print("# mitmproxy only supports Python 3.5 and above! #", file=sys.stderr) print("#" * 49, file=sys.stderr) +import argparse # noqa import os # noqa import signal # noqa +import typing # noqa from mitmproxy.tools import cmdline # noqa -from mitmproxy import exceptions # noqa +from mitmproxy import exceptions, master # noqa from mitmproxy import options # noqa from mitmproxy import optmanager # noqa from mitmproxy import proxy # noqa -from mitmproxy import log # noqa +from mitmproxy import log # noqa from mitmproxy.utils import debug # noqa @@ -53,7 +56,12 @@ def process_options(parser, opts, args): return proxy.config.ProxyConfig(opts) -def run(MasterKlass, args, extra=None): # pragma: no cover +def run( + master_cls: typing.Type[master.Master], + make_parser: typing.Callable[[options.Options], argparse.ArgumentParser], + arguments: typing.Sequence[str], + extra=typing.Callable[[typing.Any], dict] +): # pragma: no cover """ extra: Extra argument processing callable which returns a dict of options. @@ -61,12 +69,14 @@ def run(MasterKlass, args, extra=None): # pragma: no cover debug.register_info_dumpers() opts = options.Options() - parser = cmdline.mitmdump(opts) - args = parser.parse_args(args) - master = None + master = master_cls(opts) + + parser = make_parser(opts) + args = parser.parse_args(arguments) try: unknown = optmanager.load_paths(opts, args.conf) pconf = process_options(parser, opts, args) + server = None # type: typing.Any if pconf.options.server: try: server = proxy.server.ProxyServer(pconf) @@ -76,7 +86,6 @@ def run(MasterKlass, args, extra=None): # pragma: no cover else: server = proxy.server.DummyServer(pconf) - master = MasterKlass(opts) master.server = server master.addons.trigger("configure", opts.keys()) master.addons.trigger("tick") @@ -114,8 +123,13 @@ def mitmproxy(args=None): # pragma: no cover assert_utf8_env() + if not sys.stdout.isatty(): + print("Error: mitmproxy's console interface requires a tty. " + "Please run mitmproxy in an interactive shell environment.", file=sys.stderr) + sys.exit(1) + from mitmproxy.tools import console - run(console.master.ConsoleMaster, args) + run(console.master.ConsoleMaster, cmdline.mitmproxy, args) def mitmdump(args=None): # pragma: no cover @@ -125,16 +139,16 @@ def mitmdump(args=None): # pragma: no cover if args.filter_args: v = " ".join(args.filter_args) return dict( - view_filter = v, - save_stream_filter = v, + view_filter=v, + save_stream_filter=v, ) return {} - m = run(dump.DumpMaster, args, extra) + m = run(dump.DumpMaster, cmdline.mitmdump, args, extra) if m and m.errorcheck.has_errored: sys.exit(1) def mitmweb(args=None): # pragma: no cover from mitmproxy.tools import web - run(web.master.WebMaster, args) + run(web.master.WebMaster, cmdline.mitmweb, args) diff --git a/test/mitmproxy/tools/console/conftest.py b/test/mitmproxy/tools/console/conftest.py deleted file mode 100644 index afd94c6a..00000000 --- a/test/mitmproxy/tools/console/conftest.py +++ /dev/null @@ -1,9 +0,0 @@ -from unittest import mock - -import pytest - - -@pytest.fixture(scope="module", autouse=True) -def definitely_atty(): - with mock.patch("sys.stdout.isatty", lambda: True): - yield diff --git a/test/mitmproxy/tools/test_cmdline.py b/test/mitmproxy/tools/test_cmdline.py index 65cfeb07..e247dc1d 100644 --- a/test/mitmproxy/tools/test_cmdline.py +++ b/test/mitmproxy/tools/test_cmdline.py @@ -1,7 +1,8 @@ import argparse -from mitmproxy.tools import cmdline -from mitmproxy.tools import main + from mitmproxy import options +from mitmproxy.tools import cmdline, web, dump, console +from mitmproxy.tools import main def test_common(): @@ -14,17 +15,20 @@ def test_common(): def test_mitmproxy(): opts = options.Options() + console.master.ConsoleMaster(opts) ap = cmdline.mitmproxy(opts) assert ap def test_mitmdump(): opts = options.Options() + dump.DumpMaster(opts) ap = cmdline.mitmdump(opts) assert ap def test_mitmweb(): opts = options.Options() + web.master.WebMaster(opts) ap = cmdline.mitmweb(opts) assert ap |