工作中管理多地分公司办事处网络访问,经常遇到一个问题:

  • 想要在分公司之间组建一个局域网,专线费用太高;
  • 使用传统的VPN技术,所有的流量转发压力全集中在一台服务器不说,如果VPN服务器崩溃了,整个局域网都会崩溃;
  • 使用第三方VPN服务,机密数据流量安全问题无法保障;
  • 墙,墙,墙,部分VPN过墙就被拦截,根本无法进行透传;

随着P2P网络模式的流行,与之而来衍生出了一种新的概念:P2P VPN。

1. 什么是P2P VPN

P2P VPN 是一个 VPN 模型,它能解决传统的基于服务器-客户端模型的 VPN 存在的这些问题。一个 P2P VPN 中不再有一个中心的 VPN 服务器,任何拥有一个公开 IP 地址的节点都能引导其他节点进入 VPN。当连接到一个 VPN 之后,每一个节点都能与 VPN 中的任何其他节点直接通信,而不需要经过一个中间的服务器节点。当然任何节点出现故障时,VPN 中的剩余节点不会受到影响。节点中的延迟、带宽以及 VPN 扩展性在这样的设定中都有自然的提升。

一个传统的 VPN(如 OpenVPN、PPTP)由一个 VPN 服务器和一个或多个连接到这台服务器的客户端组成。当任意两个 VPN 客户端彼此通信时,VPN 服务器需要中继它们之间的 VPN 数据流量。这样一个中心辐射型的 VPN 拓扑结构存在的问题是,当连接的客户端增多以后,VPN 服务器很容易成为一个性能上的瓶颈。从某种意义上来说,中心化的 VPN 服务器也同样成为一个单点故障的来源,也就是当 VPN 服务器出现故障的时候,整个 VPN 都将无法被任何 VPN 客户端访问。

开源的 P2P VPN 实现已经有几个了,比如 Tinc、peerVPN,以及 n2n。

2. 部署教程

我们将使用 N2N来完成本篇教程。N2N目前有多个版本,v1版本、v2版本v2s版本。

v1版本

此版本为官方N2N项目v1版本的修改版,最后一次commit距今已有3年时间,已被淘汰

项目:https://github.com/meyerd/n2n/tree/master/n2n_v1

v2版本

目前最常用的版本,也是官方N2N项目重启后,在各路大神的贡献下,一直在维护并不断完善的版本

项目:https://github.com/ntop/n2n

v2s版本

此版本可以为官方N2N项目v2版本(v2项目重启前)的修改版,最后一次commit距今已有2年时间,极小部分用户仍在使用

项目:https://github.com/meyerd/n2n/tree/master/n2n_v2

本次使用v2 dev 分支版本搭建(注:git最新dev分支兼容性较差,不推荐使用旧版edge(客户端)连接新版supernode(服务端)

这里在centos7.X下编译N2N为例,该版本支持自动分配IP

 

yum install autoconf automake libtool git -y
git clone https://github.com/ntop/n2n.git -b dev
cd n2n/
./autogen.sh
./configure
make && make install

启动supernode服务器(最好有公网IP,没有公网IP也可以通过防火墙NAT端口),只有一行命令

supernode -l xxx #XXX为端口号

参数说明
-p <port>         | Set UDP main listen port to <port>
-c <path>         | File containing the allowed communities.
-l <sn host:port> | Name/IP of a known supernode:port.
-F <fed_name>     | Name of the supernodes federation (otherwise use '*Federation' by default)
-u <UID>          | User ID (numeric) to use when privileges are dropped.
-g <GID>          | Group ID (numeric) to use when privileges are dropped.
-t <port>         | Management UDP Port (for multiple supernodes on a machine).
-a <net-net/bit>  | Subnet range for auto ip address service, e.g.
                  | -a 192.168.0.0-192.168.255.0/24, defaults to 10.128.255.0-10.255.255.0/24
-v                | Increase verbosity. Can be used multiple times.
-h                | This help message.

服务端如开启iptables\firewalld等防火墙,需要放行supernode所设置的端口,否则客户端无法连接

#firewalld
firewall-cmd --zone=public --add-port=XXX/udp --permanent
firewall-cmd --reload
#iptables
iptables -I INPUT -p udp --dport XXX -j ACCEPT
service iptables save
service iptables restart

客户端

安装步骤与supernode服务器相同

配置内网节点

1.内网启动edge,可以手动启动或者加入到开机脚本。
2.内网节点开启路由转发
windows系统:将内网网卡开启共享,共享目标为虚拟的TAP网卡就可以开启路由。
Linux系统:开启防火墙的端口转发功能。

edge -d edge0 -a 本节点IP -s 子网掩码 -m MAC地址(最好指定) -c 分组名 -k 密码 -l supernode地址:端口  -b -r -E -f

例  edge -d edge0 -a 10.206.108.200 -s 255.255.255.0 -c n2n -k pass -l xxx.xxx.xxx.xxx:3389 -b -r -E -f -m xx:xx:xx:xx:xx:xx

参数说明

-d <tap device>          | tap device name
-a [mode:]<address>[/nn] | Interface address and optional subnet (cidr, default /24). For DHCP use '-r -a dhcp:0.0.0.0'
-c <community>           | n2n community name the edge belongs to.
-k <encrypt key>         | Encryption key (ASCII) - also N2N_KEY=<encrypt key>.
-l <supernode host:port> | Supernode IP:port
-i <reg_interval>        | Registration interval, for NAT hole punching (default 20 seconds)
-I <device description>  | Annotate the edge's description (hint), identified in the manage port
-L <reg_ttl>             | TTL for registration packet when UDP NAT hole punching through supernode (default 0 for not set )
-p <local port>          | Fixed local UDP port.
-u <UID>                 | User ID (numeric) to use when privileges are dropped.
-g <GID>                 | Group ID (numeric) to use when privileges are dropped.
-f                       | Do not fork and run as a daemon; rather run in foreground.
-m <MAC address>         | Fix MAC address for the TAP interface (otherwise it may be random)
                         | eg. -m 01:02:03:04:05:06
-M <mtu>                 | Specify n2n MTU of edge interface (default 1290).
-D                       | Enable PMTU discovery. PMTU discovery can reduce fragmentation but
                         | causes connections stall when not properly supported.
-r                       | Enable packet forwarding through n2n community.
-A1                      | Disable payload encryption. Do not use with key (defaulting to AES then).
-A2 ... -A5 or -A        | Choose a cipher for payload encryption, requires a key: -A2 = Twofish,
                         | -A3 or -A (deprecated) = AES (default), -A4 = ChaCha20, -A5 = Speck-CTR.
-H                       | Enable full header encryption. Requires supernode with fixed community.
-z1 ... -z2 or -z        | Enable compression for outgoing data packets: -z1 or -z = lzo1x (default=disabled).
-E                       | Accept multicast MAC addresses (default=drop).
-S                       | Do not connect P2P. Always use the supernode.
-T <tos>                 | TOS for packets (e.g. 0x48 for SSH like priority)
-n <cidr:gateway>        | Route an IPv4 network via the gw. Use 0.0.0.0/0 for the default gw. Can be set multiple times.
-v                       | Make more verbose. Repeat as required.
-R <rule_str>            | Drop or accept packets by rules. Can be set multiple times.
                         | Rule format: src_ip/len:[b_port,e_port],dst_ip/len:[s_port,e_port],TCP+/-,UDP+/-,ICMP+/-
-t <port>                | Management UDP Port (for multiple edges on a machine).
先写这么多,备忘

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐