2014-09-14 00:22:28 +00:00
|
|
|
import os.path
|
|
|
|
import tornado.web
|
2014-09-16 21:40:25 +00:00
|
|
|
import tornado.websocket
|
|
|
|
import logging
|
2014-09-17 01:58:56 +00:00
|
|
|
import json
|
2014-09-14 00:22:28 +00:00
|
|
|
|
|
|
|
|
2014-12-24 00:07:57 +00:00
|
|
|
class APIError(tornado.web.HTTPError):
|
|
|
|
pass
|
|
|
|
|
2014-09-14 00:33:07 +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
|
2014-09-14 00:33:07 +00:00
|
|
|
self.render("index.html")
|
|
|
|
|
|
|
|
|
2014-11-26 03:18:21 +00:00
|
|
|
class WebSocketEventBroadcaster(tornado.websocket.WebSocketHandler):
|
|
|
|
connections = None # raise an error if inherited class doesn't specify its own instance.
|
2014-09-16 21:40:25 +00:00
|
|
|
|
|
|
|
def open(self):
|
2014-11-26 03:18:21 +00:00
|
|
|
self.connections.add(self)
|
2014-09-16 21:40:25 +00:00
|
|
|
|
|
|
|
def on_close(self):
|
2014-11-26 03:18:21 +00:00
|
|
|
self.connections.remove(self)
|
2014-09-16 21:40:25 +00:00
|
|
|
|
|
|
|
@classmethod
|
2014-12-09 17:55:16 +00:00
|
|
|
def broadcast(cls, **kwargs):
|
|
|
|
message = json.dumps(kwargs)
|
2014-09-16 21:40:25 +00:00
|
|
|
for conn in cls.connections:
|
|
|
|
try:
|
2014-11-26 03:18:21 +00:00
|
|
|
conn.write_message(message)
|
2014-09-16 21:40:25 +00:00
|
|
|
except:
|
|
|
|
logging.error("Error sending message", exc_info=True)
|
|
|
|
|
|
|
|
|
2014-12-23 19:33:42 +00:00
|
|
|
class ClientConnection(WebSocketEventBroadcaster):
|
|
|
|
connections = set()
|
|
|
|
|
|
|
|
|
2014-12-24 00:07:57 +00:00
|
|
|
class RequestHandler(tornado.web.RequestHandler):
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
return self.application.master.state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def master(self):
|
|
|
|
return self.application.master
|
|
|
|
|
|
|
|
@property
|
|
|
|
def flow(self):
|
|
|
|
flow_id = str(self.path_kwargs["flow_id"])
|
|
|
|
flow = self.state.flows.get(flow_id)
|
|
|
|
if flow:
|
|
|
|
return flow
|
|
|
|
else:
|
|
|
|
raise APIError(400, "Flow not found.")
|
|
|
|
|
|
|
|
def write_error(self, status_code, **kwargs):
|
|
|
|
if "exc_info" in kwargs and isinstance(kwargs["exc_info"][1], APIError):
|
|
|
|
self.finish(kwargs["exc_info"][1].log_message)
|
|
|
|
else:
|
|
|
|
super(RequestHandler, self).write_error(status_code, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class Flows(RequestHandler):
|
2014-11-26 03:18:21 +00:00
|
|
|
def get(self):
|
|
|
|
self.write(dict(
|
2014-12-24 00:07:57 +00:00
|
|
|
data=[f.get_state(short=True) for f in self.state.flows]
|
2014-12-09 23:47:05 +00:00
|
|
|
))
|
|
|
|
|
2014-12-23 19:33:42 +00:00
|
|
|
|
2014-12-24 00:07:57 +00:00
|
|
|
class ClearAll(RequestHandler):
|
|
|
|
def post(self):
|
|
|
|
self.state.clear()
|
|
|
|
|
|
|
|
|
|
|
|
class AcceptFlows(RequestHandler):
|
2014-12-23 19:33:42 +00:00
|
|
|
def post(self):
|
2014-12-24 00:07:57 +00:00
|
|
|
self.state.flows.accept_all(self.master)
|
|
|
|
|
|
|
|
|
|
|
|
class AcceptFlow(RequestHandler):
|
|
|
|
def post(self, flow_id):
|
|
|
|
self.flow.accept_intercept(self.master)
|
|
|
|
|
|
|
|
|
|
|
|
class FlowHandler(RequestHandler):
|
|
|
|
def delete(self, flow_id):
|
|
|
|
self.flow.kill(self.master)
|
|
|
|
self.state.delete_flow(self.flow)
|
2014-12-23 19:33:42 +00:00
|
|
|
|
|
|
|
|
2014-12-24 00:07:57 +00:00
|
|
|
class DuplicateFlow(RequestHandler):
|
2014-12-23 19:33:42 +00:00
|
|
|
def post(self, flow_id):
|
2014-12-24 00:07:57 +00:00
|
|
|
self.master.duplicate_flow(self.flow)
|
2014-12-23 19:33:42 +00:00
|
|
|
|
2014-12-24 00:07:57 +00:00
|
|
|
class ReplayFlow(RequestHandler):
|
|
|
|
def post(self, flow_id):
|
|
|
|
self.flow.backup()
|
|
|
|
r = self.master.replay_request(self.flow)
|
|
|
|
if r:
|
|
|
|
raise APIError(400, r)
|
|
|
|
|
|
|
|
class Events(RequestHandler):
|
2014-12-09 23:47:05 +00:00
|
|
|
def get(self):
|
|
|
|
self.write(dict(
|
2014-12-24 00:07:57 +00:00
|
|
|
data=list(self.state.events)
|
2014-12-10 14:25:40 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
|
2014-12-24 00:07:57 +00:00
|
|
|
class Settings(RequestHandler):
|
2014-12-10 14:25:40 +00:00
|
|
|
def get(self):
|
|
|
|
self.write(dict(
|
|
|
|
data=dict(
|
2014-12-24 00:07:57 +00:00
|
|
|
intercept=self.state.intercept_txt
|
2014-12-10 14:25:40 +00:00
|
|
|
)
|
2014-11-26 03:18:21 +00:00
|
|
|
))
|
|
|
|
|
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":
|
2014-12-24 00:07:57 +00:00
|
|
|
self.state.set_intercept(v[0])
|
2014-12-23 19:33:42 +00:00
|
|
|
update[k] = v[0]
|
|
|
|
else:
|
|
|
|
print "Warning: Unknown setting {}: {}".format(k, v)
|
|
|
|
|
|
|
|
ClientConnection.broadcast(
|
|
|
|
type="settings",
|
|
|
|
cmd="update",
|
|
|
|
data=update
|
|
|
|
)
|
|
|
|
|
2014-11-26 03:18:21 +00:00
|
|
|
|
2014-12-23 19:33:42 +00:00
|
|
|
class Application(tornado.web.Application):
|
|
|
|
def __init__(self, master, debug):
|
|
|
|
self.master = master
|
2014-09-14 00:22:28 +00:00
|
|
|
handlers = [
|
2014-09-14 00:33:07 +00:00
|
|
|
(r"/", IndexHandler),
|
2014-09-16 21:40:25 +00:00
|
|
|
(r"/updates", ClientConnection),
|
2014-12-09 23:47:05 +00:00
|
|
|
(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),
|
2014-12-24 00:07:57 +00:00
|
|
|
(r"/flows/(?P<flow_id>[0-9a-f\-]+)", FlowHandler),
|
|
|
|
(r"/flows/(?P<flow_id>[0-9a-f\-]+)/accept", AcceptFlow),
|
|
|
|
(r"/flows/(?P<flow_id>[0-9a-f\-]+)/duplicate", DuplicateFlow),
|
|
|
|
(r"/flows/(?P<flow_id>[0-9a-f\-]+)/replay", ReplayFlow),
|
2014-12-10 14:25:40 +00:00
|
|
|
(r"/settings", Settings),
|
2014-12-24 00:07:57 +00:00
|
|
|
(r"/clear", ClearAll),
|
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)
|
|
|
|
|