Add flatpak support to the browser addon (#5200)

This commit is contained in:
Paulo Romeira 2022-03-23 09:56:20 -03:00 committed by GitHub
parent 01d67951ee
commit e534086053
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 5 deletions

View File

@ -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

View File

@ -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 = []
self.tdir = []

View File

@ -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