openVPN:如何在客户端之间建立连接?

openVPN: how to establish a connection between clients?

这个周末我正在研究我的两个 raspberry Pi(B 和新型号 2)之间的 VPN 连接。我为此选择了 openVPN。都 运行 Raspbian 气喘吁吁。

所以我的设置如下:

|B|在家里连接到互联网(DSL,静态 IP)。 另一个 Pi |2|我随身带着它通过 UMTS 路由器连接到互联网。这出乎意料地好:) 在家里|B|我有一个服务器 运行 和 |2|登录没有任何问题。

我想问你们的问题是: 我如何从我的本地网络(与 PI |B| 相同)连接,比如从我的 iPhone 连接到 |2|哪个已经打开了到 |B| 的连接?

我这样配置我的服务器:

dev tun
proto udp
port 34345
ca /etc/openvpn/easy-rsa/keys/ca.crt
cert /etc/openvpn/easy-rsa/keys/server.crt
key /etc/openvpn/easy-rsa/keys/server.key
dh /etc/openvpn/easy-rsa/keys/dh1024.pem
user nobody
group nogroup
server 10.8.0.0 255.255.255.0
persist-key
persist-tun
status /var/log/openvpn-status.log
verb 3
client-to-client
push "redirect-gateway def1 bypass-dhcp"
#set the dns servers
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
log-append /var/log/openvpn
comp-lzo
duplicate-cn
keepalive 10 120

这是我的客户端配置:

dev tun
client
proto udp
remote {myIP} 34345 #same port as on the server
resolv-retry infinite
nobind
persist-key
persist-tun
ca /home/pi/vpn/ca.crt
cert /home/pi/vpn/raspi.crt
key /home/pi/vpn/raspi.key
comp-lzo
verb 3

正如我所说,连接运行良好,如果我在新树莓派上的控制台内发出 "curl www.echoip.net/plain",我会取回我的静态 IP 地址。所以我想总的来说它是有效的。

我已经尝试访问 10.8.0.* 但这没有用,我想不出为什么?

有什么想法吗?

提前致谢, 菲利克斯

再次编辑:

当 raspi 连接成功后,服务器日志显示以下内容:

Tue Mar  3 18:59:00 2015 2.240.44.246:26966 [raspi] Peer Connection Initiated with [AF_INET]2.240.44.246:26966
Tue Mar  3 18:59:00 2015 raspi/2.240.44.246:26966 MULTI_sva: pool returned IPv4=10.8.0.6, IPv6=e8b6:d1be:808e:f8b6:34bb:fdb6:4405:79b8
Tue Mar  3 18:59:00 2015 raspi/2.240.44.246:26966 MULTI: Learn: 10.8.0.6 -> raspi/2.240.44.246:26966
Tue Mar  3 18:59:00 2015 raspi/2.240.44.246:26966 MULTI: primary virtual IP for raspi/2.240.44.246:26966: 10.8.0.6
Tue Mar  3 18:59:02 2015 raspi/2.240.44.246:26966 PUSH: Received control message: 'PUSH_REQUEST'
Tue Mar  3 18:59:02 2015 raspi/2.240.44.246:26966 send_push_reply(): safe_cap=960
Tue Mar  3 18:59:02 2015 raspi/2.240.44.246:26966 SENT CONTROL [raspi]: 'PUSH_REPLY,redirect-gateway def1 bypass-dhcp,dhcp-option DNS 8.8.8.8,dhcp-option DNS 8.8.4.4,route 10.8.0.0 255.255.255.0,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5' (status=1)

客户端 RPi 2 上的输出 运行 如下所示(同样,在成功验证后):

Tue Mar  3 18:59:00 2015 [server] Peer Connection Initiated with [AF_INET]2.240.44.246:34345
Tue Mar  3 18:59:02 2015 SENT CONTROL [server]: 'PUSH_REQUEST' (status=1)
Tue Mar  3 18:59:02 2015 PUSH: Received control message: 'PUSH_REPLY,redirect-gateway def1 bypass-dhcp,dhcp-option DNS 8.8.8.8,dhcp-option DNS 8.8.4.4,route 10.8.0.0 255.255.255.0,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5'
Tue Mar  3 18:59:02 2015 OPTIONS IMPORT: timers and/or timeouts modified
Tue Mar  3 18:59:02 2015 OPTIONS IMPORT: --ifconfig/up options modified
Tue Mar  3 18:59:02 2015 OPTIONS IMPORT: route options modified
Tue Mar  3 18:59:02 2015 OPTIONS IMPORT: --ip-win32 and/or --dhcp-option options modified
Tue Mar  3 18:59:02 2015 ROUTE default_gateway=192.168.2.201
Tue Mar  3 18:59:02 2015 TUN/TAP device tun0 opened
Tue Mar  3 18:59:02 2015 TUN/TAP TX queue length set to 100
Tue Mar  3 18:59:02 2015 do_ifconfig, tt->ipv6=0, tt->did_ifconfig_ipv6_setup=0
Tue Mar  3 18:59:02 2015 /sbin/ifconfig tun0 10.8.0.6 pointopoint 10.8.0.5 mtu 1500
Tue Mar  3 18:59:02 2015 /sbin/route add -net 2.240.44.246 netmask 255.255.255.255 gw 192.168.2.201
Tue Mar  3 18:59:02 2015 /sbin/route add -net 0.0.0.0 netmask 128.0.0.0 gw 10.8.0.5
Tue Mar  3 18:59:02 2015 /sbin/route add -net 128.0.0.0 netmask 128.0.0.0 gw 10.8.0.5
Tue Mar  3 18:59:02 2015 /sbin/route add -net 10.8.0.0 netmask 255.255.255.0 gw 10.8.0.5
Tue Mar  3 18:59:02 2015 Initialization Sequence Completed

ifconfig returns 在服务器端除了 lo 和 eth0:

tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
          inet addr:10.8.0.1  P-t-P:10.8.0.2  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:1907 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1820 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:245870 (240.1 KiB)  TX bytes:1046186 (1021.6 KiB)

在客户端看起来像这样:

tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
          inet addr:10.8.0.10  P-t-P:10.8.0.9  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:1 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:76 (76.0 B)  TX bytes:380 (380.0 B)

这是结构图: http://i.stack.imgur.com/z9QUs.jpg

为了访问你的树莓派 |2|来自其他 VPN 客户端的客户端(在您的情况下是 iphone),您必须知道树莓派 |2| 的 IP 地址客户。在您当前的场景中,动态 IP 地址分配给 RPi |2|客户端每次与服务器建立新连接时。

为了解决这个问题,RPi必须使用静态IP地址|2|客户。可以找到设置特定客户端静态 IP 地址的过程 here.