1拿到源码。
https://github.com/ntop/n2n/releases/tag/2.8
n2n is a light VPN software which makes it easy to create virtual networks bypassing intermediate firewalls.
In order to start using n2n, two elements are required:
- A supernode: it allows edge nodes to announce and discover other nodes. It must have a port publicly accessible on internet.
- edge nodes: the nodes which will be a part of the virtual networks
A virtual network shared between multiple edge nodes in n2n is called a community. A single supernode can relay multiple communities and a single computer can be part of multiple communities at the same time. An encryption key can be used by the edge nodes to encrypt the packets within their community.
n2n tries to establish a direct peer-to-peer connection via udp between the edge nodes when possible. When this is not possible (usually due to special NAT devices), the supernode is also used to relay the packets.
2编译、安装(含edge和supernode)
2.1autogen.sh
[root@VM-8-17-centos n2n-2.8]# ./autogen.sh
Wait please...
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for ZSTD_compress in -lzstd... no
Building n2n without ZSTD support
checking for AES_cbc_encrypt in -lcrypto... yes
checking for EVP_CIPHER_CTX_reset in -lcrypto... no
checking for pcap_open_live in -lpcap... no
checking for pcap_set_immediate_mode in -lpcap... no
checking for cap_get_proc in -lcap... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating tools/Makefile
config.status: creating include/config.h
2.2make
[root@VM-8-17-centos n2n-2.8]# make
gcc -g -O2 -I ./include -c -o src/transform_aes.o src/transform_aes.c
gcc -g -O2 -I ./include -c -o src/tuntap_osx.o src/tuntap_osx.c
gcc -g -O2 -I ./include -c -o src/random_numbers.o src/random_numbers.c
gcc -g -O2 -I ./include -c -o src/speck.o src/speck.c
gcc -g -O2 -I ./include -c -o src/minilzo.o src/minilzo.c
gcc -g -O2 -I ./include -c -o src/example_edge_embed.o src/example_edge_embed.c
gcc -g -O2 -I ./include -c -o src/sn_utils.o src/sn_utils.c
gcc -g -O2 -I ./include -c -o src/twofish.o src/twofish.c
gcc -g -O2 -I ./include -c -o src/sn.o src/sn.c
gcc -g -O2 -I ./include -c -o src/edge_utils_win32.o src/edge_utils_win32.c
gcc -g -O2 -I ./include -c -o src/example_sn_embed.o src/example_sn_embed.c
gcc -g -O2 -I ./include -c -o src/transform_tf.o src/transform_tf.c
gcc -g -O2 -I ./include -c -o src/transform_cc20.o src/transform_cc20.c
gcc -g -O2 -I ./include -c -o src/tuntap_linux.o src/tuntap_linux.c
gcc -g -O2 -I ./include -c -o src/wire.o src/wire.c
gcc -g -O2 -I ./include -c -o src/transform_null.o src/transform_null.c
gcc -g -O2 -I ./include -c -o src/pearson.o src/pearson.c
gcc -g -O2 -I ./include -c -o src/example_edge_embed_quick_edge_init.o src/example_edge_embed_quick_edge_init.c
gcc -g -O2 -I ./include -c -o src/tuntap_netbsd.o src/tuntap_netbsd.c
gcc -g -O2 -I ./include -c -o src/header_encryption.o src/header_encryption.c
gcc -g -O2 -I ./include -c -o src/edge_utils.o src/edge_utils.c
gcc -g -O2 -I ./include -c -o src/n2n.o src/n2n.c
gcc -g -O2 -I ./include -c -o src/tuntap_freebsd.o src/tuntap_freebsd.c
gcc -g -O2 -I ./include -c -o src/transform_speck.o src/transform_speck.c
gcc -g -O2 -I ./include -c -o src/edge.o src/edge.c
ar rcs libn2n.a src/transform_aes.o src/tuntap_osx.o src/random_numbers.o src/speck.o src/minilzo.o src/example_edge_embed.o src/sn_utils.o src/twofish.o src/sn.o src/edge_utils_win32.o src/example_sn_embed.o src/transform_tf.o src/transform_cc20.o src/tuntap_linux.o src/wire.o src/transform_null.o src/pearson.o src/example_edge_embed_quick_edge_init.o src/tuntap_netbsd.o src/header_encryption.o src/edge_utils.o src/n2n.o src/tuntap_freebsd.o src/transform_speck.o src/edge.o
gcc -g -O2 -I ./include src/edge.c libn2n.a -lcrypto -o edge
gcc -g -O2 -I ./include src/sn.c libn2n.a -o supernode
gcc -g -O2 -I ./include src/example_edge_embed_quick_edge_init.c libn2n.a -lcrypto -o example_edge_embed_quick_edge_init
gcc -g -O2 -I ./include src/example_edge_embed.c libn2n.a -lcrypto -o example_edge_embed
gcc -g -O2 -I ./include src/example_sn_embed.c libn2n.a -lcrypto -o example_sn_embed
gzip -c edge.8 > edge.8.gz
gzip -c supernode.1 > supernode.1.gz
gzip -c n2n.7 > n2n.7.gz
make -C tools
make[1]: Entering directory `/n2n-2.8/n2n-2.8/tools'
cc -I../include -g -O2 -g3 -O2 -Wall benchmark.c ../libn2n.a -lcrypto -o n2n-benchmark
make[1]: Leaving directory `/n2n-2.8/n2n-2.8/tools'
2.3安装到默认目录
[root@VM-8-17-centos n2n-2.8]# make install
echo "MANDIR=/usr/share/man"
MANDIR=/usr/share/man
mkdir -p /usr/sbin /usr/share/man/man1 /usr/share/man/man7 /usr/share/man/man8
install -m755 supernode /usr/sbin/
install -m755 edge /usr/sbin/
install -m644 edge.8.gz /usr/share/man/man8/
install -m644 supernode.1.gz /usr/share/man/man1/
install -m644 n2n.7.gz /usr/share/man/man7/
make -C tools install
make[1]: Entering directory `/n2n-2.8/n2n-2.8/tools'
install -m755 n2n-benchmark /usr/sbin/
make[1]: Leaving directory `/n2n-2.8/n2n-2.8/tools'
[root@VM-8-17-centos n2n-2.8]# which edge
/usr/sbin/edge
[root@VM-8-17-centos n2n-2.8]# which supernode
/usr/sbin/supernode
[root@VM-8-17-centos n2n-2.8]#
3启动程序
3.1supernode(Linux)
3.1.1 准备好配置文件supernode.conf和supernodes.list
参考下面的例子
[root@VM-8-17-centos n2n-2.8]# cat /etc/n2n/supernode.conf
#
# The configuration file is similar to the command line, with one option per line. An equal
# sign '=' should be used between key and value. Example: -l=7777 or --local-port=7777
# This file contains a basic configuration example, please refer to the help (-h) for the full
# list of available options.
#
# -l|--local-port
# Sets the UDP listening port.
#
-l=7777
#
# Specify in supernodes.list the list of allowed communities
#
-c=/etc/n2n/supernodes.list
[root@VM-8-17-centos n2n-2.8]# cat /etc/n2n/supernodes.list
community@database
[root@VM-8-17-centos n2n-2.8]#
3.1.2 准备服务文件supernode.service
启动命令/usr/sbin/supernode /etc/n2n/supernode.conf
[root@VM-8-17-centos n2n-2.8]# vim /etc/systemd/system/supernode.service
[root@VM-8-17-centos n2n-2.8]# cat /etc/systemd/system/supernode.service
[Unit]
Description=n2n supernode process
After=network-online.target syslog.target nfw.target
Wants=network-online.target
[Service]
Type=simple
ExecStartPre=
ExecStart=/usr/sbin/supernode /etc/n2n/supernode.conf
Restart=on-abnormal
RestartSec=5
[Install]
WantedBy=multi-user.target
Alias=
[root@VM-8-17-centos n2n-2.8]#
3.1.3 使用systemctl启动和查看服务
[root@VM-8-17-centos n2n-2.8]# systemctl daemon-reload
[root@VM-8-17-centos n2n-2.8]# systemctl enable supernode
Created symlink from /etc/systemd/system/multi-user.target.wants/supernode.service to /etc/systemd/system/supernode.service.
[root@VM-8-17-centos n2n-2.8]# vim /etc/systemd/system/supernode.service
[root@VM-8-17-centos n2n-2.8]# systemctl start supernode
[root@VM-8-17-centos n2n-2.8]# systemctl status supernode
● supernode.service - n2n supernode process
Loaded: loaded (/etc/systemd/system/supernode.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2024-12-21 10:04:05 CST; 9s ago
Main PID: 2024 (supernode)
CGroup: /system.slice/supernode.service
└─2024 /usr/sbin/supernode /etc/n2n/supernode.conf
Dec 21 10:04:05 VM-8-17-centos systemd[1]: Started n2n supernode process.
Dec 21 10:04:05 VM-8-17-centos supernode[2024]: 21/Dec/2024 10:04:05 [sn.c:81] Loaded 1 communities from /etc/n2n/....list
Dec 21 10:04:05 VM-8-17-centos supernode[2024]: 21/Dec/2024 10:04:05 [sn.c:396] supernode is listening on UDP 7777 (main)
Dec 21 10:04:05 VM-8-17-centos supernode[2024]: 21/Dec/2024 10:04:05 [sn.c:404] supernode is listening on UDP 5645...ment)
Dec 21 10:04:05 VM-8-17-centos supernode[2024]: 21/Dec/2024 10:04:05 [sn.c:412] Dropping privileges to uid=99, gid=99
Dec 21 10:04:05 VM-8-17-centos supernode[2024]: 21/Dec/2024 10:04:05 [sn.c:427] supernode started
Hint: Some lines were ellipsized, use -l to show in full.
[root@VM-8-17-centos n2n-2.8]#
3.2edge(Linux)
3.2.1 准备配置文件/etc/n2n/edge.conf
[root@VM-8-17-centos n2n-2.8]# pwd
/n2n-2.8/n2n-2.8
[root@VM-8-17-centos n2n-2.8]# find . -name *.conf*
./packages/etc/n2n/edge.conf.sample
./packages/etc/n2n/supernode.conf.sample
[root@VM-8-17-centos n2n-2.8]# mkdir /etc/n2n/
[root@VM-8-17-centos n2n-2.8]# cp ./packages/etc/n2n/edge.conf.sample /etc/n2n/edge.conf
如下例子,
[root@VM-8-17-centos n2n-2.8]# cat /etc/n2n/edge.conf
#
# The configuration file is similar to the command line, with one option per line. An equal
# sign '=' should be used between key and value. Example: -c=mynetwork or --community=mynetwork
# This file contains a basic configuration example, please refer to the help (-h) for the full
# list of available options.
#
# -d|--tun-device
# Specifies the name of the TUN interface.
#
-d=edge_twofish
#
# -c|--community
# Specifies the n2n community name the edge belongs to.
#
-c=community@database
#
# -k
# Sets the encryption key (ASCII). The environment variable N2N_KEY=<key> can also be used.
#
-k=@database.com
#
# -m
# Specified the MAC address for the TAP interface (random otherwise).
#
# -m=DE:AD:BE:EF:99:99
#
# -a
# Sets the interface address. For DHCP use '-r -a dhcp:0.0.0.0'.
#
-a=192.168.88.100
#subnet
-s=255.255.255.0
#
# -p
# Sets the local UDP port to a fixed port.
#
-p=50001
#
# -l|--supernode-list
# Specifies the supernode IP and port.
#
-l=xxx.xxx.xxx.xxx:7777
#
3.2.2 准备服务文件edge.service
vim /etc/systemd/system/edge.service,按照如下的内容填写。
[root@centos7 n2n-2.8]# cat /etc/systemd/system/edge.service
[Unit]
Description=n2n edge process
After=network-online.target syslog.target nfw.target
Wants=network-online.target
[Service]
Type=simple
ExecStartPre=
ExecStart=/usr/sbin/edge /etc/n2n/edge.conf -f
Restart=on-abnormal
RestartSec=5
[Install]
WantedBy=multi-user.target
Alias=
[root@centos7 n2n-2.8]#
3.2.3 使用systemctl启动和查看服务
[root@VM-8-17-centos n2n-2.8]# systemctl daemon-reload
[root@VM-8-17-centos n2n-2.8]# systemctl enable edge
Created symlink from /etc/systemd/system/multi-user.target.wants/edge.service to /etc/systemd/system/edge.service.
[root@VM-8-17-centos n2n-2.8]# vim /etc/systemd/system/edge.service
[root@VM-8-17-centos n2n-2.8]# systemctl start edge
[root@VM-8-17-centos n2n-2.8]# systemctl status edge
● edge.service - n2n edge process
Loaded: loaded (/etc/systemd/system/edge.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2024-12-19 16:06:53 CST; 11s ago
Main PID: 6394 (edge)
CGroup: /system.slice/edge.service
└─6394 /usr/sbin/edge /etc/n2n/edge.conf -f
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge.c:852] Starting n2n edge 2.8.0 Dec 19 2024 14:48:39
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge.c:858] Using compression: none.
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge.c:859] Using twofish cipher.
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge.c:869] ip_mode='static'
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge_utils.c:211] supernode 0 => 101.43.136.89:7777
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge_utils.c:2088] Binding to local port 50001
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge.c:918] WARNING: n2n has not been compiled wi...fail.
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge.c:923] Dropping privileges to uid=99, gid=99
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge.c:947] edge started
Dec 19 16:06:53 VM-8-17-centos edge[6394]: 19/Dec/2024 16:06:53 [edge_utils.c:727] Successfully joined multicast g...:1968
Hint: Some lines were ellipsized, use -l to show in full.
3.3 edge(Windows),待完善。
3.3.1 安装tap-windows驱动
去网上找一个tap-windows-9.21.2.exe,安装之后生成一个以太网2的网络连接,
修改其名字为edge_twofish,如下图所示。
3.3.2 配置edge.conf
windows版本的edge.conf与Linux平台的edge.conf内容一样,但是相对位置只在与edge同级目录。
#
# The configuration file is similar to the command line, with one option per line. An equal
# sign '=' should be used between key and value. Example: -c=mynetwork or --community=mynetwork
# This file contains a basic configuration example, please refer to the help (-h) for the full
# list of available options.
#
# -d|--tun-device
# Specifies the name of the TUN interface.
#
-d=edge_twofish
#
# -c|--community
# Specifies the n2n community name the edge belongs to.
#
-c=community@database
#
# -k
# Sets the encryption key (ASCII). The environment variable N2N_KEY=<key> can also be used.
#
-k=@database.com
#
# -m
# Specified the MAC address for the TAP interface (random otherwise).
#
# -m=DE:AD:BE:EF:99:99
#
# -a
# Sets the interface address. For DHCP use '-r -a dhcp:0.0.0.0'.
#
-a=192.168.88.20
# -s subnet mask
-s=255.255.255.0
#
# -p
# Sets the local UDP port to a fixed port.
#
-p=50001
#
# -l|--supernode-list
# Specifies the supernode IP and port.
-l=xxx.xxx.xxx.xxx:7777
#
3.3.3 运行edge
右键单击edge,使用管理员权限启动edge。