Skip to content

Instantly share code, notes, and snippets.

@raiman264
Created May 24, 2022 05:41
Show Gist options
  • Save raiman264/3e6421d2d7c09920a43c7ca7166c67c2 to your computer and use it in GitHub Desktop.
Save raiman264/3e6421d2d7c09920a43c7ca7166c67c2 to your computer and use it in GitHub Desktop.
Add cookies to a reversed proxy call
# docker run --rm -it -v ~/www/scripts:/home/mitmproxy/scripts -p 8080:8080 mitmproxy/mitmproxy mitmproxy --mode reverse:https://domaintorevers.com -s ./add-cookies.py
# mitmproxy --mode reverse:https://domaintorevers.com -s ./add-cookies.py
from mitmproxy import http
cookieToken = "COOKIE=value"
# def request(flow: http.HTTPFlow) -> None:
# flow.request.headers["cookie"] = cookieToken
def response(flow):
h = flow.request.headers
origin = h['origin'] if 'origin' in h else '*'
flow.response.headers["Access-Control-Allow-Origin"] = origin
def request(flow):
print(flow.request.method, flow.request.url)
# print(flow.request.headers)
# print(dir(flow.request)) # method host path url pretty_url port pretty_host
# print(flow.request.host)
# print(flow.request.path)
flow.request.headers["cookie"] = cookieToken
if flow.request.method == "OPTIONS":
h = flow.request.headers
origin = h['origin'] if 'origin' in h else '*'
flow.response = http.HTTPResponse.make(
200,
b"",
{
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Methods": "GET,POST",
"Access-Control-Allow-Headers": "Authorization",
"Access-Control-Max-Age": "1728000"
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment