Skip to content

Commit 9b95678

Browse files
committed
feat: support p2p connect via ipv6
1 parent fc4c57d commit 9b95678

File tree

17 files changed

+1413
-230
lines changed

17 files changed

+1413
-230
lines changed

Cargo.lock

Lines changed: 48 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustun"
3-
version = "0.1.0"
3+
version = "0.0.1"
44
edition = "2024"
55

66
[dependencies]
@@ -24,6 +24,9 @@ ipnet = "2.10"
2424
clap = { version = "4.5", features = ["derive"] }
2525
ureq = "2.10"
2626

27+
[dev-dependencies]
28+
tokio-test = "0.4"
29+
2730
[[bin]]
2831
name = "server"
2932
path = "src/cmd/server.rs"

README.md

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ A high-performance VPN tunnel implementation written in Rust.
1919
- 🔐 **Secure by Default** - AEAD encryption (ChaCha20-Poly1305), perfect forward secrecy, replay protection
2020
- 🚀 **Simple & Easy** - Minimal configuration, straightforward CLI, quick deployment
2121
- 🌍 **Cross-Platform** - Native support for Linux, macOS, Windows with pre-built binaries
22+
-**IPv6 P2P Direct Connection** - Automatic peer-to-peer connection with relay fallback for optimal performance
2223
- 🎯 **Multiple Encryption Options**
2324
- **ChaCha20-Poly1305** (Default, Recommended)
2425
- **AES-256-GCM** (Hardware accelerated)
@@ -32,7 +33,6 @@ A high-performance VPN tunnel implementation written in Rust.
3233
- [Configuration](#configuration)
3334
- [Usage](#usage)
3435
- [Build from Source](#build-from-source)
35-
- [Architecture](#architecture)
3636
- [Security](#security)
3737
- [Contributing](#contributing)
3838

@@ -72,17 +72,17 @@ Each release includes:
7272
**Linux/macOS:**
7373
```bash
7474
# Download and extract (example for Linux x86_64)
75-
wget https://github.com/smartethnet/rustun/releases/download/v1.0.0/rustun-v1.0.0-x86_64-unknown-linux-gnu.tar.gz
76-
tar xzf rustun-v1.0.0-x86_64-unknown-linux-gnu.tar.gz
77-
cd rustun-v1.0.0-x86_64-unknown-linux-gnu
75+
wget https://github.com/smartethnet/rustun/releases/download/0.0.1/rustun-0.0.1-x86_64-unknown-linux-gnu.tar.gz
76+
tar xzf rustun-0.0.1-x86_64-unknown-linux-gnu.tar.gz
77+
cd rustun-0.0.1-x86_64-unknown-linux-gnu
7878

7979
# Make binaries executable
8080
chmod +x server client
8181
```
8282

8383
**Windows:**
8484
```powershell
85-
# 1. Download rustun-v1.0.0-x86_64-pc-windows-msvc.zip from releases
85+
# 1. Download rustun-0.0.1-x86_64-pc-windows-msvc.zip from releases
8686
# 2. Extract to a directory
8787
# 3. Download Wintun from https://www.wintun.net/
8888
# 4. Extract wintun.dll to the same directory as client.exe
@@ -234,6 +234,10 @@ Options:
234234
Encryption method: plain, aes256:<key>, chacha20:<key>, or xor:<key>
235235
[default: chacha20:rustun]
236236
237+
--enable-p2p
238+
Enable P2P direct connection via IPv6
239+
(disabled by default, uses relay only)
240+
237241
--keepalive-interval <KEEPALIVE_INTERVAL>
238242
Keep-alive interval in seconds
239243
[default: 10]
@@ -265,6 +269,33 @@ Options:
265269
./client -s SERVER:8080 -i client-001 -c plain
266270
```
267271

272+
### P2P Direct Connection
273+
274+
By default, all traffic goes through the relay server. Enable P2P for direct IPv6 connections between clients:
275+
276+
```bash
277+
# Enable P2P direct connection
278+
./client -s SERVER:8080 -i client-001 --enable-p2p
279+
```
280+
281+
**P2P Benefits:**
282+
- 🚀 Lower latency (direct peer-to-peer)
283+
- 📉 Reduced server bandwidth usage
284+
- ⚡ Automatic fallback to relay if P2P fails
285+
286+
**Requirements:**
287+
- Both clients must have IPv6 connectivity
288+
- Both clients must use `--enable-p2p` flag
289+
- UDP port 51258 must be accessible
290+
291+
**How it works:**
292+
1. Clients exchange IPv6 addresses via server
293+
2. Keepalive packets establish direct connection
294+
3. Data sent via P2P when connection is active
295+
4. Automatic fallback to relay if P2P fails
296+
297+
For more details, see [P2P Usage Guide](./docs/P2P_USAGE.md).
298+
268299
### Example: Multi-Tenant Setup
269300

270301
#### Scenario: Two Offices (Beijing & Shanghai)
@@ -313,50 +344,6 @@ ping 10.0.2.2 # From sh-office-gw to sh-db-server
313344
# Beijing cannot reach Shanghai and vice versa
314345
```
315346

316-
## 🏗️ Architecture
317-
318-
### Network Topology
319-
320-
```
321-
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
322-
│ Client A │◄───────►│ Server │◄───────►│ Client B │
323-
│ (Beijing) │ │ (Central) │ │ (Shanghai) │
324-
└─────────────┘ └─────────────┘ └─────────────┘
325-
│ │
326-
│ Virtual IP: 10.0.1.1 Virtual IP: 10.0.2.1
327-
│ │
328-
┌──▼──────────────┐ ┌───────────▼──────┐
329-
│ LAN: 192.168.1.0│ │ LAN: 192.168.10.0│
330-
└─────────────────┘ └──────────────────┘
331-
```
332-
333-
### Components
334-
335-
- **Server**: Central relay handling all client connections
336-
- **Client**: Edge node connecting to the server
337-
- **TUN Device**: Virtual network interface for packet tunneling
338-
- **Crypto Layer**: Encryption/decryption of all traffic
339-
- **Route Manager**: Dynamic routing table management
340-
341-
### Frame Protocol
342-
343-
```
344-
Frame Header (8 bytes):
345-
┌──────────────┬─────────┬──────┬─────────────────┐
346-
│ Magic (4B) │ Ver (1B)│ Type │ Payload Len │
347-
│ 0x91929394 │ 0x01 │ (1B) │ (2B) │
348-
└──────────────┴─────────┴──────┴─────────────────┘
349-
350-
351-
Encrypted Payload
352-
```
353-
354-
**Frame Types:**
355-
- `0x01`: Handshake (client authentication)
356-
- `0x02`: KeepAlive (connection health check)
357-
- `0x03`: Data (tunneled IP packets)
358-
- `0x04`: HandshakeReply (server configuration response)
359-
360347
## 🔒 Security
361348

362349
### Encryption Algorithms
@@ -446,18 +433,16 @@ For cross-platform builds, see [BUILD.md](BUILD.md) for detailed instructions.
446433

447434
## 🗺️ Roadmap
448435

449-
- [ ] IPv6 support
450-
- [ ] P2P direct connection
436+
- [x] **IPv6 support** - ✅ Completed
437+
- [x] **P2P direct connection** - ✅ Completed (IPv6 P2P with auto fallback)
451438
- [ ] Windows service support
452439
- [ ] systemd integration for Linux
453440
- [ ] Web-based management dashboard
454441
- [ ] Dynamic route updates without restart
455-
- [ ] UDP transport support
456442
- [ ] QUIC protocol support
457443
- [ ] Mobile clients (iOS/Android)
458444
- [ ] Docker container images
459445
- [ ] Kubernetes operator
460-
- [ ] Pre-built binary releases
461446
- [ ] Auto-update mechanism
462447

463448
## 📦 Download

doc/README_CN.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Rust 编写的高性能 VPN 隧道,用于实现设备互联,异地组网。
1919
- 🔐 **默认安全** - AEAD 加密(ChaCha20-Poly1305)、完美前向保密、重放保护
2020
- 🚀 **简单易用** - 最小化配置、简洁的命令行、快速部署
2121
- 🌍 **跨平台** - 原生支持 Linux、macOS、Windows,提供预编译二进制文件
22+
-**IPv6 P2P 直连** - 自动点对点连接,中继降级,性能最优
2223
- 🎯 **多种加密选项**
2324
- **ChaCha20-Poly1305** (默认,推荐)
2425
- **AES-256-GCM** (硬件加速)
@@ -32,7 +33,6 @@ Rust 编写的高性能 VPN 隧道,用于实现设备互联,异地组网。
3233
- [配置](#配置)
3334
- [使用说明](#使用说明)
3435
- [从源码构建](#从源码构建)
35-
- [架构](#架构)
3636
- [安全性](#安全性)
3737
- [贡献](#贡献)
3838

@@ -234,6 +234,10 @@ Rustun VPN Client
234234
加密方式: plain, aes256:<key>, chacha20:<key>, 或 xor:<key>
235235
[默认: chacha20:rustun]
236236
237+
--enable-p2p
238+
启用 P2P IPv6 直连
239+
(默认禁用,仅使用中继)
240+
237241
--keepalive-interval <KEEPALIVE_INTERVAL>
238242
保活间隔(秒)
239243
[默认: 10]
@@ -265,6 +269,33 @@ Rustun VPN Client
265269
./client -s SERVER:8080 -i client-001 -c plain
266270
```
267271

272+
### P2P 直连
273+
274+
默认情况下,所有流量都通过中继服务器。启用 P2P 可实现客户端之间的 IPv6 直连:
275+
276+
```bash
277+
# 启用 P2P 直连
278+
./client -s SERVER:8080 -i client-001 --enable-p2p
279+
```
280+
281+
**P2P 优势:**
282+
- 🚀 更低延迟(点对点直连)
283+
- 📉 减少服务器带宽消耗
284+
- ⚡ P2P 失败时自动降级到中继
285+
286+
**要求:**
287+
- 双方客户端都需要 IPv6 连接
288+
- 双方客户端都需要使用 `--enable-p2p` 标志
289+
- UDP 端口 51258 需要可访问
290+
291+
**工作原理:**
292+
1. 客户端通过服务器交换 IPv6 地址
293+
2. Keepalive 数据包建立直连
294+
3. 连接激活时通过 P2P 发送数据
295+
4. P2P 失败时自动降级到中继
296+
297+
更多详情,请参阅 [P2P 使用指南](../docs/P2P_USAGE.md)
298+
268299
### 示例:多租户设置
269300

270301
#### 场景:两个办公室(北京和上海)
@@ -431,18 +462,16 @@ cargo build --release
431462

432463
## 🗺️ 路线图
433464

434-
- [ ] IPv6 支持
435-
- [ ] P2P 直连
465+
- [x] **IPv6 支持** - ✅ 已完成
466+
- [x] **P2P 直连** - ✅ 已完成(IPv6 P2P 带自动降级)
436467
- [ ] Windows 服务支持
437468
- [ ] Linux systemd 集成
438469
- [ ] 基于 Web 的管理面板
439470
- [ ] 无需重启的动态路由更新
440-
- [ ] UDP 传输支持
441471
- [ ] QUIC 协议支持
442472
- [ ] 移动客户端(iOS/Android)
443473
- [ ] Docker 容器镜像
444474
- [ ] Kubernetes operator
445-
- [ ] 预编译二进制发布
446475
- [ ] 自动更新机制
447476

448477
## 📦 下载

0 commit comments

Comments
 (0)