lmk123 / blog

个人技术博客,博文写在 Issues 里。
https://github.com/lmk123/blog/issues
623 stars 35 forks source link

给 WSL2 使用 Windows 代理的方法 #89

Open lmk123 opened 3 years ago

lmk123 commented 3 years ago

2021 年 9 月更新

再次遇到这个问题,具体步骤:

  1. 打开 Allow LAN
  2. 鼠标移动到 Allow LAN 这一栏会显示一个浮窗,可以查看 WSL 的 IP 地址,一般是 172.21.xxx.xxx
  3. 进入 WSL 后运行 export https_proxy=http://172.21.xxx.xxx:7890
  4. 测试 curl -vv https://google.com

如果不行,那么需要在控制面板搜索“允许应用通过 Windows 防火墙”,找到 clash 相关的应用,然后勾选应用名字两边的勾选框(应用名字左边一个、右边两个)

以下是原文(已失效)。


我在 Windows 上已经用高科技软件在 7890 端口开启了代理,于是我尝试了以下方式:

$ export https_proxy=http://127.0.0.1:7890
$ curl https://google.com
curl: (7) Failed to connect to 127.0.0.1 port 7890: Connection refused

我记得这种方式以前是行得通的。谷歌了一下之后,好像是微软在 WSL 2 中更改了 Linux 子系统与 Windows 主机的通信方式,WSL 1 可以通过 localhost 互相访问,但 WSL 2 不行了,见比较 WSL 1 和 WSL 2

然后,网上给出的解决方案基本都是用下面这种方式:

$ export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
$ export https_proxy="http://${hostip}:7890"
$ export http_proxy="http://${hostip}:7890"

然而我试了之后,发现还是不行:

$ curl https://google.com
curl: (7) Failed to connect to 172.xxx.xxx.xxx port 7890: Connection timeout

尝试了多种方式无果之后,我突然发现不对劲:上面的 hostip 好像是 WSL 2 的 IP,而不是我的 Windows 系统的 IP,我在高科技软件里看到的 Windows 的 IP 是 192.168.xxx.xxx

于是我又尝试了以下方式:

先安装 TAP Device,然后

$ export https_proxy=http://192.168.xxx.xxx:7890
$ curl https://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

成功了!——虽然不知道为什么 :joy:

ZedElysium commented 7 months ago

谢谢你的帖子,给了我很大的帮助

IndifferentArea commented 7 months ago

请问您还记得tap device具体怎么安装的吗

leviyanx commented 7 months ago

@IndifferentArea clash for windows里有这个选项 image

lmk123 commented 7 months ago

我在 macOS 上用的 Class for Windows v0.20.15 没有 TAP Device 选项了

IndifferentArea commented 7 months ago

终于配好了,thx!🥰🥰

@lmk123 @leviyanx

lvmaoer commented 2 months ago

简单有效👍

Z500-RAY commented 6 days ago

感谢大佬!