mitmproxy/mitmproxy/tcp.py

48 lines
1.3 KiB
Python
Raw Normal View History

import time
2016-06-01 02:06:57 +00:00
from typing import List
from mitmproxy import flow
from mitmproxy.types import serializable
class TCPMessage(serializable.Serializable):
2016-05-28 20:39:30 +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()
@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
def __repr__(self):
return "{direction} {content}".format(
direction="->" if self.from_client else "<-",
content=repr(self.content)
)
class TCPFlow(flow.Flow):
2016-05-28 20:39:30 +00:00
"""
2016-05-11 17:13:57 +00:00
A TCPFlow is a simplified representation of a TCP session.
"""
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)
self.messages = [] # type: List[TCPMessage]
_stateobject_attributes = flow.Flow._stateobject_attributes.copy()
_stateobject_attributes["messages"] = List[TCPMessage]
def __repr__(self):
return "<TCPFlow ({} messages)>".format(len(self.messages))