Skip to content

WSL Proxy Solution

Posted on:August 19, 2023 at 10:22 PM

Host side

WSL side

# 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";
# /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