Last active
November 26, 2021 07:08
-
-
Save wizcas/d9b0dd99beffaca34ff0313e50a77031 to your computer and use it in GitHub Desktop.
WSL2 Proxy Enabler
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 | |
########################################## | |
# Copyrights 2020, Wizcas <[email protected]> | |
# | |
# This script is under MIT license | |
########################################## | |
#----------- 辅助工具 ----------- | |
####### 颜色码 ######## | |
RED="\033[31m" # Error message | |
GREEN="\033[32m" # Success message | |
YELLOW="\033[33m" # Warning message | |
BLUE="\033[34m" # Info message | |
MAGENTA="\033[35m" # Info message | |
TEAL="\033[36m" # Info message | |
RAW="\033[0m" | |
colorEcho() { | |
echo -e "${1}${@:2}${RAW}" 1>&2 | |
} | |
########################## | |
# 一些预定义常量 | |
conf="${HOME}/.wslproxy.conf" | |
socks="${HOME}/socksproxy" | |
varsfile="${HOME}/wslproxy.vars" | |
cmd="wslproxy" | |
# 从WSL的DNS配置中解析当前Windows主机的访问IP,因为每次重启都会变 | |
host=$(cat /etc/resolv.conf | sed -n 's/^nameserver\W\(.*\)$/\1/p') | |
# 让用户选择代理用什么协议 | |
selectProtocol() { | |
echo -e "\n${YELLOW}[STEP 1]${BLUE} 代理协议: ${RAW}\n---- 请选择数字 ----" | |
PROTOCOLS=("http" "https" "socks5" "socks4") | |
PS3="----------------- | |
Protocol > " | |
select protocol in ${PROTOCOLS[@]}; do | |
case $protocol in | |
"http" | "https") | |
NC_PROTOCOL="connect" | |
break | |
;; | |
"socks5") | |
NC_PROTOCOL=5 | |
break | |
;; | |
"socks4") | |
NC_PROTOCOL=4 | |
break | |
;; | |
*) | |
colorEcho ${RED} "请选择一个从1到${#PROTOCOLS[@]}的数字" | |
;; | |
esac | |
done | |
} | |
# Let user input the proxy server port | |
inputPort() { | |
echo -e "\n${YELLOW}[STEP 2]${BLUE} 代理端口: ${RAW}" | |
while true; do | |
read -p "Port > " port | |
if [ -z ${port} ]; then | |
colorEcho ${RED} "你的代理总得有个端口吧?😒" | |
else | |
if [[ ${port} =~ ^[1-9][0-9]+$ ]] && [[ ${port} -le 65535 ]]; then | |
break | |
else | |
colorEcho ${RED} "无效的端口号" | |
fi | |
fi | |
done | |
} | |
# Save configs into a conf file | |
saveConf() { | |
echo "# This file is for storaging WSL proxy settings. | |
# The host IP is not recorded since it changes every time on reboot. | |
protocol=${protocol} | |
port=${port} | |
" >${conf} | |
colorEcho ${GREEN} " | |
代理配置已成功存储到 ${YELLOW}${conf}${GREEN}, 请不要删除。 | |
" | |
} | |
# Since SSH proxy depends on `nc` tool for forwarding, | |
# this function checks the prerequisite | |
checkNc() { | |
command -v nc >/dev/null | |
nc=$? | |
if [ $nc -ne 0 ]; then | |
colorEcho ${RED} "警告: 未找到命令${YELLOW}nc${RED}。SSH代理需要使用该命令,请自行安装。" | |
fi | |
} | |
# Create a utility for calling nc to forward network traffics | |
makeSocksProxy() { | |
# Makes the proxy command | |
echo "#!/bin/bash | |
nc -x ${host}:${port} -X ${NC_PROTOCOL} \$* | |
" >${socks} | |
chmod +x ${socks} | |
echo -e "\ | |
------------- | |
${RED}[需要手动设置]${RAW} | |
已创建网络请求转发脚本${YELLOW}${socks}${RAW}, | |
请进行如下配置,以便正常使用SSH代理与git通信: | |
1) 新建或编辑文件${YELLOW}~/.ssh/config\e${RAW},并写入类似下面的内容: | |
(你可能需要首先给该文件可写权限) | |
${TEAL} | |
Host github.com | |
User git | |
ProxyCommand ${socks} '%h %p' | |
${RAW} | |
2) 重新给${YELLOW}~/.ssh/config${RAW}文件一个只读权限,以通过SSH安全检查 | |
${TEAL} | |
chmod 400 ~/.ssh/config | |
${RAW} | |
-------------" | |
} | |
# The `setup` command's handler | |
setup() { | |
selectProtocol | |
inputPort | |
echo -e "你的代理服务器是: ${TEAL}${protocol}://${host}:${port}${RAW}" | |
echo | |
while true; do | |
read -p "地址是否正确? (Y/n): " CONFIRMED | |
case $CONFIRMED in | |
"y" | "Y" | "") | |
break | |
;; | |
"n" | "N") | |
colorEcho ${BLUE} "👋🏻️ 再见" | |
exit 2 | |
;; | |
*) | |
colorEcho ${RED} "啥?" | |
;; | |
esac | |
done | |
saveConf | |
checkNc | |
makeSocksProxy | |
colorEcho ${RED} "<<< 随WSL启动自动开启代理 >>>" | |
echo -e "${TEAL}注意: ${RAW}本工具的代理设置会应用到 | |
${YELLOW}http_proxy, https_proxy${RAW}和${YELLOW}.gitconfig${RAW}上 | |
若需随WSL启动自动开启代理,请将下面两行添加到${YELLOW}$(getProfile)${RAW}中: | |
${cmd} on | |
. "${varsfile}" | |
" | |
} | |
# Load proxy settings from our config file. Throws an error message if not found. | |
loadConf() { | |
if [ ! -f "${conf}" ]; then | |
colorEcho ${RED} " | |
未找到配置文件${TEAL}${conf}${RED},请首先执行${YELLOW}setup${RED}命令。 | |
${RAW}用法: ${cmd} setup" | |
exit 1 | |
fi | |
. "${conf}" | |
url="${protocol}://${host}:${port}" | |
} | |
# Determine what's the shell's profile script | |
getProfile() { | |
if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then | |
# assume Zsh | |
echo '.zshrc' | |
elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then | |
# assume Bash | |
echo '.bashrc' | |
else | |
# assume something else | |
echo "shell启动脚本" | |
fi | |
} | |
# The `on` command's handler | |
on() { | |
loadConf | |
echo -e "正在配置代理服务器 ${YELLOW}${url}${RAW}" | |
makeVars ${url} | |
applyGit ${url} | |
} | |
# Create a script for system proxy variables, which is sourced on shell startup | |
makeVars() { | |
echo " | |
export http_proxy=$1 | |
export https_proxy=$1 | |
echo \"✔️️ 系统变量设置完毕\" | |
" >${varsfile} | |
} | |
# Set the proxy settings for git | |
applyGit() { | |
# For HTTP/HTTPS protocol | |
git config --global http.proxy $1 | |
git config --global https.proxy $1 | |
# For git:// protocol | |
git config --global core.gitproxy ${socks} | |
echo "✔️️ git设置完毕" | |
} | |
# Unset all proxy settings | |
off() { | |
unset http_proxy | |
unset https_proxy | |
echo "👋🏻️ 已删除系统变量" | |
git config --global --unset http.proxy | |
git config --global --unset https.proxy | |
git config --global --unset core.gitproxy | |
echo "👋🏻️ 已删除git配置" | |
colorEcho ${RED} " | |
如果不再使用代理服务器,别忘了: | |
${RAW} | |
1. 删除或注释掉${YELLOW}$(getProfile)${RAW}中 wslproxy 相关的部分 | |
2. 删除或注释掉${YELLOW}~/.ssh/config${RAW}中的代理设置 | |
" | |
} | |
# Print the help info | |
help() { | |
echo " | |
WSL代理服务器配置工具 | |
- authored by Wizcas <[email protected]> | |
本工具自动检测WSL的Windows主机地址,并使用自定义的代理信息为 | |
HTTP,HTTPS和Git添加代理配置。 | |
Usage: | |
${cmd} <COMMAND> | |
Commands: | |
install 将脚本安装到 /usr/local/bin | |
setup 设置代理参数 (协议、端口等) ,并创建socks代理 | |
转发脚本 | |
on 开启代理设置,只有使用setup正常配置后才能生效 | |
off 关闭代理设置 | |
url 打印当前的代理服务器地址 | |
help 显示本帮助信息 | |
" | |
} | |
# installs the script to /usr/local/bin | |
install() { | |
if [ "$EUID" -ne 0 ]; then | |
colorEcho ${RED} " | |
请使用root权限执行脚本,如: | |
${RAW}sudo $0 install | |
" | |
exit 1 | |
fi | |
exePath="/usr/local/bin/${cmd}" | |
cp -f $(realpath $0) ${exePath} | |
chmod +x ${exePath} | |
echo "✔️️ 安装完成。现在可以用如下命令配置代理: | |
${cmd} setup | |
" | |
} | |
# Main entry of the bash script | |
case $1 in | |
install) | |
install | |
;; | |
setup) | |
setup | |
;; | |
off) | |
off | |
;; | |
on) | |
on | |
;; | |
url) | |
loadConf | |
echo ${url} | |
;; | |
*) | |
help | |
;; | |
esac |
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 | |
########################################## | |
# Copyrights 2020, Wizcas <[email protected]> | |
# | |
# This script is under MIT license | |
########################################## | |
#----------- Utils ----------- | |
####### color code ######## | |
RED="\033[31m" # Error message | |
GREEN="\033[32m" # Success message | |
YELLOW="\033[33m" # Warning message | |
BLUE="\033[34m" # Info message | |
MAGENTA="\033[35m" # Info message | |
TEAL="\033[36m" # Info message | |
RAW="\033[0m" | |
colorEcho() { | |
echo -e "${1}${@:2}${RAW}" 1>&2 | |
} | |
########################## | |
# Constants | |
conf="${HOME}/.wslproxy.conf" | |
socks="${HOME}/socksproxy" | |
varsfile="${HOME}/wslproxy.vars" | |
cmd="wslproxy" | |
# Extract the IP of Windows host, which is generated automatically | |
host=$(cat /etc/resolv.conf | sed -n 's/^nameserver\W\(.*\)$/\1/p') | |
# Let user select from options for proxy protocol | |
selectProtocol() { | |
echo -e "\n${RED}[STEP 1]${BLUE} Proxy Protocol: ${RAW}\n---- OPTIONS ----" | |
PROTOCOLS=("http" "https" "socks5" "socks4") | |
PS3="----------------- | |
Protocol > " | |
select protocol in ${PROTOCOLS[@]}; do | |
case $protocol in | |
"http" | "https") | |
NC_PROTOCOL="connect" | |
break | |
;; | |
"socks5") | |
NC_PROTOCOL=5 | |
break | |
;; | |
"socks4") | |
NC_PROTOCOL=4 | |
break | |
;; | |
*) | |
colorEcho ${RED} "Please select a number from 1 to ${#PROTOCOLS[@]}." | |
;; | |
esac | |
done | |
} | |
# Let user input the proxy server port | |
inputPort() { | |
echo -e "\n${RED}[STEP 2]${BLUE} Proxy Port: ${RAW}" | |
while true; do | |
read -p "Port > " port | |
if [ -z ${port} ]; then | |
colorEcho ${RED} "You have to let me know the port.${RAW}" | |
else | |
if [[ ${port} =~ ^[1-9][0-9]+$ ]] && [[ ${port} -le 65535 ]]; then | |
break | |
else | |
colorEcho ${RED} "Invalid port number.${RAW}" | |
fi | |
fi | |
done | |
} | |
# Save configs into a conf file | |
saveConf() { | |
echo "# This file is for storaging WSL proxy settings. | |
# The host IP is not recorded since it changes every time on reboot. | |
protocol=${protocol} | |
port=${port} | |
" >${conf} | |
colorEcho ${GREEN} " | |
Proxy settings have been saved to ${YELLOW}${conf}${GREEN}, please don't delete it. | |
" | |
} | |
# Since SSH proxy depends on `nc` tool for forwarding, | |
# this function checks the prerequisite | |
checkNc() { | |
command -v nc >/dev/null | |
nc=$? | |
if [ $nc -ne 0 ]; then | |
colorEcho ${RED} " | |
WARNING: command ${YELLOW}nc${RED} is not found, which is required for SSH proxy. | |
Please install it manually." | |
fi | |
} | |
# Create a utility for calling nc to forward network traffics | |
makeSocksProxy() { | |
# Makes the proxy command | |
echo "#!/bin/bash | |
nc -x ${host}:${port} -X ${NC_PROTOCOL} \$* | |
" >${socks} | |
chmod +x ${socks} | |
echo -e "\ | |
------------- | |
${RED}[MANUAL OPERATION REQUIRED]${RAW} | |
The proxy command file ${YELLOW}${socks}${RAW} is created. | |
Now to make it into use for the SSH protocol: | |
1) Edit or add the ${YELLOW}~/.ssh/config\e${RAW} file | |
with settings like the following content: | |
(You may need to grant the writing permission first) | |
${TEAL} | |
Host github.com | |
User git | |
ProxyCommand ${socks} '%h %p' | |
${RAW} | |
2) Give ${YELLOW}~/.ssh/config${RAW} a strict permission for SSH security check | |
${TEAL} | |
chmod 400 ~/.ssh/config | |
${RAW} | |
-------------" | |
} | |
# The `setup` command's handler | |
setup() { | |
selectProtocol | |
inputPort | |
echo -e "Your proxy server is: ${TEAL}${protocol}://${host}:${port}${RAW}" | |
echo | |
while true; do | |
read -p "Is it correct? (Y/n): " CONFIRMED | |
case $CONFIRMED in | |
"y" | "Y" | "") | |
break | |
;; | |
"n" | "N") | |
colorEcho ${BLUE} "Bye." | |
exit 2 | |
;; | |
*) | |
colorEcho ${RED} "What?" | |
;; | |
esac | |
done | |
saveConf | |
checkNc | |
makeSocksProxy | |
colorEcho ${RED} "<<< AUTO ENABLE >>>" | |
echo -e "${TEAL}Note: ${RAW}Proxy server settings will be applied to | |
${YELLOW}http_proxy, https_proxy${RAW} & ${YELLOW}.gitconfig${RAW}. | |
To auto enable the proxy on WSL startup, add the following | |
lines into ${YELLOW}$(getProfile)${RAW}: | |
${cmd} on | |
. "${varsfile}" | |
" | |
} | |
# Load proxy settings from our config file. Throws an error message if not found. | |
loadConf() { | |
if [ ! -f "${conf}" ]; then | |
colorEcho ${RED} " | |
The config file ${TEAL}${conf}${RED} is not found. You need to run the ${YELLOW}setup${RED} command first. | |
${RAW}Usage: ${cmd} setup" | |
exit 1 | |
fi | |
. "${conf}" | |
url="${protocol}://${host}:${port}" | |
} | |
# Determine what's the shell's profile script | |
getProfile() { | |
if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then | |
# assume Zsh | |
echo '.zshrc' | |
elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then | |
# assume Bash | |
echo '.bashrc' | |
else | |
# assume something else | |
echo "your shell's profile script" | |
fi | |
} | |
# The `on` command's handler | |
on() { | |
loadConf | |
echo -e "applying proxy settings to server ${YELLOW}${url}${RAW}" | |
makeVars ${url} | |
applyGit ${url} | |
} | |
# Create a script for system proxy variables, which is sourced on shell startup | |
makeVars() { | |
echo " | |
export http_proxy=$1 | |
export https_proxy=$1 | |
echo \"✔️️ system variables exported.\" | |
" >${varsfile} | |
} | |
# Set the proxy settings for git | |
applyGit() { | |
# For HTTP/HTTPS protocol | |
git config --global http.proxy $1 | |
git config --global https.proxy $1 | |
# For git:// protocol | |
git config --global core.gitproxy ${socks} | |
echo "✔️️ git configs applied." | |
} | |
# Unset all proxy settings | |
off() { | |
unset http_proxy | |
unset https_proxy | |
echo "👋🏻️ environment variables unset." | |
git config --global --unset http.proxy | |
git config --global --unset https.proxy | |
git config --global --unset core.gitproxy | |
echo "👋🏻️ git configs removed." | |
colorEcho ${RED} " | |
IF PROXY IS NO LONGER NEEDED, DON'T FORGET TO: | |
${RAW} | |
1. Remove or comment out the wslproxy part in ${YELLOW}$(getProfile)${RAW} | |
2. Remove or comment out ssh proxies in ${YELLOW}~/.ssh/config${RAW} | |
" | |
} | |
# Print the help info | |
help() { | |
echo " | |
A WSL proxy setting util. | |
- authored by Wizcas <[email protected]> | |
This tool will detect the Windows host address and apply | |
proxy settings to HTTP, HTTPS & Git automatically. | |
Usage: | |
${cmd} <COMMAND> | |
Commands: | |
install Installs the script to /usr/local/bin | |
setup Sets up proxy parameters (protocol, port, etc.) | |
and create a helper socks proxy tool | |
on Enables proxy settings. Works only with a proper setup | |
off Disables proxy settings. | |
url Prints the proxy server's URL | |
help Shows this help info. | |
" | |
} | |
# installs the script to /usr/local/bin | |
install() { | |
if [ "$EUID" -ne 0 ]; then | |
colorEcho ${RED} " | |
Please run this script as root user, i.e. | |
${RAW}sudo $0 install | |
" | |
exit 1 | |
fi | |
exePath="/usr/local/bin/${cmd}" | |
cp -f $(realpath $0) ${exePath} | |
chmod +x ${exePath} | |
echo "✔️️ Done. You can now sets up your proxy server by: | |
${cmd} setup | |
" | |
} | |
# Main entry of the bash script | |
case $1 in | |
install) | |
install | |
;; | |
setup) | |
setup | |
;; | |
off) | |
off | |
;; | |
on) | |
on | |
;; | |
url) | |
loadConf | |
echo ${url} | |
;; | |
*) | |
help | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment