---
title: Tailscale 架构
url: https://doc.liz6.com/networking/08-vpn-and-tunnels/02-tailscale-architecture
locale: zh
area: networking
tags:
- networking
- VPN与隧道
date: 2026-06-30
modified: 2026-06-30
description: 在 WireGuard 之上构建零配置 mesh VPN——coordination server 交换公钥和 ACL,NAT 穿透建立直接隧道,不可穿透时自动 fallback 到 DERP 中继。控制平面集中但数据平面去中心化:流量不经过中心服务器。
---

# Tailscale 架构

> 在 WireGuard 之上构建零配置 mesh VPN——coordination server 交换公钥和 ACL,NAT 穿透建立直接隧道,不可穿透时自动 fallback 到 DERP 中继。控制平面集中但数据平面去中心化:流量不经过中心服务器。

## 概述

Tailscale（2019）在 WireGuard 之上构建了零配置 mesh VPN：coordination server 交换公钥和 ACL，节点之间通过 NAT 穿透（STUN/UPnP/端口预测）建立直接 WireGuard 隧道；不可穿透时自动 fallback 到 DERP 中继。核心创新是将 WireGuard 的静态配置变为自动管理的 mesh overlay，同时保持数据平面不经过中心服务器。开源替代 headscale 允许完全自建。

## 控制平面与数据平面分离

Tailscale 不是 peer-to-peer — 它是 mesh overlay with centralized coordination:

```
控制平面 (Coordination Server):
  - HTTPS (REST + long-poll): 交换公钥, endpoint, ACL
  - 不传输用户数据
  - 开源替代: headscale

数据平面:
  - WireGuard 隧道 (user-space: wireguard-go or kernel: wireguard.ko)
  - 对端之间: 直接 UDP (如果 NAT traversal 成功) or via DERP relay
```

coordination server 提供的是 peer list + public keys + endpoint hints + ACL rules。建立连接后，节点之间用 WireGuard 直接通信。

## DERP

DERP (Detoured Encrypted Routing Protocol) 是 Tailscale 的中继网络，当直接连接失败时使用:

```
[Node A] → TLS → DERP Server → TLS → [Node B]

DERP 协议:
  1. Node → DERP: HTTPS(TLS) connect to :443 or custom port
     → 验证: node 用自己的 tailscale private key 签名 — DERP 不用登录 tailnet
  2. DERP → Node: server info (region, version)
  3. 后续: 
     - DERP 转发 WireGuard 加密的包 (不能解密)
     - 对端发现 (Peer Present / Peer Gone 消息)
     - ping/pong keepalive
```

自建 DERP: 需要 TLS 证书 + 公网端口。`verify-clients` 选项依赖 tailscaled 连上 coordination server 验证 client 身份 — 如果在中国 deployment，coordination server 连不上 (被墙)，则此选项不可用。

## NAT Traversal

Tailscale 在建立 WireGuard tunnel 前，先做穿透:

```
策略 (按优先级):
  1. 双方在同一局域网 → 直接 LAN IP (host firewall must allow)
  2. 一方 Full Cone NAT → STUN → get external IP:port → remote connects
  3. 双方 Symmetric NAT → port prediction + birthday attack:
     每个 peer 同时向对方猜测的 port 发 UDP (探测多端口)
     → 某对 pair 匹配 → 直接连通
  4. UPnP/NAT-PMP/PCP: 如果路由器支持 → 映射端口 → 等同于公网 IP
  5. 以上全失败 → DERP relay (保底)

端口预测:
  观察 NAT 的 port 分配模式 (increment, hash, random):
    递增: port_N = base_port + N
    hash: port_N = f(proto, src_ip, src_port, dst_ip, dst_port)
    random: 不可预测 → 打洞不可能 → only DERP
```

## 参考

- **tailscale blog**: "How NAT traversal works"
- **headscale**: github.com/juanfont/headscale
- **DERP**: tailscale.com/kb/1232/derp

*Keywords: Tailscale, coordination server, DERP, NAT traversal, port prediction, wireguard-go, headscale, ACL*
