Tailscale 实现 NAT to NAT(内网穿透) / Full Mesh 网络

Tailscale

Tailscale · Best VPN Service for Secure Networks

Tailscale是基于开源的WireGuard协议实现加密的点对点连接,Tailscale通信不需要走中继服务器,直接通过内网打洞的方法进行连接。

WireGuard中并没有服务器/客户端的区分,每个客户端都是一个Peer,Peer之间会首先尝试通过NAT穿透建立点对点连接。如果失败(比如设备位于无状态防火墙和Hard NAT背后)则会通过Tailscale提供的Relay进行中继(也可自行建立Relay Node来加速网络)。

Tailscale是商业软件,免费版最多支持20个设备和一个子网路由。类似的项目还有之前介绍过的 Netmaker 以及 Zerotier,但经过一段时间的时候最终还是觉得Tailscale更加符合我的需求。

Features

内网穿透

Tailscale下的每个Peer之间,首先会尝试通过UDP打洞建立连接,如果失败(设备位于无状态防火墙和Hard NAT背后),才使用Relay中继。

安全访问

基于Wireguard加密VPN实现,保证数据安全。

DNS

Tailscale网络中内置了DNS服务器,开启后默认使用设备名作为DNS Name。

Subnet Routes

Tailscale可以通过本地的子网段给内网内的其他机器,从而实现跨内网访问。

Exit Node

网络中的任意节点(iOS设备除外)都可以被配置为出口节点(可以理解为传统意义上的VPN网络)。

建立自定义 DERP Node

官方文档提供了很详细的说明:Custom DERP Servers · Tailscale

简述一下我的建立过程。

网络拓扑

目前我仅有三台设备连入了Tailscale网络

  • surfacebook2:日常办公设备,位于nat后,常使用移动4G热点上网
  • pc:另一台高性能主机,位于nat后
  • derp-hk01:腾讯云香港VPS,使用其建立DERP Server

如果主要使用的是国内网络,建议弄一台国内的机器作为Relay节点,但由于备案等等原因十分麻烦,我就用腾讯云HK凑合一下,线路也还可以,本地移动测试下来双程路由都走 AS9808-AS58453出国(CMI)。

搭建Derp Node

解析域名

Derper需要使用该域名申请SSL证书,默认占用80/443端口,因为如果有跑web的需要自行反代。

安装Golang环境

下载安装:

1
2
wget https://go.dev/dl/go1.19.1.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.19.1.linux-amd64.tar.gz

编辑 /etc/profile ,追加如下内容:

1
2
3
4
5
export GOROOT=/usr/local/go
export GOPATH=/usr/local/gopath
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin

安装Derper

1
go install tailscale.com/cmd/derper@main

默认会安装在$GOPATH/bin

编写 derper.service

编写一个服务方便管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cat /etc/systemd/system/derper.service 
[Unit]
Description=derper service
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/usr/local/gopath/bin/derper -hostname=<your_domain_name> --verify-clients -a :443 -stun

[Install]
WantedBy=multi-user.target

**请注意我这里添加了 --verify-clients **

默认情况下,任何知道该Derp Node 地址的人都可以使用该节点。

To allow only your tailnet traffic through your DERP node, run tailscaled on the same machine as your DERP node, and start derper with the --verify-clients flag:

1
sudo derper --hostname=your-hostname.com --verify-clients

若需要开启 –verify-clients,需提前将该节点也加入Tailscale网络。

启用derper

1
2
systemctl start derper
systemctl enable derper #开机启动

配置Tailscale ACLs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
// ... other parts of ACL
"derpMap": {
"OmitDefaultRegions": true,
"Regions": {
"900": {
"RegionID": 900,
"RegionCode": "myderp",
"Nodes": [
{
"Name": "1",
"RegionID": 900,
"HostName": "your-hostname.com"
}
]
}
}
}
}

Required ports

Each DERP node runs an HTTPS server and a STUN server. The ports for those two services need to be open for traffic from the internet so users in your tailnet can access them from home, coffee shops, etc.

By default, those services run on port TCP 443 (https) and UDP 3478 (STUN). To use other port numbers, set DERPNode.DERPPort or DERPNode.STUNPort, respectively.

Monitoring custom DERPs

You can use the cmd/derpprobe binary to monitor your custom DERPs and to verify they are working correctly. You’ll need to specify a --derp-map=file:// URL that is a JSON document with your DERP map to monitor.

查看生效

回到Machine详细信息页面,看到 自定义的Relay节点即生效

使用体验

打洞成功率

  • 移动4G-移动NAT网络:无法直接连接,使用Relay节点
  • 移动NAT网络1-同时移动NAT网络2:成功穿透,直连

由于我的设备较少,这部分参考价值较低。

网络速率

有待补充。


Tailscale 实现 NAT to NAT(内网穿透) / Full Mesh 网络
http://example.com/2022/09/21/Tailscale-实现-NAT-to-NAT(内网穿透)-Full-Mesh-网络/
作者
Rae
发布于
2022年9月21日
许可协议