Last active
August 18, 2016 20:45
-
-
Save smj10j/57f98e7fe6b106c39d3745dbb9d0a9ab to your computer and use it in GitHub Desktop.
Create an ad-hoc privoxy proxy
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
#!/bin/bash | |
# activate debugging | |
#set -x | |
# fail on any errors | |
set -e | |
INTERFACE=Wi-Fi | |
HOST=localhost | |
PORT=8118 | |
REMOTEHOSTS=( "proxy.01j.me:3128" ) | |
SHELL_GLOBAL_PROFILE=/etc/zprofile | |
################################## | |
##### DO NOT EDIT BELOW HERE ##### | |
################################## | |
PRIVOXY_ALREADY_RUNNING=0 | |
REMOTEHOST=${REMOTEHOSTS[$RANDOM % ${#REMOTEHOSTS[@]}]} | |
PRIVOXY_SERVICE_CHECK_OUTPUT=$(brew services list | (grep "privoxy\s*started" || echo "")) | |
[ -n "${PRIVOXY_SERVICE_CHECK_OUTPUT}" ] && PRIVOXY_ALREADY_RUNNING=1 | |
# Ask for the administrator password upfront | |
sudo -v | |
function disable_proxy() { | |
echo -n "$(tput setaf 136)" # orange | |
echo "Disabling HTTP/HTTPS proxy..." | |
sudo networksetup -setwebproxystate $INTERFACE off | |
sudo networksetup -setsecurewebproxystate $INTERFACE off | |
unset http_proxy && unset https_proxy | |
sudo sed -i ':a;N;$!ba;s/export https\?_proxy=.*//g' $SHELL_GLOBAL_PROFILE | |
sudo sed -i ':begin;$!N;$!N;s/\n\n$//;tbegin;P;D' $SHELL_GLOBAL_PROFILE # Remove double newlines from file | |
echo -n "$(tput setaf 64)" #green | |
echo "HTTP/HTTPS proxy disabled." | |
if [[ $PRIVOXY_ALREADY_RUNNING -eq 0 ]]; then | |
echo -n "$(tput setaf 136)" # orange | |
echo "Stopping Privoxy..." | |
brew services stop privoxy | |
echo -n "$(tput setaf 64)" #green | |
echo "Stopped Privoxy." | |
else | |
echo -n "$(tput setaf 136)" # orange | |
#echo "-- At Launch --" | |
#echo "${PRIVOXY_SERVICE_CHECK_OUTPUT}" | |
#echo "-- Now --" | |
#brew services list | egrep "privoxy\s+started" | |
echo "Privoxy was previously running so not stopping it." | |
fi | |
echo -n "$(tput sgr0)" # color reset | |
exit 0 | |
} | |
#This function is used to cleanly exit any script. It does this displaying a | |
# given error message, and exiting with an error code. | |
function error_exit { | |
echo -n "$(tput setaf 1)" # red | |
echo "$@" | |
echo -n "$(tput sgr0)" # color reset | |
disable_proxy | |
exit 1 | |
} | |
#Trap the killer signals so that we can exit with a good message. | |
trap "error_exit 'Received signal SIGHUP'" SIGHUP | |
trap "error_exit 'Received signal SIGINT'" SIGINT | |
trap "error_exit 'Received signal SIGTERM'" SIGTERM | |
trap "error_exit 'Received signal SIGKILL'" SIGKILL | |
trap "error_exit 'Received signal ERR'" ERR | |
#Alias the function so that it will print a message with the following format: | |
#prog-name(@line#): message | |
#We have to explicitly allow aliases, we do this because they make calling the | |
#function much easier (see example). | |
shopt -s expand_aliases | |
alias die='error_exit "Error ${0}(@`echo $(( $LINENO - 1 ))`):"' | |
# Let's roll | |
echo -n "$(tput setaf 136)" # orange | |
echo "Enabling HTTP proxy..." | |
sudo networksetup -setwebproxy $INTERFACE $HOST $PORT | |
sudo networksetup -setwebproxystate $INTERFACE on | |
echo "export http_proxy=$HOST:$PORT" | sudo tee -a $SHELL_GLOBAL_PROFILE | |
echo "Enabling HTTPS proxy..." | |
sudo networksetup -setsecurewebproxy $INTERFACE $HOST $PORT | |
sudo networksetup -setsecurewebproxystate $INTERFACE on | |
echo "export https_proxy=${REMOTEHOST%:*}:${REMOTEHOST#*:}" | sudo tee -a $SHELL_GLOBAL_PROFILE | |
echo -n "$(tput setaf 64)" # green | |
echo "HTTP/HTTPS proxy $HOST:$PORT enabled." | |
if [[ $PRIVOXY_ALREADY_RUNNING -eq 0 ]]; then | |
echo -n "$(tput setaf 136)" # orange | |
echo "Starting Privoxy..." | |
brew services start privoxy | |
echo -n "$(tput setaf 64)" # green | |
echo "Privoxy started." | |
else | |
echo -n "$(tput setaf 136)" # orange | |
echo "Existing Privoxy service is already running. We will not attempt to start it." | |
#echo "${PRIVOXY_SERVICE_CHECK_OUTPUT}" | |
fi | |
echo -n "$(tput setaf 64)" # green | |
echo "Privoxy is running." | |
echo -n "$(tput sgr0)" # color reset | |
# # Enable the SOCKS5 proxy | |
# ssh-add -l || ssh-add | |
# ./socks5-tunneled-proxy.sh & | |
# | |
# # Enable the Tor proxy & | |
# ./tor-proxy.sh | |
# Keep-alive: update existing `sudo` time stamp until finished | |
while read -n1; do | |
sleep 1 | |
done 2>/dev/null # trap ctrl-c and call disable_proxy() |
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
#!/bin/bash | |
# activate debugging | |
#set -x | |
# fail on any errors | |
set -e | |
INTERFACE=Wi-Fi | |
HOST=localhost | |
PORT=8118 | |
REMOTEHOSTS=( "host1:port" ) | |
SHELL_GLOBAL_PROFILE=/etc/zprofile | |
################################## | |
##### DO NOT EDIT BELOW HERE ##### | |
################################## | |
PRIVOXY_ALREADY_RUNNING=0 | |
REMOTEHOST=${REMOTEHOSTS[$RANDOM % ${#REMOTEHOSTS[@]}]} | |
PRIVOXY_SERVICE_CHECK_OUTPUT=$(brew services list | (grep "privoxy\s*started" || echo "")) | |
[ -n "${PRIVOXY_SERVICE_CHECK_OUTPUT}" ] && PRIVOXY_ALREADY_RUNNING=1 | |
# Ask for the administrator password upfront | |
sudo -v | |
function disable_proxy() { | |
echo -n "$(tput setaf 136)" # orange | |
echo "Disabling HTTP/HTTPS proxy..." | |
sudo networksetup -setwebproxystate $INTERFACE off | |
sudo networksetup -setsecurewebproxystate $INTERFACE off | |
unset http_proxy && unset https_proxy | |
sudo sed -i ':a;N;$!ba;s/export https\?_proxy=.*//g' $SHELL_GLOBAL_PROFILE | |
sudo sed -i ':begin;$!N;$!N;s/\n\n$//;tbegin;P;D' $SHELL_GLOBAL_PROFILE # Remove double newlines from file | |
echo -n "$(tput setaf 64)" #green | |
echo "HTTP/HTTPS proxy disabled." | |
if [[ $PRIVOXY_ALREADY_RUNNING -eq 0 ]]; then | |
echo -n "$(tput setaf 136)" # orange | |
echo "Stopping Privoxy..." | |
brew services stop privoxy | |
echo -n "$(tput setaf 64)" #green | |
echo "Stopped Privoxy." | |
else | |
echo -n "$(tput setaf 136)" # orange | |
#echo "-- At Launch --" | |
#echo "${PRIVOXY_SERVICE_CHECK_OUTPUT}" | |
#echo "-- Now --" | |
#brew services list | egrep "privoxy\s+started" | |
echo "Privoxy was previously running so not stopping it." | |
fi | |
echo -n "$(tput sgr0)" # color reset | |
exit 0 | |
} | |
#This function is used to cleanly exit any script. It does this displaying a | |
# given error message, and exiting with an error code. | |
function error_exit { | |
echo -n "$(tput setaf 1)" # red | |
echo "$@" | |
echo -n "$(tput sgr0)" # color reset | |
disable_proxy | |
exit 1 | |
} | |
#Trap the killer signals so that we can exit with a good message. | |
trap "error_exit 'Received signal SIGHUP'" SIGHUP | |
trap "error_exit 'Received signal SIGINT'" SIGINT | |
trap "error_exit 'Received signal SIGTERM'" SIGTERM | |
trap "error_exit 'Received signal SIGKILL'" SIGKILL | |
trap "error_exit 'Received signal ERR'" ERR | |
#Alias the function so that it will print a message with the following format: | |
#prog-name(@line#): message | |
#We have to explicitly allow aliases, we do this because they make calling the | |
#function much easier (see example). | |
shopt -s expand_aliases | |
alias die='error_exit "Error ${0}(@`echo $(( $LINENO - 1 ))`):"' | |
# Let's roll | |
echo -n "$(tput setaf 136)" # orange | |
echo "Enabling HTTP proxy..." | |
sudo networksetup -setwebproxy $INTERFACE $HOST $PORT | |
sudo networksetup -setwebproxystate $INTERFACE on | |
echo "export http_proxy=$HOST:$PORT" | sudo tee -a $SHELL_GLOBAL_PROFILE | |
echo "Enabling HTTPS proxy..." | |
sudo networksetup -setsecurewebproxy $INTERFACE $HOST $PORT | |
sudo networksetup -setsecurewebproxystate $INTERFACE on | |
echo "export https_proxy=${REMOTEHOST%:*}:${REMOTEHOST#*:}" | sudo tee -a $SHELL_GLOBAL_PROFILE | |
echo -n "$(tput setaf 64)" # green | |
echo "HTTP/HTTPS proxy $HOST:$PORT enabled." | |
if [[ $PRIVOXY_ALREADY_RUNNING -eq 0 ]]; then | |
echo -n "$(tput setaf 136)" # orange | |
echo "Starting Privoxy..." | |
brew services start privoxy | |
echo -n "$(tput setaf 64)" # green | |
echo "Privoxy started." | |
else | |
echo -n "$(tput setaf 136)" # orange | |
echo "Existing Privoxy service is already running. We will not attempt to start it." | |
#echo "${PRIVOXY_SERVICE_CHECK_OUTPUT}" | |
fi | |
echo -n "$(tput setaf 64)" # green | |
echo "Privoxy is running." | |
echo -n "$(tput sgr0)" # color reset | |
# # Enable the SOCKS5 proxy | |
# ssh-add -l || ssh-add | |
# ./socks5-tunneled-proxy.sh & | |
# | |
# # Enable the Tor proxy & | |
# ./tor-proxy.sh | |
# Keep-alive: update existing `sudo` time stamp until finished | |
while read -n1; do | |
sleep 1 | |
done 2>/dev/null # trap ctrl-c and call disable_proxy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment