Skip to content

Instantly share code, notes, and snippets.

@madlymad
Last active October 7, 2020 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madlymad/67b05f218780bc464595269ea2648af4 to your computer and use it in GitHub Desktop.
Save madlymad/67b05f218780bc464595269ea2648af4 to your computer and use it in GitHub Desktop.
MacOS script for loading system proxy setting at bash and git
#!/bin/bash
proxyToFile() {
local filename="$2"
# break if filename not found
if [[ ! -f $filename ]]; then return -1; fi
local add="$1"
if [[ "$add" = "true" ]]; then
perl -pi -e 's/^#http/http/' $filename
else
perl -pi -e 's/^http/#http/' $filename
fi
}
prxyIP=$(system_profiler SPNetworkDataType | grep "HTTP Proxy Server" | awk '{print $4}' | head -1)
prxyPort=$(system_profiler SPNetworkDataType | grep "HTTP Proxy Port" | awk '{print $4}' | head -1)
if [ ! -z ${prxyIP} ] && [ ${prxyIP} != "0" ]
then
prxyCfg="http://${prxyIP}:${prxyPort}"
prxy="${prxyIP}:${prxyPort}"
export http_proxy="${prxyCfg}"
export HTTP_PROXY="${prxyCfg}"
export https_proxy="${prxyCfg}"
export HTTPS_PROXY="${prxyCfg}"
export ALL_PROXY=${prxy}
export all_proxy=${prxy}
# Set proxy at git config
git config --global http.proxy ${HTTP_PROXY}
git config --global https.proxy ${HTTPS_PROXY}
# curl -x <proxy_host>:<proxy_port>
alias curl="curl -x '${prxy}'"
# Uncomment proxy from Ruby config
proxyToFile true ~/.gemrc
echo "Configured proxy $HTTPS_PROXY"
else
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
# Remove proxy from git config
git config --global --unset http.proxy
git config --global --unset https.proxy
# Comment proxy at Ruby config
proxyToFile false ~/.gemrc
fi
@madlymad
Copy link
Author

madlymad commented Oct 7, 2020

To use it add in your .bashrc or .bash_profile

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

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