aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Shao <me@matshao.com>2017-08-10 14:39:19 +0800
committerMatthew Shao <me@matshao.com>2017-08-10 14:39:19 +0800
commitbf0834051da57da604db2c4068665243c9871bc7 (patch)
treef7a6e3698489fa1ddb807af24903f480aaa9315f
parent3976ea01582717b8bf0e12cfe1160e7e1d8f0121 (diff)
downloadmitmproxy-bf0834051da57da604db2c4068665243c9871bc7.tar.gz
mitmproxy-bf0834051da57da604db2c4068665243c9871bc7.tar.bz2
mitmproxy-bf0834051da57da604db2c4068665243c9871bc7.zip
[web] Attempt of static viewer addon.
-rw-r--r--mitmproxy/tools/web/static_viewer.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/mitmproxy/tools/web/static_viewer.py b/mitmproxy/tools/web/static_viewer.py
new file mode 100644
index 00000000..ddee6c23
--- /dev/null
+++ b/mitmproxy/tools/web/static_viewer.py
@@ -0,0 +1,52 @@
+import os.path
+import shutil
+
+from mitmproxy import ctx
+from mitmproxy import flow
+
+
+class StaticViewer:
+ def __init__(self):
+ self.active_flows = set() # type: Set[flow.Flow]
+
+ def save(self, path: str) -> None:
+ """
+ Save the files for the static web view.
+ """
+ static_path = os.path.join(os.path.dirname(__file__), 'static')
+ index_path = os.path.join(os.path.dirname(__file__), 'templates', 'index.html')
+ path = os.path.expanduser(path)
+ # We want to overwrite the static files to keep track of the update.
+ try:
+ shutil.copytree(static_path, os.path.join(path, 'static'),
+ ignore=shutil.ignore_patterns('static.js'))
+ except FileExistsError:
+ shutil.rmtree(os.path.join(path, 'static'))
+ shutil.copytree(static_path, os.path.join(path, 'static'),
+ ignore=shutil.ignore_patterns('static.js'))
+
+ index_template = open(index_path, 'r')
+ index = open(os.path.join(path, 'index.html'), 'w')
+ # Change the resource files to relative path.
+ index.write(index_template.read().replace('/static/', './static/'))
+ index_template.close()
+ index.close()
+
+ static_template = open(os.path.join(static_path, 'static.js'), 'r')
+ static = open(os.path.join(path, 'static', 'static.js'), 'w')
+ # Turn on MITMWEB_STATIC variable
+ static.write(static_template.read().replace('false', 'true'))
+ static_template.close()
+ static.close()
+
+ def load(self, loader):
+ loader.add_option(
+ "web_static_viewer", str, "",
+ "The path to output a static viewer."
+ )
+
+ def configure(self, updated):
+ if "web_static_viewer" in updated and ctx.options.web_static_viewer:
+ self.save(ctx.options.web_static_viewer)
+
+