mitmproxy/libmproxy/dump.py

261 lines
7.7 KiB
Python
Raw Normal View History

2015-03-15 01:53:05 +00:00
from __future__ import absolute_import, print_function
2015-03-15 02:08:45 +00:00
import json
import sys
import os
import netlib.utils
from . import flow, filt, utils
2014-07-21 19:06:55 +00:00
from .protocol import http
class DumpError(Exception):
pass
2011-02-16 21:18:38 +00:00
2011-02-16 10:03:46 +00:00
class Options(object):
attributes = [
"app",
2013-12-08 14:46:11 +00:00
"app_host",
"app_port",
"anticache",
"anticomp",
"client_replay",
"filtstr",
"flow_detail",
"keepserving",
"kill",
"no_server",
"nopop",
"refresh_server_playback",
"replacements",
"rfile",
"rheaders",
"setheaders",
"server_replay",
"scripts",
2013-03-17 04:53:39 +00:00
"showhost",
"stickycookie",
"stickyauth",
2014-07-21 19:06:55 +00:00
"stream_large_bodies",
"verbosity",
"outfile",
"replay_ignore_content",
"replay_ignore_params",
"replay_ignore_payload_params",
"replay_ignore_host"
2011-02-16 10:03:46 +00:00
]
2011-02-16 10:03:46 +00:00
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
for i in self.attributes:
2011-02-16 21:18:38 +00:00
if not hasattr(self, i):
setattr(self, i, None)
2011-02-16 10:03:46 +00:00
def str_response(resp):
2015-05-30 00:03:28 +00:00
r = "%s %s" % (resp.code, resp.msg)
if resp.is_replay:
r = "[replay] " + r
return r
def str_request(f, showhost):
if f.client_conn:
c = f.client_conn.address.host
2011-03-05 22:21:31 +00:00
else:
c = "[replay]"
2015-05-30 00:03:28 +00:00
r = "%s %s %s" % (c, f.request.method, f.request.pretty_url(showhost))
if f.request.stickycookie:
r = "[stickycookie] " + r
return r
2011-02-16 09:10:24 +00:00
class DumpMaster(flow.FlowMaster):
def __init__(self, server, options, outfile=sys.stdout):
2011-02-16 09:10:24 +00:00
flow.FlowMaster.__init__(self, server, flow.State())
2011-02-16 10:03:46 +00:00
self.outfile = outfile
self.o = options
self.anticache = options.anticache
self.anticomp = options.anticomp
2013-03-17 04:43:31 +00:00
self.showhost = options.showhost
self.replay_ignore_params = options.replay_ignore_params
self.replay_ignore_content = options.replay_ignore_content
self.replay_ignore_host = options.replay_ignore_host
self.refresh_server_playback = options.refresh_server_playback
self.replay_ignore_payload_params = options.replay_ignore_payload_params
2014-07-21 19:06:55 +00:00
self.set_stream_large_bodies(options.stream_large_bodies)
if options.filtstr:
self.filt = filt.parse(options.filtstr)
2011-02-16 21:44:08 +00:00
else:
self.filt = None
if options.stickycookie:
self.set_stickycookie(options.stickycookie)
if options.stickyauth:
self.set_stickyauth(options.stickyauth)
if options.outfile:
path = os.path.expanduser(options.outfile[0])
2011-02-16 21:18:38 +00:00
try:
f = file(path, options.outfile[1])
self.start_stream(f, self.filt)
2015-05-30 00:03:28 +00:00
except IOError as v:
2011-02-16 21:18:38 +00:00
raise DumpError(v.strerror)
2012-03-17 04:29:25 +00:00
if options.replacements:
for i in options.replacements:
self.replacehooks.add(*i)
if options.setheaders:
for i in options.setheaders:
self.setheaders.add(*i)
if options.server_replay:
2011-03-05 22:21:31 +00:00
self.start_server_playback(
self._readflow(options.server_replay),
options.kill, options.rheaders,
not options.keepserving,
options.nopop,
options.replay_ignore_params,
options.replay_ignore_content,
options.replay_ignore_payload_params,
options.replay_ignore_host
2011-03-05 22:21:31 +00:00
)
2011-03-05 22:21:31 +00:00
if options.client_replay:
self.start_client_playback(
self._readflow(options.client_replay),
not options.keepserving
)
2011-03-05 22:21:31 +00:00
scripts = options.scripts or []
for command in scripts:
err = self.load_script(command)
if err:
raise DumpError(err)
if options.rfile:
try:
self.load_flows_file(options.rfile)
2015-05-30 00:03:28 +00:00
except flow.FlowReadError as v:
self.add_event("Flow file corrupted.", "error")
raise DumpError(v)
if self.o.app:
self.start_app(self.o.app_host, self.o.app_port)
2015-01-05 21:12:38 +00:00
def _readflow(self, paths):
2015-02-05 13:44:45 +00:00
"""
Utitility function that reads a list of flows
or raises a DumpError if that fails.
"""
2011-03-05 22:21:31 +00:00
try:
2015-02-05 13:44:45 +00:00
return flow.read_flows_from_paths(paths)
except flow.FlowReadError as e:
raise DumpError(e.strerror)
2014-03-13 00:04:45 +00:00
def add_event(self, e, level="info"):
2014-11-14 23:52:26 +00:00
needed = dict(error=0, info=1, debug=2).get(level, 1)
if self.o.verbosity >= needed:
2015-03-15 01:53:05 +00:00
print(e, file=self.outfile)
self.outfile.flush()
2015-03-15 02:08:45 +00:00
@staticmethod
def indent(n, t):
l = str(t).strip().splitlines()
pad = " " * n
return "\n".join(pad + i for i in l)
2011-02-17 01:26:50 +00:00
2015-03-15 01:53:05 +00:00
def _print_message(self, message):
if self.o.flow_detail >= 2:
2015-04-14 22:29:57 +00:00
print(self.indent(4, message.headers.format()), file=self.outfile)
2015-03-15 01:53:05 +00:00
if self.o.flow_detail >= 3:
if message.content == http.CONTENT_MISSING:
print(self.indent(4, "(content missing)"), file=self.outfile)
elif message.content:
print("", file=self.outfile)
content = message.get_decoded_content()
if not utils.isBin(content):
2015-03-15 02:08:45 +00:00
try:
jsn = json.loads(content)
2015-05-30 00:03:28 +00:00
print(
self.indent(
4,
json.dumps(
jsn,
indent=2)),
file=self.outfile)
2015-03-15 02:08:45 +00:00
except ValueError:
print(self.indent(4, content), file=self.outfile)
2015-03-15 01:53:05 +00:00
else:
d = netlib.utils.hexdump(content)
2015-05-30 00:03:28 +00:00
d = "\n".join("%s\t%s %s" % i for i in d)
2015-03-15 01:53:05 +00:00
print(self.indent(4, d), file=self.outfile)
2015-03-15 02:08:45 +00:00
if self.o.flow_detail >= 2:
print("", file=self.outfile)
2015-03-15 01:53:05 +00:00
def _process_flow(self, f):
self.state.delete_flow(f)
if self.filt and not f.match(self.filt):
2011-08-02 04:52:47 +00:00
return
2015-03-15 01:53:05 +00:00
if self.o.flow_detail == 0:
return
if f.request:
print(str_request(f, self.showhost), file=self.outfile)
self._print_message(f.request)
if f.response:
2015-03-15 01:53:05 +00:00
if f.response.content == http.CONTENT_MISSING:
sz = "(content missing)"
else:
2015-04-30 00:18:01 +00:00
sz = netlib.utils.pretty_size(len(f.response.content))
2015-05-30 00:03:28 +00:00
print(
" << %s %s" %
(str_response(
f.response),
sz),
file=self.outfile)
2015-03-15 01:53:05 +00:00
self._print_message(f.response)
if f.error:
print(" << {}".format(f.error.msg), file=self.outfile)
self.outfile.flush()
def handle_request(self, f):
flow.FlowMaster.handle_request(self, f)
if f:
f.reply()
return f
def handle_response(self, f):
flow.FlowMaster.handle_response(self, f)
2011-02-16 10:03:46 +00:00
if f:
f.reply()
self._process_flow(f)
2011-03-05 22:21:31 +00:00
return f
2011-02-16 21:18:38 +00:00
def handle_error(self, f):
flow.FlowMaster.handle_error(self, f)
if f:
self._process_flow(f)
return f
def shutdown(self): # pragma: no cover
return flow.FlowMaster.shutdown(self)
def run(self): # pragma: no cover
if self.o.rfile and not self.o.keepserving:
self.shutdown()
return
2011-02-16 09:10:24 +00:00
try:
return flow.FlowMaster.run(self)
except BaseException:
self.shutdown()
raise