diff options
Diffstat (limited to 'libmproxy/web/app.py')
-rw-r--r-- | libmproxy/web/app.py | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/libmproxy/web/app.py b/libmproxy/web/app.py index e2765a6d..4fdff783 100644 --- a/libmproxy/web/app.py +++ b/libmproxy/web/app.py @@ -3,6 +3,7 @@ import tornado.web import tornado.websocket import logging import json +from .. import flow class IndexHandler(tornado.web.RequestHandler): @@ -10,36 +11,53 @@ class IndexHandler(tornado.web.RequestHandler): self.render("index.html") -class ClientConnection(tornado.websocket.WebSocketHandler): - connections = set() +class WebSocketEventBroadcaster(tornado.websocket.WebSocketHandler): + connections = None # raise an error if inherited class doesn't specify its own instance. def open(self): - ClientConnection.connections.add(self) + self.connections.add(self) def on_close(self): - ClientConnection.connections.remove(self) + self.connections.remove(self) @classmethod def broadcast(cls, type, data): + message = json.dumps( + { + "type": type, + "data": data + } + ) for conn in cls.connections: try: - conn.write_message( - json.dumps( - { - "type": type, - "data": data - } - ) - ) + conn.write_message(message) except: logging.error("Error sending message", exc_info=True) +class FlowsHandler(tornado.web.RequestHandler): + def get(self): + self.write(dict( + flows=[f.get_state(short=True) for f in self.application.state.flows] + )) + + +class FlowUpdates(WebSocketEventBroadcaster): + connections = set() + + +class ClientConnection(WebSocketEventBroadcaster): + connections = set() + + class Application(tornado.web.Application): - def __init__(self, debug): + def __init__(self, state, debug): + self.state = state handlers = [ (r"/", IndexHandler), (r"/updates", ClientConnection), + (r"/flows", FlowsHandler), + (r"/flows/updates", FlowUpdates), ] settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), |