Tinc P2P组网 组建大局域网教程

Tinc介绍

Tinc 是一个组建虚拟专用网络的工具,以 GNU 协议发布,通过隧道及加密技术在互联网上点与点之间创建专有网络。tinc 在网络层工作,因此无需对现有软件进行修改和配置。您可以使用 tinc 搭建专属的低延迟、高带宽、可扩展的 P2P 虚拟局域网。其数据通讯经过加密和压缩,能避免敏感数据和隐私的泄露。

选择的理由

  1. 开源,截止目前还在不断更新完善;
  2. 分布式网状路由,避免单点高负载和故障;
  3. 可运行多个实例来接入多个VPN;
  4. 通过虚拟网卡通讯,无需对现有应用软件进行修改和配置;
  5. 通讯支持 加密/认证/压缩,并支持参数选择;
  6. 支持常见的操作系统和网络拓扑,适用场景广泛;
  7. 支持P2P协议组网。

说明

本教程为实现Tinc 虚拟网内设备,能打洞成功的设备将实现直连,打洞失败的设备通过中转节点链接。

搭建教程

1)主节点

这里主节点选择的话必须是有公网IP的节点来进行搭建。

环境说明

系统:Ubuntu 20.04 虚拟内网IP:10.100.1.105 公网IP:1.1.1.1 虚拟网名称:hnvps 主节点名称: hnvps

1、安装Tinc

这里安装有两种方式

  • 一种是直接使用apt安装
1
apt install tinc
  • 本教程采用编译安装 这里安装的是最新的测试版本其他版本请到官网下载:https://tinc-vpn.org
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 安装依赖
apt install gcc cmake make zlib1g-dev liblzo2-dev openssl
curl https://cdn.nextrt.com/blog/tinc-1.1pre18.tar.gz -o /tmp/tinc.tar.gz
cd /tmp
tar -zxvf tinc.tar.gz
rm -rf tinc.tar.gz
mv tinc-1.1pre18 tinc
cd tinc
./configure
make
make install
# 建立配置文件储存目录
mkdir /usr/local/etc/tinc
mkdir -p /usr/local/var/run/
sudo ln -s /usr/local/etc/tinc /etc/tinc

2、配置TINC

准备事项

1、开启内核的 ip_foward 功能,如果没有开启请手动开启转发.

1
sysctl -agrep ip_forward # 查看 ip_forward 转发是否开启 [ net.ipv4.ip_forward=1 ]

开启方法

1
sudo echo 1 > /proc/sys/net/ipv4/ip_forward

2、启用 tun 设备

1
sudo modprobe tun

检查确认是否启用成功

1
ls /dev/net/tun
开始

这里假设我们要建立的虚拟网名称为hnvps 以下为配置完成的配置文件目录结构

1
2
3
4
5
6
7
8
9
/etc/tinc
└── hnvps
├── hosts
│ ├── hnvps
│ └── openwrt
├── rsa_key.priv
├── tinc.conf
├── tinc-down
└── tinc-up

文件说明

  • tinc.conf 为 tincnet 的配置文件,
  • tinc-up 为启动该网络时自动执行的脚本
  • tinc-down 为关闭该网络时自动执行的脚本
  • hosts 文件夹保存着各个节点(路由器)的信息。
1
2
3
# 建立配置文件储存目录
mkdir /etc/tinc/hnvps
mkdir /etc/tinc/hnvps/hosts

image-1647766497412

image-1647766497412

设置配置文件/etc/tinc/hnvps/tinc.conf

1
2
3
4
5
6
7
8
Name = hnvps
Interface = tinc
Autoconnect = yes
Compression=9
Cipher = aes-256-cbc
Digest = sha256
#绑定的端口
BindToAddress = * 10010

编写启动虚拟交换器脚本/etc/tinc/hnvps/tinc-up

1
2
3
4
#!/bin/sh
ip link set $INTERFACE up
ip addr add 10.100.1.105/24 dev $INTERFACE
ip route add 10.100.1.0/24 dev $INTERFACE

编写错误关闭的脚本/etc/tinc/hnvps/tinc-down

1
2
3
4
#!/bin/sh
ip route del 10.100.1.0/24 dev $INTERFACE
ip addr del 10.100.1.105/24 dev $INTERFACE
ip link set $INTERFACE down

赋予脚本权限:

1
chmod +x /etc/tinc/hnvps/tinc-*

编写主节点的详细信息/etc/tinc/hnvps/hosts/hnvps

1
2
3
4
5
# 主节点公网IP地址
Address = 1.1.1.1
# 主节点内网地址
Subnet = 10.100.1.105/32
Port = 10010

生成密钥信息

1
cd /etc/tinc/hnvps/hosts

执行下面命令后需要输入两次回车确认

1
tinc -n hnvps generate-rsa-keys

执行下面命令后需要输入两次回车确认

1
tinc -n hnvps generate-ed25519-keys

放行防火墙

1
ufw allow 10010

创建服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
echo '
# This is a mostly empty service, but allows commands like stop, start, reload
# to propagate to all tinc@ service instances.

[Unit]
Description=Tinc VPN
Documentation=info:tinc
Documentation=man:tinc(8) man:tinc.conf(5)
Documentation=http://tinc-vpn.org/docs/
After=network.target
Wants=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecReload=/bin/true
WorkingDirectory=/usr/local/etc/tinc

[Install]
WantedBy=multi-user.target' > /lib/systemd/system/tinc.service

echo '
[Unit]
Description=Tinc net %i
Documentation=info:tinc
Documentation=man:tinc(8) man:tinc.conf(5)
Documentation=http://tinc-vpn.org/docs/
PartOf=tinc.service
ReloadPropagatedFrom=tinc.service

[Service]
Type=simple
WorkingDirectory=/usr/local/etc/tinc/%i
ExecStart=/usr/local/sbin/tincd -n %i -D
ExecReload=/usr/local/sbin/tincd -n %i reload
KillMode=mixed
Restart=on-failure
RestartSec=5
TimeoutStopSec=5

[Install]
WantedBy=tinc.service' > /lib/systemd/system/tinc@.service
# 刷新服务
sudo systemctl unmask tinc

2) 子节点安装

环境:Openwrt 21.02 网络:10.0.0.0/24 内网IP:10.0.0.1 虚拟网络IP: 10.100.1.1 虚拟网络名:openwrt 由于部分固件并没有Tinc 需要自己手工安装

  • 方法一、
1
2
opkg update
opkg install tinc

安装过程与主节点基本一致 开始(已安装tinc): 1、建立配置文件夹

1
mkdir -p /etc/tinc/openwrt/hosts/

1、配置/etc/tinc/openwrt/tinc.conf

1
2
3
4
5
6
7
8
9
Name = openwrt
Interface = tinc
#Mode = switch
Autoconnect = yes
Compression=9
Cipher = aes-256-cbc
Digest = sha256
BindToAddress = * 10010
Device = /dev/net/tun

2、配置/etc/tinc/openwrt/tinc-up: 这里有区别的是,我想让虚拟网内机器能访问到Openwrt 路由网段10.0.0.0/24内网资源,所以这里

1
2
3
4
5
6
7
#!/bin/sh
ip link set $INTERFACE up
ip addr add 10.100.1.1/32 dev $INTERFACE
ip route add 10.100.1.0/24 dev $INTERFACE
iptables -A FORWARD -o "${INTERFACE}" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i "${INTERFACE}" -j ACCEPT
iptables -t nat -A POSTROUTING -s "10.100.1.0"/"255.255.255.0" ! -o "${INTERFACE}" -j MASQUERADE

3、配置/etc/tinc/openwrt/tinc-down

1
2
3
4
5
6
7
#!/bin/sh
ip route del 10.100.1.0/24 dev $INTERFACE
ip addr del 10.100.1.1/32 dev $INTERFACE
iptables -D FORWARD -o "${INTERFACE}" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i "${INTERFACE}" -j ACCEPT
iptables -t nat -D POSTROUTING -s "10.100.1.0"/"255.255.255.0" ! -o "${INTERFACE}" -j MASQUERADE
ip link set $INTERFACE down

4、配置节点信息/etc/tinc/openwrt/hosts/openwrt:

1
2
3
4
5
# 分配给OPENWRT的虚拟LANIP
Subnet=10.100.1.1/32
# 路由器所在的网段
Subnet=10.0.0.0/24
Port = 10010

5、赋予脚本权限:

1
chmod +x /etc/tinc/openwrt/tinc-*

6、接下来创建密钥信息,在询问保存位置时直接回车使用默认位置即可:

1
2
tinc -n openwrt generate-rsa-keys
tinc -n openwrt generate-ed25519-keys

7、复制主节点中的/etc/tinc/hnvps/hosts/hnvps到openwrt 路由器节点的/etc/tinc/openwrt/hosts目录下。复制路由器的/etc/tinc/openwrt/hosts/openwrt到主节点的/etc/tinc/hnvps/hosts/目录下 此时路由器openwrt 的 /etc/tinc/openwrt/ 目录下的情况为:

1
2
3
4
5
6
7
8
├── ed25519_key.priv
├── hosts/
│ └── openwrt
│ └── hnvps
├── rsa_key.priv
├── tinc-down
├── tinc-up
└── tinc.conf

最后需要编辑 /etc/config/tinc 文件:

1
2
3
4
5
6
7
config tinc-net openwrt
option enabled 1
option Name openwrt

config tinc-host openwrt
option enabled 1
option net openwrt

启动服务

1
/etc/init.d/tinc start