Skip to content

Instantly share code, notes, and snippets.

@mietzen
Last active March 22, 2024 08:38
Show Gist options
  • Save mietzen/d0388cfc3229435ff315a473d76686e6 to your computer and use it in GitHub Desktop.
Save mietzen/d0388cfc3229435ff315a473d76686e6 to your computer and use it in GitHub Desktop.
macOS Mullvad connect on demand (Wifi SSID white/blacklisting)

This will autoconnect and disconnect mullvad on macOS based on white- and blacklisted wifi SSIDs

Make local binary directory if not existent:

mkdir -p ~/.local/bin

Get the script and make it executable:

wget -O ~/.local/bin/mullvad-wifi-connect-on-demand https://gist.githubusercontent.com/n-stone/d0388cfc3229435ff315a473d76686e6/raw/mullvad-wifi-connect-on-demand
chmod +x ~/.local/bin/mullvad-wifi-connect-on-demand

Get the service, load it and start it:

wget -O ~/Library/LaunchAgents/net.mullvad.wifi-connect-on-demand.plist https://gist.githubusercontent.com/n-stone/d0388cfc3229435ff315a473d76686e6/raw/net.mullvad.wifi-connect-on-demand.plist
launchctl load -w ~/Library/LaunchAgents/net.mullvad.wifi-connect-on-demand.plist
launchctl start net.mullvad.wifi-connect-on-demand.plist

Now Edit your wifi black / white list:

  • in CLI
nano $HOME/.config/mullvad/wifi-white-list.txt
nano $HOME/.config/mullvad/wifi-black-list.txt
  • or use Finder
open $HOME/.config/mullvad
#!/bin/bash
if [[ ! $OSTYPE == "darwin"* ]]; then
echo "This script is only for macOS"
exit 1
fi
WIFI_STATE=$(/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk -F' state: ' '/ state: / {print $2}')
if [[ ! $WIFI_STATE == "running" ]]; then
echo "No Wifi connection"
exit 0
fi
if [[ ! -f "/usr/local/bin/mullvad" ]] ; then
echo "Mullvad could not be found!"
exit 2
fi
SSID=$(/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk -F' SSID: ' '/ SSID: / {print $2}')
MULLVAD_CONFIG_FOLDER="$HOME/.config/mullvad"
SSID_WHITE_LIST="$MULLVAD_CONFIG_FOLDER/wifi-white-list.txt"
SSID_BLACK_LIST="$MULLVAD_CONFIG_FOLDER/wifi-black-list.txt"
if [[ ! -d "$MULLVAD_CONFIG_FOLDER" ]]; then
mkdir -p $MULLVAD_CONFIG_FOLDER
touch $SSID_WHITE_LIST
touch $SSID_BLACK_LIST
fi
if grep -Fxq $SSID $SSID_WHITE_LIST; then
if /usr/local/bin/mullvad status | grep -q 'Connect'; then # grep Conneted and Connecting
echo "$SSID is white listed, disconnecting..."
/usr/local/bin/mullvad disconnect
fi
fi
if grep -Fxq $SSID $SSID_BLACK_LIST; then
if /usr/local/bin/mullvad status | grep -q 'Disconnected'; then
echo "$SSID is black listed, connecting..."
/usr/local/bin/mullvad connect
fi
fi
exit 0
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>net.mullvad.wifi-connect-on-demand</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>exec $HOME/.local/bin/mullvad-wifi-connect-on-demand</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration</string>
</array>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment