mitmproxy/libmproxy/dump.py

233 lines
7.0 KiB
Python
Raw Normal View History

2012-02-23 02:52:01 +00:00
# Copyright (C) 2012 Aldo Cortesi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, os
2011-08-18 21:22:25 +00:00
import flow, filt, utils
2011-02-16 21:18:38 +00:00
class DumpError(Exception): pass
2011-02-16 10:03:46 +00:00
class Options(object):
__slots__ = [
"anticache",
"anticomp",
"client_replay",
"eventlog",
"keepserving",
"kill",
"no_server",
"nopop",
"refresh_server_playback",
"replacements",
"rfile",
"rheaders",
"server_replay",
"script",
"stickycookie",
"stickyauth",
"verbosity",
"wfile",
2011-02-16 10:03:46 +00:00
]
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
2011-02-16 21:18:38 +00:00
for i in self.__slots__:
if not hasattr(self, i):
setattr(self, i, None)
2011-02-16 10:03:46 +00:00
def str_response(resp):
r = "%s %s"%(resp.code, resp.msg)
if resp.is_replay():
r = "[replay] " + r
return r
def str_request(req):
2011-03-05 22:21:31 +00:00
if req.client_conn:
c = req.client_conn.address[0]
else:
c = "[replay]"
r = "%s %s %s"%(c, req.method, req.get_url())
if req.stickycookie:
r = "[stickycookie] " + r
return r
2011-02-16 09:10:24 +00:00
class DumpMaster(flow.FlowMaster):
2011-02-16 21:44:08 +00:00
def __init__(self, server, options, filtstr, 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
self.eventlog = options.eventlog
self.refresh_server_playback = options.refresh_server_playback
2011-02-16 21:44:08 +00:00
if filtstr:
self.filt = filt.parse(filtstr)
else:
self.filt = None
if options.stickycookie:
self.set_stickycookie(options.stickycookie)
if options.stickyauth:
self.set_stickyauth(options.stickyauth)
2011-02-16 21:18:38 +00:00
if options.wfile:
path = os.path.expanduser(options.wfile)
try:
f = file(path, "wb")
self.fwriter = flow.FlowWriter(f)
except IOError, v:
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.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
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
if options.script:
err = self.load_script(options.script)
if err:
raise DumpError(err)
if options.rfile:
path = os.path.expanduser(options.rfile)
try:
f = file(path, "r")
freader = flow.FlowReader(f)
except IOError, v:
raise DumpError(v.strerror)
try:
self.load_flows(freader)
except flow.FlowReadError, v:
raise DumpError(v)
2011-03-05 22:21:31 +00:00
def _readflow(self, path):
path = os.path.expanduser(path)
try:
f = file(path, "r")
flows = list(flow.FlowReader(f).stream())
except (IOError, flow.FlowReadError), v:
2011-03-05 22:21:31 +00:00
raise DumpError(v.strerror)
return flows
def add_event(self, e, level="info"):
if self.eventlog:
print >> self.outfile, e
2011-02-16 09:10:24 +00:00
def handle_request(self, r):
f = flow.FlowMaster.handle_request(self, r)
if f:
2011-08-03 11:02:33 +00:00
r._ack()
2011-03-05 22:21:31 +00:00
return f
2011-02-17 01:26:50 +00:00
def indent(self, n, t):
l = str(t).strip().split("\n")
return "\n".join(" "*n + i for i in l)
def _process_flow(self, f):
if self.filt and not f.match(self.filt):
2011-08-02 04:52:47 +00:00
return
if f.response:
sz = utils.pretty_size(len(f.response.content))
if self.o.verbosity > 0:
result = " << %s %s"%(str_response(f.response), sz)
if self.o.verbosity > 1:
result = result + "\n\n" + self.indent(4, f.response.headers)
if self.o.verbosity > 2:
if utils.isBin(f.response.content):
d = utils.hexdump(f.response.content)
d = "\n".join("%s\t%s %s"%i for i in d)
cont = self.indent(4, d)
elif f.response.content:
cont = self.indent(4, f.response.content)
else:
cont = ""
result = result + "\n\n" + cont
elif f.error:
result = " << %s"%f.error.msg
if self.o.verbosity == 1:
print >> self.outfile, str_request(f.request)
print >> self.outfile, result
elif self.o.verbosity == 2:
print >> self.outfile, str_request(f.request)
print >> self.outfile, self.indent(4, f.request.headers)
print >> self.outfile
print >> self.outfile, result
print >> self.outfile, "\n"
elif self.o.verbosity >= 3:
print >> self.outfile, str_request(f.request)
print >> self.outfile, self.indent(4, f.request.headers)
if utils.isBin(f.request.content):
print >> self.outfile, self.indent(4, utils.hexdump(f.request.content))
elif f.request.content:
print >> self.outfile, self.indent(4, f.request.content)
print >> self.outfile
print >> self.outfile, result
print >> self.outfile, "\n"
self.state.delete_flow(f)
if self.o.wfile:
self.fwriter.add(f)
def handle_response(self, msg):
2011-02-16 09:10:24 +00:00
f = flow.FlowMaster.handle_response(self, msg)
2011-02-16 10:03:46 +00:00
if f:
2011-08-03 11:02:33 +00:00
msg._ack()
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, msg):
f = flow.FlowMaster.handle_error(self, msg)
if f:
self._process_flow(f)
return f
def shutdown(self):
if self.o.wfile:
self.wfile.fo.close()
return flow.FlowMaster.shutdown(self)
2011-02-16 09:10:24 +00:00
# begin nocover
def run(self):
if self.o.rfile and not self.o.keepserving:
if self.script:
self.load_script(None)
return
2011-02-16 09:10:24 +00:00
try:
return flow.FlowMaster.run(self)
except BaseException:
self.shutdown()
raise