wsl2使用windows代理

使用 wsl2 克隆的过程中经常因为奇慢无比的速度而抓狂,查阅了相关资料之后,找出对自己有效的 wsl2 走 windows 代理的方法
git.jpg

一、前置

这里使用的是 clash for windows,注意需要打开允许局域网,打开的时候可能发现,鼠标悬停在开关上时,能看到 wsl 的 ip

二、具体实施

在 wsl 终端输入:

1
cat /etc/resolv.conf

可以查看 hostip:

1
2
3
4
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.24.32.1

此后输入

1
2
3
4
export https_proxy="http://${hostip}:7890"
export http_proxy="http://${hostip}:7890"
export all_proxy="socks5://${hostip}:7890"
#hostip为刚才查询的ip,后面的端口号 clash默认为 7890

三、写成脚本

1
2
3
4
5
#config.fish
set host_ip (cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")
alias setproxy "export all_proxy=http://$host_ip:7890 && export http_proxy=http://$host_ip:7890 && export https_proxy=http://$host_ip:7890"
alias unset "set --erase" # fish-shell使用unset需要 set --erase
alias unsetproxy "unset all_proxy && unset http_proxy && unset https_proxy"

将上述内容写入 fish-shell 配置文件,使用 setproxy 打开代理,unsetproxy 关闭代理

四、检测成果

测试的方法有很多,比如:

1
wget youtube.com

或者

1
w3m www.google.com