diff --git a/CHANGELOG.md b/CHANGELOG.md index b3222190f..390609054 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,7 @@ * Exit early if there are errors on startup (#4544, @mhils) * Fixed encoding guessing: only search for meta tags in HTML bodies (##4566, @Prinzhorn) * Binaries are now built with Python 3.10 (@mhils) +* Add flatpak support to the browser addon (@pauloromeira) ## 28 September 2021: mitmproxy 7.0.4 diff --git a/mitmproxy/addons/browser.py b/mitmproxy/addons/browser.py index e3aa24d7a..3c5b5de93 100644 --- a/mitmproxy/addons/browser.py +++ b/mitmproxy/addons/browser.py @@ -23,6 +23,34 @@ def get_chrome_executable() -> typing.Optional[str]: ): if shutil.which(browser): return browser + + return None + + +def get_chrome_flatpak() -> typing.Optional[str]: + if shutil.which("flatpak"): + for browser in ( + "com.google.Chrome", + "org.chromium.Chromium", + "com.github.Eloston.UngoogledChromium", + "com.google.ChromeDev", + ): + if subprocess.run( + ["flatpak", "info", browser], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ).returncode == 0: + return browser + + return None + + +def get_browser_cmd() -> typing.Optional[typing.List[str]]: + if browser := get_chrome_executable(): + return [browser] + elif browser := get_chrome_flatpak(): + return ["flatpak", "run", "-p", browser] + return None @@ -39,7 +67,7 @@ class Browser: if len(self.browser) > 0: ctx.log.alert("Starting additional browser") - cmd = get_chrome_executable() + cmd = get_browser_cmd() if not cmd: ctx.log.alert("Your platform is not supported yet - please submit a patch.") return @@ -48,7 +76,7 @@ class Browser: self.tdir.append(tdir) self.browser.append(subprocess.Popen( [ - cmd, + *cmd, "--user-data-dir=%s" % str(tdir.name), "--proxy-server={}:{}".format( ctx.options.listen_host or "127.0.0.1", @@ -61,8 +89,8 @@ class Browser: "about:blank", ], - stdout = subprocess.DEVNULL, - stderr = subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, )) def done(self): @@ -71,4 +99,4 @@ class Browser: for tdir in self.tdir: tdir.cleanup() self.browser = [] - self.tdir = [] \ No newline at end of file + self.tdir = [] diff --git a/test/mitmproxy/addons/test_browser.py b/test/mitmproxy/addons/test_browser.py index b9858cd36..ca11fb3c4 100644 --- a/test/mitmproxy/addons/test_browser.py +++ b/test/mitmproxy/addons/test_browser.py @@ -27,3 +27,33 @@ async def test_no_browser(): with taddons.context() as tctx: b.start() await tctx.master.await_log("platform is not supported") + + +async def test_get_browser_cmd_executable(): + with mock.patch("shutil.which") as which: + which.side_effect = lambda cmd: cmd == "chrome" + assert browser.get_browser_cmd() == ["chrome"] + + +async def test_get_browser_cmd_no_executable(): + with mock.patch("shutil.which") as which: + which.return_value = False + assert browser.get_browser_cmd() is None + + +async def test_get_browser_cmd_flatpak(): + def subprocess_run_mock(cmd, **kwargs): + returncode = 0 if cmd == ["flatpak", "info", "com.google.Chrome"] else 1 + return mock.Mock(returncode=returncode) + + with mock.patch("shutil.which") as which, mock.patch("subprocess.run") as subprocess_run: + which.side_effect = lambda cmd: cmd == "flatpak" + subprocess_run.side_effect = subprocess_run_mock + assert browser.get_browser_cmd() == ["flatpak", "run", "-p", "com.google.Chrome"] + + +async def test_get_browser_cmd_no_flatpak(): + with mock.patch("shutil.which") as which, mock.patch("subprocess.run") as subprocess_run: + which.side_effect = lambda cmd: cmd == "flatpak" + subprocess_run.return_value = mock.Mock(returncode=1) + assert browser.get_browser_cmd() is None