preproxy
This tool automatically sets or unsets proxy related shell variables, depending on if a VPN is up or down.
This is done before each command execution, which is superior to other methods that may make the determination once per shell launch, or possibly when the prompt is displayed.
git clone https://github.com/lathiat/preproxy ~/.preproxy
echo "source ~/.preproxy/preproxy.sh" >> ~/.bashrc
Edit ~/.preproxy/config.sh and set the appropriate variables. Examples are provided, however we leave proxy_url blank by default to ensure that we don't start setting invalid/unexpected proxies
# preproxy configuration
# * proxy_auto *
# Automatically set and unset the proxy as required everytime a command is invoked. This
# is the default behavior.
#
# If disabled, you can specifically enable or disable the proxy by using the commands
# proxy and noproxy, optionally passing in the command to run
#
# Comment this out to disable
export proxy_mode=auto
# * proxy_interface *
#
# This is the interface we check exists, in order to deside we need the proxy
export proxy_interface=vpn0
# * proxy_url *
# The proxy hostname, protocol and port as a URL
#
# Example: http://int-proxy.corp:3128
export proxy_url=
# * proxy_noproxy *
# List of hostnames not to proxy. This is not supported by all programs.
#
# Example: localhost,127.0.0.1,corp
export proxy_noproxy=localhost,127.0.0.1,::1
The default configuration will automatically determine before each command whether to use the proxy or not. You can disable that by commenting out the proxy_mode option.
Whether automatic mode is enabled or not, you can manually request the proxy is set or unset using the proxy and noproxy commands. You can run them without argument to change the current shell environment, or you can pass a command to modify the environment for that command only.
lathiat@ubuntu:~/src/preproxy$ noproxy export|grep http_proxy
lathiat@ubuntu:~/src/preproxy$ proxy export|grep http_proxy
declare -x http_proxy="http://int-proxy.corp:3128"
lathiat@ubuntu:~/src/preproxy$ noproxy; export|grep http_proxy
lathiat@ubuntu:~/src/preproxy$ proxy; export|grep http_proxy
declare -x http_proxy="http://int-proxy.corp:3128"
lathiat@ubuntu:~/src/preproxy$
I plan to implement support for using a wpad.dat file, and caching it for some amount of time to ensure speedy command execution.
Challenges in this are:
- A javascript interpreter is needed to properly interpret the file
- Some wpad.dat files do matches against the hostname (and possibly other things) to determine the appropriate proxy.. we can't really support that as the URL may or may not beas the URL may or may not beas the URL may or may not beas the URL may or may not be known.
However basic support will likely be good enough for many use cases.
Other options for network detection may also be appropriate, i.e. based on IP address, DNS domain, or GNOME/network-manager proxy settings for corporate networks rather than VPNs as I originally designed it for. If you have a use case here, please let me know.
Thanks to Ryan Caloras for bash-preexec.sh