Simplify your proxy bash configuration

Mando Stamelaki
4 min readOct 20, 2020

Have you even switched network locations in your Mac and realized that a git fetch or brew install results on a connection timeout?

This is my story about how I automate my proxy changing in git, bash and ruby based on MacOS network locations feature. It can easily extend to multiple development tools that save their proxy configurations in any kind of property files.

What is network locations feature?

MacOS has a feature called network location. Although, it is called “network location” it has nothing to do with GPS or actual locating you. To put it simple it is a drop-down list in the network settings that allow you to define a group of settings with a name. Therefore, you can easily define two network groups one with proxy settings while being at the office and one without proxy while at home. For more details and configuration on this feature check apple documentation.

This setup will work for most of the users and most of the programs, as the latter obey the system network settings for their network connections, but not for development needs.

The problem

The problem that arises here is that when you switch your drop-down settings this does not affect your bash configuration. So, if you open your terminal and do a git fetch or brew install then you end up with a timeout. This happens because the last defined proxy environment variables in .bashrc or .bash_profile files are unchanged. The configuration on them is always the one you had set when you last edited your environment. Therefore, you end up troubleshooting why the connection timeout, till you realize that you have set a proxy, because the last time you changed your settings you were at the office, so now being at home is not working. Annoying right?

The idea

Everything is in the system, configuration settings and bash. Why cannot bash just see the new proxy, and not bother me all the time?

The answer is YES, it can!

The program to report any kind of system hardware and software configuration is system_profiler. So, typing the following command to bash prints the valuable proxy configuration.

system_profiler SPNetworkDataType

Adding some ugly code we can also get the desired Proxy IP and Proxy Port

proxyIP=$(system_profiler SPNetworkDataType | grep "HTTP Proxy Server" | awk '{print $4}' | head -1)proxyPort=$(system_profiler SPNetworkDataType | grep "HTTP Proxy Port" | awk '{print $4}' | head -1)

Then it is required to check if we have to set or unset the proxy configuration in the system

if [ ! -z ${proxyIP} ] && [ ${proxyPort} != "0" ]

The rest, aligns to each program we want to set or unset the proxy settings.

For example, in order to set proxy to git you have to use:

git config --global http.proxy ${HTTP_PROXY}
git config --global https.proxy ${HTTPS_PROXY}

To remove it, we need to unset the value:

git config --global --unset http.proxy
git config --global --unset https.proxy

Put them all together

Every time your shell/bash session loads, you have to add some magic and set or unset the proxy settings.

I am not a bash expert, but the following code should work.

In order to set the script running, you can just add this line in your .bashrc or .bash_profile

if [ -f ~/path/of/proxy_config.sh ]; then source ~/path/of/proxy_config.sh; fi

You can verify that the script is running properly in your system as when you start a new bash session you see the following output.

Conclusion

The bash script changes configuration settings every time the bash session reloads or initiates. This is more than enough! As you will switch off or hibernate the machine when you are moving from work to home! As a result, there is no reason to worry again about a timeout due to proxy settings.

This can be easily extended and add proxy configurations for other development tools or programs.

--

--

Mando Stamelaki

Android Software Engineer @Atos | Mobile Apps Expert