通过 VPN 访问服务器 LAN 上的设备

通过 VPN 访问服务器 LAN 上的设备

我有一个 VPN 设置,它允许我通过隧道回家并访问网络上运行的任何家庭服务器。

但是,我只能通过 Android 设备的移动网络或连接到移动热点的 Macbook 使用它。我可以从任何网络连接到我的 OpenVPN 服务器,但当客户端设备连接到任何家庭/工作网络(ISP,由于没有更好的术语,在下面的日志中)时,连接就会超时。

不确定是否相关,但arp输出如下所示。我使用 OpenVPN Connect 连接到我家乡的 OpenVPN 服务器。

> # ISP 
> arp -a
iopsys.lan (192.168.1.1) at 34:e3:80:8c:b8:c0 on en0 ifscope [ethernet]
? (192.168.1.117) at a4:98:13:44:93:c7 on en0 ifscope [ethernet]
? (192.168.1.160) at (incomplete) on en0 ifscope [ethernet]
jbl-bar-9.lan (192.168.1.223) at 2c:fd:b3:d1:18:bc on en0 ifscope [ethernet]
? (xxx.xxx.xxx.xxx) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
? (xxx.xxx.xxx.xxx) at 1:0:5e:7f:ff:fa on en0 ifscope permanent [ethernet]

> # ISP + VPN tunnel home – can't access any home machine by its IP, although they are in the list below
> arp -a
? (192.168.1.1) at 34:e3:80:8c:b8:c0 on en0 ifscope [ethernet]
? (192.168.1.117) at a4:98:13:44:93:c7 on en0 ifscope [ethernet]
? (192.168.1.160) at (incomplete) on en0 ifscope [ethernet]
? (192.168.1.223) at 2c:fd:b3:d1:18:bc on en0 ifscope [ethernet]
? (192.168.1.224) at 24:6f:28:5b:bc:c0 on en0 ifscope [ethernet]
? (192.168.1.255) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
? (xxx.xxx.xxx.xxx) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
? (xxx.xxx.xxx.xxx) at 1:0:5e:7f:ff:fa on en0 ifscope permanent [ethernet]

> # Mobile hotspot
> arp -a
? (192.168.146.123) at ce:30:4:ae:27:6c on en0 ifscope [ethernet]
? (192.168.146.255) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
? (xxx.xxx.xxx.xxx) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
? (xxx.xxx.xxx.xxx) at 1:0:5e:7f:ff:fa on en0 ifscope permanent [ethernet]

> # Mobile hotspot + VPN tunnel home – everything works, I can access all local IPs that aren't even listed in the table below
> arp -a
? (192.168.146.123) at ce:30:4:ae:27:6c on en0 ifscope [ethernet]
? (192.168.146.255) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
? (xxx.xxx.xxx.xxx) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
? (xxx.xxx.xxx.xxx) at 1:0:5e:7f:ff:fa on en0 ifscope permanent [ethernet]

我认为我遗漏了 VPN 工作方式的一部分,即如果两个网络(客户端 LAN 和服务器 LAN)都使用相同的 IP 范围,而我想要访问服务器的 LAN,那么这两个网络是如何分离的。我尝试在线搜索,但我认为我没有使用正确的关键字来找到它。

如果有人能给我指明正确的方向,我将不胜感激,因为我真的很想了解如何让它工作以及为什么会出现这种行为。

答案1

让您更深入地了解 VPN 的工作原理(因为您问了,但我不知道您的背景知识范围)。通俗地说:您可以将安装在设备上的 VPN 客户端视为虚拟网卡(与您使用操作系统的集成 VPN 客户端相同)。它从 VPN 服务器获取 IP,通常它将是与您尝试通过 VPN 连接的远程网络不同的子网,但这取决于设置。您的 VPN 服务器充当路由器来访问远程网络。因此,ARP 表并不那么重要,您应该查看路由表和接口配置。

如果没有有关 VPN 配置的任何进一步信息,我们很难帮助您。

  • 作为基本测试,您可以使用 traceroute 来检查到目标设备的路由是否通过 VPN。

  • 确保子网不重叠。如果您的本地网络与“VPN 网络”或远程网络具有相同的子网,则可能存在路由问题,从而导致您遇到的影响。以下是有关服务器故障主题的更多信息

下面是您的接口配置和路由表的示例(原则上):

192.168.100.1/24    your router's IP address
192.168.100.101/24  your client's IP address
192.168.200.201/24  your VPN-IP (the one your VPN client got assigned)  
172.29.29.2/32      the PPP connection's IP (used for the VPN)

这将是禁用 VPN 的表格:

Network Destination          Netmask        Gateway          Interface  Metric
            0.0.0.0          0.0.0.0  192.168.100.1    192.168.100.101      30
[snip]
      192.168.100.0    255.255.255.0        on-link    192.168.100.101       1
      192.168.100.1  255.255.255.255        on-link    192.168.100.101       1
    192.168.100.101  255.255.255.255        on-link    192.168.100.101       1
    192.168.100.255  255.255.255.255        on-link    192.168.100.101       1
[snip]

这将是启用 VPN 的表格(请注意额外的路由,它通过 VPN 路由您的非本地流量,它是首选路由,因为其度量较低且用于远程网络的三个额外路由):

Network Destination          Netmask        Gateway          Interface  Metric
            0.0.0.0          0.0.0.0  192.168.100.1    192.168.100.101      30
            0.0.0.0          0.0.0.0        on-link        172.29.29.2       1
[snip]
      192.168.100.0    255.255.255.0        on-link    192.168.100.101       1
      192.168.100.1  255.255.255.255        on-link    192.168.100.101       1
    192.168.100.101  255.255.255.255        on-link    192.168.100.101       1
    192.168.100.255  255.255.255.255        on-link    192.168.100.101       1
[snip]
      192.168.200.0    255.255.255.0        on-link    192.168.200.201       1
    192.168.200.201  255.255.255.255        on-link    192.168.200.201       1
    192.168.200.255  255.255.255.255        on-link    192.168.200.201       1
[snip]

在这个特定示例中,当前 LAN 之外的所有流量(在此示例中为外部192.168.100.0/24)都应通过 VPN 路由。如果您执行跟踪路由,假设 google.com,则流量应通过172.29.29.2(通过 VPN-PPP 连接)路由,如果没有 VPN,则应通过192.168.100.1(通过路由器)路由。所以

请注意,这是一个简化的示例,因此您了解原理。它基于没有拆分隧道的 IPsec/l2tp VPN 连接(这意味着所有非 LAN 流量都通过 VPN)。您的实际用例将更加复杂,并且根据 VPN 的配置方式而有所不同。

答案2

从答案中我了解到该问题是由 IP 冲突/使用相同子网引起的。

我找到了一个满足我当前需求的令人满意的解决方案问题@Albin 链接– 通过 VPN 的网络接口临时路由 IP:

sudo route add -host 192.168.1.25 -interface utun2

(我使用的是 macOS)

谢谢大家的关心。我很荣幸 :)

相关内容