Get rid of ReplayConnection - we now have only one ClientConnection class.
This commit is contained in:
parent
2cb7429d38
commit
2ad4c5adf3
|
@ -47,7 +47,7 @@ def format_keyvals(lst, key="key", val="text", space=5, indent=0):
|
||||||
def format_flow(f, focus, extended=False, padding=3):
|
def format_flow(f, focus, extended=False, padding=3):
|
||||||
if not f.request and not f.response:
|
if not f.request and not f.response:
|
||||||
txt = [
|
txt = [
|
||||||
("title", " Connection from %s..."%(f.client_conn.address)),
|
("title", " Connection from %s..."%(f.client_conn.address[0])),
|
||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
if extended:
|
if extended:
|
||||||
|
@ -1263,7 +1263,7 @@ class ConsoleMaster(controller.Master):
|
||||||
self.refresh_connection(f)
|
self.refresh_connection(f)
|
||||||
|
|
||||||
# Handlers
|
# Handlers
|
||||||
def handle_browserconnection(self, r):
|
def handle_clientconnection(self, r):
|
||||||
f = flow.Flow(r)
|
f = flow.Flow(r)
|
||||||
self.state.add_browserconnect(f)
|
self.state.add_browserconnect(f)
|
||||||
r.ack()
|
r.ack()
|
||||||
|
|
|
@ -13,10 +13,6 @@ class RunException(Exception):
|
||||||
self.errout = errout
|
self.errout = errout
|
||||||
|
|
||||||
|
|
||||||
class ReplayConnection:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# begin nocover
|
# begin nocover
|
||||||
class ReplayThread(threading.Thread):
|
class ReplayThread(threading.Thread):
|
||||||
def __init__(self, flow, masterq):
|
def __init__(self, flow, masterq):
|
||||||
|
@ -110,6 +106,7 @@ class Flow:
|
||||||
self.response = proxy.Response.from_state(self.request, state["response"])
|
self.response = proxy.Response.from_state(self.request, state["response"])
|
||||||
if state["error"]:
|
if state["error"]:
|
||||||
self.error = proxy.Error.from_state(state["error"])
|
self.error = proxy.Error.from_state(state["error"])
|
||||||
|
self.client_conn = self.request.client_conn
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_state(klass, state):
|
def from_state(klass, state):
|
||||||
|
@ -131,7 +128,7 @@ class Flow:
|
||||||
def backup(self):
|
def backup(self):
|
||||||
if not self._backup:
|
if not self._backup:
|
||||||
self._backup = [
|
self._backup = [
|
||||||
self.client_conn.copy() if self.client_conn else None,
|
self.client_conn.copy(),
|
||||||
self.request.copy() if self.request else None,
|
self.request.copy() if self.request else None,
|
||||||
self.response.copy() if self.response else None,
|
self.response.copy() if self.response else None,
|
||||||
self.error.copy() if self.error else None,
|
self.error.copy() if self.error else None,
|
||||||
|
@ -152,7 +149,7 @@ class Flow:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def is_replay(self):
|
def is_replay(self):
|
||||||
return isinstance(self.client_conn, ReplayConnection)
|
return self.client_conn.is_replay()
|
||||||
|
|
||||||
def kill(self):
|
def kill(self):
|
||||||
if self.request and not self.request.acked:
|
if self.request and not self.request.acked:
|
||||||
|
@ -231,6 +228,8 @@ class State:
|
||||||
data = bson.loads(js)
|
data = bson.loads(js)
|
||||||
data = [Flow.from_state(i) for i in data["flows"]]
|
data = [Flow.from_state(i) for i in data["flows"]]
|
||||||
self.flow_list.extend(data)
|
self.flow_list.extend(data)
|
||||||
|
for i in data:
|
||||||
|
self.flow_map[i.client_conn] = i
|
||||||
|
|
||||||
def set_limit(self, limit):
|
def set_limit(self, limit):
|
||||||
"""
|
"""
|
||||||
|
@ -246,7 +245,7 @@ class State:
|
||||||
return tuple(self.flow_list[:])
|
return tuple(self.flow_list[:])
|
||||||
|
|
||||||
def get_client_conn(self, itm):
|
def get_client_conn(self, itm):
|
||||||
if isinstance(itm, (proxy.ClientConnection, ReplayConnection)):
|
if isinstance(itm, proxy.ClientConnection):
|
||||||
return itm
|
return itm
|
||||||
elif hasattr(itm, "client_conn"):
|
elif hasattr(itm, "client_conn"):
|
||||||
return itm.client_conn
|
return itm.client_conn
|
||||||
|
@ -284,19 +283,11 @@ class State:
|
||||||
self.delete_flow(f)
|
self.delete_flow(f)
|
||||||
|
|
||||||
def revert(self, f):
|
def revert(self, f):
|
||||||
"""
|
|
||||||
Replaces the matching client_conn object with a ReplayConnection object.
|
|
||||||
"""
|
|
||||||
conn = self.get_client_conn(f)
|
conn = self.get_client_conn(f)
|
||||||
if conn in self.flow_map:
|
|
||||||
del self.flow_map[conn]
|
|
||||||
f.revert()
|
f.revert()
|
||||||
self.flow_map[f.client_conn] = f
|
|
||||||
|
|
||||||
def replay(self, f, masterq):
|
def replay(self, f, masterq):
|
||||||
"""
|
"""
|
||||||
Replaces the matching client_conn object with a ReplayConnection object.
|
|
||||||
|
|
||||||
Returns None if successful, or error message if not.
|
Returns None if successful, or error message if not.
|
||||||
"""
|
"""
|
||||||
#begin nocover
|
#begin nocover
|
||||||
|
@ -305,16 +296,11 @@ class State:
|
||||||
if f.request:
|
if f.request:
|
||||||
f.backup()
|
f.backup()
|
||||||
conn = self.get_client_conn(f)
|
conn = self.get_client_conn(f)
|
||||||
if conn in self.flow_map:
|
f.client_conn.set_replay()
|
||||||
del self.flow_map[conn]
|
|
||||||
rp = ReplayConnection()
|
|
||||||
f.client_conn = rp
|
|
||||||
f.request.client_conn = rp
|
|
||||||
if f.request.content:
|
if f.request.content:
|
||||||
f.request.headers["content-length"] = [str(len(f.request.content))]
|
f.request.headers["content-length"] = [str(len(f.request.content))]
|
||||||
f.response = None
|
f.response = None
|
||||||
f.error = None
|
f.error = None
|
||||||
self.flow_map[rp] = f
|
|
||||||
rt = ReplayThread(f, masterq)
|
rt = ReplayThread(f, masterq)
|
||||||
rt.start()
|
rt.start()
|
||||||
#end nocover
|
#end nocover
|
||||||
|
|
|
@ -106,7 +106,7 @@ class Request(controller.Msg):
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_state(klass, state):
|
def from_state(klass, state):
|
||||||
return klass(
|
return klass(
|
||||||
None,
|
ClientConnection(None),
|
||||||
state["host"],
|
state["host"],
|
||||||
state["port"],
|
state["port"],
|
||||||
state["scheme"],
|
state["scheme"],
|
||||||
|
@ -222,10 +222,23 @@ class Response(controller.Msg):
|
||||||
|
|
||||||
|
|
||||||
class ClientConnection(controller.Msg):
|
class ClientConnection(controller.Msg):
|
||||||
def __init__(self, address, port):
|
def __init__(self, address):
|
||||||
self.address, self.port = address, port
|
"""
|
||||||
|
address is an (address, port) tuple, or None if this connection has
|
||||||
|
been replayed from within mitmproxy.
|
||||||
|
"""
|
||||||
|
self.address = address
|
||||||
controller.Msg.__init__(self)
|
controller.Msg.__init__(self)
|
||||||
|
|
||||||
|
def set_replay(self):
|
||||||
|
self.address = None
|
||||||
|
|
||||||
|
def is_replay(self):
|
||||||
|
if self.address:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
return copy.copy(self)
|
return copy.copy(self)
|
||||||
|
|
||||||
|
@ -350,10 +363,10 @@ class ProxyHandler(SocketServer.StreamRequestHandler):
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
server = None
|
server = None
|
||||||
bc = ClientConnection(*self.client_address)
|
cc = ClientConnection(self.client_address)
|
||||||
bc.send(self.mqueue)
|
cc.send(self.mqueue)
|
||||||
try:
|
try:
|
||||||
request = self.read_request(bc)
|
request = self.read_request(cc)
|
||||||
request = request.send(self.mqueue)
|
request = request.send(self.mqueue)
|
||||||
if request is None:
|
if request is None:
|
||||||
self.finish()
|
self.finish()
|
||||||
|
@ -369,7 +382,7 @@ class ProxyHandler(SocketServer.StreamRequestHandler):
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
except ProxyError, e:
|
except ProxyError, e:
|
||||||
err = Error(bc, e.msg)
|
err = Error(cc, e.msg)
|
||||||
err.send(self.mqueue)
|
err.send(self.mqueue)
|
||||||
self.send_error(e.code, e.msg)
|
self.send_error(e.code, e.msg)
|
||||||
if server:
|
if server:
|
||||||
|
|
|
@ -10,7 +10,7 @@ class uState(libpry.AutoTree):
|
||||||
|
|
||||||
connect -> request -> response
|
connect -> request -> response
|
||||||
"""
|
"""
|
||||||
bc = proxy.ClientConnection("address", 22)
|
bc = proxy.ClientConnection(("address", 22))
|
||||||
c = console.ConsoleState()
|
c = console.ConsoleState()
|
||||||
f = flow.Flow(bc)
|
f = flow.Flow(bc)
|
||||||
c.add_browserconnect(f)
|
c.add_browserconnect(f)
|
||||||
|
@ -25,7 +25,7 @@ class uState(libpry.AutoTree):
|
||||||
"""
|
"""
|
||||||
c = console.ConsoleState()
|
c = console.ConsoleState()
|
||||||
|
|
||||||
bc = proxy.ClientConnection("address", 22)
|
bc = proxy.ClientConnection(("address", 22))
|
||||||
f = flow.Flow(bc)
|
f = flow.Flow(bc)
|
||||||
c.add_browserconnect(f)
|
c.add_browserconnect(f)
|
||||||
assert c.get_focus() == (f, 0)
|
assert c.get_focus() == (f, 0)
|
||||||
|
@ -33,7 +33,7 @@ class uState(libpry.AutoTree):
|
||||||
assert c.get_from_pos(1) == (None, None)
|
assert c.get_from_pos(1) == (None, None)
|
||||||
assert c.get_next(0) == (None, None)
|
assert c.get_next(0) == (None, None)
|
||||||
|
|
||||||
bc2 = proxy.ClientConnection("address", 22)
|
bc2 = proxy.ClientConnection(("address", 22))
|
||||||
f2 = flow.Flow(bc2)
|
f2 = flow.Flow(bc2)
|
||||||
c.add_browserconnect(f2)
|
c.add_browserconnect(f2)
|
||||||
assert c.get_focus() == (f, 1)
|
assert c.get_focus() == (f, 1)
|
||||||
|
@ -117,7 +117,7 @@ class uformat_flow(libpry.AutoTree):
|
||||||
|
|
||||||
assert ('method', '[edited] ') in console.format_flow(f, True)
|
assert ('method', '[edited] ') in console.format_flow(f, True)
|
||||||
assert ('method', '[edited] ') in console.format_flow(f, True, True)
|
assert ('method', '[edited] ') in console.format_flow(f, True, True)
|
||||||
f.client_conn = flow.ReplayConnection()
|
f.client_conn = proxy.ClientConnection(None)
|
||||||
assert ('method', '[replay] ') in console.format_flow(f, True)
|
assert ('method', '[replay] ') in console.format_flow(f, True)
|
||||||
assert ('method', '[replay] ') in console.format_flow(f, True, True)
|
assert ('method', '[replay] ') in console.format_flow(f, True, True)
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ class uParsing(libpry.AutoTree):
|
||||||
|
|
||||||
class uMatching(libpry.AutoTree):
|
class uMatching(libpry.AutoTree):
|
||||||
def req(self):
|
def req(self):
|
||||||
conn = proxy.ClientConnection("one", 2222)
|
conn = proxy.ClientConnection(("one", 2222))
|
||||||
headers = utils.Headers()
|
headers = utils.Headers()
|
||||||
headers["header"] = ["qvalue"]
|
headers["header"] = ["qvalue"]
|
||||||
return proxy.Request(
|
return proxy.Request(
|
||||||
|
|
|
@ -39,8 +39,6 @@ class uFlow(libpry.AutoTree):
|
||||||
|
|
||||||
def test_getset_state(self):
|
def test_getset_state(self):
|
||||||
f = utils.tflow()
|
f = utils.tflow()
|
||||||
state = f.get_state()
|
|
||||||
assert f == flow.Flow.from_state(state)
|
|
||||||
f.response = utils.tresp()
|
f.response = utils.tresp()
|
||||||
f.request = f.response.request
|
f.request = f.response.request
|
||||||
state = f.get_state()
|
state = f.get_state()
|
||||||
|
@ -66,7 +64,7 @@ class uFlow(libpry.AutoTree):
|
||||||
assert console.format_flow(f, True)
|
assert console.format_flow(f, True)
|
||||||
assert console.format_flow(f, False)
|
assert console.format_flow(f, False)
|
||||||
|
|
||||||
f.client_conn = flow.ReplayConnection()
|
f.client_conn.set_replay()
|
||||||
assert console.format_flow(f, True)
|
assert console.format_flow(f, True)
|
||||||
assert console.format_flow(f, False)
|
assert console.format_flow(f, False)
|
||||||
|
|
||||||
|
@ -115,7 +113,7 @@ class uFlow(libpry.AutoTree):
|
||||||
|
|
||||||
class uState(libpry.AutoTree):
|
class uState(libpry.AutoTree):
|
||||||
def test_backup(self):
|
def test_backup(self):
|
||||||
bc = proxy.ClientConnection("address", 22)
|
bc = proxy.ClientConnection(("address", 22))
|
||||||
c = flow.State()
|
c = flow.State()
|
||||||
f = flow.Flow(bc)
|
f = flow.Flow(bc)
|
||||||
c.add_browserconnect(f)
|
c.add_browserconnect(f)
|
||||||
|
@ -129,7 +127,7 @@ class uState(libpry.AutoTree):
|
||||||
|
|
||||||
connect -> request -> response
|
connect -> request -> response
|
||||||
"""
|
"""
|
||||||
bc = proxy.ClientConnection("address", 22)
|
bc = proxy.ClientConnection(("address", 22))
|
||||||
c = flow.State()
|
c = flow.State()
|
||||||
f = flow.Flow(bc)
|
f = flow.Flow(bc)
|
||||||
c.add_browserconnect(f)
|
c.add_browserconnect(f)
|
||||||
|
@ -154,14 +152,14 @@ class uState(libpry.AutoTree):
|
||||||
assert not c.lookup(newresp)
|
assert not c.lookup(newresp)
|
||||||
|
|
||||||
def test_err(self):
|
def test_err(self):
|
||||||
bc = proxy.ClientConnection("address", 22)
|
bc = proxy.ClientConnection(("address", 22))
|
||||||
c = flow.State()
|
c = flow.State()
|
||||||
f = flow.Flow(bc)
|
f = flow.Flow(bc)
|
||||||
c.add_browserconnect(f)
|
c.add_browserconnect(f)
|
||||||
e = proxy.Error(bc, "message")
|
e = proxy.Error(bc, "message")
|
||||||
assert c.add_error(e)
|
assert c.add_error(e)
|
||||||
|
|
||||||
e = proxy.Error(proxy.ClientConnection("address", 22), "message")
|
e = proxy.Error(proxy.ClientConnection(("address", 22)), "message")
|
||||||
assert not c.add_error(e)
|
assert not c.add_error(e)
|
||||||
|
|
||||||
def test_view(self):
|
def test_view(self):
|
||||||
|
|
|
@ -221,7 +221,7 @@ class uRequest(libpry.AutoTree):
|
||||||
def test_simple(self):
|
def test_simple(self):
|
||||||
h = utils.Headers()
|
h = utils.Headers()
|
||||||
h["test"] = ["test"]
|
h["test"] = ["test"]
|
||||||
c = proxy.ClientConnection("addr", 2222)
|
c = proxy.ClientConnection(("addr", 2222))
|
||||||
r = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
r = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
||||||
u = r.url()
|
u = r.url()
|
||||||
assert r.set_url(u)
|
assert r.set_url(u)
|
||||||
|
@ -233,7 +233,7 @@ class uRequest(libpry.AutoTree):
|
||||||
def test_getset_state(self):
|
def test_getset_state(self):
|
||||||
h = utils.Headers()
|
h = utils.Headers()
|
||||||
h["test"] = ["test"]
|
h["test"] = ["test"]
|
||||||
c = proxy.ClientConnection("addr", 2222)
|
c = proxy.ClientConnection(("addr", 2222))
|
||||||
r = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
r = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
||||||
state = r.get_state()
|
state = r.get_state()
|
||||||
assert proxy.Request.from_state(state) == r
|
assert proxy.Request.from_state(state) == r
|
||||||
|
@ -243,7 +243,7 @@ class uResponse(libpry.AutoTree):
|
||||||
def test_simple(self):
|
def test_simple(self):
|
||||||
h = utils.Headers()
|
h = utils.Headers()
|
||||||
h["test"] = ["test"]
|
h["test"] = ["test"]
|
||||||
c = proxy.ClientConnection("addr", 2222)
|
c = proxy.ClientConnection(("addr", 2222))
|
||||||
req = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
req = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
||||||
resp = proxy.Response(req, 200, "HTTP", "msg", h.copy(), "content")
|
resp = proxy.Response(req, 200, "HTTP", "msg", h.copy(), "content")
|
||||||
assert resp.short()
|
assert resp.short()
|
||||||
|
@ -252,7 +252,7 @@ class uResponse(libpry.AutoTree):
|
||||||
def test_getset_state(self):
|
def test_getset_state(self):
|
||||||
h = utils.Headers()
|
h = utils.Headers()
|
||||||
h["test"] = ["test"]
|
h["test"] = ["test"]
|
||||||
c = proxy.ClientConnection("addr", 2222)
|
c = proxy.ClientConnection(("addr", 2222))
|
||||||
r = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
r = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
||||||
req = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
req = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
||||||
resp = proxy.Response(req, 200, "HTTP", "msg", h.copy(), "content")
|
resp = proxy.Response(req, 200, "HTTP", "msg", h.copy(), "content")
|
||||||
|
|
|
@ -2,7 +2,7 @@ from libmproxy import proxy, utils, filt, flow
|
||||||
|
|
||||||
def treq(conn=None):
|
def treq(conn=None):
|
||||||
if not conn:
|
if not conn:
|
||||||
conn = proxy.ClientConnection("address", 22)
|
conn = proxy.ClientConnection(("address", 22))
|
||||||
headers = utils.Headers()
|
headers = utils.Headers()
|
||||||
headers["header"] = ["qvalue"]
|
headers["header"] = ["qvalue"]
|
||||||
return proxy.Request(conn, "host", 80, "http", "GET", "/path", headers, "content")
|
return proxy.Request(conn, "host", 80, "http", "GET", "/path", headers, "content")
|
||||||
|
@ -17,6 +17,6 @@ def tresp(req=None):
|
||||||
|
|
||||||
|
|
||||||
def tflow():
|
def tflow():
|
||||||
bc = proxy.ClientConnection("address", 22)
|
bc = proxy.ClientConnection(("address", 22))
|
||||||
return flow.Flow(bc)
|
return flow.Flow(bc)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue