aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/addons/onboardingapp/app.py
blob: 0f09e32c097abcf1c2a94374b79dd8ed7c43e563 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os

import tornado.template
import tornado.web
import tornado.wsgi

from mitmproxy.utils import data
from mitmproxy.proxy import config

loader = tornado.template.Loader(data.pkg_data.path("addons/onboardingapp/templates"))


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
        )


class Index(tornado.web.RequestHandler):

    def get(self):
        t = loader.load("index.html")
        self.write(t.generate())


class PEM(tornado.web.RequestHandler):

    @property
    def filename(self):
        return config.CONF_BASENAME + "-ca-cert.pem"

    def head(self):
        p = os.path.join(self.request.master.options.cadir, self.filename)
        p = os.path.expanduser(p)
        content_length = os.path.getsize(p)

        self.set_header("Content-Type", "application/x-x509-ca-cert")
        self.set_header(
            "Content-Disposition",
            "inline; filename={}".format(
                self.filename))
        self.set_header("Content-Length", content_length)

    def get(self):
        p = os.path.join(self.request.master.options.cadir, self.filename)
        p = os.path.expanduser(p)
        self.set_header("Content-Type", "application/x-x509-ca-cert")
        self.set_header(
            "Content-Disposition",
            "inline; filename={}".format(
                self.filename))

        with open(p, "rb") as f:
            self.write(f.read())


class P12(tornado.web.RequestHandler):

    @property
    def filename(self):
        return config.CONF_BASENAME + "-ca-cert.p12"

    def head(self):
        p = os.path.join(self.request.master.options.cadir, self.filename)
        p = os.path.expanduser(p)
        content_length = os.path.getsize(p)

        self.set_header("Content-Type", "application/x-pkcs12")
        self.set_header(
            "Content-Disposition",
            "inline; filename={}".format(
                self.filename))

        self.set_header("Content-Length", content_length)

    def get(self):
        p = os.path.join(self.request.master.options.cadir, self.filename)
        p = os.path.expanduser(p)
        self.set_header("Content-Type", "application/x-pkcs12")
        self.set_header(
            "Content-Disposition",
            "inline; filename={}".format(
                self.filename))

        with open(p, "rb") as f:
            self.write(f.read())


application = tornado.web.Application(
    [
        (r"/", Index),
        (r"/cert/pem", PEM),
        (r"/cert/p12", P12),
        (
            r"/static/(.*)",
            tornado.web.StaticFileHandler,
            {
                "path": data.pkg_data.path("addons/onboardingapp/static")
            }
        ),
    ],
    # debug=True
)