7

I am trying to setup mitmproxy so that I can make a request from my browser to https://{my-domain} and have it return a response from my local server running at http://localhost:3000 instead, but I cannot get the https request to reach my local server. I see the debugging statements from mitmproxy. Also, I can get it working for http traffic, but not for https.

I read the mitmproxy addon docs and api docs I've installed the cert and I can monitor https through the proxy.

I'm using Mitmproxy: 4.0.4 and Python: 3.7.4

This is my addon (local-redirect.py) and how I run mitmproxy:

from mitmproxy import ctx
import mitmproxy.http

class LocalRedirect:

  def __init__(self):
    print('Loaded redirect addon')

  def request(self, flow: mitmproxy.http.HTTPFlow):
    if 'my-actual-domain-here' in flow.request.pretty_host:
      ctx.log.info("pretty host is: %s" % flow.request.pretty_host)
      flow.request.host = "localhost"
      flow.request.port = 3000
      flow.request.scheme = 'http'

addons = [
  LocalRedirect()
]
$ mitmdump -s local-redirect.py | grep pretty

When I visit the url form my server, I see the logging statement, but my browser hangs on the request and there is no request made to my local server.

J W
  • 1,170
  • 7
  • 15

1 Answers1

5

The above addon was fine, however my local server did not support HTTP2.

Using the --no-http2 option was a quick fix:

mitmproxy -s local-redirect.py --no-http2 --view-filter localhost

or

mitmdump -s local-redirect.py --no-http2 localhost
J W
  • 1,170
  • 7
  • 15
  • 1
    How did you ever had it work? For me, `request` isn't called during `CONNECT` requests (which Chrome sends for https:// URLs when using a proxy.) – Ilya Semenov Nov 21 '19 at 09:12
  • @IlyaSemenov mitm uses port `8080` by default. Did u [install the cert](https://docs.mitmproxy.org/stable/concepts-certificates/) and configure chrome correctly – J W Nov 22 '19 at 11:31
  • 1
    Of course I did. The requests *are* coming through mitmdump (and the plugin), but `request` callback isn't called for https pages (it is for http pages). mitmproxy logs `CONNECT` (rather than `HTTP GET`) requests coming from Chrome when accessing https pages. Anyway, I switched to reverse proxy mode for my purposes. – Ilya Semenov Nov 25 '19 at 08:33
  • I have the same issue, but with an app I'm working on. So is this a thing that only happens when a proxy is active? – Paul May 30 '22 at 21:22