From 50ac988363afcfc463bdca19c6aab6fb09e27c03 Mon Sep 17 00:00:00 2001 From: Marc Liyanage Date: Wed, 15 May 2013 15:24:18 -0700 Subject: [PATCH 1/2] Add mitmproxywrapper.py --- examples/README | 1 + examples/mitmproxywrapper.py | 89 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100755 examples/mitmproxywrapper.py diff --git a/examples/README b/examples/README index 0a4684a7f..46d25acd2 100644 --- a/examples/README +++ b/examples/README @@ -7,3 +7,4 @@ proxapp How to embed a WSGI app in a mitmproxy server stub.py Script stub with a method definition for every event. stickycookies An example of writing a custom proxy with libmproxy. upsidedownternet.py Rewrites traffic to turn PNGs upside down. +mitmproxywrapper.py Bracket mitmproxy run with proxy enable/disable on OS X diff --git a/examples/mitmproxywrapper.py b/examples/mitmproxywrapper.py new file mode 100755 index 000000000..48963896b --- /dev/null +++ b/examples/mitmproxywrapper.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +# +# Helper tool to enable/disable OS X proxy and wrap mitmproxy +# +# Get usage information with: +# +# mitmproxywrapper.py -h +# + +import subprocess +import re +import argparse + +class Wrapper(object): + + def __init__(self, port): + self.port = port + self.primary_service_name = self.find_primary_service_name() + + def run_networksetup_command(self, *arguments): + return subprocess.check_output(['sudo', 'networksetup'] + list(arguments)) + + def proxy_state_for_service(self, service): + state = self.run_networksetup_command('-getwebproxy', service).splitlines() + return dict([re.findall(r'([^:]+): (.*)', line)[0] for line in state]) + + def enable_proxy_for_service(self, service): + print 'Enabling proxy on {}...'.format(service) + for subcommand in ['-setwebproxy', '-setsecurewebproxy']: + self.run_networksetup_command(subcommand, service, '127.0.0.1', str(self.port)) + + def disable_proxy_for_service(self, service): + print 'Disabling proxy on {}...'.format(service) + for subcommand in ['-setwebproxystate', '-setsecurewebproxystate']: + self.run_networksetup_command(subcommand, service, 'Off') + + def interface_name_to_service_name_map(self): + order = self.run_networksetup_command('-listnetworkserviceorder') + mapping = re.findall(r'\(\d+\)\s(.*)$\n\(.*Device: (.+)\)$', order, re.MULTILINE) + return dict([(b, a) for (a, b) in mapping]) + + def run_command_with_input(self, command, input): + popen = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + (stdout, stderr) = popen.communicate(input) + return stdout + + def primary_interace_name(self): + scutil_script = 'get State:/Network/Global/IPv4\nd.show\n' + stdout = self.run_command_with_input('/usr/sbin/scutil', scutil_script) + interface, = re.findall(r'PrimaryInterface\s*:\s*(.+)', stdout) + return interface + + def find_primary_service_name(self): + return self.interface_name_to_service_name_map()[self.primary_interace_name()] + + def proxy_enabled_for_service(self, service): + return self.proxy_state_for_service(service)['Enabled'] == 'Yes' + + def toggle_proxy(self): + if self.proxy_enabled_for_service(self.primary_service_name): + self.disable_proxy_for_service(self.primary_service_name) + else: + self.enable_proxy_for_service(self.primary_service_name) + + def wrap_mitmproxy(self): + if not self.proxy_enabled_for_service(self.primary_service_name): + self.enable_proxy_for_service(self.primary_service_name) + + subprocess.check_call(['mitmproxy', '-p', str(self.port), '--palette', 'light']) + + if self.proxy_enabled_for_service(self.primary_service_name): + self.disable_proxy_for_service(self.primary_service_name) + + @classmethod + def main(cls): + parser = argparse.ArgumentParser(description='Helper tool for OS X proxy configuration and mitmproxy') + parser.add_argument('-t', '--toggle', action='store_true', help='just toggle the proxy configuration') + parser.add_argument('-p', '--port', type=int, help='override the default port of 8080', default=8080) + args = parser.parse_args() + + wrapper = cls(port=args.port) + if args.toggle: + wrapper.toggle_proxy() + else: + wrapper.wrap_mitmproxy() + +if __name__ == '__main__': + Wrapper.main() + From aaba1bd7d10556f44922df9d0ef4f775b6f4d6ae Mon Sep 17 00:00:00 2001 From: Kit Randel Date: Fri, 24 May 2013 15:23:47 +1200 Subject: [PATCH 2/2] Added requirements.txt to ease installation of deps for hacking on mitmproxy. --- README.mkd | 2 ++ requirements.txt | 14 ++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 requirements.txt diff --git a/README.mkd b/README.mkd index a711d506c..8f564d7ff 100644 --- a/README.mkd +++ b/README.mkd @@ -55,6 +55,8 @@ The following components are needed if you plan to hack on mitmproxy: framework and requires [pathod](http://pathod.org) and [flask](http://flask.pocoo.org/). * Rendering the documentation requires [countershape](http://github.com/cortesi/countershape). +For convenience, all dependencies save countershape, can be installed from pypi to a virtualenv with 'pip install -r requirements.txt'. + Please ensure that all patches are accompanied by matching changes in the test suite. The project maintains 100% test coverage. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..ce1659aad --- /dev/null +++ b/requirements.txt @@ -0,0 +1,14 @@ +Flask==0.9 +Jinja2==2.7 +MarkupSafe==0.18 +PIL==1.1.7 +Werkzeug==0.8.3 +lxml==3.2.1 +netlib==0.9 +nose==1.3.0 +pathod==0.9 +pyOpenSSL==0.13 +pyasn1==0.1.7 +requests==1.2.2 +urwid==1.1.1 +wsgiref==0.1.2