Tuesday, March 19, 2013

mitmproxy as a reverse proxy

It turns out that thanks to the powerful scripting interface, mitmproxy can be used as a reverse proxy to redirect calls to different servers easily:

revProxy.py

import os

proxyHost = os.environ['PROXYHOST']

def request(ctx, r):
   if r.request.path.startswith('/api'):
       # Proxy calls with /api prefix to proxyHost
       r.request.headers['Host'] = [proxyHost]
       r.request.scheme = 'https'
       r.request.host = proxyHost
       r.request.port = 443
   else:
       # Proxy other calls to localhost:8000 with some rewriting

       if r.request.path.endswith('/'):
           r.request.path += 'index.html'

       r.request.scheme = 'http'
       r.request.host = 'localhost'
       r.request.port = 8000
   # end if
# end def request
Then use mitmproxy as:
PROXYHOST="my.apps.domain" mitmproxy -s revProxy.py

1 comment:

Anonymous said...

Exactly what i was looking for, thank you!!!