1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
import sys, os
import flow, filt, utils
class DumpError(Exception): pass
class Options(object):
__slots__ = [
"anticache",
"anticomp",
"autodecode",
"client_replay",
"eventlog",
"keepserving",
"kill",
"no_server",
"refresh_server_playback",
"request_script",
"response_script",
"rfile",
"rheaders",
"server_replay",
"stickycookie",
"stickyauth",
"verbosity",
"wfile",
]
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
for i in self.__slots__:
if not hasattr(self, i):
setattr(self, i, None)
def str_response(resp):
r = "%s %s"%(resp.code, resp.msg)
if resp.is_replay():
r = "[replay] " + r
return r
def str_request(req):
if req.client_conn:
c = req.client_conn.address[0]
else:
c = "[replay]"
r = "%s %s %s"%(c, req.method, req.url())
if req.stickycookie:
r = "[stickycookie] " + r
return r
class DumpMaster(flow.FlowMaster):
def __init__(self, server, options, filtstr, outfile=sys.stdout):
flow.FlowMaster.__init__(self, server, flow.State())
self.outfile = outfile
self.o = options
self.anticache = options.anticache
self.anticomp = options.anticomp
self.autodecode = options.autodecode
self.eventlog = options.eventlog
self.refresh_server_playback = options.refresh_server_playback
if filtstr:
self.filt = filt.parse(filtstr)
else:
self.filt = None
if self.o.response_script:
self.set_response_script(self.o.response_script)
if self.o.request_script:
self.set_request_script(self.o.request_script)
if options.stickycookie:
self.set_stickycookie(options.stickycookie)
if options.stickyauth:
self.set_stickyauth(options.stickyauth)
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)
if options.server_replay:
self.start_server_playback(
self._readflow(options.server_replay),
options.kill, options.rheaders,
not options.keepserving
)
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)
self.load_flows(freader)
if options.client_replay:
self.start_client_playback(
self._readflow(options.client_replay),
not options.keepserving
)
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:
raise DumpError(v.strerror)
return flows
def _runscript(self, f, script):
try:
ret = f.run_script(script)
if self.o.verbosity > 0:
print >> self.outfile, ret
except flow.RunException, e:
if e.errout:
eout = "Script output:\n" + self.indent(4, e.errout) + "\n"
else:
eout = ""
raise DumpError(
"%s: %s\n%s"%(script, e.args[0], eout)
)
def handle_clientconnect(self, c):
if self.eventlog:
print >> self.outfile, "Connection from: %s:%s"%c.address
return flow.FlowMaster.handle_clientconnect(self, c)
def handle_clientdisconnect(self, c):
if self.eventlog:
print >> self.outfile, "Disconnect from: %s:%s"%tuple(c.client_conn.address),
print >> self.outfile, "(handled %s requests)"%c.client_conn.requestcount
if c.client_conn.connection_error:
print >> self.outfile, "\terror: %s"%c.client_conn.connection_error
return flow.FlowMaster.handle_clientconnect(self, c)
def handle_request(self, r):
f = flow.FlowMaster.handle_request(self, r)
if self.eventlog:
print >> self.outfile, "Request: %s"%str_request(r)
if f:
r.ack()
return f
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):
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):
f = flow.FlowMaster.handle_response(self, msg)
if f:
msg.ack()
self._process_flow(f)
return f
def handle_error(self, msg):
f = flow.FlowMaster.handle_error(self, msg)
if f:
msg.ack()
self._process_flow(f)
return f
# begin nocover
def run(self):
if self.o.rfile and not self.o.keepserving:
return
try:
return flow.FlowMaster.run(self)
except BaseException:
self.shutdown()
raise
|