From 8a8437470e224ee53acbd9a353c7d01a83168cd4 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 27 Dec 2014 23:06:51 +1300 Subject: [PATCH] Move onboarding app to Tornado Two reasons for this. First, this removes flask and its dependencies, which are quite sizeable. Second, pyinstaller now barfs on simplejson, which is a Flask dependency. I just don't have time to fix this upstream, so doing what we should be doing anyway is a no-brainer. --- libmproxy/onboarding/app.py | 83 +++++++++++++++++----- libmproxy/onboarding/templates/frame.html | 4 +- libmproxy/onboarding/templates/index.html | 2 +- libmproxy/onboarding/templates/layout.html | 2 +- setup.py | 1 - test/tservers.py | 4 +- 6 files changed, 73 insertions(+), 23 deletions(-) diff --git a/libmproxy/onboarding/app.py b/libmproxy/onboarding/app.py index 4023fae2e..f0aecc157 100644 --- a/libmproxy/onboarding/app.py +++ b/libmproxy/onboarding/app.py @@ -1,29 +1,80 @@ from __future__ import absolute_import -import flask import os +import tornado.web +import tornado.wsgi +import tornado.template + +from .. import utils from ..proxy import config -mapp = flask.Flask(__name__) -mapp.debug = True + +loader = tornado.template.Loader(utils.pkg_data.path("onboarding/templates")) -def master(): - return flask.request.environ["mitmproxy.master"] +class Adapter(tornado.wsgi.WSGIAdapter): + # Tornado doesn't make the WSGI environment available to pages, so this + # hideous monkey patch is the easiest way to get to the mitmproxy.master + # variable. + def __init__(self, application): + self._application = application + + def application(self, request): + request.master = self.environ["mitmproxy.master"] + return self._application(request) + + def __call__(self, environ, start_response): + self.environ = environ + return tornado.wsgi.WSGIAdapter.__call__( + self, + environ, + start_response + ) -@mapp.route("/") -def index(): - return flask.render_template("index.html", section="home") +class Index(tornado.web.RequestHandler): + def get(self): + t = loader.load("index.html") + self.write(t.generate()) -@mapp.route("/cert/pem") -def certs_pem(): - p = os.path.join(master().server.config.cadir, config.CONF_BASENAME + "-ca-cert.pem") - return flask.Response(open(p, "rb").read(), mimetype='application/x-x509-ca-cert') +class PEM(tornado.web.RequestHandler): + def get(self): + p = os.path.join( + self.request.master.server.config.cadir, + config.CONF_BASENAME + "-ca-cert.pem" + ) + self.set_header( + "Content-Type", "application/x-x509-ca-cert" + ) + self.write(open(p, "rb").read()) -@mapp.route("/cert/p12") -def certs_p12(): - p = os.path.join(master().server.config.cadir, config.CONF_BASENAME + "-ca-cert.p12") - return flask.Response(open(p, "rb").read(), mimetype='application/x-pkcs12') +class P12(tornado.web.RequestHandler): + def get(self): + p = os.path.join( + self.request.master.server.config.cadir, + config.CONF_BASENAME + "-ca-cert.p12" + ) + self.set_header( + "Content-Type", "application/x-pkcs12" + ) + self.write(open(p, "rb").read()) + + +application = tornado.web.Application( + [ + (r"/", Index), + (r"/cert/pem", PEM), + (r"/cert/p12", P12), + ( + r"/static/(.*)", + tornado.web.StaticFileHandler, + { + "path": utils.pkg_data.path("onboarding/static") + } + ), + ], + #debug=True +) +mapp = Adapter(application) diff --git a/libmproxy/onboarding/templates/frame.html b/libmproxy/onboarding/templates/frame.html index b5c5c67c8..f00e1a66e 100644 --- a/libmproxy/onboarding/templates/frame.html +++ b/libmproxy/onboarding/templates/frame.html @@ -3,7 +3,7 @@
{% block body %} - {% endblock %} + {% end %}
-{% endblock %} +{% end %} diff --git a/libmproxy/onboarding/templates/index.html b/libmproxy/onboarding/templates/index.html index 65fda5d2b..1bcff1b88 100644 --- a/libmproxy/onboarding/templates/index.html +++ b/libmproxy/onboarding/templates/index.html @@ -32,4 +32,4 @@ between mitmproxy installations. -{% endblock %} +{% end %} diff --git a/libmproxy/onboarding/templates/layout.html b/libmproxy/onboarding/templates/layout.html index 622858bea..8726a788e 100644 --- a/libmproxy/onboarding/templates/layout.html +++ b/libmproxy/onboarding/templates/layout.html @@ -25,7 +25,7 @@
{% block content %} - {% endblock %} + {% end %}
diff --git a/setup.py b/setup.py index 79398a186..953df2bb2 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,6 @@ deps = { "netlib>=%s, <%s" % (version.MINORVERSION, version.NEXT_MINORVERSION), "pyasn1>0.1.2", "pyOpenSSL>=0.14", - "Flask>=0.10.1", "tornado>=4.0.2", "configargparse>=0.9.3" } diff --git a/test/tservers.py b/test/tservers.py index 12154ba7d..37929d1ab 100644 --- a/test/tservers.py +++ b/test/tservers.py @@ -158,10 +158,10 @@ class HTTPProxTest(ProxTestBase): if self.ssl: p = libpathod.pathoc.Pathoc(("127.0.0.1", self.proxy.port), True) p.connect((APP_HOST, APP_PORT)) - return p.request("get:'/%s'"%page) + return p.request("get:'%s'"%page) else: p = self.pathoc() - return p.request("get:'http://%s/%s'"%(APP_HOST, page)) + return p.request("get:'http://%s%s'"%(APP_HOST, page)) class TResolver: