Skip to content

Instantly share code, notes, and snippets.

@maelvls
Last active November 10, 2023 13:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maelvls/f71b6a9539a13d4fa7ff84d74491eb94 to your computer and use it in GitHub Desktop.
Save maelvls/f71b6a9539a13d4fa7ff84d74491eb94 to your computer and use it in GitHub Desktop.
A mitmproxy script that makes sure the GET requests with ?watch=true (Kubernetes clients) are properly streamed
"""
Enable the streaming mode [1] whenever a request contains the query
parameter ?watch=true. The watch=true parameter is passed by Kubernertes
clients (such as client-go) when they intend to be notified of object
updates.
Use with:
mitmproxy -p 9090 -s watch-stream.py
[1]: https://docs.mitmproxy.org/stable/overview-features/#streaming
"""
from mitmproxy import ctx, http
import mitmproxy.coretypes.multidict
def responseheaders(flow: http.HTTPFlow):
"""
Enables streaming for all requests that contain the query
param?watch=true.
"""
q: mitmproxy.coretypes.multidict.MultiDictView = flow.request.query
ctx.log.info("query params: " + q.__str__())
watch = ""
try:
watch = q["watch"]
except KeyError as _:
pass
flow.response.stream = False
if watch == "true":
flow.response.stream = True
ctx.log.info("streaming request")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment