2011-02-17 01:26:50 +00:00
|
|
|
import sys, os, traceback
|
|
|
|
import flow, filt, utils
|
2010-03-01 01:58:04 +00:00
|
|
|
|
2011-02-16 21:18:38 +00:00
|
|
|
class DumpError(Exception): pass
|
|
|
|
|
|
|
|
|
2011-02-16 10:03:46 +00:00
|
|
|
class Options(object):
|
|
|
|
__slots__ = [
|
|
|
|
"verbosity",
|
|
|
|
"wfile",
|
|
|
|
]
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2010-03-01 01:58:04 +00:00
|
|
|
|
2011-02-16 21:44:08 +00:00
|
|
|
if filtstr:
|
|
|
|
self.filt = filt.parse(filtstr)
|
|
|
|
else:
|
|
|
|
self.filt = None
|
|
|
|
|
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)
|
|
|
|
|
2011-02-16 09:10:24 +00:00
|
|
|
def handle_clientconnection(self, r):
|
|
|
|
flow.FlowMaster.handle_clientconnection(self, r)
|
|
|
|
r.ack()
|
|
|
|
|
|
|
|
def handle_error(self, r):
|
|
|
|
flow.FlowMaster.handle_error(self, r)
|
|
|
|
r.ack()
|
|
|
|
|
|
|
|
def handle_request(self, r):
|
|
|
|
flow.FlowMaster.handle_request(self, r)
|
|
|
|
r.ack()
|
2010-03-01 01:58:04 +00:00
|
|
|
|
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)
|
|
|
|
|
2010-03-01 01:58:04 +00:00
|
|
|
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-02-16 21:44:08 +00:00
|
|
|
msg.ack()
|
|
|
|
if self.filt and not f.match(self.filt):
|
|
|
|
return
|
2011-02-16 10:03:46 +00:00
|
|
|
if self.o.verbosity == 1:
|
2011-02-17 01:26:50 +00:00
|
|
|
print >> self.outfile, f.client_conn.address[0],
|
|
|
|
print >> self.outfile, f.request.short()
|
|
|
|
print >> self.outfile, " <<",
|
|
|
|
print >> self.outfile, f.response.short(), utils.pretty_size(len(f.response.content))
|
2011-02-16 10:03:46 +00:00
|
|
|
elif self.o.verbosity == 2:
|
2011-02-17 01:26:50 +00:00
|
|
|
print >> self.outfile, f.client_conn.address[0],
|
|
|
|
print >> self.outfile, f.request.short()
|
|
|
|
print >> self.outfile, self.indent(4, f.request.headers)
|
2011-02-17 02:10:50 +00:00
|
|
|
print >> self.outfile
|
2011-02-17 01:26:50 +00:00
|
|
|
print >> self.outfile, " <<", f.response.short()
|
|
|
|
print >> self.outfile, self.indent(4, f.response.headers)
|
2011-02-17 02:10:50 +00:00
|
|
|
print >> self.outfile, "\n\n"
|
2011-02-16 10:03:46 +00:00
|
|
|
elif self.o.verbosity == 3:
|
2011-02-17 02:10:50 +00:00
|
|
|
print >> self.outfile, f.client_conn.address[0],
|
|
|
|
print >> self.outfile, f.request.short()
|
|
|
|
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, " <<", f.response.short()
|
|
|
|
print >> self.outfile, self.indent(4, f.response.headers)
|
|
|
|
if utils.isBin(f.response.content):
|
|
|
|
print >> self.outfile, self.indent(4, utils.hexdump(f.response.content))
|
|
|
|
elif f.response.content:
|
|
|
|
print >> self.outfile, self.indent(4, f.response.content)
|
|
|
|
print >> self.outfile, "\n\n"
|
2011-02-16 10:03:46 +00:00
|
|
|
self.state.delete_flow(f)
|
2011-02-16 21:18:38 +00:00
|
|
|
if self.o.wfile:
|
|
|
|
self.fwriter.add(f)
|
|
|
|
|
2011-02-16 09:10:24 +00:00
|
|
|
# begin nocover
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
return flow.FlowMaster.run(self)
|
|
|
|
except KeyboardInterrupt:
|
2011-02-17 01:26:50 +00:00
|
|
|
pass
|
|
|
|
except Exception, v:
|
|
|
|
traceback.print_exc()
|
|
|
|
self.shutdown()
|