mozproxy 1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mozproxy/__init__.py +44 -0
- mozproxy/__main__.py +10 -0
- mozproxy/backends/__init__.py +3 -0
- mozproxy/backends/base.py +29 -0
- mozproxy/backends/mitm/__init__.py +6 -0
- mozproxy/backends/mitm/android.py +245 -0
- mozproxy/backends/mitm/desktop.py +161 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-11.0.0-linux64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-11.0.0-osx-arm64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-11.0.0-osx.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-11.0.0-win.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-12.2.1-linux64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-12.2.1-osx-arm64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-12.2.1-osx.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-12.2.1-win.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-4.0.4-linux64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-4.0.4-osx.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-4.0.4-win.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-5.0.1-linux64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-5.0.1-osx.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-5.0.1-win.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-5.1.1-linux64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-5.1.1-osx.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-5.1.1-win.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-6.0.2-linux64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-6.0.2-osx.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-6.0.2-win.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-7.0.4-linux64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-7.0.4-osx.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-7.0.4-win.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-8.1.1-linux64.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-8.1.1-osx.manifest +10 -0
- mozproxy/backends/mitm/manifests/mitmproxy-rel-bin-8.1.1-win.manifest +10 -0
- mozproxy/backends/mitm/mitm.py +507 -0
- mozproxy/backends/mitm/mitmproxy_requirements.txt +34 -0
- mozproxy/backends/mitm/scripts/__init__.py +4 -0
- mozproxy/backends/mitm/scripts/alt-serverplayback.py +262 -0
- mozproxy/backends/mitm/scripts/alternate-server-replay.py +310 -0
- mozproxy/backends/mitm/scripts/catapult/LICENSE +27 -0
- mozproxy/backends/mitm/scripts/catapult/deterministic.js +71 -0
- mozproxy/backends/mitm/scripts/http_protocol_extractor.py +83 -0
- mozproxy/backends/mitm/scripts/inject-deterministic.py +198 -0
- mozproxy/driver.py +168 -0
- mozproxy/recordings.py +163 -0
- mozproxy/server.py +26 -0
- mozproxy/utils.py +257 -0
- mozproxy-1.0.dist-info/METADATA +33 -0
- mozproxy-1.0.dist-info/RECORD +51 -0
- mozproxy-1.0.dist-info/WHEEL +5 -0
- mozproxy-1.0.dist-info/entry_points.txt +2 -0
- mozproxy-1.0.dist-info/top_level.txt +1 -0
mozproxy/__init__.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def path_join(*args):
|
|
10
|
+
path = os.path.join(*args)
|
|
11
|
+
return os.path.abspath(path)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
mozproxy_src_dir = os.path.dirname(os.path.realpath(__file__))
|
|
15
|
+
mozproxy_dir = path_join(mozproxy_src_dir, "..")
|
|
16
|
+
mozbase_dir = path_join(mozproxy_dir, "..")
|
|
17
|
+
|
|
18
|
+
# needed so unit tests can find their imports
|
|
19
|
+
if os.environ.get("SCRIPTSPATH", None) is not None:
|
|
20
|
+
# in production it is env SCRIPTS_PATH
|
|
21
|
+
mozharness_dir = os.environ["SCRIPTSPATH"]
|
|
22
|
+
else:
|
|
23
|
+
# locally it's in source tree
|
|
24
|
+
mozharness_dir = path_join(mozbase_dir, "..", "mozharness")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_playback(config):
|
|
28
|
+
"""Returns an instance of the right Playback class"""
|
|
29
|
+
sys.path.insert(0, mozharness_dir)
|
|
30
|
+
sys.path.insert(0, mozproxy_dir)
|
|
31
|
+
sys.path.insert(0, mozproxy_src_dir)
|
|
32
|
+
|
|
33
|
+
from .server import get_backend
|
|
34
|
+
from .utils import LOG
|
|
35
|
+
|
|
36
|
+
tool_name = config.get("playback_tool", None)
|
|
37
|
+
if tool_name is None:
|
|
38
|
+
LOG.critical("playback_tool name not found in config")
|
|
39
|
+
return None
|
|
40
|
+
try:
|
|
41
|
+
return get_backend(tool_name, config)
|
|
42
|
+
except KeyError:
|
|
43
|
+
LOG.critical("specified playback tool is unsupported: %s" % tool_name)
|
|
44
|
+
return None
|
mozproxy/__main__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from mozproxy.driver import main
|
|
8
|
+
|
|
9
|
+
if __name__ == "__main__":
|
|
10
|
+
sys.exit(main())
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
4
|
+
|
|
5
|
+
from abc import ABCMeta, abstractmethod
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# abstract class for all playback tools
|
|
9
|
+
class Playback(metaclass=ABCMeta):
|
|
10
|
+
def __init__(self, config):
|
|
11
|
+
self.config = config
|
|
12
|
+
self.host = None
|
|
13
|
+
self.port = None
|
|
14
|
+
|
|
15
|
+
@abstractmethod
|
|
16
|
+
def download(self):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
@abstractmethod
|
|
20
|
+
def setup(self):
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def start(self):
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
@abstractmethod
|
|
28
|
+
def stop(self):
|
|
29
|
+
pass
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"""Functions to download, install, setup, and use the mitmproxy playback tool"""
|
|
2
|
+
|
|
3
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
4
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
5
|
+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
6
|
+
import glob
|
|
7
|
+
import os
|
|
8
|
+
import shutil
|
|
9
|
+
import subprocess
|
|
10
|
+
import sys
|
|
11
|
+
import tempfile
|
|
12
|
+
from subprocess import PIPE
|
|
13
|
+
|
|
14
|
+
import mozinfo
|
|
15
|
+
|
|
16
|
+
from mozproxy.backends.mitm.mitm import Mitmproxy
|
|
17
|
+
from mozproxy.utils import LOG, download_file_from_url, tooltool_download
|
|
18
|
+
|
|
19
|
+
# path for mitmproxy certificate, generated auto after mitmdump is started
|
|
20
|
+
# on local machine it is 'HOME', however it is different on production machines
|
|
21
|
+
try:
|
|
22
|
+
DEFAULT_CERT_PATH = os.path.join(
|
|
23
|
+
os.getenv("HOME"), ".mitmproxy", "mitmproxy-ca-cert.cer"
|
|
24
|
+
)
|
|
25
|
+
except Exception:
|
|
26
|
+
DEFAULT_CERT_PATH = os.path.join(
|
|
27
|
+
os.getenv("HOMEDRIVE"),
|
|
28
|
+
os.getenv("HOMEPATH"),
|
|
29
|
+
".mitmproxy",
|
|
30
|
+
"mitmproxy-ca-cert.cer",
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# On Windows, deal with mozilla-build having forward slashes in $HOME:
|
|
34
|
+
if os.name == "nt" and "/" in DEFAULT_CERT_PATH:
|
|
35
|
+
DEFAULT_CERT_PATH = DEFAULT_CERT_PATH.replace("/", "\\")
|
|
36
|
+
|
|
37
|
+
FIREFOX_ANDROID_APPS = [
|
|
38
|
+
"geckoview",
|
|
39
|
+
"refbrow",
|
|
40
|
+
"fenix",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class MitmproxyAndroid(Mitmproxy):
|
|
45
|
+
def setup(self):
|
|
46
|
+
self.download_and_install_host_utils()
|
|
47
|
+
self.install_mitmproxy_cert()
|
|
48
|
+
|
|
49
|
+
def download_and_install_host_utils(self):
|
|
50
|
+
"""
|
|
51
|
+
If running locally:
|
|
52
|
+
1. Will use the `certutil` tool from the local Firefox desktop build
|
|
53
|
+
|
|
54
|
+
If running in production:
|
|
55
|
+
1. Get the tooltools manifest file for downloading hostutils (contains certutil)
|
|
56
|
+
2. Get the `certutil` tool by downloading hostutils using the tooltool manifest
|
|
57
|
+
"""
|
|
58
|
+
if self.config["run_local"]:
|
|
59
|
+
# when running locally, it is found in the Firefox desktop build (..obj../dist/bin)
|
|
60
|
+
self.certutil_path = os.path.join(os.environ["MOZ_HOST_BIN"], "certutil")
|
|
61
|
+
if not (
|
|
62
|
+
os.path.isfile(self.certutil_path)
|
|
63
|
+
and os.access(self.certutil_path, os.X_OK)
|
|
64
|
+
):
|
|
65
|
+
raise Exception(
|
|
66
|
+
f"Abort: unable to execute certutil: {self.certutil_path}"
|
|
67
|
+
)
|
|
68
|
+
self.certutil_path = os.environ["MOZ_HOST_BIN"]
|
|
69
|
+
os.environ["LD_LIBRARY_PATH"] = self.certutil_path
|
|
70
|
+
else:
|
|
71
|
+
# must download certutil inside hostutils via tooltool; use this manifest:
|
|
72
|
+
# mozilla-central/testing/config/tooltool-manifests/linux64/hostutils.manifest
|
|
73
|
+
# after it will be found here inside the worker/bitbar container:
|
|
74
|
+
# /builds/worker/workspace/build/hostutils/host-utils-66.0a1.en-US.linux-x86_64
|
|
75
|
+
LOG.info("downloading certutil binary (hostutils)")
|
|
76
|
+
|
|
77
|
+
# get path to the hostutils tooltool manifest; was set earlier in
|
|
78
|
+
# mozharness/configs/raptor/android_hw_config.py, to the path i.e.
|
|
79
|
+
# mozilla-central/testing/config/tooltool-manifests/linux64/hostutils.manifest
|
|
80
|
+
# the bitbar container is always linux64
|
|
81
|
+
if os.environ.get("GECKO_HEAD_REPOSITORY", None) is None:
|
|
82
|
+
raise Exception("Abort: unable to get GECKO_HEAD_REPOSITORY")
|
|
83
|
+
|
|
84
|
+
if os.environ.get("GECKO_HEAD_REV", None) is None:
|
|
85
|
+
raise Exception("Abort: unable to get GECKO_HEAD_REV")
|
|
86
|
+
|
|
87
|
+
if os.environ.get("HOSTUTILS_MANIFEST_PATH", None) is not None:
|
|
88
|
+
manifest_url = os.path.join(
|
|
89
|
+
os.environ["GECKO_HEAD_REPOSITORY"],
|
|
90
|
+
"raw-file",
|
|
91
|
+
os.environ["GECKO_HEAD_REV"],
|
|
92
|
+
os.environ["HOSTUTILS_MANIFEST_PATH"],
|
|
93
|
+
)
|
|
94
|
+
else:
|
|
95
|
+
raise Exception("Abort: unable to get HOSTUTILS_MANIFEST_PATH!")
|
|
96
|
+
|
|
97
|
+
# first need to download the hostutils tooltool manifest file itself
|
|
98
|
+
_dest = os.path.join(self.mozproxy_dir, "hostutils.manifest")
|
|
99
|
+
have_manifest = download_file_from_url(manifest_url, _dest)
|
|
100
|
+
if not have_manifest:
|
|
101
|
+
raise Exception("failed to download the hostutils tooltool manifest")
|
|
102
|
+
|
|
103
|
+
# now use the manifest to download hostutils so we can get certutil
|
|
104
|
+
tooltool_download(_dest, self.config["run_local"], self.mozproxy_dir)
|
|
105
|
+
|
|
106
|
+
# the production bitbar container host is always linux
|
|
107
|
+
self.certutil_path = glob.glob(
|
|
108
|
+
os.path.join(self.mozproxy_dir, "host-utils*[!z|checksum]")
|
|
109
|
+
)[0]
|
|
110
|
+
|
|
111
|
+
# must add hostutils/certutil to the path
|
|
112
|
+
os.environ["LD_LIBRARY_PATH"] = self.certutil_path
|
|
113
|
+
|
|
114
|
+
bin_suffix = mozinfo.info.get("bin_suffix", "")
|
|
115
|
+
self.certutil_path = os.path.join(self.certutil_path, "certutil" + bin_suffix)
|
|
116
|
+
if os.path.isfile(self.certutil_path):
|
|
117
|
+
LOG.info("certutil is found at: %s" % self.certutil_path)
|
|
118
|
+
else:
|
|
119
|
+
raise Exception("unable to find certutil at %s" % self.certutil_path)
|
|
120
|
+
|
|
121
|
+
def install_mitmproxy_cert(self):
|
|
122
|
+
"""Install the CA certificate generated by mitmproxy, into geckoview android
|
|
123
|
+
|
|
124
|
+
1. Create an NSS certificate database in the geckoview browser profile dir, only
|
|
125
|
+
if it doesn't already exist. Use this certutil command:
|
|
126
|
+
`certutil -N -d sql:<path to profile> --empty-password`
|
|
127
|
+
2. Import the mitmproxy certificate into the database, i.e.:
|
|
128
|
+
`certutil -A -d sql:<path to profile> -n "some nickname" -t TC,, -a -i <path to CA.pem>`
|
|
129
|
+
"""
|
|
130
|
+
# If the app isn't in FIREFOX_ANDROID_APPS then we have to create the tempdir
|
|
131
|
+
# because we don't have it available in the local_profile_dir
|
|
132
|
+
if self.config["app"] in FIREFOX_ANDROID_APPS:
|
|
133
|
+
tempdir = self.config["local_profile_dir"]
|
|
134
|
+
else:
|
|
135
|
+
tempdir = tempfile.mkdtemp()
|
|
136
|
+
cert_db_location = "sql:%s/" % tempdir
|
|
137
|
+
|
|
138
|
+
if not self.cert_db_exists(cert_db_location):
|
|
139
|
+
self.create_cert_db(cert_db_location)
|
|
140
|
+
|
|
141
|
+
# DEFAULT_CERT_PATH has local path and name of mitmproxy cert i.e.
|
|
142
|
+
# /home/cltbld/.mitmproxy/mitmproxy-ca-cert.cer
|
|
143
|
+
self.import_certificate_in_cert_db(cert_db_location, DEFAULT_CERT_PATH)
|
|
144
|
+
|
|
145
|
+
# cannot continue if failed to add CA cert to Firefox, need to check
|
|
146
|
+
if not self.is_mitmproxy_cert_installed(cert_db_location):
|
|
147
|
+
LOG.error("Aborting: failed to install mitmproxy CA cert into Firefox")
|
|
148
|
+
self.stop_mitmproxy_playback()
|
|
149
|
+
sys.exit()
|
|
150
|
+
if self.config["app"] not in FIREFOX_ANDROID_APPS:
|
|
151
|
+
try:
|
|
152
|
+
shutil.rmtree(tempdir)
|
|
153
|
+
except Exception:
|
|
154
|
+
LOG.warning("unable to remove directory: %s" % tempdir)
|
|
155
|
+
|
|
156
|
+
def import_certificate_in_cert_db(self, cert_db_location, local_cert_path):
|
|
157
|
+
# import mitmproxy cert into the db
|
|
158
|
+
args = [
|
|
159
|
+
"-A",
|
|
160
|
+
"-d",
|
|
161
|
+
cert_db_location,
|
|
162
|
+
"-n",
|
|
163
|
+
"mitmproxy-cert",
|
|
164
|
+
"-t",
|
|
165
|
+
"TC,,",
|
|
166
|
+
"-a",
|
|
167
|
+
"-i",
|
|
168
|
+
local_cert_path,
|
|
169
|
+
]
|
|
170
|
+
LOG.info("importing mitmproxy cert into db using command")
|
|
171
|
+
self.certutil(args)
|
|
172
|
+
|
|
173
|
+
def create_cert_db(self, cert_db_location):
|
|
174
|
+
# create cert db if it doesn't already exist; it may exist already
|
|
175
|
+
# if a previous pageload test ran in the same test suite
|
|
176
|
+
args = ["-d", cert_db_location, "-N", "--empty-password"]
|
|
177
|
+
|
|
178
|
+
LOG.info("creating nss cert database")
|
|
179
|
+
|
|
180
|
+
self.certutil(args)
|
|
181
|
+
|
|
182
|
+
if not self.cert_db_exists(cert_db_location):
|
|
183
|
+
raise Exception("nss cert db creation command failed. Cert db not created.")
|
|
184
|
+
|
|
185
|
+
def cert_db_exists(self, cert_db_location):
|
|
186
|
+
# check if the nss ca cert db already exists in the device profile
|
|
187
|
+
LOG.info(
|
|
188
|
+
"checking if the nss cert db already exists in the android browser profile"
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
args = ["-d", cert_db_location, "-L"]
|
|
192
|
+
cert_db_exists = self.certutil(args, raise_exception=False)
|
|
193
|
+
|
|
194
|
+
if cert_db_exists:
|
|
195
|
+
LOG.info("the nss cert db exists")
|
|
196
|
+
return True
|
|
197
|
+
else:
|
|
198
|
+
LOG.info("nss cert db doesn't exist yet.")
|
|
199
|
+
return False
|
|
200
|
+
|
|
201
|
+
def is_mitmproxy_cert_installed(self, cert_db_location):
|
|
202
|
+
"""Verify mitmxproy CA cert was added to Firefox on android"""
|
|
203
|
+
LOG.info("verifying that the mitmproxy ca cert is installed on android")
|
|
204
|
+
|
|
205
|
+
# list the certifcates that are in the nss cert db (inside the browser profile dir)
|
|
206
|
+
LOG.info(
|
|
207
|
+
"getting the list of certs in the nss cert db in the android browser profile"
|
|
208
|
+
)
|
|
209
|
+
args = ["-d", cert_db_location, "-L"]
|
|
210
|
+
|
|
211
|
+
cmd_output = self.certutil(args)
|
|
212
|
+
|
|
213
|
+
if "mitmproxy-cert" in cmd_output.decode("utf-8"):
|
|
214
|
+
LOG.info(
|
|
215
|
+
"verfied the mitmproxy-cert is installed in the nss cert db on android"
|
|
216
|
+
)
|
|
217
|
+
return True
|
|
218
|
+
return False
|
|
219
|
+
|
|
220
|
+
def certutil(self, args, raise_exception=True):
|
|
221
|
+
cmd = [self.certutil_path] + list(args)
|
|
222
|
+
LOG.info("Certutil: Running command: %s" % " ".join(cmd))
|
|
223
|
+
try:
|
|
224
|
+
cmd_proc = subprocess.Popen(
|
|
225
|
+
cmd, stdout=PIPE, stderr=PIPE, env=os.environ.copy()
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
cmd_output, errs = cmd_proc.communicate()
|
|
229
|
+
except subprocess.SubprocessError:
|
|
230
|
+
LOG.critical("could not run the certutil command")
|
|
231
|
+
raise
|
|
232
|
+
|
|
233
|
+
if cmd_proc.returncode == 0:
|
|
234
|
+
# Debug purpose only remove if stable
|
|
235
|
+
LOG.info("Certutil returncode: %s" % cmd_proc.returncode)
|
|
236
|
+
LOG.info("Certutil output: %s" % cmd_output)
|
|
237
|
+
return cmd_output
|
|
238
|
+
elif raise_exception:
|
|
239
|
+
LOG.critical("Certutil command failed!!")
|
|
240
|
+
LOG.info("Certutil returncode: %s" % cmd_proc.returncode)
|
|
241
|
+
LOG.info("Certutil output: %s" % cmd_output)
|
|
242
|
+
LOG.info("Certutil error: %s" % errs)
|
|
243
|
+
raise Exception("Certutil command failed!!")
|
|
244
|
+
else:
|
|
245
|
+
return False
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"""Functions to download, install, setup, and use the mitmproxy playback tool"""
|
|
2
|
+
|
|
3
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
4
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
5
|
+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
|
|
9
|
+
import mozinfo
|
|
10
|
+
|
|
11
|
+
from mozproxy.backends.mitm.mitm import Mitmproxy
|
|
12
|
+
from mozproxy.utils import LOG
|
|
13
|
+
|
|
14
|
+
# to install mitmproxy certificate into Firefox and turn on/off proxy
|
|
15
|
+
POLICIES_CONTENT_ON = """{
|
|
16
|
+
"policies": {
|
|
17
|
+
"Certificates": {
|
|
18
|
+
"Install": ["%(cert)s"]
|
|
19
|
+
},
|
|
20
|
+
"Proxy": {
|
|
21
|
+
"Mode": "manual",
|
|
22
|
+
"HTTPProxy": "%(host)s:%(port)d",
|
|
23
|
+
"SSLProxy": "%(host)s:%(port)d",
|
|
24
|
+
"Passthrough": "%(host)s",
|
|
25
|
+
"Locked": true
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}"""
|
|
29
|
+
|
|
30
|
+
POLICIES_CONTENT_OFF = """{
|
|
31
|
+
"policies": {
|
|
32
|
+
"Proxy": {
|
|
33
|
+
"Mode": "none",
|
|
34
|
+
"Locked": false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}"""
|
|
38
|
+
|
|
39
|
+
# path for mitmproxy certificate, generated auto after mitmdump is started
|
|
40
|
+
# on local machine it is 'HOME', however it is different on production machines
|
|
41
|
+
try:
|
|
42
|
+
DEFAULT_CERT_PATH = os.path.join(
|
|
43
|
+
os.getenv("HOME"), ".mitmproxy", "mitmproxy-ca-cert.cer"
|
|
44
|
+
)
|
|
45
|
+
except Exception:
|
|
46
|
+
DEFAULT_CERT_PATH = os.path.join(
|
|
47
|
+
os.getenv("HOMEDRIVE"),
|
|
48
|
+
os.getenv("HOMEPATH"),
|
|
49
|
+
".mitmproxy",
|
|
50
|
+
"mitmproxy-ca-cert.cer",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# On Windows, deal with mozilla-build having forward slashes in $HOME:
|
|
54
|
+
if os.name == "nt" and "/" in DEFAULT_CERT_PATH:
|
|
55
|
+
DEFAULT_CERT_PATH = DEFAULT_CERT_PATH.replace("/", "\\")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class MitmproxyDesktop(Mitmproxy):
|
|
59
|
+
def setup(self):
|
|
60
|
+
"""
|
|
61
|
+
Installs certificates.
|
|
62
|
+
|
|
63
|
+
For Firefox we need to install the generated mitmproxy CA cert. For
|
|
64
|
+
Chromium this is not necessary as it will be started with the
|
|
65
|
+
--ignore-certificate-errors cmd line arg.
|
|
66
|
+
"""
|
|
67
|
+
if not self.config["app"] == "firefox":
|
|
68
|
+
return
|
|
69
|
+
# install the generated CA certificate into Firefox desktop
|
|
70
|
+
self.install_mitmproxy_cert(self.browser_path)
|
|
71
|
+
|
|
72
|
+
def install_mitmproxy_cert(self, browser_path):
|
|
73
|
+
"""Install the CA certificate generated by mitmproxy, into Firefox
|
|
74
|
+
1. Create a dir called 'distribution' in the same directory as the Firefox executable
|
|
75
|
+
2. Create the policies.json file inside that folder; which points to the certificate
|
|
76
|
+
location, and turns on the the browser proxy settings
|
|
77
|
+
"""
|
|
78
|
+
LOG.info("Installing mitmproxy CA certificate into Firefox")
|
|
79
|
+
|
|
80
|
+
# browser_path is the exe, we want the folder
|
|
81
|
+
self.policies_dir = os.path.dirname(browser_path)
|
|
82
|
+
# on macosx we need to remove the last folders 'MacOS'
|
|
83
|
+
# and the policies json needs to go in ../Content/Resources/
|
|
84
|
+
if "mac" in self.config["platform"]:
|
|
85
|
+
self.policies_dir = os.path.join(self.policies_dir[:-6], "Resources")
|
|
86
|
+
# for all platforms the policies json goes in a 'distribution' dir
|
|
87
|
+
self.policies_dir = os.path.join(self.policies_dir, "distribution")
|
|
88
|
+
|
|
89
|
+
self.cert_path = DEFAULT_CERT_PATH
|
|
90
|
+
# for windows only
|
|
91
|
+
if mozinfo.os == "win":
|
|
92
|
+
self.cert_path = self.cert_path.replace("\\", "\\\\")
|
|
93
|
+
|
|
94
|
+
if not os.path.exists(self.policies_dir):
|
|
95
|
+
LOG.info("creating folder: %s" % self.policies_dir)
|
|
96
|
+
os.makedirs(self.policies_dir)
|
|
97
|
+
else:
|
|
98
|
+
LOG.info("folder already exists: %s" % self.policies_dir)
|
|
99
|
+
|
|
100
|
+
self.write_policies_json(
|
|
101
|
+
self.policies_dir,
|
|
102
|
+
policies_content=POLICIES_CONTENT_ON
|
|
103
|
+
% {"cert": self.cert_path, "host": self.host, "port": self.port},
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# cannot continue if failed to add CA cert to Firefox, need to check
|
|
107
|
+
if not self.is_mitmproxy_cert_installed():
|
|
108
|
+
LOG.error(
|
|
109
|
+
"Aborting: failed to install mitmproxy CA cert into Firefox desktop"
|
|
110
|
+
)
|
|
111
|
+
self.stop_mitmproxy_playback()
|
|
112
|
+
sys.exit()
|
|
113
|
+
|
|
114
|
+
def write_policies_json(self, location, policies_content):
|
|
115
|
+
policies_file = os.path.join(location, "policies.json")
|
|
116
|
+
LOG.info("writing: %s" % policies_file)
|
|
117
|
+
|
|
118
|
+
with open(policies_file, "w") as fd:
|
|
119
|
+
fd.write(policies_content)
|
|
120
|
+
|
|
121
|
+
def read_policies_json(self, location):
|
|
122
|
+
policies_file = os.path.join(location, "policies.json")
|
|
123
|
+
LOG.info("reading: %s" % policies_file)
|
|
124
|
+
|
|
125
|
+
with open(policies_file) as fd:
|
|
126
|
+
return fd.read()
|
|
127
|
+
|
|
128
|
+
def is_mitmproxy_cert_installed(self):
|
|
129
|
+
"""Verify mitmxproy CA cert was added to Firefox"""
|
|
130
|
+
try:
|
|
131
|
+
# read autoconfig file, confirm mitmproxy cert is in there
|
|
132
|
+
contents = self.read_policies_json(self.policies_dir)
|
|
133
|
+
LOG.info("Firefox policies file contents:")
|
|
134
|
+
LOG.info(contents)
|
|
135
|
+
if (
|
|
136
|
+
POLICIES_CONTENT_ON
|
|
137
|
+
% {"cert": self.cert_path, "host": self.host, "port": self.port}
|
|
138
|
+
) in contents:
|
|
139
|
+
LOG.info("Verified mitmproxy CA certificate is installed in Firefox")
|
|
140
|
+
else:
|
|
141
|
+
return False
|
|
142
|
+
except Exception as e:
|
|
143
|
+
LOG.info("failed to read Firefox policies file, exeption: %s" % e)
|
|
144
|
+
return False
|
|
145
|
+
return True
|
|
146
|
+
|
|
147
|
+
def stop(self):
|
|
148
|
+
LOG.info("MitmproxyDesktop stop!!")
|
|
149
|
+
super().stop()
|
|
150
|
+
self.turn_off_browser_proxy()
|
|
151
|
+
|
|
152
|
+
def turn_off_browser_proxy(self):
|
|
153
|
+
"""Turn off the browser proxy that was used for mitmproxy playback. In Firefox
|
|
154
|
+
we need to change the autoconfig files to revert the proxy; for Chromium the proxy
|
|
155
|
+
was setup on the cmd line, so nothing is required here."""
|
|
156
|
+
if self.config["app"] == "firefox" and self.policies_dir is not None:
|
|
157
|
+
LOG.info("Turning off the browser proxy")
|
|
158
|
+
|
|
159
|
+
self.write_policies_json(
|
|
160
|
+
self.policies_dir, policies_content=POLICIES_CONTENT_OFF
|
|
161
|
+
)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"filename": "mitmproxy-11.0.0-linux-x86_64.tar.gz",
|
|
4
|
+
"size": 150230619,
|
|
5
|
+
"algorithm": "sha512",
|
|
6
|
+
"digest": "a9d14723d943e97cd59a9fcb33554d734ca9e90ff26498e786a158328badd5cb53e4f424296b574aea8f3b16809ab9a232d1eeb432a7ea1c4b1594670a99172b",
|
|
7
|
+
"unpack": true,
|
|
8
|
+
"visibility": "public"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"filename": "mitmproxy-11.0.0-macos-arm64.tar.gz",
|
|
4
|
+
"size": 51462941,
|
|
5
|
+
"algorithm": "sha512",
|
|
6
|
+
"digest": "db5556ec9204a550435ef3dfdbe5ce237a2d398f7abe5d43d471eb929135c5aff1d8518ec55ebbcaa97ff03659fefe392b57d466a65114ab1f8fa2bdb1c1e637",
|
|
7
|
+
"unpack": true,
|
|
8
|
+
"visibility": "public"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"filename": "mitmproxy-11.0.0-macos-x86_64.tar.gz",
|
|
4
|
+
"size": 52375352,
|
|
5
|
+
"algorithm": "sha512",
|
|
6
|
+
"digest": "924b2418fffe0cbe99412eb283241986441e19c75eafa8e185fb3288e5cf952f51ba09afc7afc5b4af83986a34def50575c5f1832fe193499e7719b5eeeec706",
|
|
7
|
+
"unpack": true,
|
|
8
|
+
"visibility": "public"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"filename": "mitmproxy-11.0.0-windows-x86_64.zip",
|
|
4
|
+
"size": 77930913,
|
|
5
|
+
"algorithm": "sha512",
|
|
6
|
+
"digest": "d2bf43bade8f20c9d9df6cb50bced4d4d0b17a7f6d854d1244b777a808945785d4ec7799185082a34962eceb4a714c74058086065ee833165bd4c54ea1ac5c92",
|
|
7
|
+
"unpack": true,
|
|
8
|
+
"visibility": "public"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"filename": "mitmproxy-12.2.1-linux-x86_64.tar.gz",
|
|
4
|
+
"size": 119971878,
|
|
5
|
+
"algorithm": "sha512",
|
|
6
|
+
"digest": "c892e066b8525caac9d174c248683fe783528c10f1225a7e962fc1e59a228bc7ffad85cd9eadb7e62cf1dca99d55a243113eae6a8a783f5e33fda724a8601202",
|
|
7
|
+
"unpack": true,
|
|
8
|
+
"visibility": "public"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"filename": "mitmproxy-12.2.1-macos-arm64.tar.gz",
|
|
4
|
+
"size": 54643269,
|
|
5
|
+
"algorithm": "sha512",
|
|
6
|
+
"digest": "24d6a86b066f762e861c1800e9550be021be0f92c76e63a5d11db263792eddfb5dab2debbd4a0c71b0716c97a5fbdbe1a178d50086da93f52bf31f3041a5605f",
|
|
7
|
+
"unpack": true,
|
|
8
|
+
"visibility": "public"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"filename": "mitmproxy-12.2.1-macos-x86_64.tar.gz",
|
|
4
|
+
"size": 55117915,
|
|
5
|
+
"algorithm": "sha512",
|
|
6
|
+
"digest": "6b484f61fea3e5d8dfc03bc0f5543d4f0d16b338707587ad08be9abb056e7b2568c16bb3e223d5ba15ff8bbeabbc735006af48961c93b978eee5645af66e6a57",
|
|
7
|
+
"unpack": true,
|
|
8
|
+
"visibility": "public"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"filename": "mitmproxy-12.2.1-windows-x86_64.zip",
|
|
4
|
+
"size": 86373069,
|
|
5
|
+
"algorithm": "sha512",
|
|
6
|
+
"digest": "40ea287d0816fd2d0644ad10687e46e25ce2def36d203a6a6bb4daa4b95170a902616e7cf57c8fb9e389ebd78f4044ddcb4656e4dfe66dead3e85e280eaf6064",
|
|
7
|
+
"unpack": true,
|
|
8
|
+
"visibility": "public"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"size": 21805767,
|
|
4
|
+
"visibility": "public",
|
|
5
|
+
"digest": "69b314f9189b1c09e5412680869c03d07510b98b34e309f0f1b6961e5d8963f935994fe2f5ca86827f08631cbb271292a72aba2f0973b144832c2e7049a464a8",
|
|
6
|
+
"algorithm": "sha512",
|
|
7
|
+
"filename": "mitmproxy-4.0.4-linux.tar.gz",
|
|
8
|
+
"unpack": true
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"size": 36939029,
|
|
4
|
+
"visibility": "public",
|
|
5
|
+
"digest": "3f933b142d7afb7cd409f436bc1151cc44c6f207368644ebe9fc9015607448b2c2d52dad4e5c71595bb6aa8accf4869e534f8db0ff6144725fad8f98daf50b40",
|
|
6
|
+
"algorithm": "sha512",
|
|
7
|
+
"filename": "mitmproxy-4.0.4-osx.tar.gz",
|
|
8
|
+
"unpack": true
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"size": 35903223,
|
|
4
|
+
"visibility": "public",
|
|
5
|
+
"digest": "cf5a2bd056ae655d4db0a953ec1bf80229990f449672ae9b636568540fae192a1c361c7742f1e2a8b560426a11e69955358b6a37445f37117dfcac154ef84713",
|
|
6
|
+
"algorithm": "sha512",
|
|
7
|
+
"filename": "mitmproxy-4.0.4-windows.zip",
|
|
8
|
+
"unpack": true
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"size": 76345093,
|
|
4
|
+
"visibility": "public",
|
|
5
|
+
"digest": "4d653c0c74a8677e8e78cd72d109b1b54c75ef57b2e2ce980c5cfd602966c310065cf0e95c35f4fbfb1fe817f062ac6cf9cc129d72d42184b0237fb9b0bde081",
|
|
6
|
+
"algorithm": "sha512",
|
|
7
|
+
"filename": "mitmproxy-5.0.1-linux.tar.gz",
|
|
8
|
+
"unpack": true
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"size": 41341221,
|
|
4
|
+
"visibility": "public",
|
|
5
|
+
"digest": "4624bc26638cd7f3ab8c8d2ee8ab44ab81018114714a2b82614883adbfad540de73a97455fb228a3003006b4d03a8a9a597d70f8b27ccf1718f78380f911bdd8",
|
|
6
|
+
"algorithm": "sha512",
|
|
7
|
+
"filename": "mitmproxy-5.0.1-osx.tar.gz",
|
|
8
|
+
"unpack": true
|
|
9
|
+
}
|
|
10
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"size": 37875625,
|
|
4
|
+
"visibility": "public",
|
|
5
|
+
"digest": "d66234c9ca692d03412dd194b3f47c098e8ee1b17b178034fc86e0d8ada4d4f6cd1fbcfb62b7e7016539a878b1274ef83451a0acaca7011efaacb291fa52918d",
|
|
6
|
+
"algorithm": "sha512",
|
|
7
|
+
"filename": "mitmproxy-5.0.1-windows.zip",
|
|
8
|
+
"unpack": true
|
|
9
|
+
}
|
|
10
|
+
]
|