aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/models
diff options
context:
space:
mode:
Diffstat (limited to 'mitmproxy/models')
-rw-r--r--mitmproxy/models/__init__.py5
-rw-r--r--mitmproxy/models/flow.py9
-rw-r--r--mitmproxy/models/http.py6
-rw-r--r--mitmproxy/models/tcp.py50
4 files changed, 63 insertions, 7 deletions
diff --git a/mitmproxy/models/__init__.py b/mitmproxy/models/__init__.py
index df86eff4..3d9d9dae 100644
--- a/mitmproxy/models/__init__.py
+++ b/mitmproxy/models/__init__.py
@@ -7,9 +7,11 @@ from .http import (
from netlib.http import decoded
from .connections import ClientConnection, ServerConnection
from .flow import Flow, Error
+from .tcp import TCPFlow
FLOW_TYPES = dict(
- http=HTTPFlow
+ http=HTTPFlow,
+ tcp=TCPFlow,
)
__all__ = [
@@ -18,5 +20,6 @@ __all__ = [
"make_connect_response", "expect_continue_response",
"ClientConnection", "ServerConnection",
"Flow", "Error",
+ "TCPFlow"
"FLOW_TYPES"
]
diff --git a/mitmproxy/models/flow.py b/mitmproxy/models/flow.py
index 594147ec..1019c9fb 100644
--- a/mitmproxy/models/flow.py
+++ b/mitmproxy/models/flow.py
@@ -40,6 +40,9 @@ class Error(stateobject.StateObject):
def __str__(self):
return self.msg
+ def __repr__(self):
+ return self.msg
+
@classmethod
def from_state(cls, state):
# the default implementation assumes an empty constructor. Override
@@ -99,6 +102,12 @@ class Flow(stateobject.StateObject):
self._backup = state.pop("backup")
super(Flow, self).set_state(state)
+ @classmethod
+ def from_state(cls, state):
+ f = cls(None, None)
+ f.set_state(state)
+ return f
+
def copy(self):
f = copy.copy(self)
diff --git a/mitmproxy/models/http.py b/mitmproxy/models/http.py
index 77a809cf..75ffbfd0 100644
--- a/mitmproxy/models/http.py
+++ b/mitmproxy/models/http.py
@@ -191,12 +191,6 @@ class HTTPFlow(Flow):
response=HTTPResponse
)
- @classmethod
- def from_state(cls, state):
- f = cls(None, None)
- f.set_state(state)
- return f
-
def __repr__(self):
s = "<HTTPFlow"
for a in ("request", "response", "error", "client_conn", "server_conn"):
diff --git a/mitmproxy/models/tcp.py b/mitmproxy/models/tcp.py
new file mode 100644
index 00000000..7e966b95
--- /dev/null
+++ b/mitmproxy/models/tcp.py
@@ -0,0 +1,50 @@
+import time
+from typing import List
+
+from netlib.utils import Serializable
+from .flow import Flow
+
+
+class TCPMessage(Serializable):
+ def __init__(self, from_client, content, timestamp=None):
+ self.content = content
+ self.from_client = from_client
+ if timestamp is None:
+ timestamp = time.time()
+ self.timestamp = timestamp
+
+ @classmethod
+ def from_state(cls, state):
+ return cls(*state)
+
+ def get_state(self):
+ return self.from_client, self.content, self.timestamp
+
+ def set_state(self, state):
+ self.from_client = state.pop("from_client")
+ self.content = state.pop("content")
+ self.timestamp = state.pop("timestamp")
+
+ def __repr__(self):
+ return "{direction} {content}".format(
+ direction="->" if self.from_client else "<-",
+ content=repr(self.content)
+ )
+
+
+class TCPFlow(Flow):
+ """
+ A TCPFlow is a simplified representation of a TCP session.
+ """
+
+ def __init__(self, client_conn, server_conn, live=None):
+ super(TCPFlow, self).__init__("tcp", client_conn, server_conn, live)
+ self.messages = [] # type: List[TCPMessage]
+
+ _stateobject_attributes = Flow._stateobject_attributes.copy()
+ _stateobject_attributes.update(
+ messages=List[TCPMessage]
+ )
+
+ def __repr__(self):
+ return "<TCPFlow ({} messages)>".format(len(self.messages))