2016-05-10 20:03:14 +00:00
|
|
|
import time
|
2016-06-01 02:06:57 +00:00
|
|
|
|
2016-05-10 20:03:14 +00:00
|
|
|
from typing import List
|
|
|
|
|
2016-10-19 02:25:39 +00:00
|
|
|
from mitmproxy import flow
|
2016-10-19 21:22:23 +00:00
|
|
|
from mitmproxy.types import serializable
|
2016-05-10 20:03:14 +00:00
|
|
|
|
|
|
|
|
2016-10-19 21:22:23 +00:00
|
|
|
class TCPMessage(serializable.Serializable):
|
2016-05-28 20:39:30 +00:00
|
|
|
|
2016-05-10 20:03:14 +00:00
|
|
|
def __init__(self, from_client, content, timestamp=None):
|
|
|
|
self.from_client = from_client
|
2017-02-08 14:00:31 +00:00
|
|
|
self.content = content
|
2016-11-13 16:50:51 +00:00
|
|
|
self.timestamp = timestamp or time.time()
|
2016-05-10 20:03:14 +00:00
|
|
|
|
|
|
|
@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):
|
2017-02-08 14:00:31 +00:00
|
|
|
self.from_client, self.content, self.timestamp = state
|
2016-05-10 20:03:14 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "{direction} {content}".format(
|
|
|
|
direction="->" if self.from_client else "<-",
|
|
|
|
content=repr(self.content)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-10-19 02:25:39 +00:00
|
|
|
class TCPFlow(flow.Flow):
|
2016-05-28 20:39:30 +00:00
|
|
|
|
2016-05-10 20:03:14 +00:00
|
|
|
"""
|
2016-05-11 17:13:57 +00:00
|
|
|
A TCPFlow is a simplified representation of a TCP session.
|
2016-05-10 20:03:14 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, client_conn, server_conn, live=None):
|
2016-10-17 04:34:46 +00:00
|
|
|
super().__init__("tcp", client_conn, server_conn, live)
|
2016-05-10 20:03:14 +00:00
|
|
|
self.messages = [] # type: List[TCPMessage]
|
|
|
|
|
2016-10-19 02:25:39 +00:00
|
|
|
_stateobject_attributes = flow.Flow._stateobject_attributes.copy()
|
2017-03-16 03:50:41 +00:00
|
|
|
_stateobject_attributes["messages"] = List[TCPMessage]
|
2016-05-10 20:03:14 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<TCPFlow ({} messages)>".format(len(self.messages))
|