Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lovemyliwu/948a0a816b1f60e0dd19f23c9e074d93 to your computer and use it in GitHub Desktop.
Save lovemyliwu/948a0a816b1f60e0dd19f23c9e074d93 to your computer and use it in GitHub Desktop.
import json
from mitmproxy import ctx
from mitmproxy import command
class Analysis:
def __init__(self):
self.info = {}
def request(self, flow):
source = flow.client_conn.address[0]
self.ensure_counter(source)
counter = self.info.get(source)
size = len(str(flow.request.headers))
size += len(flow.request.raw_content)
counter['upload'] += size
ctx.log.info(f'{source} upload cost {size} bytes')
def ensure_counter(self, ip):
self.info.setdefault(ip, {'upload': 0, 'download': 0})
def response(self, flow):
source = flow.client_conn.address[0]
self.ensure_counter(source)
counter = self.info.get(source)
size = len(str(flow.response.headers))
size += len(flow.response.raw_content)
counter['download'] += size
ctx.log.info(f"{source} download cost {size} bytes")
def done(self):
with open('/tmp/flow_bytes.log', 'w') as file:
file.write(json.dumps(self.info))
@command.command("analysis.show_info")
def show_info(self):
ctx.log.info(json.dumps(self.info))
addons = [
Analysis()
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment