使用此脚本时,需先安装好代理软件并正确配置代理。该脚本默认的代理Host是
127.0.0.1
,端口是11223
,可自行修改
export HTTP_PROXY_HOST="127.0.0.1"
export HTTP_PROXY_PORT=11223
用法
- 文档
http_proxy: Command line proxy switching tool.
usage: http_proxy <option> [args]
options:
status - Display the status of each configured proxy
enable [term/git] - Enable proxy for term/git, default to term
disable [term/git] - Disable proxy for term/git, default to term
- 示例
# 查看代理状态
http_proxy status
# 启用代理
http_proxy enable term
# 禁用代理
http_proxy disable term
代码
[ -z "$HTTP_PROXY_HOST" ] && export HTTP_PROXY_HOST="127.0.0.1"
[ -z "$HTTP_PROXY_PORT" ] && export HTTP_PROXY_PORT="11223"
http_proxy__help() {
echo "http_proxy: Command line proxy switching tool."
echo "usage: http_proxy <option> [args]"
echo
echo " options:"
echo " help - Show this help documentation"
echo " status - Display the status of each configured proxy"
echo " enable [term/git] - Enable proxy for term/git, default to term"
echo " disable [term/git] - Disable proxy for term/git, default to term"
echo
}
http_proxy__status() {
if [[ -z "$1" || "$1" == "term" ]]; then
echo "term:"
echo " http_proxy: $http_proxy"
echo " https_proxy: $https_proxy"
fi
if [[ -z "$1" || "$1" == "git" ]]; then
echo "git:"
echo " http.proxy: $(git config --global http.proxy)"
echo " https.proxy: $(git config --global https.proxy)"
fi
}
http_proxy__enable() {
local http_proxy_url="http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT"
if [[ -z "$1" || "$1" == "term" ]]; then
export http_proxy="$http_proxy_url"
export https_proxy="$http_proxy_url"
fi
if [ "$1" = "git" ]; then
git config --global http.proxy $http_proxy_url
git config --global https.proxy $http_proxy_url
fi
http_proxy__status
}
http_proxy__disable() {
if [[ -z "$1" || "$1" == "term" ]]; then
if [ ! -z "$http_proxy" ]; then
unset http_proxy
fi
if [ ! -z "$https_proxy" ]; then
unset https_proxy
fi
fi
if [ "$1" = "git" ]; then
if [ ! -z "$(git config --global http.proxy)" ]; then
git config --global --unset http.proxy
fi
if [ ! -z "$(git config --global https.proxy)" ]; then
git config --global --unset https.proxy
fi
fi
http_proxy__status
}
http_proxy() {
if [[ -z "$1" || "$1" == "help" || "$1" == "-h" || "$1" == "--help" ]]; then
http_proxy__help
elif [[ "$1" == "status" ]]; then
http_proxy__status "$2"
elif [[ "$1" == "enable" ]]; then
http_proxy__enable "$2"
elif [[ "$1" == "disable" ]]; then
http_proxy__disable "$2"
else
echo "invalid option: $1"
http_proxy__help
fi
}