diff --git a/mitmproxy/proxy/server.py b/mitmproxy/proxy/server.py index 50a2b76b8..5171fbee6 100644 --- a/mitmproxy/proxy/server.py +++ b/mitmproxy/proxy/server.py @@ -48,6 +48,8 @@ class ProxyServer(tcp.TCPServer): if config.options.mode == "transparent": platform.init_transparent_mode() except Exception as e: + if self.socket: + self.socket.close() raise exceptions.ServerException( 'Error starting proxy server: ' + repr(e) ) from e diff --git a/mitmproxy/tools/main.py b/mitmproxy/tools/main.py index d8fac077a..568c51770 100644 --- a/mitmproxy/tools/main.py +++ b/mitmproxy/tools/main.py @@ -13,8 +13,7 @@ from mitmproxy.tools import cmdline # noqa from mitmproxy import exceptions # noqa from mitmproxy import options # noqa from mitmproxy import optmanager # noqa -from mitmproxy.proxy import config # noqa -from mitmproxy.proxy import server # noqa +from mitmproxy import proxy from mitmproxy.utils import version_check # noqa from mitmproxy.utils import debug # noqa @@ -49,15 +48,7 @@ def process_options(parser, opts, args): adict[n] = getattr(args, n) opts.merge(adict) - pconf = config.ProxyConfig(opts) - if opts.server: - try: - return server.ProxyServer(pconf) - except exceptions.ServerException as v: - print(str(v), file=sys.stderr) - sys.exit(1) - else: - return server.DummyServer(pconf) + return proxy.config.ProxyConfig(opts) def run(MasterKlass, args, extra=None): # pragma: no cover @@ -74,7 +65,16 @@ def run(MasterKlass, args, extra=None): # pragma: no cover master = None try: unknown = optmanager.load_paths(opts, args.conf) - server = process_options(parser, opts, args) + pconf = process_options(parser, opts, args) + if pconf.options.server: + try: + server = proxy.server.ProxyServer(pconf) + except exceptions.ServerException as v: + print(str(v), file=sys.stderr) + sys.exit(1) + else: + server = proxy.server.DummyServer(pconf) + master = MasterKlass(opts, server) master.addons.trigger("configure", opts.keys()) master.addons.trigger("tick") diff --git a/test/mitmproxy/test_proxy.py b/test/mitmproxy/test_proxy.py index e1d0da006..299abab3c 100644 --- a/test/mitmproxy/test_proxy.py +++ b/test/mitmproxy/test_proxy.py @@ -32,8 +32,7 @@ class TestProcessProxyOptions: opts = options.Options() cmdline.common_options(parser, opts) args = parser.parse_args(args=args) - main.process_options(parser, opts, args) - pconf = config.ProxyConfig(opts) + pconf = main.process_options(parser, opts, args) return parser, pconf def assert_noerr(self, *args):