gpt4 book ai didi

vpn - PulseSecure VPN 阻止 WSL2 互联网连接

转载 作者:行者123 更新时间:2023-12-03 23:44:10 30 4
gpt4 key购买 nike

在我的企业中,在听到社区的赞誉后,我们开始迁移到基于 WSL2 的解决方案。
但是,在我们连接到我们的企业 VPN 后,WSL 无法连接到外部网站(以及 VPN 内部的网站)。
似乎是windows内部的路由问题,我该如何解决?

最佳答案

问题主要在于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)
    在此示例中,我们路由到的接口(interface)是 WSL 接口(interface)。
    我们激活VPN后,这个规则的接口(interface)应该改成VPN接口(interface)。
    或者,我可以只使用 route在 WSL 中,并查看网关 IP。这实际上是我必须在 route PRINT -4 中寻找的规则 ip在我的 Windows 机器中。
    重复条目
    让我们假设我们发现的规则来自 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 接口(interface),更改较早。
    我们仍然希望任何以 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 接口(interface)描述。 (同样,很容易来自 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

    关于vpn - PulseSecure VPN 阻止 WSL2 互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63972437/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com