Last active
September 4, 2020 12:27
-
-
Save krzko/2b3a278ddf4df95c6afc67abd7eca3d6 to your computer and use it in GitHub Desktop.
Checks to see if alpaca proxy is running, restarts accordingly on network changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Script to restart/start alpaca on network changes. | |
# | |
# To register plist: | |
# launchctl load network_change.plist | |
# launchctl start network_change | |
# | |
# Vars | |
BIN="/usr/local/bin/alpaca" | |
DNS_SERVER="1.1.1.1" | |
FILE="/etc/resolv.conf" | |
PAC="http://some_proxy_server:8080/some_pac_file.pac" | |
PROCESS="alpaca" | |
# | |
# Functions | |
function do_alert { | |
local title=$1 | |
local message=$2 | |
osascript -e "display notification \"$message\" with title \"$title\"" | |
} | |
function check_file_contents { | |
local contents=$1 | |
local file=$2 | |
echo "===> Checking $file for $contents" | |
# sleep 3 | |
grep -q $contents $file | |
} | |
function check_process { | |
local process=$1 | |
echo "===> Checking process: $process" | |
if pgrep -x $process >/dev/null | |
then | |
# 0 = true | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function kill_process { | |
local process=$1 | |
echo "===> Killing process: $process" | |
pkill $process | |
} | |
function start_alpaca { | |
local bin=$1 | |
echo "===> Starting binary: $bin" | |
$bin | |
do_alert "Alpaca" "Started $PROCESS at 🏠 home" | |
} | |
function start_alpaca_at_work { | |
local bin=$1 | |
local pac=$2 | |
echo "===> Starting binary: $bin" | |
$bin -C $pac | |
do_alert "Alpaca" "Started $PROCESS at 🏭 work" | |
} | |
# | |
# Main | |
echo "===> Network change detected..." | |
if check_file_contents $DNS_SERVER $FILE | |
then | |
echo "===> Welcome home" | |
echo "===> Getting process: $PROCESS" | |
if check_process $PROCESS | |
then | |
echo "===> Restarting process: $PROCESS" | |
kill_process $PROCESS | |
start_alpaca $BIN | |
else | |
start_alpaca $BIN | |
fi | |
else | |
echo "===> Off to work we go..." | |
echo "===> Getting process: $PROCESS" | |
if check_process $PROCESS | |
then | |
echo "===> Restarting process: $PROCESS" | |
kill_process $PROCESS | |
start_alpaca_at_work $BIN $PAC | |
else | |
start_alpaca_at_work $BIN $PAC | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment