From 39ba8a202f4010286b99c3ac34003ffa329153c5 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 10 Oct 2022 10:55:46 +0200 Subject: [proxy] load console_scripts entry instead of launching mitmproxy with os.subprocess --- src/hydrilla/mitmproxy_launcher/launch.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/hydrilla/mitmproxy_launcher/launch.py b/src/hydrilla/mitmproxy_launcher/launch.py index 8133413..e197ad5 100644 --- a/src/hydrilla/mitmproxy_launcher/launch.py +++ b/src/hydrilla/mitmproxy_launcher/launch.py @@ -37,12 +37,18 @@ import os import subprocess as sp from pathlib import Path +# The following import requires at least Python 3.8. There is no point adding +# a workaround for Python 3.7 because mitmproxy itself (which we're loading +# here) relies on Python 3.9+. This does not affect the Hydrilla server and +# builder which continue to work under Python 3.7. +from importlib.metadata import distribution import click from .. import _version from ..translations import smart_gettext as _ + addon_script_text = ''' from hydrilla.proxy.addon import HaketiloAddon @@ -70,12 +76,19 @@ def launch(port: int, directory: str): script_path.write_text(addon_script_text) - code = sp.call(['mitmdump', - '-p', str(port), - '--set', f'confdir={directory_path / "mitmproxy"}', - '--set', 'upstream_cert=false', - '--set', 'connection_strategy=lazy', - '--set', f'haketilo_dir={directory_path}', - '--scripts', str(script_path)]) + sys.argv = [ + 'mitmdump', + '-p', str(port), + '--set', f'confdir={directory_path / "mitmproxy"}', + '--set', 'upstream_cert=false', + '--set', 'connection_strategy=lazy', + '--set', f'haketilo_dir={directory_path}', + '--scripts', str(script_path) + ] + + for entry_point in distribution('mitmproxy').entry_points: + if entry_point.group == 'console_scripts' and \ + entry_point.name == 'mitmdump': + sys.exit(entry_point.load()()) - sys.exit(code) + sys.exit(1) -- cgit v1.2.3