Search code examples
pythoncurlreverse-proxymitmproxy

mitmproxy redirect request to localhost and set Host header


I'm running a http server locally on port 8181. I have my mitmproxy configured on my devices and now I need to forward requests from a specific domain to my local server instance. So I'm using the request redirect example script described here:

def request(flow):
    # pretty_host takes the "Host" header of the request into account,
    # which is useful in transparent mode where we usually only have the IP
    # otherwise.

    # Method 2: Redirect the request to a different server
    if flow.request.pretty_host.endswith("mydomain.com"):
        flow.request.headers["Host"] = flow.request.headers
        flow.request.host = "localhost"
        flow.request.port = 8181
        flow.request.scheme = 'http'

This works, but I need to set the Host header to the original request host so I followed the add header example described here doing like

flow.request.headers["Host"] = flow.request.headers

but when in mitmproxy I cannot see this header set in the request, and I don't get it on the localhost server logs.

So what I want to do it the same as cURL

curl -b c -c c "http://localhost:8181/something" -H "Host: mydomain.com"

in order to have at least the needed request headers like

*   Trying ::1...
* Connected to localhost (::1) port 8181 (#0)
> GET /something HTTP/1.1
> Host:mydomain.com
> User-Agent: curl/7.43.0
> Accept: */*

Solution

  • try: flow.request.headers["Host"] = ["mydomain.com"] refer: https://github.com/mitmproxy/mitmproxy/blob/v0.9.2/examples/redirect_requests.py