mitmproxy/libmproxy/models/flow.py

166 lines
4.3 KiB
Python
Raw Normal View History

from __future__ import (absolute_import, print_function, division)
import copy
import uuid
2015-08-30 01:42:11 +00:00
2014-02-04 04:02:17 +00:00
from .. import stateobject, utils, version
2015-08-30 13:27:29 +00:00
from .connections import ClientConnection, ServerConnection
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:
flow: Flow object
msg: Message describing the error
timestamp: Seconds since the epoch
"""
2015-05-30 00:03:28 +00:00
2014-02-04 04:02:17 +00:00
def __init__(self, msg, timestamp=None):
"""
@type msg: str
@type timestamp: float
"""
self.flow = None # will usually be set by the flow backref mixin
2014-02-04 04:02:17 +00:00
self.msg = msg
self.timestamp = timestamp or utils.timestamp()
_stateobject_attributes = dict(
msg=str,
timestamp=float
)
def __str__(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)
f.load_state(state)
2014-02-04 04:02:17 +00:00
return f
def copy(self):
c = copy.copy(self)
return c
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
2014-10-18 16:29:35 +00:00
def __init__(self, type, client_conn, server_conn, live=None):
self.type = type
self.id = str(uuid.uuid4())
2014-02-04 04:02:17 +00:00
self.client_conn = client_conn
"""@type: ClientConnection"""
2014-02-04 04:02:17 +00:00
self.server_conn = server_conn
"""@type: ServerConnection"""
2014-09-03 21:44:54 +00:00
self.live = live
2014-08-24 12:22:11 +00:00
"""@type: LiveConnection"""
2014-02-04 04:02:17 +00:00
self.error = None
"""@type: Error"""
2014-12-23 19:33:42 +00:00
self.intercepted = False
"""@type: bool"""
self._backup = None
2014-12-23 19:33:42 +00:00
self.reply = None
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=ClientConnection,
server_conn=ServerConnection,
2014-12-23 19:33:42 +00:00
type=str,
intercepted=bool
2014-02-04 04:02:17 +00:00
)
2016-02-08 01:10:10 +00:00
def get_state(self):
d = super(Flow, self).get_state()
2014-02-04 04:02:17 +00:00
d.update(version=version.IVERSION)
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
def __eq__(self, other):
return self is other
2014-02-04 04:02:17 +00:00
def copy(self):
f = copy.copy(self)
2014-12-24 00:07:57 +00:00
f.id = str(uuid.uuid4())
f.live = False
2014-02-04 04:02:17 +00:00
f.client_conn = self.client_conn.copy()
f.server_conn = self.server_conn.copy()
if self.error:
f.error = self.error.copy()
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:
self.load_state(self._backup)
2014-03-10 20:57:50 +00:00
self._backup = None
2014-12-23 19:33:42 +00:00
def kill(self, master):
"""
Kill this request.
"""
2015-08-30 13:27:29 +00:00
from ..protocol import Kill
2014-12-23 19:33:42 +00:00
self.error = Error("Connection killed")
self.intercepted = False
2015-08-30 13:27:29 +00:00
self.reply(Kill)
2014-12-23 19:33:42 +00:00
master.handle_error(self)
def intercept(self, master):
"""
Intercept this Flow. Processing will stop until accept_intercept is
called.
"""
2014-12-24 00:07:57 +00:00
if self.intercepted:
return
2014-12-23 19:33:42 +00:00
self.intercepted = True
master.handle_intercept(self)
def accept_intercept(self, master):
"""
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
self.reply()
master.handle_accept_intercept(self)