Throttling feature


#1

I would appreciate any hints on how to implement connection throttling. I have to cap speed available to some particular connections. There is no need to cap any other connections within same cli-srv session, so it would be best if I could define caps on per-connection basis. …


#2

Generally speaking, that could be tricky. Your best bet is looking at mitmproxy.net.tcp’s Reader.


#3

Sorry for bumping but I may found something close to OP asked.

This is a script I used to test browser behavior on particularly slow css / js / etc. It adds a delay before streaming response body (it’s per-request, not connection).

Maybe one can base the condition of delay on flow.client_conn, and do a real throttle.

# usage: mitmproxy -s THIS_FILE.py
# add a delay before starting streaming response body to client 

from time import sleep
from urllib.parse import urlparse

def delay_before_streaming_response(flow):
    url = urlparse(flow.request.url)
    if url.path.lower().endswith('.css'):
        return 2
    return 0

def responseheaders(flow):
    def modify(chunks):
        sleep(delay_before_streaming_response(flow))
        # continue to stream original response
        yield from chunks
    flow.response.stream = modify