Skip to content

Instantly share code, notes, and snippets.

@kontrafiktion
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kontrafiktion/8782042 to your computer and use it in GitHub Desktop.
Save kontrafiktion/8782042 to your computer and use it in GitHub Desktop.
#!/bin/bash
# make sure, script is sourced
EXECUTED=`echo "$BASH"`
if [[ "$EXECUTED" != "" ]]; then
echo "the script must be sourced not executed. Aborting"
exit 1
fi
# make sure, script runs on OS X
if [ "Darwin" != `uname` ]; then
echo "This script is only tested on OSX. Aborting"
return 1
fi
# make sure, pacparser is installed
type pacparser >/dev/null 2>&1 || { echo >&2 "'pacparser' required. Aborting."; return 2; }
NETWORK=$1
if [ -z "$NETWORK" ]; then
echo "using 'Wi-Fi' (call 'networksetup -listallnetworkservices' for a list of network services)"
NETWORK="Wi-Fi"
# networksetup -listallnetworkservices
fi
function enabled() {
echo "$1" | grep "^Enabled" | sed "s/[a-zA-Z]*: \(.*\)/\1/"
}
#--------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------
# proxy explicitely set?
PROXY_INFO=`networksetup -getwebproxy "$NETWORK"`
PROXY_ENABLED=`enabled "$PROXY_INFO"`
if [ "$PROXY_ENABLED" = "Yes" ]; then
export PROXY_HOST=`echo "$PROXY_INFO" | grep Server | sed "s/[a-zA-Z]*: \(.*\)/\1/"`
export PROXY_PORT=`echo "$PROXY_INFO" | grep Port | sed "s/[a-zA-Z]*: \(.*\)/\1/"`
PROXY="$PROXY_HOST:$PROXY_PORT"
else
# proxy not explicitely set, try proxy.pac
PROXY_INFO=`networksetup -getautoproxyurl $NETWORK`
PROXY_ENABLED=`enabled "$PROXY_INFO"`
if [ "$PROXY_ENABLED" = "Yes" ]; then
PROXY_PAC=`echo "$PROXY_INFO" | grep "^URL:" | sed "s/URL: //"`
echo "proxy.pac: $PROXY_PAC"
if [ -e /tmp/proxy.pac ]; then
if test "`find /tmp/proxy.pac -mmin +1440`"; then
curl $PROXY_PAC > /tmp/proxy.pac
fi
else
curl $PROXY_PAC > /tmp/proxy.pac
fi
PROXY=`pacparser | sed "s/PROXY //"`
PROXY_PARTS=( `echo "$PROXY" | tr -s ':' ' '` )
export PROXY_HOST="$PROXY_PARTS[1]"
export PROXY_PORT="$PROXY_PARTS[2]"
fi
fi
echo "$PROXY"
# set/unset environment variables, git and Maven settings
if [ "$PROXY_ENABLED" = "Yes" ]; then
echo "setting http_proxy: $PROXY"
export http_proxy=`echo "$PROXY"`
export https_proxy=`echo "$PROXY"`
git config --global http.proxy "http://$PROXY"
mvn-proxy-patch true
else
echo "unset http_proxy"
export http_proxy=
git config --global --unset http.proxy
mvn-proxy-patch false
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment