2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2011-08-02 04:14:33 +00:00
|
|
|
import sys, os
|
2012-09-23 23:21:12 +00:00
|
|
|
import netlib.utils
|
2014-03-10 21:36:47 +00:00
|
|
|
from . 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):
|
2013-03-02 23:26:20 +00:00
|
|
|
attributes = [
|
2013-07-23 22:32:56 +00:00
|
|
|
"app",
|
2013-12-08 14:46:11 +00:00
|
|
|
"app_external",
|
|
|
|
"app_host",
|
|
|
|
"app_port",
|
2011-03-09 00:15:31 +00:00
|
|
|
"anticache",
|
2011-07-15 03:21:04 +00:00
|
|
|
"anticomp",
|
2011-03-09 00:15:31 +00:00
|
|
|
"client_replay",
|
|
|
|
"keepserving",
|
2011-02-20 22:08:35 +00:00
|
|
|
"kill",
|
2011-05-13 22:44:25 +00:00
|
|
|
"no_server",
|
2012-03-10 00:42:10 +00:00
|
|
|
"nopop",
|
2011-03-10 22:56:10 +00:00
|
|
|
"refresh_server_playback",
|
2012-03-17 04:20:34 +00:00
|
|
|
"replacements",
|
2011-05-14 23:54:12 +00:00
|
|
|
"rfile",
|
2011-03-09 00:15:31 +00:00
|
|
|
"rheaders",
|
2012-08-18 12:14:16 +00:00
|
|
|
"setheaders",
|
2011-03-05 00:03:26 +00:00
|
|
|
"server_replay",
|
2013-06-13 14:04:04 +00:00
|
|
|
"scripts",
|
2013-03-17 04:53:39 +00:00
|
|
|
"showhost",
|
2011-03-09 00:15:31 +00:00
|
|
|
"stickycookie",
|
2011-03-20 04:31:54 +00:00
|
|
|
"stickyauth",
|
2011-02-20 22:08:35 +00:00
|
|
|
"verbosity",
|
|
|
|
"wfile",
|
2011-02-16 10:03:46 +00:00
|
|
|
]
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
setattr(self, k, v)
|
2013-03-02 23:26:20 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2011-02-25 08:11:44 +00:00
|
|
|
def str_response(resp):
|
|
|
|
r = "%s %s"%(resp.code, resp.msg)
|
2014-01-31 03:45:39 +00:00
|
|
|
if resp.is_replay:
|
2011-02-25 08:11:44 +00:00
|
|
|
r = "[replay] " + r
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
2013-03-17 04:43:31 +00:00
|
|
|
def str_request(req, showhost):
|
2014-01-31 03:45:39 +00:00
|
|
|
if req.flow.client_conn:
|
|
|
|
c = req.flow.client_conn.address.host
|
2011-03-05 22:21:31 +00:00
|
|
|
else:
|
|
|
|
c = "[replay]"
|
2013-03-17 04:43:31 +00:00
|
|
|
r = "%s %s %s"%(c, req.method, req.get_url(showhost))
|
2011-02-25 08:23:44 +00:00
|
|
|
if req.stickycookie:
|
|
|
|
r = "[stickycookie] " + r
|
|
|
|
return r
|
2011-02-25 08:11:44 +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
|
2011-05-15 00:23:34 +00:00
|
|
|
self.anticache = options.anticache
|
2011-07-15 03:21:04 +00:00
|
|
|
self.anticomp = options.anticomp
|
2013-03-17 04:43:31 +00:00
|
|
|
self.showhost = options.showhost
|
2011-05-15 00:23:34 +00:00
|
|
|
self.refresh_server_playback = options.refresh_server_playback
|
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-25 04:32:03 +00:00
|
|
|
if options.stickycookie:
|
|
|
|
self.set_stickycookie(options.stickycookie)
|
|
|
|
|
2011-03-20 04:31:54 +00:00
|
|
|
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")
|
2013-03-13 20:19:43 +00:00
|
|
|
self.start_stream(f, self.filt)
|
2011-02-16 21:18:38 +00:00
|
|
|
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)
|
2012-03-17 04:20:34 +00:00
|
|
|
|
2012-08-18 12:14:16 +00:00
|
|
|
if options.setheaders:
|
|
|
|
for i in options.setheaders:
|
|
|
|
self.setheaders.add(*i)
|
|
|
|
|
2011-03-05 00:03:26 +00:00
|
|
|
if options.server_replay:
|
2011-03-05 22:21:31 +00:00
|
|
|
self.start_server_playback(
|
|
|
|
self._readflow(options.server_replay),
|
2011-03-06 04:08:56 +00:00
|
|
|
options.kill, options.rheaders,
|
2012-03-10 00:42:10 +00:00
|
|
|
not options.keepserving,
|
|
|
|
options.nopop
|
2011-03-05 22:21:31 +00:00
|
|
|
)
|
2011-02-20 20:54:39 +00:00
|
|
|
|
2011-03-05 22:21:31 +00:00
|
|
|
if options.client_replay:
|
2011-03-06 03:54:49 +00:00
|
|
|
self.start_client_playback(
|
|
|
|
self._readflow(options.client_replay),
|
|
|
|
not options.keepserving
|
|
|
|
)
|
2011-03-05 22:21:31 +00:00
|
|
|
|
2013-06-17 14:48:06 +00:00
|
|
|
scripts = options.scripts or []
|
2014-01-12 10:01:59 +00:00
|
|
|
for command in scripts:
|
|
|
|
err = self.load_script(command)
|
2011-08-03 04:36:20 +00:00
|
|
|
if err:
|
|
|
|
raise DumpError(err)
|
2011-03-09 00:15:31 +00:00
|
|
|
|
2011-08-04 21:41:29 +00:00
|
|
|
if options.rfile:
|
|
|
|
path = os.path.expanduser(options.rfile)
|
|
|
|
try:
|
2012-07-24 01:06:12 +00:00
|
|
|
f = file(path, "rb")
|
2011-08-04 21:41:29 +00:00
|
|
|
freader = flow.FlowReader(f)
|
|
|
|
except IOError, v:
|
|
|
|
raise DumpError(v.strerror)
|
2012-02-08 23:09:40 +00:00
|
|
|
try:
|
|
|
|
self.load_flows(freader)
|
|
|
|
except flow.FlowReadError, v:
|
2012-07-24 03:15:41 +00:00
|
|
|
self.add_event("Flow file corrupted. Stopped loading.")
|
2011-08-04 21:41:29 +00:00
|
|
|
|
2013-07-23 22:32:56 +00:00
|
|
|
if self.o.app:
|
2013-08-18 18:03:53 +00:00
|
|
|
self.start_app(self.o.app_host, self.o.app_port, self.o.app_external)
|
2013-07-23 22:32:56 +00:00
|
|
|
|
2011-03-05 22:21:31 +00:00
|
|
|
def _readflow(self, path):
|
|
|
|
path = os.path.expanduser(path)
|
|
|
|
try:
|
2013-06-15 22:23:44 +00:00
|
|
|
f = file(path, "rb")
|
2011-03-05 22:21:31 +00:00
|
|
|
flows = list(flow.FlowReader(f).stream())
|
2011-03-11 02:16:31 +00:00
|
|
|
except (IOError, flow.FlowReadError), v:
|
2011-03-05 22:21:31 +00:00
|
|
|
raise DumpError(v.strerror)
|
|
|
|
return flows
|
2011-02-25 04:32:03 +00:00
|
|
|
|
2011-08-03 01:33:18 +00:00
|
|
|
def add_event(self, e, level="info"):
|
2014-03-12 21:13:00 +00:00
|
|
|
#FIXME refactor_loglevel
|
|
|
|
print >> self.outfile, e
|
|
|
|
self.outfile.flush()
|
2011-07-23 00:57:54 +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)
|
|
|
|
|
2011-03-11 00:06:51 +00:00
|
|
|
def _process_flow(self, f):
|
2013-03-13 20:19:43 +00:00
|
|
|
self.state.delete_flow(f)
|
2011-03-11 00:06:51 +00:00
|
|
|
if self.filt and not f.match(self.filt):
|
2011-08-02 04:52:47 +00:00
|
|
|
return
|
2011-03-11 00:06:51 +00:00
|
|
|
|
|
|
|
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):
|
2012-09-23 23:21:12 +00:00
|
|
|
d = netlib.utils.hexdump(f.response.content)
|
2011-03-11 00:06:51 +00:00
|
|
|
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:
|
2013-03-17 04:43:31 +00:00
|
|
|
print >> self.outfile, str_request(f.request, self.showhost)
|
2011-03-11 00:06:51 +00:00
|
|
|
print >> self.outfile, result
|
|
|
|
elif self.o.verbosity == 2:
|
2013-03-17 04:43:31 +00:00
|
|
|
print >> self.outfile, str_request(f.request, self.showhost)
|
2011-03-11 00:06:51 +00:00
|
|
|
print >> self.outfile, self.indent(4, f.request.headers)
|
|
|
|
print >> self.outfile
|
|
|
|
print >> self.outfile, result
|
|
|
|
print >> self.outfile, "\n"
|
2011-07-22 23:50:30 +00:00
|
|
|
elif self.o.verbosity >= 3:
|
2013-03-17 04:43:31 +00:00
|
|
|
print >> self.outfile, str_request(f.request, self.showhost)
|
2011-03-11 00:06:51 +00:00
|
|
|
print >> self.outfile, self.indent(4, f.request.headers)
|
|
|
|
if utils.isBin(f.request.content):
|
2012-09-23 23:21:12 +00:00
|
|
|
print >> self.outfile, self.indent(4, netlib.utils.hexdump(f.request.content))
|
2011-03-11 00:06:51 +00:00
|
|
|
elif f.request.content:
|
|
|
|
print >> self.outfile, self.indent(4, f.request.content)
|
|
|
|
print >> self.outfile
|
|
|
|
print >> self.outfile, result
|
|
|
|
print >> self.outfile, "\n"
|
2012-06-30 21:37:03 +00:00
|
|
|
if self.o.verbosity:
|
|
|
|
self.outfile.flush()
|
2011-03-11 00:06:51 +00:00
|
|
|
|
2013-02-16 23:42:48 +00:00
|
|
|
def handle_log(self, l):
|
|
|
|
self.add_event(l.msg)
|
|
|
|
l.reply()
|
|
|
|
|
|
|
|
def handle_request(self, r):
|
|
|
|
f = flow.FlowMaster.handle_request(self, r)
|
|
|
|
if f:
|
|
|
|
r.reply()
|
|
|
|
return f
|
|
|
|
|
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:
|
2013-02-16 23:42:48 +00:00
|
|
|
msg.reply()
|
2011-03-11 00:06:51 +00:00
|
|
|
self._process_flow(f)
|
2011-03-05 22:21:31 +00:00
|
|
|
return f
|
2011-02-16 21:18:38 +00:00
|
|
|
|
2011-03-11 00:06:51 +00:00
|
|
|
def handle_error(self, msg):
|
|
|
|
f = flow.FlowMaster.handle_error(self, msg)
|
|
|
|
if f:
|
|
|
|
self._process_flow(f)
|
|
|
|
return f
|
|
|
|
|
2012-06-09 01:55:55 +00:00
|
|
|
def shutdown(self): # pragma: no cover
|
2012-04-14 22:35:19 +00:00
|
|
|
return flow.FlowMaster.shutdown(self)
|
|
|
|
|
2012-06-09 01:55:55 +00:00
|
|
|
def run(self): # pragma: no cover
|
2011-05-14 23:54:12 +00:00
|
|
|
if self.o.rfile and not self.o.keepserving:
|
2014-02-04 17:40:12 +00:00
|
|
|
self.shutdown()
|
2011-05-14 23:54:12 +00:00
|
|
|
return
|
2011-02-16 09:10:24 +00:00
|
|
|
try:
|
|
|
|
return flow.FlowMaster.run(self)
|
2011-08-02 04:14:33 +00:00
|
|
|
except BaseException:
|
2011-02-17 23:40:45 +00:00
|
|
|
self.shutdown()
|
|
|
|
raise
|