Skip to content

Instantly share code, notes, and snippets.

@jkereako
Last active March 24, 2025 16:07
Show Gist options
  • Save jkereako/6aeeb4e2d869cf1cd92b978a6dd1c95b to your computer and use it in GitHub Desktop.
Save jkereako/6aeeb4e2d869cf1cd92b978a6dd1c95b to your computer and use it in GitHub Desktop.
Automatically toggle device proxies when mitmproxy starts and stops.
# Written by GitHub Copilot
#
# Automatically sets the system proxies for HTTP and HTTPS to 127.0.0.1:8080
#
# This only works on on macOS
import subprocess
import logging
class SetSystemProxy:
def __init__(self):
# List your devices here.
self.devices = ["USB-C Dock Ethernet", "Wi-Fi"]
self.host = "127.0.0.1"
self.port = "8080"
def load(self, loader):
logging.info("Setting proxies for %s to 127.0.0.1:8080" % ', '.join(self.devices))
self.set_proxy()
def done(self):
logging.info("Turning off proxies for %s" % ', '.join(self.devices))
self.unset_proxy()
def set_proxy(self):
for device in self.devices:
try:
# Set HTTP proxy
subprocess.run(["networksetup", "-setwebproxy", device, self.host, self.port], check=True)
subprocess.run(["networksetup", "-setwebproxystate", device, "on"], check=True)
# Set HTTPS proxy
subprocess.run(["networksetup", "-setsecurewebproxy", device, self.host, self.port], check=True)
subprocess.run(["networksetup", "-setsecurewebproxystate", device, "on"], check=True)
logging.info("Successfully updated proxy for %s." % device)
except subprocess.CalledProcessError as e:
logging.error(f"Failed to set proxy settings: {e}")
def unset_proxy(self):
for device in self.devices:
try:
# Unset HTTP proxy
subprocess.run(["networksetup", "-setwebproxystate", device, "off"], check=True)
# Unset HTTPS proxy
subprocess.run(["networksetup", "-setsecurewebproxystate", device, "off"], check=True)
logging.info("Successfully turned off proxy for %s." % device)
except subprocess.CalledProcessError as e:
logging.error(f"Failed to unset proxy settings: {e}")
addons = [
SetSystemProxy()
]
@jkereako
Copy link
Author

Description

This script automatically turns on proxies for as many network devices as you indicate in self.devices.

Usage

First, open terminal and type networksetup -listnetworkserviceorder. This will list all of the available network devices.

❯ networksetup -listnetworkserviceorder
An asterisk (*) denotes that a network service is disabled.
(1) USB-C Dock Ethernet
(Hardware Port: USB-C Dock Ethernet, Device: en7)

(2) USB 10/100/1000 LAN
(Hardware Port: USB 10/100/1000 LAN, Device: en8)

(3) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)

(4) Thunderbolt Bridge
(Hardware Port: Thunderbolt Bridge, Device: bridge0)

Copy the name of the network devices and add at least one of them to self.devices. To trigger this script when mitmproxy launches, add this script to your mitmproxy config file. In the example below, I placed this in the directory ~/.mitmproxy/scripts.

❯ cat ~/.mitmproxy/config.yaml         
--- 
console_eventlog_verbosity: info
console_mouse: false
console_palette: dark
scripts: 
  - ~/.mitmproxy/scripts/set_system_proxy.py
flow_detail: 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment