-
-
Save xingheng/c43933c88e8714275cfb415bf46c9e64 to your computer and use it in GitHub Desktop.
proxy management for daily development.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function proxy() { | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-p | --port) | |
PORT="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--all) | |
CORE_ENABLED=true | |
GIT_ENABLED=true | |
NPM_ENABLED=true | |
SYSTEM_ENABLED=true | |
shift | |
;; | |
--core) | |
CORE_ENABLED=true | |
shift | |
;; | |
--git) | |
GIT_ENABLED=true | |
shift | |
;; | |
--npm) | |
NPM_ENABLED=true | |
shift | |
;; | |
--system) | |
SYSTEM_ENABLED=true | |
shift | |
;; | |
-s | --netservice) | |
NETWORK_SERVICE="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-h | --help | help) | |
echo "proxy [on|off|list] [--all] [--core] [--git] [--npm] [--system] [-h|--help|help] [-p|--port NUM] [-s|--netservice SERVICE]" | |
return 0 | |
;; | |
on | off | status) | |
ACTION="$1" | |
shift | |
;; | |
*) | |
echo "Unknown argument: $1" | |
return 1 | |
;; | |
esac | |
done | |
PORT=${PORT:-7890} | |
NETWORK_SERVICE=${NETWORK_SERVICE:-Wi-Fi} | |
ACTION=${ACTION:-list} | |
# Use --core option if no specified. | |
[[ $GIT_ENABLED != true ]] && [[ $NPM_ENABLED != true ]] && [[ $SYSTEM_ENABLED != true ]] && CORE_ENABLED=true | |
case ${ACTION} in | |
on) | |
if [[ $CORE_ENABLED == true ]]; then | |
export HTTP_PROXY="http://127.0.0.1:$PORT" | |
export HTTPS_PROXY=$HTTP_PROXY | |
export http_proxy=$HTTP_PROXY | |
export https_proxy=$HTTP_PROXY | |
export FTP_PROXY=$HTTP_PROXY | |
export SOCKS_PROXY=$HTTP_PROXY | |
export all_proxy=$HTTP_PROXY | |
export NO_PROXY="localhost,127.0.0.1,::1" | |
env | grep -e _PROXY -e _proxy | sort | |
echo -e "Proxy-related environment variables set." | |
fi | |
if [[ $GIT_ENABLED == true ]]; then | |
export GIT_CURL_VERBOSE=true | |
export GIT_TRACE=true | |
export GIT_TRACE_PACKET=true | |
export GIT_SSL_NO_VERIFY=true | |
# git config --global http.proxy "$HTTP_PROXY" | |
# git config --global http.sslVerify false | |
# git config --global http.sslcainfo /bin/curl-ca-bundle.crt | |
# git config --system http.sslcainfo /bin/curl-ca-bundle.crt | |
env | grep -e GIT_ | sort | |
echo -e "Git proxy related variables set." | |
fi | |
if [[ $NPM_ENABLED == true ]]; then | |
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/" | |
echo -e "npm proxy related variables set." | |
fi | |
if [[ $SYSTEM_ENABLED == true ]]; then | |
networksetup -setwebproxy "$NETWORK_SERVICE" 127.0.0.1 "$PORT" off | |
networksetup -setwebproxystate "$NETWORK_SERVICE" on | |
networksetup -setsecurewebproxy "$NETWORK_SERVICE" 127.0.0.1 "$PORT" off | |
networksetup -setsecurewebproxystate "$NETWORK_SERVICE" on | |
echo -e "System network settings on interface $NETWORK_SERVICE set." | |
fi | |
;; | |
off) | |
if [[ $CORE_ENABLED == true ]]; then | |
unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy FTP_PROXY SOCKS_PROXY NO_PROXY | |
env | grep -e _PROXY -e _proxy | sort | |
echo -e "Proxy-related environment variables removed." | |
fi | |
if [[ $GIT_ENABLED == true ]]; then | |
unset GIT_CURL_VERBOSE GIT_TRACE GIT_TRACE_PACKET GIT_SSL_NO_VERIFY | |
# git config --global --unset http.proxy | |
# git config --global --unset http.sslVerify | |
# git config --global --unset http.sslcainfo | |
# git config --system --unset http.sslcainfo | |
env | grep -e GIT_ | sort | |
echo -e "Git proxy related variables removed." | |
fi | |
if [[ $NPM_ENABLED == true ]]; then | |
npm config delete proxy | |
npm config delete https-proxy | |
npm config delete strict-ssl | |
npm config delete registry | |
echo -e "npm proxy related variables removed." | |
fi | |
if [[ $SYSTEM_ENABLED == true ]]; then | |
networksetup -setwebproxy "$NETWORK_SERVICE" "" "" off | |
networksetup -setwebproxystate "$NETWORK_SERVICE" off | |
networksetup -setsecurewebproxy "$NETWORK_SERVICE" "" "" off | |
networksetup -setsecurewebproxystate "$NETWORK_SERVICE" off | |
echo -e "System network settings on interface $NETWORK_SERVICE removed." | |
fi | |
;; | |
status) | |
if [[ $CORE_ENABLED == true ]]; then | |
echo -e "Proxy-related environment variables' value:" | |
env | grep -e _PROXY -e _proxy -e GIT_ | sort | |
fi | |
if [[ $GIT_ENABLED == true ]]; then | |
printf "Git global proxy:" | |
printf "%s" "$(git config --global --get http.proxy)" | |
printf "%s" "$(git config --global --get http.sslVerify)" | |
printf "\n" | |
fi | |
if [[ $NPM_ENABLED == true ]]; then | |
printf "npm proxy:\n" | |
printf "proxy: %s\n" "$(npm config get proxy)" | |
printf "https-proxy: %s\n" "$(npm config get https-proxy)" | |
printf "strict-ssl: %s\n" "$(npm config get strict-ssl)" | |
printf "registry: %s\n" "$(npm config get registry)" | |
fi | |
if [[ $SYSTEM_ENABLED == true ]]; then | |
printf "System global proxy (http):\n" | |
printf "%s" "$(networksetup -getwebproxy "$NETWORK_SERVICE")" | |
printf "System global proxy (https):\n" | |
printf "%s" "$(networksetup -getsecurewebproxy "$NETWORK_SERVICE")" | |
fi | |
;; | |
*) | |
echo -n "available actions: on, off, status" | |
return 1 | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment