0

I have an Android app, which is making request to example.com. How can I setup mitmproxy such that all requests to example.com/etc/else will go to dev.example.com/etc/else?

I tried this:

My script (rewrite.py):

import mitmproxy
from mitmproxy.models import HTTPResponse
from netlib.http import Headers

def request(context, flow):
    if 'example.com' in flow.request.url :
        flow.request.host = 'dev.example.com'

Also, for some reason I don't see logging output, for example:

from mitmproxy import ctx
 ...
ctx.log.info("This is some informative text.")

I'm running mitmproxy like this:

mitmproxy -p 8765 -e -s rewrite.py
valk
  • 9,363
  • 12
  • 59
  • 79
  • Which version of mitmproxy are you running? What happens if you run the same with mitmdump? – Maximilian Hils Dec 13 '16 at 12:05
  • @MaximilianHils my version is 0.18.2. Same with mitmdump just prints me CONNECT example.com:443, but not dev.example.com – valk Dec 13 '16 at 12:57
  • In addition `def request(context, flow)` needs only one argument, so I left it with `def request(flow)` – valk Dec 13 '16 at 12:59
  • 1
    Yes - we simplified the API with 0.18 in that regard; albeit there is a very unfortunate bug that silences loading errors. https://github.com/mitmproxy/mitmproxy/blob/0.18.x/examples/redirect_requests.py works fine for me - if you are still running into issues, please feel to just ping me in the mitmproxy slack for quicker turnaround times. Happy to update this here with a solution afterwards. :) – Maximilian Hils Dec 13 '16 at 14:59

1 Answers1

2

So with mitmproxy v0.18.2 the solution is:

import mitmproxy
from mitmproxy.models import HTTPResponse
from netlib.http import Headers
from mitmproxy import ctx

def request( flow):
    if flow.request.pretty_host.endswith('example.com'):
        flow.request.host = 'dev.example.com'
        flow.request.scheme = 'http'
        flow.request.port = 80
        ctx.log.info(" --->" + flow.request.url)
valk
  • 9,363
  • 12
  • 59
  • 79