Skip to content

Instantly share code, notes, and snippets.

@kernelsmith
Last active January 14, 2016 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kernelsmith/7ec5bfb7f13c76671727 to your computer and use it in GitHub Desktop.
Save kernelsmith/7ec5bfb7f13c76671727 to your computer and use it in GitHub Desktop.
easy_shell_proxy
#
# Functions used to change or display the proxy state
#
# usually it's all the same host, but you could add more host vars if needed
_PROXY_HOST="proxy.company.com"
_HPROXY_PORT="8080"
_HSPROXY_PORT="8080"
_SPROXY_PORT="1080" # this is usually different than http/s
# comma separated (no spaces) list of hosts/ip ranges(cidr) to not proxy
_PROXY_EXCEPTIONS="localhost,127.0.0.0/8,10.0.0.0/8,192.168.0.0/16"
# if an investigate function isn't already defined, define one
# allows you to easily debug variables as varname:varvalue or similar
if ! type -t investigate &> /dev/null; then
investigate() {
# if $3 isn't given, don't prefix output with anything
local output_prefix='' # could be something like [*]
if [ -n "$3" ]; then output_prefix="$3";fi
# if $2 isn't given, default separator to something
local output_sep=": " # could be ", " ": " etc
if [ -n "$2" ]; then output_sep="$2";fi
# if $1 is given, then good, if not, well jeez, don't do anything
local var2investigate=''
if [ -n "$1" ]; then
var2investigate="$1"
echo -n "${output_prefix}${var2investigate}${output_sep}"
v='echo -n $'
v="${v}$(echo -n $var2investigate)"
eval $v
echo
fi
}
# might as well make this function available for the shell user
export -f investigate
fi
# we don't use "HTTP_PROXY" or "http_proxy" to avoid interfering w/any env vars
# most http proxies can be constructed thusly ;)
_HTTP_PROXY=http://${_PROXY_HOST}:${_HPROXY_PORT}
# some https proxies use http://, some use https://, change as needed
# some https proxies use a different port than the http one, change as needed
_HTTPS_PROXY=http://${_PROXY_HOST}:${_HSPROXY_PORT}
_SOCKS_PROXY=socks://${_PROXY_HOST}:${_SPROXY_PORT}
# define the functions to be used in the shell
proxyon() {
# if first arg is -s then don't mess w/socks stuff
local no_socks=$1
export http_proxy=$_HTTP_PROXY
export https_proxy=$_HTTPS_PROXY
[ -z "$no_socks" ] && export socks_proxy=$_SOCKS_PROXY
export no_proxy=$_PROXY_EXCEPTIONS
}
proxyoff() {
# if first arg is -s then don't mess w/socks stuff
local no_socks=$1
unset http_proxy
unset https_proxy
[ -z "$no_socks" ] && unset socks_proxy
unset no_proxy
}
# Display the state of the proxy variables
proxystate() {
# if first arg is -s then don't show state of socks proxy
local no_socks=$1
local HTTP="off"
local HTTPS="off"
local SOCKS="off"
local EXCEPTS="none"
export | grep -q http_proxy && HTTP=$http_proxy
export | grep -q https_proxy && HTTPS=$https_proxy
export | grep -q socks_proxy && SOCKS=$socks_proxy
export | grep -q no_proxy && EXCEPTS=$no_proxy
# Show the current state of proxy variables
vars2show="HTTP HTTPS EXCEPTS"
[ -z "$no_socks" ] && vars2show="$vars2show SOCKS"
for proto in $vars2show; do investigate $proto;done
}
export -f proxystate
# as soon as this file is sourced, turn the proxy on
proxyon
@kernelsmith
Copy link
Author

updated w/no_proxy settings & simplified some checks

@kernelsmith
Copy link
Author

I didn't add the 172 net cuz people who use that are evil and/or masochistic ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment