Host side
- V2ray proxy
-
Enable connections from local network, and export port 10810, 10811 for local network connextions
-
Enable Windows firewall inbound rules on port 10810, 10811 for both TCP, DCP traffic
-
WSL side
- Get hostβs local ip address dinamically (/etc/resolve.conf)
- Set global proxy by environment variables
# global proxy settings
HOST_IP=$(cat /etc/resolv.conf | grep --color=never nameserver | awk '{print $2}')
export http_proxy="http://${HOST_IP}:10811";
export https_proxy="http://${HOST_IP}:10811";
export HTTP_PROXY="http://${HOST_IP}:10811";
export HTTPS_PROXY="http://${HOST_IP}:10811";
- Set proxy for apt-get
# /etc/apt/apt.conf, manually change __HOST_IP_PLACEHOLDER__
Acquire::http::Proxy "http://__HOST_IP_PLACEHOLDER__:10811/";
# or else if you enable no-passwd sudo, add following lines into .bashrc
sudo rm -f /etc/apt/apt.conf && \
eval 'echo Acquire::http::Proxy \"http://${HOST_IP}:10811/\"\;' > ${HOME}/apt.conf.tmp && \
sudo mv ${HOME}/apt.conf.tmp /etc/apt/apt.conf
Advanced
If you have another VPN service running on host machine like me(Corp VPN). You can edit routing rules in V2ray, from proxy
to direct
, then traffic will be transport from WSL to Host V2ray to Host Proxy(which is your current effective VPN service).
cheatsheet
#!/bin/bash
# global proxy settings
HOST_IP=$(cat /etc/resolv.conf | grep --color=never nameserver | awk '{print $2}')
# use netcat to test proxy port first
nc $HOST_IP 10811 -c 'echo testconnectivity' -w 1 2>&1 >/dev/null
if [[ "$?" == "0" ]]; then
export http_proxy="http://${HOST_IP}:10811";
export https_proxy="http://${HOST_IP}:10811";
export HTTP_PROXY="http://${HOST_IP}:10811";
export HTTPS_PROXY="http://${HOST_IP}:10811";
sudo rm -f /etc/apt/apt.conf && \
eval 'echo Acquire::http::Proxy \"http://${HOST_IP}:10811/\"\;' > ${HOME}/apt.conf.tmp && \
sudo mv ${HOME}/apt.conf.tmp /etc/apt/apt.conf
fi