Skip to content

Instantly share code, notes, and snippets.

@manmal
Last active October 24, 2020 11:08
Show Gist options
  • Save manmal/1f1facb38a67fbfe2e29e75658b30c9f to your computer and use it in GitHub Desktop.
Save manmal/1f1facb38a67fbfe2e29e75658b30c9f to your computer and use it in GitHub Desktop.
Remove macOS proxies set by Charles, unless Charles is running
# Whenever Charles is quit unexpectedly (e.g. hard reboot due to kernel panic),
# it leaves its proxy settings active, breaking outbound internet connections.
# This has happened so often to me recently that I decided I need a script for this.
#
# Store this in an .sh file, and make it executable with
# chmod x+ {PATH_TO_FILE}
# I put it in my user's crontab to run every minute (even when logged out):
# echo "* * * * * /Users/{YOUR_USERNAME}/{...}/remove_proxies_unless_charles_runs.sh" | crontab -
# Since I'm impatient, I want this to run every 30 seconds even, so I also added:
# echo "* * * * * sleep 30; /Users/{YOUR_USERNAME}/{...}/remove_proxies_unless_charles_runs.sh" | crontab -
# Note that the script will only run in cron if you used the chmod command on the file.
#
# I checked with `times` how long `networksetup -setwebproxystate` and `networksetup -getwebproxystate`
# run, and it turns out that `-setwebproxystate` returns faster than `-getwebproxystate`. So querying
# the state before setting it would likely be worse, perfomance-wise.
#!/bin/bash
charlesrunning=$(ps aux | grep -v grep | grep -c Charles)
if [[ "$charlesrunning" == "1" ]] ;then
echo "Charles is running, doing nothing"
else
echo "Charles is not running, disabling proxies"
/usr/sbin/networksetup -setwebproxystate Wi-Fi off
/usr/sbin/networksetup -setsecurewebproxystate Wi-Fi off
# Substitute with your own additional interface name(s):
/usr/sbin/networksetup -setwebproxystate "USB 10/100/1000 LAN" off
/usr/sbin/networksetup -setsecurewebproxystate "USB 10/100/1000 LAN" off
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment