Skip to content

Instantly share code, notes, and snippets.

@kernelsmith
Last active December 26, 2015 22:09
Show Gist options
  • Save kernelsmith/7221206 to your computer and use it in GitHub Desktop.
Save kernelsmith/7221206 to your computer and use it in GitHub Desktop.
All-in-one CLI environment proxy helper
# Note, the local keyword is not strictly posix compliant but supported in
# most shells. You can remove them w/o affecting the scripts except those
# variables will still be in scope outside the function, which can cause some
# info leakage unless you clear them (and you'd want to also trap so they'd be
# cleared when the script is interrupted with something like ctl-c etc)
prox="http://proxy.check:8080"
alias proxyon="export http_proxy=$prox && export https_proxy=$prox"
alias proxyoff='unset http_proxy && unset https_proxy'
echo "Turning http(s) proxies on"
proxyon
# investigate function is repeated here for portability but, we
# only define it if investigate isn't already a defined function
if ! type -t investigate &> /dev/null; then
investigate() {
local output_prefix='' # could be something like [*]
local output_sep=": " # could be ", " ": " etc
for varname in $@; do
echo -n "${output_prefix}${varname}${output_sep}"
v='echo $'
v="${v}$(echo $varname)"
eval $v
done
}
export -f investigate
fi
# Display the state of the proxy variables
proxystate() {
local HTTP="off"
local HTTPS="off"
local SOCKS="off"
if export | grep -q http_proxy;then
HTTP=$http_proxy
fi
if export | grep -q https_proxy;then
HTTPS=$https_proxy
fi
if export | grep -q socks_proxy;then
SOCKS=$socks_proxy
fi
echo "[*] Current state of proxy variables"
investigate HTTP HTTPS SOCKS
}
export -f proxystate
# detect_http_proxy - Returns a HTTP proxy which is available for use
detect_http_proxy() {
# Author: Lekensteyn <lekensteyn@gmail.com>
# Modified by: kernelsmith@kernelsmith.com
# If desired:
# Put this file in /etc/apt/detect-http-proxy and create and add the below
# configuration in /etc/apt/apt.conf.d/30detectproxy
# Acquire::http::ProxyAutoDetect "/etc/apt/detect-http-proxy";
# APT calls this script for each host that should be connected to. Therefore
# you may see the proxy messages multiple times (LP 814130). If you find this
# annoying and wish to disable these messages, set show_proxy_messages to 0
local show_proxy_messages=0
# one or more proxies can be specified. Note that each will introduce a routing
# delay and therefore its recommended to put the proxy which is most likely to
# be available on the top. If no proxy is available, a direct connection will
# be used
try_proxies=(
proxy.check:8080
)
print_msg() {
# \x0d clears the line so [Working] is hidden
[ "$show_proxy_messages" = 1 ] && printf '\x0d%s\n' "$1" >&2
}
active_proxy=""
# adjust the -w timeout if needed for accuracy
for proxy in "${try_proxies[@]}"; do
# if the host machine / proxy is reachable...
if nc -w 3 -z $(echo ${proxy/:/ }) &> /dev/null; then
active_proxy="http://$proxy"
print_msg "Proxy found: $active_proxy"
echo "$active_proxy"
break
fi
done
print_msg "No active proxies"
# Workaround for Launchpad bug 654393 so it works with Debian Squeeze (<0.8.11)
# not required if not < 0.8.11 or not using this w/APT
#echo DIRECT
}
export -f detect_http_proxy
active_proxy=$(detect_http_proxy)
http_proxy=$active_proxy
https_proxy=$active_proxy
proxystate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment