Skip to content

Instantly share code, notes, and snippets.

@patik
Last active October 13, 2024 15:27
Show Gist options
  • Save patik/6d40ded40bf93c2f381b to your computer and use it in GitHub Desktop.
Save patik/6d40ded40bf93c2f381b to your computer and use it in GitHub Desktop.
Bash configure proxy for corporate network
# configure proxy for git while on corporate network
# From https://gist.github.com/garystafford/8196920
function proxy_on(){
# assumes $USERDOMAIN, $USERNAME, $USERDNSDOMAIN
# are existing Windows system-level environment variables
# assumes $PASSWORD, $PROXY_SERVER, $PROXY_PORT
# are existing Windows current user-level environment variables (your user)
# environment variables are UPPERCASE even in git bash
export HTTP_PROXY="http://$USERNAME:$PASSWORD@$PROXY_SERVER:$PROXY_PORT"
export HTTPS_PROXY=$HTTP_PROXY
export FTP_PROXY=$HTTP_PROXY
export SOCKS_PROXY=$HTTP_PROXY
export NO_PROXY="localhost,127.0.0.1,$USERDNSDOMAIN"
# Update git and npm to use the proxy
git config --global http.proxy $HTTP_PROXY
git config --system http.sslcainfo /bin/curl-ca-bundle.crt
git config --global http.sslcainfo /bin/curl-ca-bundle.crt
# npm config set proxy $HTTP_PROXY
# npm config set https-proxy $HTTP_PROXY
# npm config set strict-ssl false
# npm config set registry "http://registry.npmjs.org/"
# optional for debugging
export GIT_CURL_VERBOSE=1
# optional Self Signed SSL certs and
# internal CA certificate in an corporate environment
export GIT_SSL_NO_VERIFY=1
env | grep -e _PROXY -e GIT_ | sort
# echo -e "\nProxy-related environment variables set."
# clear
}
# Enable proxy settings immediately
proxy_on
# Disable proxy settings
function proxy_off(){
variables=( \
"HTTP_PROXY" "HTTPS_PROXY" "FTP_PROXY" "SOCKS_PROXY" \
"NO_PROXY" "GIT_CURL_VERBOSE" "GIT_SSL_NO_VERIFY" \
)
for i in "${variables[@]}"
do
unset $i
done
env | grep -e _PROXY -e GIT_ | sort
echo -e "\nProxy-related environment variables removed."
}

This will automatically set the proxy variables when you launch a bash shell. At any time you can type proxy_off to disable them.

You may need to hardcode your password, proxy server, and proxy port on line 11. Just replace each $VAR_NAME with the actual value.

Put this file in your home directory. Make sure the file is called .bashrc with nothing before the dot — if you're on Windows, try saving it from a decent text editor like Sublime Text or Notepad++ as Explorer will not let you name a file in this way.

@xingheng
Copy link

Where does /bin/curl-ca-bundle.crt come from? I didn't find it in my Mac, Can someone give me a point? Thanks!

@kesavabjs2
Copy link

But .bashrc will not be called by default for login shells of macos rite, so how can this be enforced to load when each time the shell is loaded?

@xingheng
Copy link

It seems the curl uses the http_proxy with lower case environment variable, maybe that should be included.

@finalcut
Copy link

But .bashrc will not be called by default for login shells of macos rite, so how can this be enforced to load when each time the shell is loaded?

I'm pretty sure you can put something like this in your .bash_profile to make sure .bashrc gets called when you launch your terminal

test -f ~/.bashrc && . ~/.bashrc

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