Tweaks and cleanups for mitmdump.

This commit is contained in:
Aldo Cortesi 2011-02-16 23:03:46 +13:00
parent 8a9f20b78c
commit f5511350eb
3 changed files with 52 additions and 30 deletions

View File

@ -1,10 +1,21 @@
import sys
import flow
class Options(object):
__slots__ = [
"verbosity",
"wfile",
]
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
class DumpMaster(flow.FlowMaster):
def __init__(self, server, verbosity, outfile=sys.stderr):
self.verbosity, self.outfile = verbosity, outfile
def __init__(self, server, options, outfile=sys.stdout):
flow.FlowMaster.__init__(self, server, flow.State())
self.outfile = outfile
self.o = options
def handle_clientconnection(self, r):
flow.FlowMaster.handle_clientconnection(self, r)
@ -20,31 +31,30 @@ class DumpMaster(flow.FlowMaster):
def handle_response(self, msg):
f = flow.FlowMaster.handle_response(self, msg)
if not f:
return
if 0 < self.verbosity < 3:
print >> self.outfile, ">>",
print >> self.outfile, msg.request.short()
if self.verbosity == 1:
print >> self.outfile, "<<",
print >> self.outfile, msg.short()
elif self.verbosity == 2:
print >> self.outfile, "<<"
for i in msg.assemble().splitlines():
print >> self.outfile, "\t", i
print >> self.outfile, "<<"
elif self.verbosity == 3:
print >> self.outfile, ">>"
for i in msg.request.assemble().splitlines():
print >> self.outfile, "\t", i
print >> self.outfile, ">>"
print >> self.outfile, "<<"
for i in msg.assemble().splitlines():
print >> self.outfile, "\t", i
print >> self.outfile, "<<"
msg.ack()
self.state.delete_flow(f)
print [i.get_state() for i in self.state.flow_list]
if f:
if 0 < self.o.verbosity < 3:
print >> self.outfile, ">>",
print >> self.outfile, msg.request.short()
if self.o.verbosity == 1:
print >> self.outfile, "<<",
print >> self.outfile, msg.short()
elif self.o.verbosity == 2:
print >> self.outfile, "<<"
for i in msg.assemble().splitlines():
print >> self.outfile, "\t", i
print >> self.outfile, "<<"
elif self.o.verbosity == 3:
print >> self.outfile, ">>"
for i in msg.request.assemble().splitlines():
print >> self.outfile, "\t", i
print >> self.outfile, ">>"
print >> self.outfile, "<<"
for i in msg.assemble().splitlines():
print >> self.outfile, "\t", i
print >> self.outfile, "<<"
msg.ack()
self.state.delete_flow(f)
# begin nocover

View File

@ -76,7 +76,12 @@ if __name__ == '__main__':
ciphers = options.ciphers
)
server = proxy.ProxyServer(options.port)
m = dump.DumpMaster(server, options.verbose)
dumpopts = dump.Options(
verbosity = options.verbose
)
m = dump.DumpMaster(server, dumpopts)
if options.verbose > 0:
print >> sys.stderr, "Running on port %s"%options.port
m.run()

View File

@ -4,7 +4,6 @@ from libmproxy import dump
import utils
class uDumpMaster(libpry.AutoTree):
def _dummy_cycle(self, m):
req = utils.treq()
@ -14,10 +13,18 @@ class uDumpMaster(libpry.AutoTree):
m.handle_request(req)
m.handle_response(resp)
def test_options(self):
o = dump.Options(verbosity = 2)
assert o.verbosity == 2
libpry.raises(AttributeError, dump.Options, nonexistent = 2)
def test_basic_verbosities(self):
for i in (1, 2, 3):
cs = StringIO()
m = dump.DumpMaster(None, i, cs)
o = dump.Options(
verbosity = i
)
m = dump.DumpMaster(None, o, cs)
self._dummy_cycle(m)
assert "GET" in cs.getvalue()