准备工作

V2Ray设置 允许来自互联网的连接

image

记下V2Ray本地的IP

image

新增两个脚本

start_proxy.sh(开启代理的脚本)

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh
# 获取wsl2 ip地址
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
# 设置v2ray的IP(准备工作第二步)
port=10809
PROXY_HTTP="http://${hostip}:${port}"

export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"

export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"

stop_proxy.sh(关闭代理的脚本)

1
2
3
4
5
#!/bin/sh
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_proxy

给这两个脚本增加权限

1
2
chmod 777 start_proxy.sh
chmod 777 stop_proxy.sh

打开代理

1
source start_proxy.sh

运行export查看是否添加成功,如下图

image

运行curl -vv google.com查看代理是否正常

image

关闭代理

1
source stop_proxy.sh

运行export查看是否删除成功,如下图

image

运行curl -vv google.com查看是否正常关闭代理,无法访问

image