2011-01-25 02:02:48 +00:00
|
|
|
"""
|
|
|
|
This module provides more sophisticated flow tracking. These match requests
|
|
|
|
with their responses, and provide filtering and interception facilities.
|
|
|
|
"""
|
2011-02-23 21:33:39 +00:00
|
|
|
import subprocess, base64, sys, json, hashlib, Cookie, cookielib, copy
|
2011-02-24 02:15:51 +00:00
|
|
|
import proxy, threading, netstring, filt
|
2011-03-08 23:35:38 +00:00
|
|
|
import controller, version
|
2011-01-25 02:02:48 +00:00
|
|
|
|
2011-02-01 22:44:28 +00:00
|
|
|
class RunException(Exception):
|
|
|
|
def __init__(self, msg, returncode, errout):
|
|
|
|
Exception.__init__(self, msg)
|
|
|
|
self.returncode = returncode
|
|
|
|
self.errout = errout
|
|
|
|
|
2011-01-31 00:26:56 +00:00
|
|
|
|
2011-01-27 01:19:48 +00:00
|
|
|
# begin nocover
|
2011-02-20 19:47:19 +00:00
|
|
|
class RequestReplayThread(threading.Thread):
|
2011-01-25 02:02:48 +00:00
|
|
|
def __init__(self, flow, masterq):
|
|
|
|
self.flow, self.masterq = flow, masterq
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
server = proxy.ServerConnection(self.flow.request)
|
2011-02-16 06:37:40 +00:00
|
|
|
server.send_request(self.flow.request)
|
2011-01-25 02:02:48 +00:00
|
|
|
response = server.read_response()
|
|
|
|
response.send(self.masterq)
|
|
|
|
except proxy.ProxyError, v:
|
2011-03-04 00:08:43 +00:00
|
|
|
err = proxy.Error(self.flow.request, v.msg)
|
2011-01-25 02:02:48 +00:00
|
|
|
err.send(self.masterq)
|
2011-01-27 01:19:48 +00:00
|
|
|
# end nocover
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
|
2011-03-04 00:08:43 +00:00
|
|
|
class ClientPlaybackState:
|
2011-03-06 03:54:49 +00:00
|
|
|
def __init__(self, flows, exit):
|
|
|
|
self.flows, self.exit = flows, exit
|
2011-03-04 00:08:43 +00:00
|
|
|
self.current = None
|
|
|
|
|
|
|
|
def count(self):
|
|
|
|
return len(self.flows)
|
|
|
|
|
2011-03-06 03:54:49 +00:00
|
|
|
def done(self):
|
|
|
|
if len(self.flows) == 0 and not self.current:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2011-03-04 00:08:43 +00:00
|
|
|
def clear(self, flow):
|
|
|
|
"""
|
|
|
|
A request has returned in some way - if this is the one we're
|
|
|
|
servicing, go to the next flow.
|
|
|
|
"""
|
|
|
|
if flow is self.current:
|
|
|
|
self.current = None
|
|
|
|
|
|
|
|
def tick(self, master, testing=False):
|
|
|
|
"""
|
|
|
|
testing: Disables actual replay for testing.
|
|
|
|
"""
|
|
|
|
if self.flows and not self.current:
|
2011-03-05 22:21:31 +00:00
|
|
|
n = self.flows.pop(0)
|
2011-03-06 03:02:28 +00:00
|
|
|
n.request.client_conn = None
|
2011-03-05 22:21:31 +00:00
|
|
|
self.current = master.handle_request(n.request)
|
2011-03-06 01:48:16 +00:00
|
|
|
if not testing and not self.current.response:
|
2011-03-04 00:08:43 +00:00
|
|
|
#begin nocover
|
2011-03-15 02:11:03 +00:00
|
|
|
master.replay_request(self.current)
|
2011-03-04 00:08:43 +00:00
|
|
|
#end nocover
|
2011-03-06 01:48:16 +00:00
|
|
|
elif self.current.response:
|
|
|
|
master.handle_response(self.current.response)
|
2011-03-04 00:08:43 +00:00
|
|
|
|
|
|
|
|
2011-02-20 19:47:19 +00:00
|
|
|
class ServerPlaybackState:
|
2011-03-06 04:08:56 +00:00
|
|
|
def __init__(self, headers, flows, exit):
|
2011-02-22 21:54:51 +00:00
|
|
|
"""
|
|
|
|
headers: A case-insensitive list of request headers that should be
|
|
|
|
included in request-response matching.
|
|
|
|
"""
|
2011-03-06 04:08:56 +00:00
|
|
|
self.headers, self.exit = headers, exit
|
2011-02-20 19:47:19 +00:00
|
|
|
self.fmap = {}
|
|
|
|
for i in flows:
|
2011-02-20 22:08:35 +00:00
|
|
|
if i.response:
|
|
|
|
h = self._hash(i)
|
|
|
|
l = self.fmap.setdefault(self._hash(i), [])
|
|
|
|
l.append(i)
|
2011-02-20 19:47:19 +00:00
|
|
|
|
2011-03-04 00:08:43 +00:00
|
|
|
def count(self):
|
|
|
|
return sum([len(i) for i in self.fmap.values()])
|
|
|
|
|
2011-02-20 19:47:19 +00:00
|
|
|
def _hash(self, flow):
|
|
|
|
"""
|
|
|
|
Calculates a loose hash of the flow request.
|
|
|
|
"""
|
|
|
|
r = flow.request
|
|
|
|
key = [
|
|
|
|
str(r.host),
|
|
|
|
str(r.port),
|
|
|
|
str(r.scheme),
|
|
|
|
str(r.method),
|
|
|
|
str(r.path),
|
|
|
|
str(r.content),
|
|
|
|
]
|
2011-02-22 21:54:51 +00:00
|
|
|
if self.headers:
|
|
|
|
hdrs = []
|
|
|
|
for i in self.headers:
|
|
|
|
v = r.headers.get(i, [])
|
|
|
|
# Slightly subtle: we need to convert everything to strings
|
|
|
|
# to prevent a mismatch between unicode/non-unicode.
|
|
|
|
v = [str(x) for x in v]
|
|
|
|
hdrs.append((i, v))
|
|
|
|
key.append(repr(hdrs))
|
2011-02-20 19:47:19 +00:00
|
|
|
return hashlib.sha256(repr(key)).digest()
|
|
|
|
|
|
|
|
def next_flow(self, request):
|
|
|
|
"""
|
|
|
|
Returns the next flow object, or None if no matching flow was
|
|
|
|
found.
|
|
|
|
"""
|
|
|
|
l = self.fmap.get(self._hash(request))
|
|
|
|
if not l:
|
|
|
|
return None
|
|
|
|
return l.pop(0)
|
|
|
|
|
|
|
|
|
2011-02-23 21:33:39 +00:00
|
|
|
class StickyCookieState:
|
2011-02-24 02:15:51 +00:00
|
|
|
def __init__(self, flt):
|
|
|
|
"""
|
|
|
|
flt: A compiled filter.
|
|
|
|
"""
|
2011-02-23 21:33:39 +00:00
|
|
|
self.jar = {}
|
2011-02-24 02:15:51 +00:00
|
|
|
self.flt = flt
|
2011-02-23 21:33:39 +00:00
|
|
|
|
2011-02-24 02:15:51 +00:00
|
|
|
def ckey(self, m, f):
|
|
|
|
"""
|
|
|
|
Returns a (domain, port, path) tuple.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
m["domain"] or f.request.host,
|
|
|
|
f.request.port,
|
|
|
|
m["path"] or "/"
|
|
|
|
)
|
2011-02-23 21:33:39 +00:00
|
|
|
|
2011-02-24 02:15:51 +00:00
|
|
|
def handle_response(self, f):
|
|
|
|
for i in f.response.headers.get("set-cookie", []):
|
2011-03-10 22:56:10 +00:00
|
|
|
# FIXME: We now know that Cookie.py screws up some cookies with
|
|
|
|
# valid RFC 822/1123 datetime specifications for expiry. Sigh.
|
2011-02-23 21:33:39 +00:00
|
|
|
c = Cookie.SimpleCookie(i)
|
|
|
|
m = c.values()[0]
|
2011-02-24 02:15:51 +00:00
|
|
|
k = self.ckey(m, f)
|
|
|
|
if cookielib.domain_match(f.request.host, k[0]):
|
|
|
|
self.jar[self.ckey(m, f)] = m
|
|
|
|
|
|
|
|
def handle_request(self, f):
|
|
|
|
if f.match(self.flt):
|
|
|
|
cs = []
|
|
|
|
for i in self.jar.keys():
|
|
|
|
match = [
|
|
|
|
cookielib.domain_match(i[0], f.request.host),
|
|
|
|
f.request.port == i[1],
|
|
|
|
f.request.path.startswith(i[2])
|
|
|
|
]
|
|
|
|
if all(match):
|
|
|
|
l = f.request.headers.setdefault("cookie", [])
|
2011-02-25 08:23:44 +00:00
|
|
|
f.request.stickycookie = True
|
2011-02-24 02:15:51 +00:00
|
|
|
l.append(self.jar[i].output(header="").strip())
|
2011-02-23 21:33:39 +00:00
|
|
|
|
|
|
|
|
2011-03-20 04:31:54 +00:00
|
|
|
class StickyAuthState:
|
|
|
|
def __init__(self, flt):
|
|
|
|
"""
|
|
|
|
flt: A compiled filter.
|
|
|
|
"""
|
|
|
|
self.flt = flt
|
|
|
|
self.hosts = {}
|
|
|
|
|
|
|
|
def handle_request(self, f):
|
|
|
|
if "authorization" in f.request.headers:
|
|
|
|
self.hosts[f.request.host] = f.request.headers["authorization"]
|
|
|
|
elif f.match(self.flt):
|
|
|
|
if f.request.host in self.hosts:
|
|
|
|
f.request.headers["authorization"] = self.hosts[f.request.host]
|
|
|
|
|
2011-02-23 21:33:39 +00:00
|
|
|
|
2011-01-25 02:02:48 +00:00
|
|
|
class Flow:
|
2011-02-19 04:00:24 +00:00
|
|
|
def __init__(self, request):
|
|
|
|
self.request = request
|
|
|
|
self.response, self.error = None, None
|
2011-01-25 02:02:48 +00:00
|
|
|
self.intercepting = False
|
|
|
|
self._backup = None
|
|
|
|
|
2011-03-06 03:11:45 +00:00
|
|
|
@classmethod
|
|
|
|
def from_state(klass, state):
|
|
|
|
f = klass(None)
|
|
|
|
f.load_state(state)
|
|
|
|
return f
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.get_state() == other.get_state()
|
|
|
|
|
2011-01-31 00:26:56 +00:00
|
|
|
def script_serialize(self):
|
|
|
|
data = self.get_state()
|
2011-02-16 02:10:00 +00:00
|
|
|
return json.dumps(data)
|
2011-01-31 00:26:56 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def script_deserialize(klass, data):
|
|
|
|
try:
|
2011-02-16 02:10:00 +00:00
|
|
|
data = json.loads(data)
|
2011-01-31 00:26:56 +00:00
|
|
|
except Exception:
|
|
|
|
return None
|
|
|
|
return klass.from_state(data)
|
|
|
|
|
|
|
|
def run_script(self, path):
|
2011-01-30 22:44:52 +00:00
|
|
|
"""
|
2011-02-01 21:08:24 +00:00
|
|
|
Run a script on a flow.
|
2011-01-31 00:26:56 +00:00
|
|
|
|
2011-02-01 21:08:24 +00:00
|
|
|
Returns a (flow, stderr output) tuple, or raises RunException if
|
|
|
|
there's an error.
|
2011-01-30 22:44:52 +00:00
|
|
|
"""
|
2011-02-01 22:44:28 +00:00
|
|
|
self.backup()
|
2011-01-31 00:26:56 +00:00
|
|
|
data = self.script_serialize()
|
|
|
|
try:
|
2011-02-01 21:08:24 +00:00
|
|
|
p = subprocess.Popen(
|
|
|
|
[path],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
2011-01-31 00:26:56 +00:00
|
|
|
except OSError, e:
|
2011-02-01 22:44:28 +00:00
|
|
|
raise RunException(e.args[1], None, None)
|
2011-01-31 00:26:56 +00:00
|
|
|
so, se = p.communicate(data)
|
|
|
|
if p.returncode:
|
2011-02-01 22:44:28 +00:00
|
|
|
raise RunException(
|
|
|
|
"Script returned error code %s"%p.returncode,
|
|
|
|
p.returncode,
|
|
|
|
se
|
|
|
|
)
|
2011-01-31 00:26:56 +00:00
|
|
|
f = Flow.script_deserialize(so)
|
|
|
|
if not f:
|
2011-02-01 22:44:28 +00:00
|
|
|
raise RunException(
|
|
|
|
"Invalid response from script.",
|
|
|
|
p.returncode,
|
|
|
|
se
|
|
|
|
)
|
2011-02-17 23:40:45 +00:00
|
|
|
self.load_state(f.get_state())
|
|
|
|
return se
|
2011-01-30 22:44:52 +00:00
|
|
|
|
2011-02-05 21:28:43 +00:00
|
|
|
def get_state(self, nobackup=False):
|
|
|
|
d = dict(
|
2011-01-26 01:52:03 +00:00
|
|
|
request = self.request.get_state() if self.request else None,
|
|
|
|
response = self.response.get_state() if self.response else None,
|
|
|
|
error = self.error.get_state() if self.error else None,
|
2011-03-08 23:35:38 +00:00
|
|
|
version = version.IVERSION
|
2011-01-26 01:52:03 +00:00
|
|
|
)
|
2011-02-05 21:28:43 +00:00
|
|
|
if nobackup:
|
|
|
|
d["backup"] = None
|
|
|
|
else:
|
|
|
|
d["backup"] = self._backup
|
|
|
|
return d
|
2011-01-26 01:52:03 +00:00
|
|
|
|
2011-01-31 00:26:56 +00:00
|
|
|
def load_state(self, state):
|
2011-02-05 21:28:43 +00:00
|
|
|
self._backup = state["backup"]
|
2011-02-19 20:36:13 +00:00
|
|
|
if self.request:
|
|
|
|
self.request.load_state(state["request"])
|
|
|
|
else:
|
|
|
|
self.request = proxy.Request.from_state(state["request"])
|
|
|
|
|
2011-01-26 01:52:03 +00:00
|
|
|
if state["response"]:
|
2011-02-19 20:36:13 +00:00
|
|
|
if self.response:
|
|
|
|
self.response.load_state(state["response"])
|
|
|
|
else:
|
|
|
|
self.response = proxy.Response.from_state(self.request, state["response"])
|
|
|
|
else:
|
|
|
|
self.response = None
|
|
|
|
|
2011-01-26 01:52:03 +00:00
|
|
|
if state["error"]:
|
2011-02-19 20:36:13 +00:00
|
|
|
if self.error:
|
|
|
|
self.error.load_state(state["error"])
|
|
|
|
else:
|
|
|
|
self.error = proxy.Error.from_state(state["error"])
|
|
|
|
else:
|
|
|
|
self.error = None
|
2011-01-31 00:26:56 +00:00
|
|
|
|
2011-02-01 22:44:28 +00:00
|
|
|
def modified(self):
|
|
|
|
# FIXME: Save a serialization in backup, compare current with
|
|
|
|
# backup to detect if flow has _really_ been modified.
|
|
|
|
if self._backup:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2011-01-25 02:02:48 +00:00
|
|
|
def backup(self):
|
2011-02-05 21:28:43 +00:00
|
|
|
self._backup = self.get_state(nobackup=True)
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def revert(self):
|
|
|
|
if self._backup:
|
2011-02-05 21:28:43 +00:00
|
|
|
self.load_state(self._backup)
|
2011-02-01 22:44:28 +00:00
|
|
|
self._backup = None
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def match(self, pattern):
|
|
|
|
if pattern:
|
|
|
|
if self.response:
|
|
|
|
return pattern(self.response)
|
|
|
|
elif self.request:
|
|
|
|
return pattern(self.request)
|
|
|
|
return False
|
|
|
|
|
2011-03-13 03:50:11 +00:00
|
|
|
def kill(self, master):
|
2011-03-11 00:06:51 +00:00
|
|
|
self.error = proxy.Error(self.request, "Connection killed")
|
2011-01-27 00:32:24 +00:00
|
|
|
if self.request and not self.request.acked:
|
2011-02-03 22:39:28 +00:00
|
|
|
self.request.ack(None)
|
2011-01-27 00:32:24 +00:00
|
|
|
elif self.response and not self.response.acked:
|
2011-02-03 22:39:28 +00:00
|
|
|
self.response.ack(None)
|
2011-03-13 03:50:11 +00:00
|
|
|
master.handle_error(self.error)
|
2011-03-15 04:53:29 +00:00
|
|
|
self.intercepting = False
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def intercept(self):
|
|
|
|
self.intercepting = True
|
|
|
|
|
|
|
|
def accept_intercept(self):
|
|
|
|
if self.request:
|
|
|
|
if not self.request.acked:
|
|
|
|
self.request.ack()
|
|
|
|
elif self.response and not self.response.acked:
|
|
|
|
self.response.ack()
|
|
|
|
self.intercepting = False
|
|
|
|
|
|
|
|
|
|
|
|
class State:
|
|
|
|
def __init__(self):
|
2011-02-19 04:00:24 +00:00
|
|
|
self.client_connections = []
|
2011-01-25 02:02:48 +00:00
|
|
|
self.flow_map = {}
|
|
|
|
self.flow_list = []
|
2011-02-19 04:00:24 +00:00
|
|
|
|
2011-01-25 02:02:48 +00:00
|
|
|
# These are compiled filt expressions:
|
|
|
|
self.limit = None
|
|
|
|
self.intercept = None
|
2011-03-12 22:24:49 +00:00
|
|
|
self.limit_txt = None
|
2011-01-25 02:02:48 +00:00
|
|
|
|
2011-03-06 03:11:45 +00:00
|
|
|
def flow_count(self):
|
|
|
|
return len(self.flow_map)
|
|
|
|
|
|
|
|
def active_flow_count(self):
|
|
|
|
c = 0
|
|
|
|
for i in self.flow_list:
|
|
|
|
if not i.response and not i.error:
|
|
|
|
c += 1
|
|
|
|
return c
|
|
|
|
|
2011-02-19 04:00:24 +00:00
|
|
|
def clientconnect(self, cc):
|
|
|
|
self.client_connections.append(cc)
|
|
|
|
|
|
|
|
def clientdisconnect(self, dc):
|
2011-01-25 02:02:48 +00:00
|
|
|
"""
|
|
|
|
Start a browser connection.
|
|
|
|
"""
|
2011-02-19 04:00:24 +00:00
|
|
|
self.client_connections.remove(dc.client_conn)
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def add_request(self, req):
|
|
|
|
"""
|
|
|
|
Add a request to the state. Returns the matching flow.
|
|
|
|
"""
|
2011-02-19 04:00:24 +00:00
|
|
|
f = Flow(req)
|
2011-03-19 20:31:39 +00:00
|
|
|
self.flow_list.append(f)
|
2011-02-19 04:00:24 +00:00
|
|
|
self.flow_map[req] = f
|
2011-01-25 02:02:48 +00:00
|
|
|
return f
|
|
|
|
|
|
|
|
def add_response(self, resp):
|
|
|
|
"""
|
|
|
|
Add a response to the state. Returns the matching flow.
|
|
|
|
"""
|
2011-02-19 04:00:24 +00:00
|
|
|
f = self.flow_map.get(resp.request)
|
2011-01-25 02:02:48 +00:00
|
|
|
if not f:
|
|
|
|
return False
|
|
|
|
f.response = resp
|
|
|
|
return f
|
|
|
|
|
|
|
|
def add_error(self, err):
|
|
|
|
"""
|
|
|
|
Add an error response to the state. Returns the matching flow, or
|
|
|
|
None if there isn't one.
|
|
|
|
"""
|
2011-02-20 00:29:41 +00:00
|
|
|
f = self.flow_map.get(err.request) if err.request else None
|
2011-01-25 02:02:48 +00:00
|
|
|
if not f:
|
|
|
|
return None
|
|
|
|
f.error = err
|
|
|
|
return f
|
|
|
|
|
2011-02-16 02:10:00 +00:00
|
|
|
def load_flows(self, flows):
|
|
|
|
self.flow_list.extend(flows)
|
|
|
|
for i in flows:
|
2011-02-19 04:00:24 +00:00
|
|
|
self.flow_map[i.request] = i
|
2011-01-26 03:50:17 +00:00
|
|
|
|
2011-03-12 22:24:49 +00:00
|
|
|
def set_limit(self, txt):
|
|
|
|
if txt:
|
|
|
|
f = filt.parse(txt)
|
|
|
|
if not f:
|
|
|
|
return "Invalid filter expression."
|
|
|
|
self.limit = f
|
|
|
|
self.limit_txt = txt
|
|
|
|
else:
|
|
|
|
self.limit = None
|
|
|
|
self.limit_txt = None
|
|
|
|
|
|
|
|
def set_intercept(self, txt):
|
|
|
|
if txt:
|
|
|
|
f = filt.parse(txt)
|
|
|
|
if not f:
|
|
|
|
return "Invalid filter expression."
|
|
|
|
self.intercept = f
|
|
|
|
self.intercept_txt = txt
|
|
|
|
else:
|
|
|
|
self.intercept = None
|
|
|
|
self.intercept_txt = None
|
2011-01-25 02:02:48 +00:00
|
|
|
|
2011-01-26 03:50:17 +00:00
|
|
|
@property
|
|
|
|
def view(self):
|
|
|
|
if self.limit:
|
|
|
|
return tuple([i for i in self.flow_list if i.match(self.limit)])
|
|
|
|
else:
|
|
|
|
return tuple(self.flow_list[:])
|
|
|
|
|
2011-01-25 02:02:48 +00:00
|
|
|
def delete_flow(self, f):
|
2011-03-15 04:53:29 +00:00
|
|
|
if f.request in self.flow_map:
|
|
|
|
del self.flow_map[f.request]
|
|
|
|
self.flow_list.remove(f)
|
|
|
|
return True
|
2011-01-25 02:02:48 +00:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
for i in self.flow_list[:]:
|
|
|
|
self.delete_flow(i)
|
|
|
|
|
|
|
|
def accept_all(self):
|
|
|
|
for i in self.flow_list[:]:
|
|
|
|
i.accept_intercept()
|
|
|
|
|
|
|
|
def revert(self, f):
|
|
|
|
f.revert()
|
|
|
|
|
2011-02-16 01:33:04 +00:00
|
|
|
|
|
|
|
|
2011-02-16 03:03:22 +00:00
|
|
|
class FlowMaster(controller.Master):
|
|
|
|
def __init__(self, server, state):
|
|
|
|
controller.Master.__init__(self, server)
|
|
|
|
self.state = state
|
2011-03-05 00:03:26 +00:00
|
|
|
self.server_playback = None
|
|
|
|
self.client_playback = None
|
2011-02-20 22:40:49 +00:00
|
|
|
self.scripts = {}
|
|
|
|
self.kill_nonreplay = False
|
2011-03-13 02:55:47 +00:00
|
|
|
|
2011-02-24 02:15:51 +00:00
|
|
|
self.stickycookie_state = False
|
2011-03-13 02:55:47 +00:00
|
|
|
self.stickycookie_txt = None
|
2011-03-10 22:56:10 +00:00
|
|
|
|
2011-03-20 04:31:54 +00:00
|
|
|
self.stickyauth_state = False
|
|
|
|
self.stickyauth_txt = None
|
|
|
|
|
2011-03-09 00:15:31 +00:00
|
|
|
self.anticache = False
|
2011-03-10 22:56:10 +00:00
|
|
|
self.refresh_server_playback = False
|
2011-02-20 20:54:39 +00:00
|
|
|
|
2011-02-20 22:40:49 +00:00
|
|
|
def _runscript(self, f, script):
|
2011-02-24 02:44:08 +00:00
|
|
|
#begin nocover
|
|
|
|
raise NotImplementedError
|
|
|
|
#end nocover
|
2011-02-20 20:54:39 +00:00
|
|
|
|
2011-02-20 22:40:49 +00:00
|
|
|
def set_response_script(self, s):
|
|
|
|
self.scripts["response"] = s
|
|
|
|
|
|
|
|
def set_request_script(self, s):
|
|
|
|
self.scripts["request"] = s
|
|
|
|
|
2011-02-24 02:15:51 +00:00
|
|
|
def set_stickycookie(self, txt):
|
|
|
|
if txt:
|
|
|
|
flt = filt.parse(txt)
|
|
|
|
if not flt:
|
|
|
|
return "Invalid filter expression."
|
|
|
|
self.stickycookie_state = StickyCookieState(flt)
|
2011-03-12 22:43:55 +00:00
|
|
|
self.stickycookie_txt = txt
|
2011-02-24 02:15:51 +00:00
|
|
|
else:
|
|
|
|
self.stickycookie_state = None
|
2011-03-12 22:43:55 +00:00
|
|
|
self.stickycookie_txt = None
|
2011-02-24 02:15:51 +00:00
|
|
|
|
2011-03-20 04:31:54 +00:00
|
|
|
def set_stickyauth(self, txt):
|
|
|
|
if txt:
|
|
|
|
flt = filt.parse(txt)
|
|
|
|
if not flt:
|
|
|
|
return "Invalid filter expression."
|
|
|
|
self.stickyauth_state = StickyAuthState(flt)
|
|
|
|
self.stickyauth_txt = txt
|
|
|
|
else:
|
|
|
|
self.stickyauth_state = None
|
|
|
|
self.stickyauth_txt = None
|
|
|
|
|
2011-03-06 03:54:49 +00:00
|
|
|
def start_client_playback(self, flows, exit):
|
2011-03-05 00:03:26 +00:00
|
|
|
"""
|
|
|
|
flows: A list of flows.
|
|
|
|
"""
|
2011-03-06 03:54:49 +00:00
|
|
|
self.client_playback = ClientPlaybackState(flows, exit)
|
2011-03-05 00:03:26 +00:00
|
|
|
|
2011-03-17 20:38:51 +00:00
|
|
|
def stop_client_playback(self):
|
|
|
|
self.client_playback = None
|
|
|
|
|
2011-03-06 04:08:56 +00:00
|
|
|
def start_server_playback(self, flows, kill, headers, exit):
|
2011-02-20 22:40:49 +00:00
|
|
|
"""
|
|
|
|
flows: A list of flows.
|
|
|
|
kill: Boolean, should we kill requests not part of the replay?
|
|
|
|
"""
|
2011-03-06 04:08:56 +00:00
|
|
|
self.server_playback = ServerPlaybackState(headers, flows, exit)
|
2011-02-20 22:40:49 +00:00
|
|
|
self.kill_nonreplay = kill
|
|
|
|
|
2011-03-17 20:43:47 +00:00
|
|
|
def stop_server_playback(self):
|
|
|
|
self.server_playback = None
|
|
|
|
|
2011-03-05 00:03:26 +00:00
|
|
|
def do_server_playback(self, flow):
|
2011-02-20 20:54:39 +00:00
|
|
|
"""
|
|
|
|
This method should be called by child classes in the handle_request
|
|
|
|
handler. Returns True if playback has taken place, None if not.
|
|
|
|
"""
|
2011-03-05 00:03:26 +00:00
|
|
|
if self.server_playback:
|
|
|
|
rflow = self.server_playback.next_flow(flow)
|
2011-02-20 20:54:39 +00:00
|
|
|
if not rflow:
|
|
|
|
return None
|
|
|
|
response = proxy.Response.from_state(flow.request, rflow.response.get_state())
|
|
|
|
response.set_replay()
|
|
|
|
flow.response = response
|
2011-03-10 22:56:10 +00:00
|
|
|
if self.refresh_server_playback:
|
|
|
|
response.refresh()
|
2011-02-20 20:54:39 +00:00
|
|
|
flow.request.ack(response)
|
|
|
|
return True
|
|
|
|
return None
|
2011-02-16 03:03:22 +00:00
|
|
|
|
2011-03-05 00:03:26 +00:00
|
|
|
def tick(self, q):
|
|
|
|
if self.client_playback:
|
2011-03-06 03:54:49 +00:00
|
|
|
e = [
|
|
|
|
self.client_playback.done(),
|
|
|
|
self.client_playback.exit,
|
|
|
|
self.state.active_flow_count() == 0
|
|
|
|
]
|
|
|
|
if all(e):
|
|
|
|
self.shutdown()
|
2011-03-05 22:21:31 +00:00
|
|
|
self.client_playback.tick(self)
|
2011-03-06 04:08:56 +00:00
|
|
|
|
|
|
|
if self.server_playback:
|
|
|
|
if self.server_playback.exit and self.server_playback.count() == 0:
|
|
|
|
self.shutdown()
|
|
|
|
|
2011-06-27 02:01:08 +00:00
|
|
|
return controller.Master.tick(self, q)
|
2011-03-05 00:03:26 +00:00
|
|
|
|
2011-05-14 23:22:35 +00:00
|
|
|
def load_flows(self, fr):
|
|
|
|
"""
|
|
|
|
Load flows from a FlowReader object.
|
|
|
|
"""
|
|
|
|
for i in fr.stream():
|
|
|
|
if i.request:
|
2011-05-14 23:54:12 +00:00
|
|
|
self.handle_request(i.request)
|
2011-05-14 23:22:35 +00:00
|
|
|
if i.response:
|
2011-05-14 23:54:12 +00:00
|
|
|
self.handle_response(i.response)
|
2011-05-14 23:22:35 +00:00
|
|
|
if i.error:
|
2011-05-14 23:54:12 +00:00
|
|
|
self.handle_error(i.error)
|
2011-05-14 23:22:35 +00:00
|
|
|
|
2011-03-15 02:11:03 +00:00
|
|
|
def process_new_request(self, f):
|
|
|
|
if self.stickycookie_state:
|
|
|
|
self.stickycookie_state.handle_request(f)
|
2011-03-20 04:31:54 +00:00
|
|
|
if self.stickyauth_state:
|
|
|
|
self.stickyauth_state.handle_request(f)
|
|
|
|
|
2011-03-15 02:11:03 +00:00
|
|
|
if "request" in self.scripts:
|
|
|
|
self._runscript(f, self.scripts["request"])
|
|
|
|
if self.anticache:
|
|
|
|
f.request.anticache()
|
|
|
|
if self.server_playback:
|
|
|
|
pb = self.do_server_playback(f)
|
|
|
|
if not pb:
|
|
|
|
if self.kill_nonreplay:
|
|
|
|
f.kill(self)
|
|
|
|
else:
|
|
|
|
f.request.ack()
|
|
|
|
|
2011-05-14 23:22:35 +00:00
|
|
|
def process_new_response(self, f):
|
|
|
|
if self.stickycookie_state:
|
|
|
|
self.stickycookie_state.handle_response(f)
|
|
|
|
if "response" in self.scripts:
|
|
|
|
self._runscript(f, self.scripts["response"])
|
|
|
|
|
2011-03-15 02:11:03 +00:00
|
|
|
def replay_request(self, f):
|
|
|
|
"""
|
|
|
|
Returns None if successful, or error message if not.
|
|
|
|
"""
|
|
|
|
#begin nocover
|
|
|
|
if f.intercepting:
|
|
|
|
return "Can't replay while intercepting..."
|
|
|
|
if f.request:
|
|
|
|
f.request.set_replay()
|
|
|
|
if f.request.content:
|
|
|
|
f.request.headers["content-length"] = [str(len(f.request.content))]
|
|
|
|
f.response = None
|
|
|
|
f.error = None
|
|
|
|
self.process_new_request(f)
|
|
|
|
rt = RequestReplayThread(f, self.masterq)
|
|
|
|
rt.start()
|
|
|
|
#end nocover
|
|
|
|
|
2011-02-19 04:00:24 +00:00
|
|
|
def handle_clientconnect(self, r):
|
|
|
|
self.state.clientconnect(r)
|
|
|
|
r.ack()
|
|
|
|
|
|
|
|
def handle_clientdisconnect(self, r):
|
|
|
|
self.state.clientdisconnect(r)
|
2011-02-16 03:03:22 +00:00
|
|
|
r.ack()
|
|
|
|
|
|
|
|
def handle_error(self, r):
|
|
|
|
f = self.state.add_error(r)
|
2011-03-05 00:03:26 +00:00
|
|
|
if self.client_playback:
|
|
|
|
self.client_playback.clear(f)
|
2011-02-19 04:21:08 +00:00
|
|
|
r.ack()
|
2011-02-16 03:03:22 +00:00
|
|
|
return f
|
|
|
|
|
|
|
|
def handle_request(self, r):
|
2011-02-20 22:40:49 +00:00
|
|
|
f = self.state.add_request(r)
|
2011-03-15 02:11:03 +00:00
|
|
|
self.process_new_request(f)
|
2011-02-20 22:40:49 +00:00
|
|
|
return f
|
2011-02-16 03:03:22 +00:00
|
|
|
|
|
|
|
def handle_response(self, r):
|
|
|
|
f = self.state.add_response(r)
|
2011-03-05 00:03:26 +00:00
|
|
|
if self.client_playback:
|
|
|
|
self.client_playback.clear(f)
|
2011-02-16 03:03:22 +00:00
|
|
|
if not f:
|
|
|
|
r.ack()
|
2011-05-14 23:22:35 +00:00
|
|
|
self.process_new_response(f)
|
2011-02-16 03:43:35 +00:00
|
|
|
return f
|
2011-02-16 03:03:22 +00:00
|
|
|
|
2011-02-16 01:33:04 +00:00
|
|
|
|
|
|
|
class FlowWriter:
|
|
|
|
def __init__(self, fo):
|
|
|
|
self.fo = fo
|
|
|
|
self.ns = netstring.FileEncoder(fo)
|
|
|
|
|
|
|
|
def add(self, flow):
|
|
|
|
d = flow.get_state()
|
|
|
|
s = json.dumps(d)
|
|
|
|
self.ns.write(s)
|
|
|
|
|
2011-03-12 00:47:37 +00:00
|
|
|
|
2011-03-11 02:16:31 +00:00
|
|
|
class FlowReadError(Exception):
|
|
|
|
@property
|
|
|
|
def strerror(self):
|
|
|
|
return self.args[0]
|
2011-02-16 01:33:04 +00:00
|
|
|
|
2011-03-12 00:47:37 +00:00
|
|
|
|
2011-02-16 01:33:04 +00:00
|
|
|
class FlowReader:
|
|
|
|
def __init__(self, fo):
|
|
|
|
self.fo = fo
|
|
|
|
self.ns = netstring.decode_file(fo)
|
|
|
|
|
|
|
|
def stream(self):
|
|
|
|
"""
|
|
|
|
Yields Flow objects from the dump.
|
|
|
|
"""
|
2011-03-11 02:16:31 +00:00
|
|
|
try:
|
|
|
|
for i in self.ns:
|
|
|
|
data = json.loads(i)
|
|
|
|
yield Flow.from_state(data)
|
|
|
|
except netstring.DecoderError:
|
|
|
|
raise FlowReadError("Invalid data format.")
|
2011-02-16 01:33:04 +00:00
|
|
|
|