Warm tip: This article is reproduced from serverfault.com, please click

routes-仅将 OpenVPN 连接用于内部资源

(routes - Use OpenVPN connection only for internal resources)

发布于 2020-12-28 15:00:41

我在 Linux Mint 上连接到 OpenVPN,然后:

  1. 我立即可以访问我的内部 VPN 资源,但我也通过 VPN 访问 Internet,而不是通过我自己的连接,所以我输入:

sudo 路由添加默认 gw 1​​92.168.1.1 wlp3s0

  1. 然后我可以通过我自己的连接(而不是通过 VPN)访问 Internet,但是我无法访问 VPN 内部资源。

  2. 然后我输入:

路由添加 -net 10.100.0.0 网络掩码 255.255.0.0 dev tun0

现在我通过自己的连接访问 Internet,并且可以通过 VPN 访问 VPN 内部资源。

这是经过上述所有步骤后在Linux机器上的路由:

$ route -v
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         funbox.home     0.0.0.0         UG    0      0        0 wlp3s0
default         _gateway        0.0.0.0         UG    50     0        0 tun0
default         funbox.home     0.0.0.0         UG    600    0        0 wlp3s0
10.8.0.0        _gateway        255.255.255.0   UG    50     0        0 tun0
10.8.0.1        _gateway        255.255.255.255 UGH   50     0        0 tun0
_gateway        0.0.0.0         255.255.255.255 UH    50     0        0 tun0
10.100.0.0      0.0.0.0         255.255.0.0     U     0      0        0 tun0
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 wlp3s0
178.183.8.254.p funbox.home     255.255.255.255 UGH   600    0        0 wlp3s0
192.168.0.0     _gateway        255.255.0.0     UG    50     0        0 tun0
192.168.1.0     0.0.0.0         255.255.255.0   U     600    0        0 wlp3s0
funbox.home     0.0.0.0         255.255.255.255 UH    600    0        0 wlp3s0

它在 Linux 上运行良好,但我想在 Windows 上实现相同的目标。不同之处在于,在 Linux 上我使用的是 Wi-Fi,而在 Windows 机器上,我通过以太网电缆连接到 Internet 到路由器。

在Windows上,我使用OpenVPN软件进行连接,现在我可以访问内部VPN资源,访问Internet时,它是通过VPN而不是我的连接完成的。

我尝试在 Windows 上输入以下内容:

route add 192.168.1.1 mask 255.255.255.255 192.168.1.10 (this is my internal IP assigned by router)
route add 10.100.0.0 mask 255.255.0.0 10.8.0.22 (this is my IP assigned by OpenVPN)

当我输入它们时,我无法访问 VPN 的内部资源,并且仍然可以通过 VPN 访问 Internet。

这是连接OpenVPN后的路由:

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      192.168.1.1     192.168.1.10     25
          0.0.0.0        128.0.0.0        10.8.0.21        10.8.0.22    281
         10.8.0.0    255.255.255.0        10.8.0.21        10.8.0.22    281
         10.8.0.1  255.255.255.255        10.8.0.21        10.8.0.22    281
        10.8.0.20  255.255.255.252         On-link         10.8.0.22    281
        10.8.0.22  255.255.255.255         On-link         10.8.0.22    281
        10.8.0.23  255.255.255.255         On-link         10.8.0.22    281
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
        128.0.0.0        128.0.0.0        10.8.0.21        10.8.0.22    281
    178.183.8.254  255.255.255.255      192.168.1.1     192.168.1.10    281
      192.168.0.0      255.255.0.0        10.8.0.21        10.8.0.22    281
      192.168.1.0    255.255.255.0         On-link      192.168.1.10    281
     192.168.1.10  255.255.255.255         On-link      192.168.1.10    281
    192.168.1.255  255.255.255.255         On-link      192.168.1.10    281
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link         10.8.0.22    281
        224.0.0.0        240.0.0.0         On-link      192.168.1.10    281
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  255.255.255.255  255.255.255.255         On-link         10.8.0.22    281
  255.255.255.255  255.255.255.255         On-link      192.168.1.10    281

我尝试编辑 OpenVPN 配置文件并注释掉这一行:

#redirect-gateway def1

然后我可以通过我的连接访问互联网,但无法访问内部资源,添加路由添加 10.100.0.0 也无济于事。

如何通过我自己的连接在 Windows 上访问 Internet 并且不会失去对内部 VPN 资源的访问权限(如在 Linux 上)?

Questioner
Michał Kochanowicz
Viewed
0
Michał Kochanowicz 2020-12-29 00:18:11

通过编辑 OpenVPN 配置文件解决了该问题。现在我不需要手动添加任何路由了!

我已经注释掉了重定向网关 def1 行:

#redirect-gateway def1

添加了带有 DNS 服务器的 dhcp 选项:

dhcp-option DNS 10.100.20.10

并在“拉”命令之前添加了一条路线:

route 10.100.0.0 255.255.0.0

可以通过我自己的连接访问Internet,可以通过VPN连接访问VPN内部资源,很棒:)