Skip to content

Instantly share code, notes, and snippets.

@JerryFleming
Created March 28, 2014 04:16
Show Gist options
  • Save JerryFleming/9825285 to your computer and use it in GitHub Desktop.
Save JerryFleming/9825285 to your computer and use it in GitHub Desktop.
Set up proxy for terminial and GUI apps (cocoa) on Mac. Use '. proxy.sh [none|atl|sgp|corp|china]' to execute.
#!/bin/bash
# Available choices:
# [atl]=web-proxy.atl.hp.com
# [sgp]=web-proxy.sgp.hp.com
# [corp]=web-proxy.corp.hp.com
# [china]=web-proxy.china.hp.com
#
# web-proxy.aus.hp.com port 8080
# proxy.houston.hp.com port 8080
# proxy.austin.hp.com port 8080
# proxy.sgp.hp.com port 8080
# web-proxy.rose.hp.com port 8088
#
SERVICE=Wi-Fi
networksetup='sudo networksetup'
_PORT=:8080
__PORT=${_PORT#:}
_PROTOCOL=http://
select=${1:-sgp}
if [[ $select == none ]]
then
_PROXY=
elif [[ $select == ?*.*? ]]
then
_PROXY=$select
else
_PROXY=web-proxy.$select.hp.com
fi
[ -n "$_PROXY" ] && {
export proxy=$_PROTOCOL$_PROXY$_PORT # for curl, etc
export http_proxy=$_PROTOCOL$_PROXY$_PORT
export https_proxy=$_PROTOCOL$_PROXY$_PORT
export ftp_proxy=$_PROTOCOL$_PROXY$_PORT
export all_proxy=socks5h://$_PROXY:1080
export no_proxy='localhost,127.0.0.0/8,192.168.0.0/16,public,*.local'
proxycommand="ncat --proxy $_PROXY$_PORT %h %p"
sed -Ei '' "s/ProxyCommand (none|ncat).*$/ProxyCommand $proxycommand/" ~/.ssh/config
sudo echo 'ncat --proxy '$_PROXY$_PORT' $*' > /etc/gitproxy
export GIT_PROXY_COMMAND=/etc/gitproxy
$networksetup -setwebproxy $SERVICE $_PROXY $__PORT off
$networksetup -setsecurewebproxy $SERVICE $_PROXY $__PORT off
$networksetup -setftpproxy $SERVICE $_PROXY $__PORT off
$networksetup -setsocksfirewallproxy $SERVICE $_PROXY 1080 off
$networksetup -setwebproxystate $SERVICE on
$networksetup -setsecurewebproxystate $SERVICE on
$networksetup -setftpproxystate $SERVICE on
$networksetup -setsocksfirewallproxystate $SERVICE on
} || {
unset proxy_command
unset proxy
unset http_proxy
unset https_proxy
unset ftp_proxy
unset all_proxy
unset no_proxy
unset GIT_PROXY_COMMAND
sed -i '' 's/ProxyCommand ncat.*$/ProxyCommand none/' ~/.ssh/config
$networksetup -setwebproxystate $SERVICE off
$networksetup -setsecurewebproxystate $SERVICE off
$networksetup -setftpproxystate $SERVICE off
$networksetup -setsocksfirewallproxystate $SERVICE off
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment