mitmproxy/libmproxy/web/app.py

269 lines
8.1 KiB
Python
Raw Normal View History

2014-09-14 00:22:28 +00:00
import os.path
import re
2014-09-14 00:22:28 +00:00
import tornado.web
import tornado.websocket
import logging
import json
2014-12-26 02:10:24 +00:00
from .. import version, filt
2014-09-14 00:22:28 +00:00
2014-12-24 00:07:57 +00:00
class APIError(tornado.web.HTTPError):
pass
class RequestHandler(tornado.web.RequestHandler):
def set_default_headers(self):
super(RequestHandler, self).set_default_headers()
self.set_header("Server", version.NAMEVERSION)
self.set_header("X-Frame-Options", "DENY")
self.add_header("X-XSS-Protection", "1; mode=block")
self.add_header("X-Content-Type-Options", "nosniff")
self.add_header(
"Content-Security-Policy",
"default-src 'self'; "
"connect-src 'self' ws://* ; "
"style-src 'self' 'unsafe-inline'"
)
@property
def json(self):
2015-09-05 18:45:58 +00:00
if not self.request.headers.get("Content-Type").startswith("application/json"):
return None
return json.loads(self.request.body)
@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 IndexHandler(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")
2014-12-26 02:10:24 +00:00
class FiltHelp(RequestHandler):
def get(self):
self.write(dict(
commands=filt.help
))
2015-05-30 00:03:28 +00:00
class WebSocketEventBroadcaster(tornado.websocket.WebSocketHandler):
2015-05-30 00:03:28 +00:00
# raise an error if inherited class doesn't specify its own instance.
connections = None
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, ensure_ascii=False)
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-12-24 00:07:57 +00:00
class Flows(RequestHandler):
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-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
def put(self, flow_id):
flow = self.flow
2015-05-01 15:24:44 +00:00
flow.backup()
for a, b in self.json.iteritems():
if a == "request":
request = flow.request
for k, v in b.iteritems():
if k in ["method", "scheme", "host", "path", "http_version"]:
setattr(request, k, str(v))
elif k == "port":
request.port = int(v)
2015-03-26 17:17:30 +00:00
elif k == "headers":
request.headers.load_state(v)
else:
print "Warning: Unknown update {}.{}: {}".format(a, k, v)
elif a == "response":
response = flow.response
for k, v in b.iteritems():
if k == "msg":
response.msg = str(v)
elif k == "code":
response.status_code = int(v)
elif k == "http_version":
response.http_version = str(v)
2015-03-26 17:17:30 +00:00
elif k == "headers":
response.headers.load_state(v)
else:
print "Warning: Unknown update {}.{}: {}".format(a, k, v)
else:
print "Warning: Unknown update {}: {}".format(a, b)
self.state.update_flow(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
class RevertFlow(RequestHandler):
def post(self, flow_id):
self.state.revert(self.flow)
2014-12-24 00:07:57 +00:00
class ReplayFlow(RequestHandler):
def post(self, flow_id):
2015-03-27 17:51:25 +00:00
self.flow.backup()
self.flow.response = None
self.state.update_flow(self.flow)
2014-12-24 00:07:57 +00:00
r = self.master.replay_request(self.flow)
if r:
raise APIError(400, r)
class FlowContent(RequestHandler):
def get(self, flow_id, message):
message = getattr(self.flow, message)
if not message.content:
raise APIError(400, "No content.")
2015-09-05 18:45:58 +00:00
content_encoding = message.headers.get("Content-Encoding", None)
if content_encoding:
content_encoding = re.sub(r"[^\w]", "", content_encoding)
self.set_header("Content-Encoding", content_encoding)
2015-09-05 18:45:58 +00:00
original_cd = message.headers.get("Content-Disposition", None)
filename = None
if original_cd:
filename = re.search("filename=([\w\" \.\-\(\)]+)", original_cd)
if filename:
filename = filename.group(1)
if not filename:
filename = self.flow.request.path.split("?")[0].split("/")[-1]
filename = re.sub(r"[^\w\" \.\-\(\)]", "", filename)
cd = "attachment; filename={}".format(filename)
self.set_header("Content-Disposition", cd)
self.set_header("Content-Type", "application/text")
self.set_header("X-Content-Type-Options", "nosniff")
self.set_header("X-Frame-Options", "DENY")
self.write(message.content)
2014-12-24 00:07:57 +00:00
class Events(RequestHandler):
def get(self):
self.write(dict(
2014-12-24 00:07:57 +00:00
data=list(self.state.events)
))
2014-12-24 00:07:57 +00:00
class Settings(RequestHandler):
def get(self):
self.write(dict(
data=dict(
2014-12-25 01:08:58 +00:00
version=version.VERSION,
2014-12-25 01:03:55 +00:00
mode=str(self.master.server.config.mode),
2014-12-24 00:07:57 +00:00
intercept=self.state.intercept_txt
)
))
def put(self):
2014-12-23 19:33:42 +00:00
update = {}
for k, v in self.json.iteritems():
if k == "intercept":
self.state.set_intercept(v)
update[k] = v
2014-12-23 19:33:42 +00:00
else:
print("Warning: Unknown setting {}: {}".format(k, v))
2014-12-23 19:33:42 +00:00
ClientConnection.broadcast(
type="settings",
cmd="update",
data=update
)
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 = [
(r"/", IndexHandler),
2014-12-26 02:10:24 +00:00
(r"/filter-help", FiltHelp),
(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),
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),
(r"/flows/(?P<flow_id>[0-9a-f\-]+)/revert", RevertFlow),
(r"/flows/(?P<flow_id>[0-9a-f\-]+)/(?P<message>request|response)/content", FlowContent),
(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,
)
super(Application, self).__init__(handlers, **settings)