WSL2 访问 Windows 代理


获取 WSL2 IP 地址

在 WSL2 中,要访问 Windows 上运行的网络应用(例如在 Windows 上运行的代理软件、SQL 服务器等),需要使用 WSL2 的 IP 地址,而不是 localhost

WSL2 的 IP 地址保存在 /etc/resolv.conf 文件中的 nameserver,可以通过一下命令获取:

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

设置环境变量

假定代理软件的默认 sock5 的监听端口为 10808

export PROXY_PORT=10808
export WINDOWS_NAMESERVER=`cat /etc/resolv.conf|grep nameserver|awk '{print $2}'`
export WIN_PROXY=socks5://$WINDOWS_NAMESERVER:$PROXY_PORT
export HTTP_PROXY=$WIN_PROXY
export HTTPS_PROXY=$WIN_PROXY

Windows 防火墙以及代理软件设置

默认情况下代理软件是不能通过防火墙的,需要允许代理软件通过 Windows 的防火墙,设置如图:

windows 防火墙

代理软件必须开启允许来自局域网的连接(connection from private net work)

现在可以通过 curl www.google.com 来测试代理是否生效,如果控制台有输出即为设置成功。

最后附上一个脚本,可以通过 proxy start|stop|status 开启、关闭或者检查代理。

#!/bin/bash
PROXY_PORT=10808
WINDOWS_NAMESERVER=`cat /etc/resolv.conf|grep nameserver|awk '{print $2}'`
ALL_PROXY=socks5://$WINDOWS_NAMESERVER:$PROXY_PORT
GIT_HTTP_PROXY=`git config --global --get http.proxy`
GIT_HTTPS_PROXY=`git config --global --get https.proxy`

case $1 in
        start)
            export HTTP_PROXY=$ALL_PROXY
            export HTTPS_PROXY=$ALL_PROXY

            if [ "`git config --global --get http.proxy`" != $ALL_PROXY ]; then
                git config --global http.proxy $ALL_PROXY
                git config --global https.proxy $ALL_PROXY
            fi
            ;;
        stop)
            unset HTTP_PROXY
            unset HTTPS_PROXY

            if [ "`git config --global --get http.proxy`" = $ALL_PROXY ]; then
                git config --global --unset http.proxy
                git config --global --unset https.proxy
            fi
            ;;
        status)
            echo "HTTP_PROXY:" $HTTP_PROXY
            echo "HTTPS_PROXY:" $HTTPS_PROXY
            echo "GIT_HTTP_PROXY: `git config --global --get http.proxy`"
            echo "GIT_HTTPS_PROXY: `git config --global --get https.proxy`"
            ;;
        *)
            echo "usage: source $0 start|stop|status"
            ;;
esac

使得命令生效的话还需要在 ~/.bashrc 文件末尾新增一条命令(~/.set-wsl2-proxy.sh 表示脚本路径):

alias proxy="source ~/set-wsl2-proxy.sh"


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM