Skip to content

Instantly share code, notes, and snippets.

@duruyao
Last active October 18, 2022 08:04
Show Gist options
  • Save duruyao/67cc2d8a7e351132d28cb5a708f5152f to your computer and use it in GitHub Desktop.
Save duruyao/67cc2d8a7e351132d28cb5a708f5152f to your computer and use it in GitHub Desktop.
Set environment variables for current shell to enable or disable proxy.
#!/usr/bin/env bash
## date: 2022-01-19
## author: duruyao@gmail.com
## desc: unset environment variables for current shell to disable proxy
## usage: proxy-close
set -euo pipefail
function warning_ln() {
# usage: warning_ln "warning message"
printf "\033[1;32;33m%s\n\033[m" "${1}"
}
function info_ln() {
# usage: info_ln "info message"
printf "\033[1;32;32m%s\n\033[m" "${1}"
}
echo "unset http_proxy
unset https_proxy
unset ftp_proxy
unset no_proxy" >/tmp/proxy_env
if [ -n "$(command -v xclip)" ]; then
info_ln "Run the following command (paste directly from clipboard):"
echo "source /tmp/proxy_env"
echo "source /tmp/proxy_env && env | grep -E \"proxy|PROXY\"" | xclip
echo "source /tmp/proxy_env && env | grep -E \"proxy|PROXY\"" | xclip -sel clip
else
warning_ln "xclip is required but not installed, try run 'sudo apt-get install -y xclip'"
info_ln "Run the following command:"
echo "source /tmp/proxy_env"
fi
#!/usr/bin/env bash
## date: 2022-01-19
## author: duruyao@gmail.com
## desc: set environment variables for current shell to enable proxy
## usage: proxy-open [PROXY_IP:PROXY_PORT]
set -euo pipefail
function warning_ln() {
# usage: warning_ln "warning message"
printf "\033[1;32;33m%s\n\033[m" "${1}"
}
function info_ln() {
# usage: info_ln "info message"
printf "\033[1;32;32m%s\n\033[m" "${1}"
}
proxy_addr="127.0.0.1:7890"
if [ $# -gt 0 ]; then
proxy_addr="$1"
fi
echo "export http_proxy=\"http://${proxy_addr}\"
export https_proxy=\"http://${proxy_addr}\"
export ftp_proxy=\"http://${proxy_addr}\"
export no_proxy=\"localhost,127.0.0.0/8,::1\"" >/tmp/proxy_env
if [ -n "$(command -v xclip)" ]; then
info_ln "Run the following command (paste directly from clipboard):"
echo "source /tmp/proxy_env"
echo "source /tmp/proxy_env && env | grep -E \"proxy|PROXY\"" | xclip
echo "source /tmp/proxy_env && env | grep -E \"proxy|PROXY\"" | xclip -sel clip
else
warning_ln "xclip is required but not installed, try run 'sudo apt-get install -y xclip'"
info_ln "Run the following command:"
echo "source /tmp/proxy_env"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment