---
title: "点对点（P2P）：设备直连 | Wantastic Docs"
description: "两台 wantasticd 设备如何直接通信、如何检查，以及一直停留在 relay 时如何获得直连路径。"
url: "https://wantastic.app/zh/docs/p2p-connections"
category: "Remote access"
reading_time: "4 分钟"
---

# 点对点（P2P）：设备直连

两台 wantasticd 设备如何直接通信、如何检查，以及一直停留在 relay 时如何获得直连路径。

> **Outcome:** 你的设备可以通过 VPN IP 互相访问，wantasticd peers 显示 PATH direct。

## Before you start

- 两台设备都已安装 wantasticd 且为 Online
- 两台设备在同一个 Wantastic 团队中

## P2P 的工作方式

P2P 是自动的。无需开启任何开关，也无需开放端口。

- 每台 wantasticd 设备都可以通过 VPN IP 访问你团队中的任何其他设备。
- 最初的数据包经由 Wantastic 服务器（relay）转发，因此连接立即可用。
- 两台设备会在后台寻找直连路径。成功后，流量会切换过去（PATH direct）。
- 如果无法建立直连，流量会继续走 relay。连接仍然可用，只是延迟更高。
- P2P 适用于运行 wantasticd 的设备：Linux、Raspberry Pi、OpenWrt、macOS、Windows 和 Docker。MikroTik 路由器和使用 WireGuard 应用的手机始终经由服务器。

## 连接两台设备

1. 在两台设备上安装 wantasticd 并认领它们。
2. 在其中一台设备上运行 wantasticd peers，记下另一台设备的 VPN IP。
3. 用任意应用连接该 VPN IP，例如 SSH、浏览器或 ping。
4. 等待最多一分钟，再次运行 wantasticd peers。PATH 应从 relay 变为 direct。

列出其他设备:

```sh
wantasticd peers
```

测试连接:

```sh
wantasticd ping -c 4 <vpn-ip>
```

然后使用，例如 SSH:

```sh
ssh user@<vpn-ip>
```

> **Tip:** wantasticd ping 也接受设备名称，例如 wantasticd ping office-gw。

## 检查是 direct 还是 relay

| PATH | 含义 |
| --- | --- |
| direct | P2P。流量在两台设备之间直接传输。 |
| 中继 | 流量经由 Wantastic 服务器转发。连接可用，正在重试直连路径。 |
| 服务器 | Wantastic 服务器本身，或未运行 wantasticd 的设备（MikroTik、WireGuard 应用）。 |

*wantasticd peers: PATH direct 表示 P2P。relay 表示流量目前经由服务器转发。*

```
$ wantasticd peers
HOSTNAME   VPN IP       PATH    ENDPOINT              HANDSHAKE  RX        TX       STATE
(server)   -            server  172.239.44.171:51820  8s ago     14.1 KiB  7.9 KiB  -
office-gw  10.200.0.3   direct  203.0.113.24:41641    21s ago    3.2 KiB   1.1 KiB  established
lab-pi     10.200.0.9   relay   -                     never      0 B       0 B      trying
```

> **Operational note:** 在 macOS 和 Windows 上，托盘菜单显示相同信息：Peers: N (P2P: x, Relay: y)。

## 获得直连路径

大多数设备会自动切换到 direct。如果某台设备一直停留在 relay，请按顺序尝试以下操作。

1. 在每台设备的路由器上开启 UPnP 或 NAT-PMP。这是最有效的方法。
2. 在 macOS 上，如果出现 "Do you want the application wantasticd to accept incoming network connections?"，点击 Allow。
3. 在 Windows 上，如果 Windows 防火墙询问 wantasticd.exe，点击 Allow。
4. 在两台设备上重启 wantasticd，然后再次发送流量。

> **Warning:** 移动网络和部分家庭宽带使用 CGNAT。在这些网络中直连常常失败，设备会停留在 relay。这是正常的，连接仍然可用。

## P2P 与出口节点

出口节点需要直连路径。在 Topology 中，Assign Exit Node 只列出已支持 P2P 的设备。如果托盘显示 "Relayed right now: exit traffic starts once a direct path is up"，请等待 PATH direct。

## Verify the result

- wantasticd peers 列出了另一台设备。
- wantasticd ping -c 4 <vpn-ip> 显示 0% 丢包。
- PATH 显示 direct（在 CGNAT 网络中为 relay，同样可用）。

## Common issues

### 另一台设备没有列出。

```
No peers.
```

- **Where:** wantasticd peers
- **Likely cause:** 另一台设备离线、未认领，或在另一个团队中。
- **Fix:** 在控制台中确认两台设备在同一团队中且为 Online，然后再次运行 wantasticd peers。

### 某台设备一直停留在 relay。

```
[P2P] Punch timeout for peer N after M targets
```

- **Where:** wantasticd 日志
- **Likely cause:** 路由器或防火墙阻止了直连路径。
- **Fix:** 在路由器上开启 UPnP 或 NAT-PMP。wantasticd 会自动重试，期间 relay 继续工作。

- **Verify:** wantasticd peers 显示 PATH direct。

### 直连路径建立后又回退到 relay。

```
[P2P] Direct path validation failed for peer N; relay remains active: direct handshake timeout
```

- **Where:** wantasticd 日志
- **Likely cause:** 打洞包已通过，但加密握手未通过，通常是防火墙过于严格。
- **Fix:** 在两台设备的防火墙中允许 wantasticd，然后重启 wantasticd。

### macOS 一直停留在 relay。

```
[P2P] Could not add … to the application firewall
```

- **Where:** wantasticd 日志（macOS）
- **Likely cause:** macOS 防火墙阻止了到 wantasticd 的入站连接。
- **Fix:** 打开 System Settings > Network > Firewall > Options，将 wantasticd 设为 Allow incoming connections，然后重启 wantasticd。

在 macOS 上重启:

```sh
sudo launchctl kickstart -k system/com.wantastic.wantasticd
```

### MikroTik 路由器始终显示 PATH server。

- **Likely cause:** MikroTik 和 WireGuard 应用设备不运行 wantasticd，因此始终经由服务器。
- **Fix:** 无需修复。它们仍可通过 VPN IP 访问。

## Continue with

- [Topology, group links and exit nodes](https://wantastic.app/zh/docs/topology-and-access): See the network map, let devices reach each other with group links, and send a device's internet through another.
- [wantasticd CLI reference and operations](https://wantastic.app/zh/docs/wantasticd): The most used wantasticd commands first, then every command and flag, environment variables, config and state paths per OS, log lines, local access, exit node, and the claim flow (genkey / --wait-claim).
- [Connect and runtime errors](https://wantastic.app/zh/docs/errors-connect-runtime): Fix service start failures, config errors, TUN problems, missing handshakes and DNS warnings.
- [Choose your install method](https://wantastic.app/zh/docs/install-overview): Find your device in one table and copy one command. Most devices install in 2 minutes: install, scan a QR code, click Claim Device.
- [Connect a MikroTik router (RouterOS 7)](https://wantastic.app/zh/docs/remote-mikrotik): Connect a RouterOS 7 router in two minutes: copy one script from the console and paste it into the router terminal. No package and no open WAN port.
