├── LICENSE ├── README-zh.md ├── README.md ├── wsl2proxy-en └── wsl2proxy-zh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Wizcas.0x1 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README-zh.md: -------------------------------------------------------------------------------- 1 | # WSL2Proxy 2 | 3 | 本仓库中的 Bash 脚本用于配置 WSL2 环境,以便让 http、https 连接和 git 客户端能够正确访问 Windows 下的代理服务器。 4 | 5 | # 适用场景 6 | 7 | WSL2 目前不能够直接通过`localhost`访问本地的 Windows 网络服务。相反,Windows 会为 WSL2 分配一个动态 IP, 8 | 而 WSL2 下的程序则必须用这个动态 IP 访问 Windows 中监听的端口。 9 | 10 | 更烦人的是,这个 IP 每次重启电脑都会变。 11 | 12 | 所以我写了这些脚本,让 WSL2 在启动时可以自动获取 Windows IP(或手动执行),并将 IP 和代理服务器端口应用到: 13 | 14 | - `http_proxy` 环境变量 15 | - `https_proxy` 环境变量 16 | - `.gitconfig`里的`http.proxy` 17 | - `.gitconfig`里的`https.proxy` 18 | - `.gitconfig`里的`core.gitproxy` 19 | - 一个名为`socksproxy`的用于进行 SSH 代理的辅助脚本。其实就是包装了一下`nc`命令的调用。 20 | 21 | 一旦微软解决了这个问题,这些脚本应该就没用了。 22 | 23 | # 如何使用 24 | 25 | ## 安装 26 | 27 | ### 一键脚本 28 | 29 | 执行下面的命令来安装最新脚本: 30 | 31 | ```bash 32 | # 中文版 33 | wget -O w2p.sh -q https://git.io/JJvfD && sudo bash w2p.sh install 34 | 35 | # 英文版 36 | wget -O w2p.sh -q https://git.io/JJvf1 && sudo bash w2p.sh install 37 | ``` 38 | 39 | ### 手动安装 40 | 41 | 你可以手动克隆并安装脚本: 42 | 43 | ```bash 44 | 45 | $ git clone https://github.com/wizcas/wsl2proxy.git 46 | $ cd wsl2proxy 47 | 48 | # 中文版 49 | $ sudo bash ./wsl2proxy-zh install 50 | # 英文版 51 | $ sudo bash ./wsl2proxy-en install 52 | 53 | ``` 54 | 55 | ### 设置代理服务器 56 | 57 | 在激活 WSL2Proxy 之前,必须首先进行代理服务器的相关设置,包括: 58 | 59 | - 代理协议 60 | - 代理端口 61 | 62 | 你不用设置代理服务器的 IP 地址,因为 WSL2Proxy 会自动获取! 63 | 64 | 执行下面的命令进行代理服务器的设置: 65 | 66 | ```bash 67 | $ wsl2proxy setup 68 | ``` 69 | 70 | > 代理服务器设置被保存在`~/.wsl2proxy.conf`。 71 | 72 | ## 启用/禁用 WSL2Proxy 73 | 74 | ### 手动启用 75 | 76 | 执行下面的命令 (**别漏了命令开头的`.`,否则环境变量不会应用到当前的环境中**): 77 | 78 | ```bash 79 | . wsl2proxy on 80 | ``` 81 | 82 | ### 手动禁用 83 | 84 | 执行下面的命令 (**别漏了命令开头的`.`,否则环境变量不会应用到当前的环境中**): 85 | 86 | ```bash 87 | . wsl2proxy off 88 | ``` 89 | 90 | ### 随 WSL2 启动 91 | 92 | 在你的 Shell 启动脚本中 (`.bashrc`, `.zshrc`等) 添加这行代码: 93 | 94 | ``` 95 | . wsl2proxy on 96 | ``` 97 | 98 | ### SSH 代理 99 | 100 | 脚本会在 home 目录下自动生成一个名为`socksproxy`的辅助可执行脚本,用来快速设置 SSH 代理。 101 | 102 | 比如说我想让所有访问`github.com`的 SSH 连接都通过代理(在使用`git@github.com:...`这样的地址克隆仓库时走代理了), 103 | 只需要编辑`~/.ssh/config`文件并添加下面的配置: 104 | 105 | ``` 106 | Host github.com 107 | User git 108 | ProxyCommand ~/socksproxy '%h %p' 109 | ``` 110 | 111 | **注意** 本脚本不负责自动修改你的`.ssh/config`文件,所以你必须自己正确处理该文件的权限。另外,如果让 SSH 112 | 走代理了,你也需要手动移除`config`文件中的相关配置。 113 | 114 | ## 卸载 115 | 116 | 1. 如果设置了随 WSL2 自动启用代理,请删除 Shell 启动脚本中的 `. wsl2proxy on` 这行。 117 | 2. 如果修改了 `~/.ssh/config` 文件,请删除其中相关的代理配置。 118 | 3. 删除文件 `/usr/local/bin/wsl2proxy`。 119 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WSL2Proxy 2 | 3 | [中文文档](README-zh.md) 4 | 5 | The repo contains bash scripts to configure your WSL2 environment, 6 | so to utilize the proxy server running on local Windows for http, https & git 7 | connections. 8 | 9 | # Why This Repo 10 | 11 | The WSL2 is currently unable to access ports listened by Windows programs via 12 | `localhost`. Instead, programs in WSL2 have to connect to a dynamic IP address 13 | assigned automatically and changes everytime your compute reboots. 14 | 15 | This is quite annoying. 16 | 17 | The scripts here fetches the latest Windows IP on WSL2's startup (or anytime you 18 | prefer) once properly setup, and assign the IP & port to: 19 | 20 | - `http_proxy` environment variable 21 | - `https_proxy` environment variable 22 | - `http.proxy` in `.gitconfig` 23 | - `https.proxy` in `.gitconfig` 24 | - `core.gitproxy` in `.gitconfig` 25 | - a helper executable script named `socksproxy`, which essentially calls the `nc` 26 | command for proxying SSH connections. 27 | 28 | Once Microsft addresses the issue, there should be no more use of these scripts. 29 | 30 | # How To Use 31 | 32 | ## Installation 33 | 34 | ### One-liner Installation 35 | 36 | You can run this command to download and install the latest script: 37 | 38 | ```bash 39 | # The English Version 40 | wget -O w2p.sh -q https://git.io/JJvf1 && sudo bash w2p.sh install 41 | 42 | # Or the Chinese Version 43 | wget -O w2p.sh -q https://git.io/JJvfD && sudo bash w2p.sh install 44 | ``` 45 | 46 | ### Manually Installation 47 | 48 | You can also clone the repo and install a script manually: 49 | 50 | ```bash 51 | 52 | $ git clone https://github.com/wizcas/wsl2proxy.git 53 | $ cd wsl2proxy 54 | 55 | # The English Version 56 | $ sudo bash ./wsl2proxy-en install 57 | # Or The Chinese Version 58 | $ sudo bash ./wsl2proxy-zh install 59 | 60 | ``` 61 | 62 | ### Setup Proxy Settings 63 | 64 | Before activating the WSL2Proxy you'll have to provide proxy settings first, 65 | including: 66 | 67 | - Proxy protocol 68 | - Proxy port 69 | 70 | No need to provide the proxy IP here because WSL2Proxy takes care of it automatically! 71 | 72 | Run this command and follow the instructions to setup: 73 | 74 | ```bash 75 | $ wsl2proxy setup 76 | ``` 77 | 78 | > The settings are saved at `~/.wsl2proxy.conf`, if you concern. 79 | 80 | ## Enable/Disable WSL2Proxy 81 | 82 | ### Manually Enabling 83 | 84 | Run this command (**don't miss the leading `.`, or environment variables will be set only in forked child process**): 85 | 86 | ```bash 87 | . wsl2proxy on 88 | ``` 89 | 90 | ### Manually Disabling 91 | 92 | Run this command (**don't miss the leading `.`, or environment variables will be set only in forked child process**): 93 | 94 | ```bash 95 | . wsl2proxy off 96 | ``` 97 | 98 | ### Auto Enabling on WSL2 Startup 99 | 100 | And add this line to your shell startup script (`.bashrc`, `.zshrc`, etc.): 101 | 102 | ``` 103 | . wsl2proxy on 104 | ``` 105 | 106 | ### SSH Proxying 107 | 108 | The script generates a helper script named `socksproxy` in your home directory. It is used 109 | for easy SSH proxying. 110 | 111 | Say, I want all the ssh connections with `github.com` are through my proxy server, 112 | so to enable my proxy server for `git@github.com:...`. 113 | Just edit the `~/.ssh/config` file and add lines like this: 114 | 115 | ``` 116 | Host github.com 117 | User git 118 | ProxyCommand ~/socksproxy '%h %p' 119 | ``` 120 | 121 | **Note that** the script is not responsible for modifying `.ssh/config` automatically. You'll 122 | have to set the correct permission for this file after any modification. And you'll need to remove 123 | the proxy settings within manually for disabling SSH proxying. 124 | 125 | ## Uninstall 126 | 127 | 1. Remove the `. wsl2proxy on` line from your shell startup script if ever added. 128 | 2. Remove the proxy settings from `~/.ssh/config` if the changes are ever made. 129 | 3. Delete `/usr/local/bin/wsl2proxy`. 130 | -------------------------------------------------------------------------------- /wsl2proxy-en: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ########################################## 4 | # Copyrights 2020, Wizcas 5 | # 6 | # This script is under MIT license 7 | ########################################## 8 | 9 | #----------- Utils ----------- 10 | ####### color code ######## 11 | RED="\033[31m" # Error message 12 | GREEN="\033[32m" # Success message 13 | YELLOW="\033[33m" # Warning message 14 | BLUE="\033[34m" # Info message 15 | MAGENTA="\033[35m" # Info message 16 | TEAL="\033[36m" # Info message 17 | RAW="\033[0m" 18 | 19 | colorEcho() { 20 | echo -e "${1}${@:2}${RAW}" 1>&2 21 | } 22 | ########################## 23 | 24 | # Constants 25 | VERSION="0.2.0" 26 | conf="${HOME}/.wsl2proxy.conf" 27 | socks="${HOME}/socksproxy" 28 | varsfile="${HOME}/wsl2proxy.vars" 29 | cmd="wsl2proxy" 30 | 31 | # Extract the IP of Windows host, which is generated automatically 32 | host=$(cat /etc/resolv.conf | sed -n 's/^nameserver\W\(.*\)$/\1/p') 33 | 34 | # Let user select from options for proxy protocol 35 | selectProtocol() { 36 | echo -e "\n${RED}[STEP 1]${BLUE} Proxy Protocol: ${RAW}\n---- OPTIONS ----" 37 | PROTOCOLS=("http" "https" "socks5" "socks4") 38 | PS3="----------------- 39 | Protocol > " 40 | select protocol in ${PROTOCOLS[@]}; do 41 | case $protocol in 42 | "http" | "https") 43 | ncProtocol="connect" 44 | break 45 | ;; 46 | "socks5") 47 | ncProtocol=5 48 | break 49 | ;; 50 | "socks4") 51 | ncProtocol=4 52 | break 53 | ;; 54 | *) 55 | colorEcho ${RED} "Please select a number from 1 to ${#PROTOCOLS[@]}." 56 | ;; 57 | esac 58 | done 59 | } 60 | 61 | # Let user input the proxy server port 62 | inputPort() { 63 | echo -e "\n${RED}[STEP 2]${BLUE} Proxy Port: ${RAW}" 64 | while true; do 65 | read -p "Port > " port 66 | if [ -z ${port} ]; then 67 | colorEcho ${RED} "You have to let me know the port.${RAW}" 68 | else 69 | if [[ ${port} =~ ^[1-9][0-9]+$ ]] && [[ ${port} -le 65535 ]]; then 70 | break 71 | else 72 | colorEcho ${RED} "Invalid port number.${RAW}" 73 | fi 74 | fi 75 | done 76 | } 77 | 78 | # Save configs into a conf file 79 | saveConf() { 80 | echo "# This file is for storaging WSL proxy settings. 81 | # The host IP is not recorded since it changes every time on reboot. 82 | 83 | protocol=${protocol} 84 | ncProtocol=${ncProtocol} 85 | port=${port} 86 | " >${conf} 87 | 88 | colorEcho ${GREEN} " 89 | Proxy settings have been saved to ${YELLOW}${conf}${GREEN}, please don't delete it. 90 | " 91 | } 92 | 93 | # Since SSH proxy depends on `nc` tool for forwarding, 94 | # this function checks the prerequisite 95 | checkNc() { 96 | command -v nc >/dev/null 97 | nc=$? 98 | if [ $nc -ne 0 ]; then 99 | colorEcho ${RED} " 100 | WARNING: command ${YELLOW}nc${RED} is not found, which is required for SSH proxy. 101 | Please install it manually." 102 | fi 103 | } 104 | 105 | # Create a utility for calling nc to forward network traffics 106 | setupSocksProxy() { 107 | updateSocksProxy 108 | 109 | echo -e "\ 110 | ------------- 111 | ${RED}[MANUAL OPERATION REQUIRED]${RAW} 112 | 113 | The proxy command file ${YELLOW}${socks}${RAW} is created. 114 | Now to make it into use for the SSH protocol: 115 | 116 | 1) Edit or add the ${YELLOW}~/.ssh/config\e${RAW} file 117 | with settings like the following content: 118 | (You may need to grant the writing permission first) 119 | ${TEAL} 120 | Host github.com 121 | User git 122 | ProxyCommand ${socks} '%h %p' 123 | ${RAW} 124 | 2) Give ${YELLOW}~/.ssh/config${RAW} a strict permission for SSH security check 125 | ${TEAL} 126 | chmod 400 ~/.ssh/config 127 | ${RAW} 128 | -------------" 129 | } 130 | 131 | updateSocksProxy() { 132 | # Makes the proxy command 133 | echo "#!/bin/bash 134 | nc -x ${host}:${port} -X ${ncProtocol} \$* 135 | " >${socks} 136 | chmod +x ${socks} 137 | } 138 | 139 | # The `setup` command's handler 140 | setup() { 141 | selectProtocol 142 | inputPort 143 | echo -e "Your proxy server is: ${TEAL}${protocol}://${host}:${port}${RAW}" 144 | echo 145 | while true; do 146 | read -p "Is it correct? (Y/n): " CONFIRMED 147 | case $CONFIRMED in 148 | "y" | "Y" | "") 149 | break 150 | ;; 151 | "n" | "N") 152 | colorEcho ${BLUE} "Bye." 153 | exit 2 154 | ;; 155 | *) 156 | colorEcho ${RED} "What?" 157 | ;; 158 | esac 159 | done 160 | 161 | saveConf 162 | checkNc 163 | setupSocksProxy 164 | 165 | colorEcho ${RED} "<<< AUTO ENABLE >>>" 166 | echo -e "${TEAL}Note: ${RAW}Proxy server settings will be applied to 167 | ${YELLOW}http_proxy, https_proxy${RAW} & ${YELLOW}.gitconfig${RAW}. 168 | 169 | To auto enable the proxy on WSL startup, add the following 170 | line into ${YELLOW}$(getProfile)${RAW} (don't miss the leading '.'): 171 | 172 | ${TEAL}.${cmd} on${RAW} 173 | " 174 | } 175 | 176 | # Load proxy settings from our config file. Throws an error message if not found. 177 | loadConf() { 178 | if [ ! -f "${conf}" ]; then 179 | colorEcho ${RED} " 180 | The config file ${TEAL}${conf}${RED} is not found. You need to run the ${YELLOW}setup${RED} command first. 181 | 182 | ${RAW}Usage: ${cmd} setup" 183 | exit 1 184 | fi 185 | . "${conf}" 186 | url="${protocol}://${host}:${port}" 187 | } 188 | 189 | # Determine what's the shell's profile script 190 | getProfile() { 191 | if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then 192 | # assume Zsh 193 | echo '.zshrc' 194 | elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then 195 | # assume Bash 196 | echo '.bashrc' 197 | else 198 | # assume something else 199 | echo "your shell's profile script" 200 | fi 201 | } 202 | 203 | # The `on` command's handler 204 | on() { 205 | loadConf 206 | echo -e "applying proxy settings to server ${YELLOW}${url}${RAW}" 207 | export http_proxy=${url} 208 | export https_proxy=${url} 209 | applyGit ${url} 210 | updateSocksProxy 211 | } 212 | 213 | # Set the proxy settings for git 214 | applyGit() { 215 | # For HTTP/HTTPS protocol 216 | git config --global http.proxy $1 217 | git config --global https.proxy $1 218 | # For git:// protocol 219 | git config --global core.gitproxy ${socks} 220 | echo "✔️️ git configs applied." 221 | } 222 | 223 | # Unset all proxy settings 224 | off() { 225 | unset http_proxy 226 | unset https_proxy 227 | echo "👋🏻️ environment variables unset." 228 | git config --global --unset http.proxy 229 | git config --global --unset https.proxy 230 | git config --global --unset core.gitproxy 231 | echo "👋🏻️ git configs removed." 232 | colorEcho ${RED} " 233 | IF PROXY IS NO LONGER NEEDED, DON'T FORGET TO: 234 | ${RAW} 235 | 1. Remove or comment out the wsl2proxy part in ${YELLOW}$(getProfile)${RAW} 236 | 2. Remove or comment out ssh proxies in ${YELLOW}~/.ssh/config${RAW} 237 | " 238 | } 239 | 240 | # Print the help info 241 | help() { 242 | echo -e " 243 | A WSL proxy setting util. 244 | 245 | - authored by Wizcas 246 | 247 | This tool will detect the Windows host address and apply 248 | proxy settings to HTTP, HTTPS & Git automatically. 249 | 250 | Usage: 251 | ${cmd} 252 | 253 | Commands: 254 | install Installs the script to /usr/local/bin. 255 | setup Sets up proxy parameters (protocol, port, etc.) 256 | and create a helper socks proxy tool. 257 | on Enables proxy settings. Works only with a proper setup. 258 | To execute in current env: ${TEAL}. ${cmd} on ${RAW} 259 | off Disables proxy settings. 260 | To execute in current env: ${TEAL}. ${cmd} off ${RAW} 261 | url Prints the proxy server's URL. 262 | help Shows this help info. 263 | version Shows the script version. 264 | " 265 | } 266 | 267 | # installs the script to /usr/local/bin 268 | install() { 269 | if [ "$EUID" -ne 0 ]; then 270 | colorEcho ${RED} " 271 | Please run this script as root user, i.e. 272 | 273 | ${RAW}sudo $0 install 274 | " 275 | exit 1 276 | fi 277 | exePath="/usr/local/bin/${cmd}" 278 | cp -f $(realpath $0) ${exePath} 279 | chmod +x ${exePath} 280 | echo "✔️️ Done. You can now sets up your proxy server by: 281 | 282 | ${cmd} setup 283 | " 284 | } 285 | 286 | # Main entry of the bash script 287 | case $1 in 288 | install) 289 | install 290 | ;; 291 | setup) 292 | setup 293 | ;; 294 | off) 295 | off 296 | ;; 297 | on) 298 | on 299 | ;; 300 | url) 301 | loadConf 302 | echo ${url} 303 | ;; 304 | version) 305 | echo WSL2Proxy: ${VERSION} 306 | ;; 307 | *) 308 | help 309 | ;; 310 | esac 311 | -------------------------------------------------------------------------------- /wsl2proxy-zh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ########################################## 4 | # Copyrights 2020, Wizcas 5 | # 6 | # This script is under MIT license 7 | ########################################## 8 | 9 | #----------- 辅助工具 ----------- 10 | ####### 颜色码 ######## 11 | RED="\033[31m" # Error message 12 | GREEN="\033[32m" # Success message 13 | YELLOW="\033[33m" # Warning message 14 | BLUE="\033[34m" # Info message 15 | MAGENTA="\033[35m" # Info message 16 | TEAL="\033[36m" # Info message 17 | RAW="\033[0m" 18 | 19 | colorEcho() { 20 | echo -e "${1}${@:2}${RAW}" 1>&2 21 | } 22 | ########################## 23 | 24 | # 一些预定义常量 25 | VERSION='0.2.0' 26 | conf="${HOME}/.wsl2proxy.conf" 27 | socks="${HOME}/socksproxy" 28 | varsfile="${HOME}/wsl2proxy.vars" 29 | cmd="wsl2proxy" 30 | 31 | # 从WSL的DNS配置中解析当前Windows主机的访问IP,因为每次重启都会变 32 | host=$(cat /etc/resolv.conf | sed -n 's/^nameserver\W\(.*\)$/\1/p') 33 | 34 | # 让用户选择代理用什么协议 35 | selectProtocol() { 36 | echo -e "\n${YELLOW}[STEP 1]${BLUE} 代理协议: ${RAW}\n---- 请选择数字 ----" 37 | PROTOCOLS=("http" "https" "socks5" "socks4") 38 | PS3="----------------- 39 | Protocol > " 40 | select protocol in ${PROTOCOLS[@]}; do 41 | case $protocol in 42 | "http" | "https") 43 | ncProtocol="connect" 44 | break 45 | ;; 46 | "socks5") 47 | ncProtocol=5 48 | break 49 | ;; 50 | "socks4") 51 | ncProtocol=4 52 | break 53 | ;; 54 | *) 55 | colorEcho ${RED} "请选择一个从1到${#PROTOCOLS[@]}的数字" 56 | ;; 57 | esac 58 | done 59 | } 60 | 61 | # Let user input the proxy server port 62 | inputPort() { 63 | echo -e "\n${YELLOW}[STEP 2]${BLUE} 代理端口: ${RAW}" 64 | while true; do 65 | read -p "Port > " port 66 | if [ -z ${port} ]; then 67 | colorEcho ${RED} "你的代理总得有个端口吧?😒" 68 | else 69 | if [[ ${port} =~ ^[1-9][0-9]+$ ]] && [[ ${port} -le 65535 ]]; then 70 | break 71 | else 72 | colorEcho ${RED} "无效的端口号" 73 | fi 74 | fi 75 | done 76 | } 77 | 78 | # Save configs into a conf file 79 | saveConf() { 80 | echo "# This file is for storaging WSL proxy settings. 81 | # The host IP is not recorded since it changes every time on reboot. 82 | 83 | protocol=${protocol} 84 | ncProtocol=${ncProtocol} 85 | port=${port} 86 | " >${conf} 87 | 88 | colorEcho ${GREEN} " 89 | 代理配置已成功存储到 ${YELLOW}${conf}${GREEN}, 请不要删除。 90 | " 91 | } 92 | 93 | # Since SSH proxy depends on `nc` tool for forwarding, 94 | # this function checks the prerequisite 95 | checkNc() { 96 | command -v nc >/dev/null 97 | nc=$? 98 | if [ $nc -ne 0 ]; then 99 | colorEcho ${RED} "警告: 未找到命令${YELLOW}nc${RED}。SSH代理需要使用该命令,请自行安装。" 100 | fi 101 | } 102 | 103 | # Create a utility for calling nc to forward network traffics 104 | setupSocksProxy() { 105 | updateSocksProxy 106 | 107 | echo -e "\ 108 | ------------- 109 | ${RED}[需要手动设置]${RAW} 110 | 111 | 已创建网络请求转发脚本${YELLOW}${socks}${RAW}, 112 | 请进行如下配置,以便正常使用SSH代理与git通信: 113 | 114 | 1) 新建或编辑文件${YELLOW}~/.ssh/config\e${RAW},并写入类似下面的内容: 115 | (你可能需要首先给该文件可写权限) 116 | ${TEAL} 117 | Host github.com 118 | User git 119 | ProxyCommand ${socks} '%h %p' 120 | ${RAW} 121 | 2) 重新给${YELLOW}~/.ssh/config${RAW}文件一个只读权限,以通过SSH安全检查 122 | ${TEAL} 123 | chmod 400 ~/.ssh/config 124 | ${RAW} 125 | -------------" 126 | } 127 | 128 | updateSocksProxy() { 129 | # Makes the proxy command 130 | echo "#!/bin/bash 131 | nc -x ${host}:${port} -X ${ncProtocol} \$* 132 | " >${socks} 133 | chmod +x ${socks} 134 | } 135 | 136 | # The `setup` command's handler 137 | setup() { 138 | selectProtocol 139 | inputPort 140 | echo -e "你的代理服务器是: ${TEAL}${protocol}://${host}:${port}${RAW}" 141 | echo 142 | while true; do 143 | read -p "地址是否正确? (Y/n): " CONFIRMED 144 | case $CONFIRMED in 145 | "y" | "Y" | "") 146 | break 147 | ;; 148 | "n" | "N") 149 | colorEcho ${BLUE} "👋🏻️ 再见" 150 | exit 2 151 | ;; 152 | *) 153 | colorEcho ${RED} "啥?" 154 | ;; 155 | esac 156 | done 157 | 158 | saveConf 159 | checkNc 160 | setupSocksProxy 161 | 162 | colorEcho ${RED} "<<< 随WSL启动自动开启代理 >>>" 163 | echo -e "${TEAL}注意: ${RAW}本工具的代理设置会应用到 164 | ${YELLOW}http_proxy, https_proxy${RAW}和${YELLOW}.gitconfig${RAW}上 165 | 166 | 若需随WSL启动自动开启代理,请将下面这行代码添加到${YELLOW}$(getProfile)${RAW}中 167 | (注意不要遗漏命令开头的‘.’): 168 | 169 | ${TEAL}. ${cmd} on${RAW} 170 | " 171 | } 172 | 173 | # Load proxy settings from our config file. Throws an error message if not found. 174 | loadConf() { 175 | if [ ! -f "${conf}" ]; then 176 | colorEcho ${RED} " 177 | 未找到配置文件${TEAL}${conf}${RED},请首先执行${YELLOW}setup${RED}命令。 178 | 179 | ${RAW}用法: ${cmd} setup" 180 | exit 1 181 | fi 182 | . "${conf}" 183 | url="${protocol}://${host}:${port}" 184 | } 185 | 186 | # Determine what's the shell's profile script 187 | getProfile() { 188 | if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then 189 | # assume Zsh 190 | echo '.zshrc' 191 | elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then 192 | # assume Bash 193 | echo '.bashrc' 194 | else 195 | # assume something else 196 | echo "shell启动脚本" 197 | fi 198 | } 199 | 200 | # The `on` command's handler 201 | on() { 202 | loadConf 203 | echo -e "正在配置代理服务器 ${YELLOW}${url}${RAW}" 204 | export http_proxy=${url} 205 | export https_proxy=${url} 206 | applyGit ${url} 207 | updateSocksProxy 208 | } 209 | 210 | # Set the proxy settings for git 211 | applyGit() { 212 | # For HTTP/HTTPS protocol 213 | git config --global http.proxy $1 214 | git config --global https.proxy $1 215 | # For git:// protocol 216 | git config --global core.gitproxy ${socks} 217 | echo "✔️️ git设置完毕" 218 | } 219 | 220 | # Unset all proxy settings 221 | off() { 222 | unset http_proxy 223 | unset https_proxy 224 | echo "👋🏻️ 已删除系统变量" 225 | git config --global --unset http.proxy 226 | git config --global --unset https.proxy 227 | git config --global --unset core.gitproxy 228 | echo "👋🏻️ 已删除git配置" 229 | colorEcho ${RED} " 230 | 如果不再使用代理服务器,别忘了: 231 | ${RAW} 232 | 1. 删除或注释掉${YELLOW}$(getProfile)${RAW}中 wsl2proxy 相关的部分 233 | 2. 删除或注释掉${YELLOW}~/.ssh/config${RAW}中的代理设置 234 | " 235 | } 236 | 237 | # Print the help info 238 | help() { 239 | echo -e " 240 | WSL代理服务器配置工具 241 | 242 | - authored by Wizcas 243 | 244 | 本工具自动检测WSL的Windows主机地址,并使用自定义的代理信息为 245 | HTTP,HTTPS和Git添加代理配置。 246 | 247 | Usage: 248 | ${cmd} 249 | 250 | Commands: 251 | install 将脚本安装到 /usr/local/bin 252 | setup 设置代理参数 (协议、端口等) ,并创建socks代理 253 | 转发脚本 254 | on 开启代理设置,只有使用setup正常配置后才能生效。 255 | 执行方式:${TEAL}. ${cmd} on${RAW} 256 | off 关闭代理设置。 257 | 执行方式:${TEAL}. ${cmd} off${RAW} 258 | url 打印当前的代理服务器地址 259 | help 显示本帮助信息 260 | version 显示脚本版本 261 | " 262 | } 263 | 264 | # installs the script to /usr/local/bin 265 | install() { 266 | if [ "$EUID" -ne 0 ]; then 267 | colorEcho ${RED} " 268 | 请使用root权限执行脚本,如: 269 | 270 | ${RAW}sudo $0 install 271 | " 272 | exit 1 273 | fi 274 | exePath="/usr/local/bin/${cmd}" 275 | cp -f $(realpath $0) ${exePath} 276 | chmod +x ${exePath} 277 | echo "✔️️ 安装完成。现在可以用如下命令配置代理: 278 | 279 | ${cmd} setup 280 | " 281 | } 282 | 283 | # Main entry of the bash script 284 | case $1 in 285 | install) 286 | install 287 | ;; 288 | setup) 289 | setup 290 | ;; 291 | off) 292 | off 293 | ;; 294 | on) 295 | on 296 | ;; 297 | url) 298 | loadConf 299 | echo ${url} 300 | ;; 301 | version) 302 | echo WSL2Proxy: ${VERSION} 303 | ;; 304 | *) 305 | help 306 | ;; 307 | esac 308 | --------------------------------------------------------------------------------