Skip to content

Instantly share code, notes, and snippets.

@Hackl0us
Last active February 23, 2020 09:32
Show Gist options
  • Select an option

  • Save Hackl0us/1be2e51a4b7b4ca48878c1385531ff29 to your computer and use it in GitHub Desktop.

Select an option

Save Hackl0us/1be2e51a4b7b4ca48878c1385531ff29 to your computer and use it in GitHub Desktop.
Terminal Proxy Switcher
#!/usr/bin/env zsh
shell_config="${HOME}/.zshrc"
proxyOn(){
echo "export https_proxy=http://127.0.0.1:8888" >> ${shell_config}
echo "export http_proxy=http://127.0.0.1:8888" >> ${shell_config}
echo "export all_proxy=socks5://127.0.0.1:8889" >> ${shell_config}
source ${shell_config}
echo -e "Proxy setings has been \033[32mENABLED\033[0m"
}
proxyOff(){
sed -i ".bak" '/_proxy=/d' ${shell_config}
source ${shell_config}
echo -e "Proxy setings has been \033[31mDISABLED\033[0m"
}
showUsage(){
echo -e "\nThis script only accept at most 1 argument which is on/off, 1/0\n"
echo "Usage:"
echo "a) $0"
echo "b) $0 (on|off)"
echo -e "c) $0 (1|0)\n"
exit 1
}
if [ $# -eq 0 ];then
proxy_status=$(cat ${shell_config} | grep '_proxy=')
if [ ! -z "${proxy_status}" ];then
proxyOff
exit 0
else
proxyOn
exit 0
fi
elif [ $# -eq 1 ];then
switch=$(echo $1 | tr 'A-Z' 'a-z'-l)
if [ ${switch} == "off" ] || [ ${switch} == "0" ];then
proxyOff
exit 0
elif [ ${switch} == "on" ] || [ ${switch} == "1" ];then
proxyOn
exit 0
else
echo -e "\033[33m Invalid arguments! \033[0m"
showUsage
exit 1
fi
else
echo -e "\033[33m Too many arguments! \033[0m"
showUsage
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment