mitmproxy/mitmproxy/flow.py

183 lines
4.8 KiB
Python
Raw Normal View History

import time
import typing # noqa
import uuid
2015-08-30 01:42:11 +00:00
from mitmproxy import connections
from mitmproxy import controller, exceptions # noqa
from mitmproxy import stateobject
2016-10-19 20:20:44 +00:00
from mitmproxy import version
2014-03-10 20:57:50 +00:00
class Error(stateobject.StateObject):
2016-01-27 09:12:18 +00:00
2014-02-04 04:02:17 +00:00
"""
An Error.
This is distinct from an protocol error response (say, a HTTP code 500),
which is represented by a normal HTTPResponse object. This class is
responsible for indicating errors that fall outside of normal protocol
communications, like interrupted connections, timeouts, protocol errors.
2014-02-04 04:02:17 +00:00
Exposes the following attributes:
msg: Message describing the error
timestamp: Seconds since the epoch
"""
2015-05-30 00:03:28 +00:00
2016-11-01 21:59:33 +00:00
def __init__(self, msg: str, timestamp=None) -> None:
2014-02-04 04:02:17 +00:00
"""
@type msg: str
@type timestamp: float
"""
self.msg = msg
self.timestamp = timestamp or time.time()
2014-02-04 04:02:17 +00:00
_stateobject_attributes = dict(
msg=str,
timestamp=float
)
def __str__(self):
return self.msg
def __repr__(self):
return self.msg
2014-02-04 04:02:17 +00:00
@classmethod
def from_state(cls, state):
# the default implementation assumes an empty constructor. Override
# accordingly.
f = cls(None)
2016-02-08 03:19:25 +00:00
f.set_state(state)
2014-02-04 04:02:17 +00:00
return f
class Flow(stateobject.StateObject):
2016-01-27 09:12:18 +00:00
"""
A Flow is a collection of objects representing a single transaction.
This class is usually subclassed for each protocol, e.g. HTTPFlow.
"""
2015-05-30 00:03:28 +00:00
2016-10-17 03:56:46 +00:00
def __init__(
self,
type: str,
client_conn: connections.ClientConnection,
server_conn: connections.ServerConnection,
2016-11-01 21:59:33 +00:00
live: bool=None
) -> None:
2014-10-18 16:29:35 +00:00
self.type = type
self.id = str(uuid.uuid4())
2016-10-17 03:56:46 +00:00
self.client_conn = client_conn
self.server_conn = server_conn
2014-09-03 21:44:54 +00:00
self.live = live
2016-11-01 21:59:33 +00:00
self.error = None # type: typing.Optional[Error]
2016-07-07 04:03:17 +00:00
self.intercepted = False # type: bool
2016-11-01 21:59:33 +00:00
self._backup = None # type: typing.Optional[Flow]
self.reply = None # type: typing.Optional[controller.Reply]
2016-07-19 06:47:43 +00:00
self.marked = False # type: bool
self.metadata = dict() # type: typing.Dict[str, typing.Any]
2014-02-04 04:02:17 +00:00
_stateobject_attributes = dict(
id=str,
2014-02-04 04:02:17 +00:00
error=Error,
client_conn=connections.ClientConnection,
server_conn=connections.ServerConnection,
2014-12-23 19:33:42 +00:00
type=str,
2016-07-19 06:47:43 +00:00
intercepted=bool,
marked=bool,
metadata=typing.Dict[str, typing.Any],
2014-02-04 04:02:17 +00:00
)
2016-02-08 01:10:10 +00:00
def get_state(self):
2016-10-17 04:34:46 +00:00
d = super().get_state()
d.update(version=version.FLOW_FORMAT_VERSION)
if self._backup and self._backup != d:
2016-02-08 01:10:10 +00:00
d.update(backup=self._backup)
2014-02-04 04:02:17 +00:00
return d
2016-02-08 03:19:25 +00:00
def set_state(self, state):
2017-11-06 14:24:54 +00:00
state = state.copy()
2016-02-08 03:19:25 +00:00
state.pop("version")
if "backup" in state:
self._backup = state.pop("backup")
2016-10-17 04:34:46 +00:00
super().set_state(state)
2016-02-08 03:19:25 +00:00
@classmethod
def from_state(cls, state):
f = cls(None, None)
f.set_state(state)
return f
2014-02-04 04:02:17 +00:00
def copy(self):
2017-02-09 13:34:31 +00:00
f = super().copy()
2014-12-24 00:07:57 +00:00
f.live = False
2017-03-12 20:53:52 +00:00
if self.reply is not None:
2017-03-12 20:25:50 +00:00
f.reply = controller.DummyReply()
2014-02-04 04:02:17 +00:00
return f
def modified(self):
"""
Has this Flow been modified?
"""
if self._backup:
return self._backup != self.get_state()
2014-02-04 04:02:17 +00:00
else:
return False
def backup(self, force=False):
"""
Save a backup of this Flow, which can be reverted to using a
call to .revert().
"""
if not self._backup:
self._backup = self.get_state()
2014-02-04 04:02:17 +00:00
def revert(self):
"""
Revert to the last backed up state.
"""
if self._backup:
2016-02-08 03:19:25 +00:00
self.set_state(self._backup)
2014-03-10 20:57:50 +00:00
self._backup = None
2016-08-10 05:29:07 +00:00
@property
def killable(self):
return (
self.reply and
self.reply.state in {"start", "taken"} and
self.reply.value != exceptions.Kill
)
2016-08-10 05:29:07 +00:00
def kill(self):
2014-12-23 19:33:42 +00:00
"""
Kill this request.
"""
self.error = Error("Connection killed")
self.intercepted = False
2016-08-10 03:26:24 +00:00
self.reply.kill(force=True)
2016-12-11 21:52:17 +00:00
self.live = False
2014-12-23 19:33:42 +00:00
def intercept(self):
2014-12-23 19:33:42 +00:00
"""
Intercept this Flow. Processing will stop until resume is
2014-12-23 19:33:42 +00:00
called.
"""
2014-12-24 00:07:57 +00:00
if self.intercepted:
return
2014-12-23 19:33:42 +00:00
self.intercepted = True
2016-08-10 03:26:24 +00:00
self.reply.take()
2014-12-23 19:33:42 +00:00
def resume(self):
2014-12-23 19:33:42 +00:00
"""
Continue with the flow - called after an intercept().
"""
2014-12-24 00:07:57 +00:00
if not self.intercepted:
return
2014-12-23 19:33:42 +00:00
self.intercepted = False
2017-12-12 13:41:26 +00:00
# If a flow is intercepted and then duplicated, the duplicated one is not taken.
if self.reply.state == "taken":
self.reply.ack()
self.reply.commit()