Skip to content

Instantly share code, notes, and snippets.

@TheLandolorien
Created August 29, 2018 18:14
Show Gist options
  • Save TheLandolorien/d1256f4edf052572ceb25ceaef6b68ea to your computer and use it in GitHub Desktop.
Save TheLandolorien/d1256f4edf052572ceb25ceaef6b68ea to your computer and use it in GitHub Desktop.
Bash Proxy Script for Corporate Networks
#!/bin/bash
PROXY=<PROXY>
BYPASS_PROXY="localhost,127.0.0.1,<BYPASS_PROXY>"
ENTERPRISE_GITHUB="<ENTERPRISE_GITHUB>"
if [[ "$1" = "on" ]]; then
# Configure CLI Proxy Settings for Corporate Network
echo -n "Turning on proxy..."
# Set Proxy Environment Variables
export http_proxy=$PROXY
export HTTP_PROXY=$PROXY
export https_proxy=$PROXY
export HTTPS_PROXY=$PROXY
export no_proxy=$BYPASS_PROXY
export NO_PROXY=$BYPASS_PROXY
# Set npm Proxy Settings
npm config set proxy $PROXY
npm config set https-proxy $PROXY
# Clear git Proxy Tunnel Settings (from `proxy tunnel`)
git config --global --unset http.https://$ENTERPRISE_GITHUB.proxy
git config --global --remove-section http.https://$ENTERPRISE_GITHUB > /dev/null 2>&1 # Suppress fatal: No such section!
git config --global --unset url.https://$ENTERPRISE_GITHUB/.insteadOf
git config --global --remove-section url.https://$ENTERPRISE_GITHUB/ > /dev/null 2>&1 # Suppress fatal: No such section!
# Set git Proxy Settings
git config --global url.ssh://$ENTERPRISE_GITHUB/.insteadOf https://$ENTERPRISE_GITHUB/
git config --global http.proxy $PROXY
echo "done."
elif [[ "$1" = "off" ]]; then
# Configure CLI Proxy Settings for Non-Corporate Networks
echo -n "Turning off proxy..."
# Clear Proxy Environment Variables
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset no_proxy
unset NO_PROXY
# Clear npm Proxy Settings
npm config delete proxy
npm config delete https-proxy
# Clear git Corporate Proxy Settings (from `proxy on`)
git config --global --unset http.proxy
git config --global --remove-section http > /dev/null 2>&1 # Suppress fatal: No such section!
git config --global --unset url.ssh://$ENTERPRISE_GITHUB/.insteadOf
git config --global --remove-section url.ssh://$ENTERPRISE_GITHUB/ > /dev/null 2>&1 # Suppress fatal: No such section!
# Clear git Proxy Tunnel Settings (from `proxy tunnel`)
git config --global --unset http.https://$ENTERPRISE_GITHUB.proxy
git config --global --remove-section http.https://$ENTERPRISE_GITHUB > /dev/null 2>&1 # Suppress fatal: No such section!
git config --global --unset url.https://$ENTERPRISE_GITHUB/.insteadOf
git config --global --remove-section url.https://$ENTERPRISE_GITHUB/ > /dev/null 2>&1 # Suppress fatal: No such section!
echo "done."
else
echo "Usage: proxy [on|off]"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment