PulseSecure VPN阻止WSL2互联网连接

问题描述

在我的企业中,在得到社区的好评之后,我们开始迁移到基于WSL2的解决方案。

但是,在我们连接到企业VPN之后,WSL无法连接到外部网站(以及VPN内的站点)。

好像是Windows内的路由问题,我该如何解决

解决方法

我修改了@Aviv 发布的脚本,并在我打包之后取得了一些成功。双引号中的字符并取出centos的检查。以下是对我有用的最终产品。

#Test VPN is ON
$VpnIsOn = Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Measure-Object -Line
if ($VpnIsOn.Lines -eq 0)
{
        Write-Host "No VPN is ON!";
        exit
}

$gateway_ip = wsl -- route -ne "|" grep ^[1-9] "|" cut -d" " -f1
$gw_mask    = wsl -- route -ne "|" grep ^[1-9] "|" column -t "|" cut -d" " -f5
$tmp        =  Get-NetIPConfiguration -InterfaceAlias "*WSL*" | select InterfaceIndex
$tmp -match "\d{1,4}"
$ifindex = $matches[0]

# recreate the route entry to WSL (pulse secure turs the GW to it own NIC)
route ADD $gateway_ip MASK $gw_mask 0.0.0.0 METRIC 2 IF $ifindex

# set priorities - VPN to 6000 WSL to 4000 (WSL should be done)
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Set-NetIPInterface -InterfaceMetric 6000
,

问题主要出在Windows的路由表中。

解决方案包括:

  • 找出路线
  • 重复的路线条目
  • 修改两个条目的指标
  • 调整DNS

找出所需的路由规则

使用 route PRINT -4 可以看到现有的路由条目。

您可以通过手动删除任何WSL条目来找出WSL所需的路由条目,直到WSL中的连接断开。 找到之后,可以通过禁用和启用WSL网络来恢复WSL路由条目:

netsh interface set interface "vEthernet (WSL)" disable
netsh interface set interface "vEthernet (WSL)" enable

假设我们的规则如下:

192.168.74.32  255.255.255.240         On-link     192.168.74.33   5256
^ inet         ^ mask of subnet                    ^Interface      ^Metric (cost of routing)

在此示例中,我们路由到的接口是WSL接口。 激活VPN后,应将此规则的接口更改为VPN接口。

或者,我可以只在WSL中使用route,然后查看网关IP。这实际上是我必须在Windows计算机中的route PRINT -4中查找的IP规则。

重复条目

假设我们从route找到的规则是:

192.168.74.32  255.255.255.240         On-link     172.22.0.1   1
^ inet         ^ mask of subnet                    ^Interface   ^Metric (cost of routing)

172.22.0.1显然是先前更改的VPN接口。 我们仍然希望发往192.168.74.32的所有流量都能到达我们的WSL设备。因此,我们将通过以下方式创建另一个路线条目:

route ADD 192.168.74.32 MASK 255.255.255.240 0.0.0.0 METRIC 2 IF NN
NN -> interface index of WSL. you can find it out easily from ipconfig /all

好。我们现在在表中有两个条目

192.168.74.32  255.255.255.240         On-link     172.22.0.1   1
192.168.74.32  255.255.255.240         On-link     192.168.74.33   522

请注意,路由成本可能与您在命令中设置的成本有所不同。但是,VPN更有可能获得较小的权重(因此优先于WSL路由条目)。

修改两个条目的指标

在此阶段,我们将使VPN路由条目的吸引力不如类似的WSL路由条目。

为此,我们将调整VPN路由条目度量标准,使其高于WSL的路由条目。为此,只需执行:

Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Set-NetIPInterface -InterfaceMetric 6000

注意:

  • 确保字符串“ Juniper”唯一且单独地描述您的VPN接口描述。 (同样,很容易从 ipconfig /all 中获得)
  • 如果您的WSL规则高于它,则可以将InterfaceMetric值设置为大于6000。

完成这些操作后,Mazal Tov,链接完成,您的WSL可以(理论上)连接到互联网

调整DNS

这部分可能不会影响您,但仍然是我必须提供的解决方案的一部分。 已经充分讨论了此问题,并且已经存在解决方案。 例如this github repo

在此解决方案中,我们仅将nameserver 8.8.8.8(Google DNS)添加到WSL中的 /etc/resolv.conf 文件中。 无需重新启动,保存更改后应立即应用

包装成脚本

我用这个简单但非常原始的脚本自动化了这个过程。 请注意,该脚本假定WSL运行Centos7,并且不会自动执行DNS修改(这需要WSL内部具有root权限)

#Test VPN is ON
$VpnIsOn = Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Measure-Object -Line
if ($VpnIsOn.Lines -eq 0)
{
        Write-Host "No VPN is ON!";
        exit
}

$activeWSLDist = wsl -l -q --running
if (-Not $activeWSLDist -Contains 'Centos7')
{
        Write-Host "Turn ON WSL!";
        exit
}

$dns = wsl -- grep -e allot -e rdlab /etc/resolv.conf | wc -l
if ($dns -eq "0")
{
        Write-Host "add 'nameserver 8.8.8.8' in /etc/resolv.conf"
}
else
{
        Write-Host "DNS Already configured"
}

$gateway_ip = wsl -- route -ne | grep ^[1-9] | cut -d" " -f1
$gw_mask    = wsl -- route -ne | grep ^[1-9] | column -t | cut -d" " -f5
$tmp        =  Get-NetIPConfiguration -InterfaceAlias "*WSL*" | select InterfaceIndex
$tmp -match "\d{1,4}"
$ifindex = $matches[0]

# recreate the route entry to WSL (pulse secure turs the GW to it own NIC)
route ADD $gateway_ip MASK $gw_mask 0.0.0.0 METRIC 2 IF $ifindex

# set priorities - VPN to 6000 WSL to 4000 (WSL should be done)
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Set-NetIPInterface -InterfaceMetric 6000
# Not needed - but good to mention anyways
#Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "WSL"} | Set-NetIPInterface -InterfaceMetric 4000
,

将来自 Windows 的 Pulse DNS 服务器和 DNS 后缀添加到 WSL2 内的 /etc/resolv.conf 后,它对我有用

现在看起来像下面

# 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
search internal.*.com
nameserver **.***.*.*
nameserver **.***.*.*
nameserver ***.**.***.***

上面的*是通配符

此外,您必须编辑上面评论中提到的 /etc/wsl.conf。否则 /etc/resolv.conf 将被覆盖

我想当这些 DNS 服务器更改时我必须刷新上面的地址