1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import shutil
import subprocess
import tempfile
import typing
from mitmproxy import command
from mitmproxy import ctx
def get_chrome_executable() -> typing.Optional[str]:
for browser in (
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
# https://stackoverflow.com/questions/40674914/google-chrome-path-in-windows-10
r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
r"C:\Program Files (x86)\Google\Application\chrome.exe",
# Linux binary names from Python's webbrowser module.
"google-chrome",
"google-chrome-stable",
"chrome",
"chromium",
"chromium-browser",
"google-chrome-unstable",
):
if shutil.which(browser):
return browser
return None
class Browser:
browser = None
tdir = None
@command.command("browser.start")
def start(self) -> None:
"""
Start an isolated instance of Chrome that points to the currently
running proxy.
"""
if self.browser:
if self.browser.poll() is None:
ctx.log.alert("Browser already running")
return
else:
self.done()
cmd = get_chrome_executable()
if not cmd:
ctx.log.alert("Your platform is not supported yet - please submit a patch.")
return
self.tdir = tempfile.TemporaryDirectory()
self.browser = subprocess.Popen(
[
cmd,
"--user-data-dir=%s" % str(self.tdir.name),
"--proxy-server=%s:%s" % (
ctx.options.listen_host or "127.0.0.1",
ctx.options.listen_port
),
"--disable-fre",
"--no-default-browser-check",
"--no-first-run",
"--disable-extensions",
"about:blank",
],
stdout = subprocess.DEVNULL,
stderr = subprocess.DEVNULL,
)
def done(self):
if self.browser:
self.browser.kill()
self.tdir.cleanup()
self.browser = None
self.tdir = None
|