Skip to content

Instantly share code, notes, and snippets.

@navono
Created September 30, 2021 01:59
Show Gist options
  • Save navono/a37ac129001b8bb86107bf9537711ab0 to your computer and use it in GitHub Desktop.
Save navono/a37ac129001b8bb86107bf9537711ab0 to your computer and use it in GitHub Desktop.
.bashrc for WSL2
# CUSTOM MIRRORS
export HOST_IP=$(grep -oP '(?<=nameserver\ ).*' /etc/resolv.conf)
export PORXY_ADDR="http://$HOST_IP:1080"
# PROXY FOR APT
function proxy_apt() {
# make sure apt.conf existed
sudo touch /etc/apt/apt.conf
sudo sed -i '/^Acquire::https\?::Proxy/d' /etc/apt/apt.conf
# add proxy
echo -e "Acquire::http::Proxy \"$PORXY_ADDR\";\nAcquire::https::Proxy \"$PORXY_ADDR\";" | sudo tee -a /etc/apt/apt.conf >/dev/null
echo "current apt proxy status: using $PORXY_ADDR, proxying"
}
function unproxy_apt() {
sudo sed -i '/^Acquire::https\?::Proxy/d' /etc/apt/apt.conf
echo "current apt proxy status: direct connect, not proxying"
}
# PROXY FOR NPM
function proxy_yarn() {
yarn config set proxy $PORXY_ADDR
yarn config set https-proxy $PORXY_ADDR
npm config set proxy $PORXY_ADDR
npm config set https-proxy $PORXY_ADDR
}
function unproxy_yarn() {
yarn config delete proxy
yarn config delete https-proxy
npm config delete proxy
npm config delete https-proxy
}
function proxy_npm() {
proxy_yarn
}
function unproxy_npm() {
unproxy_yarn
}
# if docker is not running, then start docker service
# if docker running, then `:`, else `sudo service docker start`
# The `:` Do nothing beyond expanding arguments and performing redirections. The return status is zero.
# https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins
ps -C dockerd 1>/dev/null && : || sudo service docker start 1>/dev/null
# PROXY FOR DOCKER
function proxy_docker() {
# make sure /etc/default/docker existed
sudo touch /etc/default/docker
sudo sed -i '/^export https\?_proxy/Id' /etc/default/docker
# add proxy
echo -e "export http_proxy=\"$PORXY_ADDR\";
export https_proxy=\"$PORXY_ADDR\";
export HTTP_PROXY=\"$PORXY_ADDR\";
export HTTPS_PROXY=\"$PORXY_ADDR\";" | sudo tee -a /etc/default/docker >/dev/null &&
sudo service docker restart &&
echo "current docker proxy status: using $PORXY_ADDR, proxying"
}
function unproxy_docker() {
# sudo sed -i '/^Acquire::https\?::Proxy/d' /etc/apt/apt.conf
# 删除代理设置
sudo sed -i '/^export https\?_proxy/Id' /etc/default/docker &&
sudo service docker restart &&
echo "current docker proxy status: direct connect, not proxying"
}
# GLOBAL PROXY FOR BASH
# if ~/.bash_proxy not exist , then create it.
if [ ! -f ~/.bash_proxy ]; then
touch ~/.bash_proxy
fi
# execute ~/.bash_proxy
. ~/.bash_proxy
function proxy() {
echo -e "export {all_proxy,http_proxy,https_proxy,ALL_PROXY,HTTP_PROXY,HTTPS_PROXY}=\"$PORXY_ADDR\";" | tee ~/.bash_proxy >/dev/null
# apply
. ~/.bash_proxy
# declare
echo "current proxy status: using $PORXY_ADDR, proxying"
}
function unproxy() {
# unset all_proxy http_proxy https_proxy ALL_PROXY HTTP_PROXY HTTPS_PROXY
echo -e "unset all_proxy http_proxy https_proxy ALL_PROXY HTTP_PROXY HTTPS_PROXY" | tee ~/.bash_proxy >/dev/null
# apply
. ~/.bash_proxy
# declare
echo "current proxy status: direct connect, not proxying"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment