Problem
I need Ubuntu WSL to use proxied connections hosted on Windows. I face slow or inaccessible resources and want to use a proxy server hosted on the Windows host, typically running on a localhost port.
Solution
To ensure the proxy settings are applied every time you start a new shell session, add the following script to your .bashrc
file (or whatever init shell you use):
# Get the IP address of the Windows host from the WSL resolv.conf file
export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
# Create an alias to set the proxy environment variables
alias setss='export https_proxy="http://${hostip}:7890";export http_proxy="http://${hostip}:7890";'
# Create an alias to unset the proxy environment variables
alias unsetss='unset all_proxy'
Source .bashrc to apply changes
source ~/.bashrc
Whenever I want to use a proxied connection, I simply call the alias in WSL Ubuntu:
setss # to enable
# or
unsetss # to disable