WSL2 配置代理

WSL2 配置代理

WSL2不像WSL,可以直接使用主机的代理,WSL2 基于 Hyper-V 运行,导致 Linux 子系统和 Windows 在网络上是两台各自独立的机器,从 Linux 子系统访问 Windows 首先需要找到 Windows 的 IP。

配置 WSL2 访问 Windows 上的代理

开放windows防火墙

使用管理员模式下的powershell

# 直接放开 `vEthernet (WSL)` 这张网卡的防火墙
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow

同时代理软件也要允许来自局域网的连接
例如clash
image.png

获取windows IP

在WSL2中获取主机ip,可以使用命令

cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }'

获取到之后可以设置代理

export all_proxy="${hostip}:7890"

一键设置代理

由于主机ip地址会不停的变动,因此我们可以直接写一个脚本来每次自动获得ip并设置代理,我的终端使用了zsh,因此编辑~/.zshrc

vim ~./zshrc

在里面添加

proxy () {
  export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
  export ALL_PROXY="http://${hostip}:7890"
  export all_proxy="http://${hostip}:7890"
  # 设置apt的代理
  # echo -e "Acquire::http::Proxy \"http://${hostip}:7890\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
  # echo -e "Acquire::https::Proxy \"http://${hostip}:7890\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
  # 测试是否代理成功
  curl google.com
  curl https://ip.gs
}

noproxy () {
  unset ALL_PROXY
  unset all_proxy
  # 取消apt代理
  # sudo sed -i -e '/Acquire::http::Proxy/d' /etc/apt/apt.conf
  # sudo sed -i -e '/Acquire::https::Proxy/d' /etc/apt/apt.conf
  curl https://ip.gs
}

添加完保存,并重新激活

source ~/.zshrc

之后输入proxynoproxy就能够启动和取消代理了。

记录

上文属于比较简单的配置
这里抄了一份网上大佬的配置,记录在这里,需要时再用

# Proxy configuration
getIp() {
    export winip=$(ip route | grep default | awk '{print $3}')
    export wslip=$(hostname -I | awk '{print $1}')
    export PROXY_SOCKS5="socks5://${winip}:7890"
    export PROXY_HTTP="http://${winip}:7890"
}

proxy_git() {
    ssh_proxy="${winip}:7890"
    git config --global http.https://github.com.proxy ${PROXY_HTTP}
    if ! grep -qF "Host github.com" ~/.ssh/config ; then
        echo "Host github.com" >> ~/.ssh/config
        echo "    User git" >> ~/.ssh/config
        echo "    ProxyCommand nc -X 5 -x ${ssh_proxy} %h %p" >> ~/.ssh/config
    else
        lino=$(($(awk '/Host github.com/{print NR}'  ~/.ssh/config)+2))
        sed -i "${lino}c\    ProxyCommand nc -X 5 -x ${ssh_proxy} %h %p" ~/.ssh/config
    fi
}

winip_() {
    getIp
    echo ${winip}
}

wslip_() {
    getIp
    echo ${wslip}
}

x11() {
    getIp
    if [ ! $1 ]; then
        # null
        export DISPLAY=${winip}:0.0
    else
        export DISPLAY=${winip}:$1.0
    fi
    echo $DISPLAY
    export XDG_SESSION_TYPE=x11
    export XDG_RUNTIME_DIR=/tmp/runtime-root
    export LIBGL_ALWAYS_INDIRECT=1
    export PULSE_SERVER=tcp:$winip
}

ip_() {
    getIp
    https --follow -b https://api.ip.sb/geoip/$1
    echo "WIN ip: ${winip}"
    echo "WSL ip: ${wslip}"
}

proxy_npm() {
    getIp
    npm config set proxy ${PROXY_HTTP}
    npm config set https-proxy ${PROXY_HTTP}
    yarn config set proxy ${PROXY_HTTP}
    yarn config set https-proxy ${PROXY_HTTP}
}

unpro_npm() {
    npm config delete proxy
    npm config delete https-proxy
    yarn config delete proxy
    yarn config delete https-proxy
}

proxy() {
    getIp
    # pip can read http_proxy & https_proxy
    export http_proxy="${PROXY_HTTP}"
    export HTTP_PROXY="${PROXY_HTTP}"
    export https_proxy="${PROXY_HTTP}"
    export HTTPS_PROXY="${PROXY_HTTP}"
    export ftp_proxy="${PROXY_HTTP}"
    export FTP_PROXY="${PROXY_HTTP}"
    export rsync_proxy="${PROXY_HTTP}"
    export RSYNC_PROXY="${PROXY_HTTP}"
    export ALL_PROXY="${PROXY_SOCKS5}"
    export all_proxy="${PROXY_SOCKS5}"
    proxy_git
    if [ ! $1 ]; then
        ip_
    fi
    echo "Acquire::http::Proxy \"${PROXY_HTTP}\";" | sudo tee /etc/apt/apt.conf.d/proxy.conf >/dev/null 2>&1
    echo "Acquire::https::Proxy \"${PROXY_HTTP}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf >/dev/null 2>&1
}

unpro () {
    unset http_proxy
    unset HTTP_PROXY
    unset https_proxy
    unset HTTPS_PROXY
    unset ftp_proxy
    unset FTP_PROXY
    unset rsync_proxy
    unset RSYNC_PROXY
    unset ALL_PROXY
    unset all_proxy
    sudo rm /etc/apt/apt.conf.d/proxy.conf
    git config --global --unset http.https://github.com.proxy
    ip_
}

参考

包括主机访问WSL2的问题
WSL2 的一些网络访问问题