How to redirect HTTPS traffic to local HTTP server using mitmproxy?
Asked Answered
D

1

9

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.

Darkish answered 23/8, 2019 at 13:16 Comment(0)
D
7

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
Darkish answered 25/8, 2019 at 9:14 Comment(4)
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.)Claudell
@IlyaSemenov mitm uses port 8080 by default. Did u install the cert and configure chrome correctlyDarkish
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.Claudell
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?Decelerate

© 2022 - 2024 — McMap. All rights reserved.