Last active
August 30, 2019 16:38
-
-
Save wizcas/f8775356788010656816711e4046772e to your computer and use it in GitHub Desktop.
git代理配置工具 (WSL/Linux) | GIT PROXY CONFIG TOOL FOR WSL/LINUX
This file contains hidden or 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/bash | |
echo -e "\ | |
====================== | |
GIT PROXY SETUP TOOL | |
by wizcas | |
====================== | |
Prerequisites: | |
- An HTTP proxy and you know its host and port | |
- Your system supports the \e[33mnc\e[0m command | |
" | |
checkNc() { | |
command -v nc > /dev/null | |
nc=$? | |
if [ $nc -ne 0 ] | |
then | |
echo -e "\e[31mERR: \e[0m\e[33mnc\e[0m command is not found, which is required for SSH proxy" | |
exit 1 | |
fi | |
} | |
selectProtocol() { | |
echo -e "\n\e[33m[STEP 1]\e[34m Proxy Protocol: \e[0m\n---- OPTIONS ----" | |
PROTOCOLS=("http" "https" "socks5" "socks4") | |
PS3="----------------- | |
Protocol > " | |
select protocol in ${PROTOCOLS[@]} | |
do | |
case $protocol in | |
"http"|"https") | |
HTTP_PROXY_PROTOCOL=$protocol | |
NC_PROTOCOL="connect" | |
break | |
;; | |
"socks5") | |
HTTP_PROXY_PROTOCOL=$protocol | |
NC_PROTOCOL=5 | |
break | |
;; | |
"socks4") | |
HTTP_PROXY_PROTOCOL=$protocol | |
NC_PROTOCOL=4 | |
break | |
;; | |
*) | |
echo -e "\e[31mPlease select a number from 1 to ${#PROTOCOLS[@]}\e.[0m" | |
;; | |
esac | |
done | |
} | |
inputHost() { | |
echo -e "\n\e[33m[STEP 2]\e[34m Proxy Host: \e[0m" | |
read -p "Host (127.0.0.1) > " HTTP_PROXY_HOST | |
if [ -z ${HTTP_PROXY_HOST} ] | |
then | |
HTTP_PROXY_HOST="127.0.0.1" | |
fi | |
} | |
inputPort() { | |
echo -e "\n\e[33m[STEP 3]\e[34m Proxy Port: \e[0m" | |
while true | |
do | |
read -p "Port > " HTTP_PROXY_PORT | |
if [ -z ${HTTP_PROXY_PORT} ] | |
then | |
echo -e "\e[31mYou have to let me know the port.\e[0m" | |
else | |
if [[ $HTTP_PROXY_PORT =~ ^[1-9][0-9]+$ ]] && [[ $HTTP_PROXY_PORT -le 65535 ]]; then | |
break | |
else | |
echo -e "\e[31mInvalid port number.\e[0m" | |
fi | |
fi | |
done | |
} | |
confirm() { | |
echo -e " | |
\e[34m[ Your proxy configuration ]\e[0m | |
Protocol: \e[32m${HTTP_PROXY_PROTOCOL}\e[0m | |
Host: \e[32m${HTTP_PROXY_HOST}\e[0m | |
Port: \e[32m${HTTP_PROXY_PORT}\e[0m | |
" | |
while true | |
do | |
read -p "Is everything good to go? (Y/n): " CONFIRMED | |
case $CONFIRMED in | |
"y"|"Y"|"") | |
break | |
;; | |
"n"|"N") | |
echo -e "Bye." | |
exit 2 | |
;; | |
*) | |
echo -e "\e[31mWhat?\e[0m" | |
;; | |
esac | |
done | |
} | |
checkNc | |
selectProtocol | |
inputHost | |
inputPort | |
confirm | |
echo " | |
Applying... | |
" | |
# Resets git proxy settings | |
git config --global --unset http.proxy | |
git config --global --unset https.proxy | |
git config --global --unset core.gitproxy | |
# For HTTP/HTTPS protocol | |
git config --global http.proxy ${HTTP_PROXY_PROTOCOL}://${HTTP_PROXY_HOST}:${HTTP_PROXY_PORT} | |
git config --global https.proxy ${HTTP_PROXY_PROTOCOL}://${HTTP_PROXY_HOST}:${HTTP_PROXY_PORT} | |
# Makes the proxy command | |
cat > ~/socksproxy << EOL | |
#!/bin/bash | |
nc -x ${HTTP_PROXY_HOST}:${HTTP_PROXY_PORT} -X ${NC_PROTOCOL} \$* | |
EOL | |
chmod +x ~/socksproxy | |
# For git:// protocol | |
git config --global core.gitproxy "~/socksproxy" | |
echo -e "" | |
echo -e "http.proxy\t= \e[33m$(git config --global --get http.proxy)\e[0m" | |
echo -e "https.proxy\t= \e[33m$(git config --global --get https.proxy)\e[0m" | |
echo -e "core.gitproxy\t= \e[33m$(git config --global --get core.gitproxy)\e[0m" | |
echo -e "\ | |
------------- | |
\e[32m[MANUAL OPERATION REQUIRED]\e[0m | |
The proxy file \e[33m~/socksproxy\e[0m is created. | |
Now to make it into use for the git:// protocol: | |
1) Edit or add to \e[33m~/.ssh/config\e[0m file with following entry. | |
You may need to grant write permission to the file first. | |
\e[36m | |
Host * | |
User git | |
ProxyCommand ~/socksproxy '%h %p' | |
\e[0m | |
2) Give \e[33m~/.ssh/config\e[0m a strict permission for SSH | |
\e[36m | |
chmod 400 ~/.ssh/config | |
\e[0m | |
------------- | |
" | |
echo -e "\nComplete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment