mitmproxy/libmproxy/web/app.py

128 lines
3.4 KiB
Python
Raw Normal View History

2014-09-14 00:22:28 +00:00
import os.path
2014-11-28 18:16:47 +00:00
import sys
2014-09-14 00:22:28 +00:00
import tornado.web
import tornado.websocket
import logging
import json
from .. import flow
2014-09-14 00:22:28 +00:00
class IndexHandler(tornado.web.RequestHandler):
def get(self):
2014-11-28 18:16:47 +00:00
_ = self.xsrf_token # https://github.com/tornadoweb/tornado/issues/645
self.render("index.html")
class WebSocketEventBroadcaster(tornado.websocket.WebSocketHandler):
connections = None # raise an error if inherited class doesn't specify its own instance.
def open(self):
self.connections.add(self)
def on_close(self):
self.connections.remove(self)
@classmethod
2014-12-09 17:55:16 +00:00
def broadcast(cls, **kwargs):
message = json.dumps(kwargs)
for conn in cls.connections:
try:
conn.write_message(message)
except:
logging.error("Error sending message", exc_info=True)
2014-12-23 19:33:42 +00:00
class ClientConnection(WebSocketEventBroadcaster):
connections = set()
2014-11-28 18:16:47 +00:00
class Flows(tornado.web.RequestHandler):
def get(self):
self.write(dict(
data=[f.get_state(short=True) for f in self.application.state.flows]
))
2014-12-23 19:33:42 +00:00
class AcceptFlows(tornado.web.RequestHandler):
def post(self):
self.application.state.flows.accept_all(self.application.master)
class AcceptFlow(tornado.web.RequestHandler):
def post(self, flow_id):
flow_id = str(flow_id)
for flow in self.application.state.flows:
if flow.id == flow_id:
flow.accept_intercept(self.application.master)
break
class Events(tornado.web.RequestHandler):
def get(self):
self.write(dict(
data=list(self.application.state.events)
))
class Settings(tornado.web.RequestHandler):
def get(self):
self.write(dict(
data=dict(
2014-12-23 19:33:42 +00:00
intercept=self.application.state.intercept_txt
)
))
2014-12-23 19:33:42 +00:00
def put(self, *update, **kwargs):
update = {}
for k, v in self.request.arguments.iteritems():
if len(v) != 1:
print "Warning: Unknown length for setting {}: {}".format(k, v)
continue
if k == "_xsrf":
continue
elif k == "intercept":
self.application.state.set_intercept(v[0])
update[k] = v[0]
else:
print "Warning: Unknown setting {}: {}".format(k, v)
ClientConnection.broadcast(
type="settings",
cmd="update",
data=update
)
2014-12-13 00:56:04 +00:00
class Clear(tornado.web.RequestHandler):
2014-11-28 18:16:47 +00:00
def post(self):
self.application.state.clear()
2014-12-23 19:33:42 +00:00
class Application(tornado.web.Application):
2014-12-23 19:33:42 +00:00
@property
def state(self):
return self.master.state
2014-12-23 19:33:42 +00:00
def __init__(self, master, debug):
self.master = master
2014-09-14 00:22:28 +00:00
handlers = [
(r"/", IndexHandler),
(r"/updates", ClientConnection),
(r"/events", Events),
2014-11-28 18:16:47 +00:00
(r"/flows", Flows),
2014-12-23 19:33:42 +00:00
(r"/flows/accept", AcceptFlows),
(r"/flows/([0-9a-f\-]+)/accept", AcceptFlow),
(r"/settings", Settings),
2014-12-13 00:56:04 +00:00
(r"/clear", Clear),
2014-09-14 00:22:28 +00:00
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=True,
2014-11-28 18:16:47 +00:00
cookie_secret=os.urandom(256),
2014-09-14 00:22:28 +00:00
debug=debug,
)
tornado.web.Application.__init__(self, handlers, **settings)