互联网 NAT 穿透 P2P 高速传输方案(待测)

核心原理

1
2
3
主机A (NAT后) ←→ FRP/STUN服务器 ←→ 主机B (NAT后)
↑ ↑
└──────── UDP 打洞直连 ────────────────┘

方案一:使用 frp 的 xtcp 模式(推荐)

frp 内置支持 P2P 穿透,打洞成功后直接传输,不经过服务器。

服务端配置 (frps.toml)

1
2
bindPort = 7000
bindUdpPort = 7001 # UDP 打洞端口

主机A - 提供方 (frpc.toml)

1
2
3
4
5
6
7
8
9
serverAddr = "your-frp-server.com"
serverPort = 7000

[[proxies]]
name = "p2p_file"
type = "xtcp"
secretKey = "your-secret-key"
localIP = "127.0.0.1"
localPort = 22 # 或任何你要暴露的服务端口

主机B - 访问方 (frpc.toml)

1
2
3
4
5
6
7
8
9
10
11
serverAddr = "your-frp-server.com"
serverPort = 7000

[[visitors]]
name = "p2p_file_visitor"
type = "xtcp"
serverName = "p2p_file"
secretKey = "your-secret-key"
bindAddr = "127.0.0.1"
bindPort = 6000
keepTunnelOpen = true

文件传输

1
2
3
4
5
6
7
8
9
10
# 主机A 启动服务(使用高速传输工具)
# 方法1: 简单 HTTP
python3 -m http.server 22 --directory /path/to/share

# 方法2: rsync daemon
rsync --daemon --port=22 --config=/etc/rsyncd.conf

# 主机B 下载
wget http://127.0.0.1:6000/filename
rsync -avP rsync://127.0.0.1:6000/module/file /local/path

方案二:WireGuard + 打洞工具(最高性能)

1. 使用 wg-quick + netmaker/wirehole

1
2
3
4
5
6
7
8
# 两台主机都安装 wireguard
apt install wireguard

# 主机A 生成密钥
wg genkey | tee privatekey_a | wg pubkey > publickey_a

# 主机B 生成密钥
wg genkey | tee privatekey_b | wg pubkey > publickey_b

2. 使用 udp2raw + WireGuard 打洞

1
2
3
4
5
6
7
8
9
# 安装 udp2raw(伪装 UDP 为 TCP,穿透性更强)
git clone https://github.com/wangyu-/udp2raw.git
cd udp2raw && make

# 主机A(服务端模式,先通过 frp 暴露)
./udp2raw -s -l 0.0.0.0:4096 -r 127.0.0.1:51820 -k "password" --raw-mode faketcp

# 主机B(客户端模式)
./udp2raw -c -l 0.0.0.0:3333 -r 主机A公网IP:4096 -k "password" --raw-mode faketcp

方案三:n2n - 专用 P2P VPN(强烈推荐)

n2n 是开源的 P2P VPN,可利用现有服务器做超级节点,数据直连传输。

安装

1
2
3
4
5
6
# Ubuntu/Debian
apt install n2n

# 或编译最新版
git clone https://github.com/ntop/n2n.git
cd n2n && ./autogen.sh && ./configure && make && sudo make install

超级节点(在你的 frp 服务器上运行)

1
supernode -l 7654 -f

主机A(边缘节点)

1
2
3
4
edge -c mynetwork -k mypassword \
-a 10.10.10.1 \
-l your-frp-server.com:7654 \
-f

主机B(边缘节点)

1
2
3
4
edge -c mynetwork -k mypassword \
-a 10.10.10.2 \
-l your-frp-server.com:7654 \
-f

直接传输

1
2
3
4
5
6
7
8
# 现在两台机器在同一虚拟网络,直接通信
# 主机B 上:
scp user@10.10.10.1:/file /local/
rsync -avP --progress user@10.10.10.1:/data /local/

# 高速传输
iperf3 -s # 主机A
iperf3 -c 10.10.10.1 # 主机B 测速

方案四:croc - 最简单的 P2P 传输工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装
curl https://getcroc.schollz.com | bash

# 或
go install github.com/schollz/croc/v10@latest

# 主机A 发送
croc send --relay your-frp-server.com:9009 myfile.tar.gz

# 主机B 接收(会给出一个 code)
croc --relay your-frp-server.com:9009 code-phrase

# 自建 relay(在 frp 服务器上)
croc relay --ports 9009,9010,9011

方案五:syncthing P2P 同步(持续同步场景)

1
2
3
4
5
6
7
8
9
# 安装
apt install syncthing

# 启动(两台机器都运行)
syncthing

# 访问 Web UI: http://localhost:8384
# 添加对方设备 ID,配置共享文件夹
# 会自动尝试 P2P 直连,relay 仅作为回退

配置直连优先:

1
2
3
4
5
6
<!-- ~/.config/syncthing/config.xml -->
<options>
<relaysEnabled>false</relaysEnabled> <!-- 禁用 relay,强制直连 -->
<globalAnnounceEnabled>true</globalAnnounceEnabled>
<stunServer>default</stunServer>
</options>

方案六:zerotier-one(开源自建)

1
2
3
4
5
6
7
8
9
10
# 自建 controller(在你的服务器上)
docker run -d --name ztncui \
-p 4000:4000 \
-v ~/zt:/opt/key-networks/ztncui/etc \
keynetworks/ztncui

# 客户端加入
zerotier-cli join <network-id>

# 完全 P2P 直连,带宽最大化

最大化带宽的传输工具

一旦打洞成功(使用上述任一方案),用这些工具传输:

1. rclone(多线程)

1
rclone copy /source remote:/dest --transfers 16 --checkers 8

2. rsync + parallel

1
rsync -avP --progress -e "ssh -o Compression=no" /source/ user@peer:/dest/

3. bbcp(大文件专用)

1
bbcp -P 2 -w 8m -s 16 /local/file user@peer:/remote/

4. lftp mirror(并行传输)

1
lftp -e "mirror -P 10 /remote /local; quit" sftp://user@peer

性能对比

方案 配置复杂度 打洞成功率 性能 适用场景
frp xtcp ⭐⭐ 70% 已有 frp 环境
n2n ⭐⭐ 85% 最高 长期使用
croc 75% 中高 临时传文件
WireGuard ⭐⭐⭐ 60% 最高 需要 VPN
syncthing 80% 持续同步

推荐优先级:n2n > frp xtcp > croc(看你的具体需求)