软件:Clash

代理软件设置

  • 查看代理端口,Clash默认端口为7890
  • 打开LAN口开关

wsl设置

原理

原理是使用本机作为代理服务器,为wsl提供代理服务,因此需要本机ip地址和代理服务端口,并将wsl的代理设置为该代理服务器。

脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/sh
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
wslip=$(hostname -I | awk '{print $1}')
port=7890

PROXY_HTTP="http://${hostip}:${port}"

set_proxy(){
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}
}

unset_proxy(){
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset ALL_PROXY
unset all_proxy
}

test_setting(){
echo "Host ip:" ${hostip}
echo "WSL ip:" ${wslip}
echo "Current proxy:" $https_proxy
}

if [ "$1" = "set" ]
then
set_proxy

elif [ "$1" = "unset" ]
then
unset_proxy

elif [ "$1" = "test" ]
then
test_setting
else
echo "Unsupported arguments."
fi

解释一下脚本的使用:

  • . proxy.sh set:获取本机ip,并将代理服务器添加到wsl系统中。
  • . proxy.sh unset:取消代理
  • . proxy.sh test:查看信息
    新建文件proxy.sh,并添加脚本到系统变量中,可以更方便的使用脚本,同时开机启动代理。
    打开.bashrc,添加一下代码:
1
2
alias proxy="source ~/proxy.sh"
. ~/proxy.sh set

记得运行命令source ~/.bashrc,刷新环境变量