Skip to content

Instantly share code, notes, and snippets.

@ichenhe
Created September 26, 2020 06:02
Show Gist options
  • Save ichenhe/252e510d3563d602c5b41321eabfd390 to your computer and use it in GitHub Desktop.
Save ichenhe/252e510d3563d602c5b41321eabfd390 to your computer and use it in GitHub Desktop.
Setup proxy for WSL2
  1. Add the following code to .bashrc.
  2. Run source .bashrc to reload.
  3. Run proxy to setup proxy, and unproxy to unproxy.
function getip {
        export winip=$(ip route | grep default | awk '{print $3}')
        export wslip=$(hostname -I | awk '{print $1}')
        echo "WIN IP: ${winip}"
        echo "WSL IP: ${wslip}"
        export PROXY_HTTP="http://${winip}:7890"      # SET YOUR HTTP PORT
        export PROXY_SOCKS5="socks5://${winip}:7890"  # SET YOUR SOCKS PORT
}

function proxy {
        getip
        export http_proxy="${PROXY_HTTP}"
        export HTTP_PROXY="${PROXY_HTTP}"
        export https_proxy="${PROXY_HTTP}"
        export HTTPS_PROXY="${PROXY_HTTP}"
        export all_proxy="${PROXY_SOCKS5}"
        export ALL_PROXY="${PROXY_SOCKS5}"

        echo "http: ${PROXY_HTTP}"
        echo "socks5: ${PROXY_SOCKS5}"
        echo "DONE!"
}

function unproxy {
        unset http_proxy
        unset HTTP_PROXY
        unset https_proxy
        unset HTTPS_PROXY
        unset all_proxy
        unset ALL_PROXY
        echo "DONE!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment