Created
June 23, 2023 10:43
-
-
Save dylanliuh2o/b15e0b66b4f3f83a84f8ac855f83af90 to your computer and use it in GitHub Desktop.
WSL2-proxy-config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }') | |
wslip=$(hostname -I | awk '{print $1}') | |
port=7890 | |
PROXY_HTTP="http://${hostip}:${port}" | |
set_proxy(){ | |
export http_proxy="${PROXY_HTTP}" | |
export HTTP_PROXY="${PROXY_HTTP}" | |
export https_proxy="${PROXY_HTTP}" | |
export HTTPS_proxy="${PROXY_HTTP}" | |
export ALL_PROXY="${PROXY_SOCKS5}" | |
export all_proxy=${PROXY_SOCKS5} | |
git config --global http.https://git.521000.best.proxy ${PROXY_HTTP} | |
git config --global https.https://git.521000.best.proxy ${PROXY_HTTP} | |
echo "Proxy has been opened." | |
} | |
unset_proxy(){ | |
unset http_proxy | |
unset HTTP_PROXY | |
unset https_proxy | |
unset HTTPS_PROXY | |
unset ALL_PROXY | |
unset all_proxy | |
git config --global --unset http.https://git.521000.best.proxy | |
git config --global --unset https.https://git.521000.best.proxy | |
echo "Proxy has been closed." | |
} | |
test_setting(){ | |
echo "Host IP:" ${hostip} | |
echo "WSL IP:" ${wslip} | |
echo "Try to connect to Google..." | |
resp=$(curl -I -s --connect-timeout 5 -m 5 -w "%{http_code}" -o /dev/null www.google.com) | |
if [ ${resp} = 200 ]; then | |
echo "Proxy setup succeeded!" | |
else | |
echo "Proxy setup failed!" | |
fi | |
} | |
if [ "$1" = "set" ] | |
then | |
set_proxy | |
elif [ "$1" = "unset" ] | |
then | |
unset_proxy | |
elif [ "$1" = "test" ] | |
then | |
test_setting | |
else | |
echo "Unsupported arguments." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment