diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/README | 1 | ||||
-rwxr-xr-x | examples/mitmproxywrapper.py | 89 | ||||
-rwxr-xr-x | examples/proxapp | 6 | ||||
-rwxr-xr-x | examples/stickycookies | 8 |
4 files changed, 97 insertions, 7 deletions
diff --git a/examples/README b/examples/README index 0a4684a7..46d25acd 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 00000000..48963896 --- /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() + diff --git a/examples/proxapp b/examples/proxapp index eb5bdbb7..3a94cd55 100755 --- a/examples/proxapp +++ b/examples/proxapp @@ -1,8 +1,8 @@ #!/usr/bin/env python """ - This example shows how to graft a WSGI app onto mitmproxy. In this - instance, we're using the Bottle framework (http://bottlepy.org/) to expose - a single simplest-possible page. +This example shows how to graft a WSGI app onto mitmproxy. In this +instance, we're using the Bottle framework (http://bottlepy.org/) to expose +a single simplest-possible page. """ import bottle import os diff --git a/examples/stickycookies b/examples/stickycookies index b07820fc..17cd6019 100755 --- a/examples/stickycookies +++ b/examples/stickycookies @@ -1,9 +1,9 @@ #!/usr/bin/env python """ - This example builds on mitmproxy's base proxying infrastructure to - implement functionality similar to the "sticky cookies" option. This is at - a lower level than the Flow mechanism, so we're dealing directly with - request and response objects. +This example builds on mitmproxy's base proxying infrastructure to +implement functionality similar to the "sticky cookies" option. This is at +a lower level than the Flow mechanism, so we're dealing directly with +request and response objects. """ from libmproxy import controller, proxy import os |