Skip to content

Instantly share code, notes, and snippets.

@duskmoon314
Last active March 17, 2023 16:01
Show Gist options
  • Save duskmoon314/35581180a2a9ec5e8c148e2506a4ec83 to your computer and use it in GitHub Desktop.
Save duskmoon314/35581180a2a9ec5e8c148e2506a4ec83 to your computer and use it in GitHub Desktop.
a shell script to set proxy in wsl2 and Linux / MacOS
#!/bin/sh
if [ $(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip') ]; then
hostip=${host:=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')}
else
hostip=${host:-127.0.0.1}
fi
port_http=${port_http:-10809}
port_sock=${port_sock:-10808}
PROXY_HTTP="http://${hostip}:${port_http}"
PROXY_SOCK="socks5://${hostip}:${port_sock}"
set_proxy_http() {
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"
export https_proxy="${PROXY_HTTP}"
export HTTPS_PROXY="${PROXY_HTTP}"
git config --global http.proxy "${PROXY_HTTP}"
git config --global https.proxy "${PROXY_HTTP}"
}
set_proxy_sock() {
export http_proxy="${PROXY_SOCK}"
export HTTP_PROXY="${PROXY_SOCK}"
export https_proxy="${PROXY_SOCK}"
export HTTPS_PROXY="${PROXY_SOCK}"
git config --global http.proxy "${PROXY_SOCK}"
git config --global https.proxy "${PROXY_SOCK}"
}
unset_proxy() {
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
git config --global --unset http.proxy
git config --global --unset https.proxy
}
test_setting_wsl() {
wslip=$(hostname -I | awk '{print $1}')
echo "Host ip:" ${hostip}
echo "WSL ip:" ${wslip}
echo "Current proxy:" $https_proxy
}
test_setting() {
echo "Current proxy:" $https_proxy
}
if [ "$1" = "help" ]; then
echo "[host=127.0.0.1] [port_http=10809] [port_sock=10808] source proxy.sh <help|set|set_sock|unset|test|test_wsl>"
elif [ "$1" = "set" ]; then
set_proxy_http
elif [ "$1" = "set_sock" ]; then
set_proxy_sock
elif [ "$1" = "unset" ]; then
unset_proxy
elif [ "$1" = "test" ]; then
test_setting
elif [ "$1" = "test_wsl" ]; then
test_setting_wsl
else
echo "Unsupported arguments."
fi
@duskmoon314
Copy link
Author

duskmoon314 commented Aug 19, 2021

DEPRECATED

See duskmoon314/dotfiles for my current usage.


Usage

Place this script in Home, and add alias in .zshrc / .bashrc

alias proxy "source ~/proxy.sh"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment