🏗️ 工作区架构
[workspace]
resolver = "2"
members = [
"crates/twocha-protocol",
"crates/twocha-core",
"crates/twocha-lib",
"crates/twocha-cli",
]
模块化设计理念
项目采用 Cargo Workspace 多包架构,将不同功能模块分离为独立的crate,体现了清晰的关注点分离原则:
| twocha-protocol | 协议层 | 定义VPN通信协议、数据包格式、握手流程 |
| twocha-core | 核心层 | 加密解密、隧道管理、路由表操作等核心逻辑 |
| twocha-lib | 库层 | 对外暴露的API接口,供其他Rust项目集成 |
| twocha-cli | 命令行层 | 用户交互界面,解析命令行参数,调用底层功能 |
设计优势:
- 高内聚低耦合:各模块独立演进,修改协议不影响CLI界面
- 可测试性:可单独测试核心加密模块
- 复用性:其他项目可引入twocha-lib构建GUI版本
- 编译优化:只有修改的crate需要重新编译
📊 元数据配置
[workspace.package]
version = "0.7.0"
edition = "2021"
license = "MIT"
authors = ["keepinfov"]
repository = "https://github.com/keepinfov/2cha"
| version | 版本号 | 遵循语义化版本规范 |
| edition | Rust版本 | 2021版Rust特性支持 |
| license | 开源协议 | MIT宽松许可证 |
| authors | 作者 | 便于社区联系 |
| repository | 源码仓库 | 开源协作地址 |
🔗 内部依赖管理
[workspace.dependencies]
# Internal crates
twocha-protocol = { path = "crates/twocha-protocol" }
twocha-core = { path = "crates/twocha-core" }
twocha-lib = { path = "crates/twocha-lib" }
使用本地路径引用,开发时实时同步修改,发布时替换为版本号。
📚 外部依赖全景分析
1. 日志系统
log = "0.4" # 日志门面接口
env_logger = "0.11" # 环境变量日志实现
- log:Rust标准日志trait,提供info!, debug!, error!等宏
- env_logger:通过RUST_LOG环境变量控制日志级别,便于生产环境调试
2. 配置序列化
serde = { version = "1.0", features = ["derive"] }
toml = "0.9.8"
- serde:Rust最强大的序列化框架,derive特性自动生成序列化代码
- toml:TOML格式解析器,用于读取server.toml、client.toml配置文件
3. 加密库(RustCrypto生态)
chacha20poly1305 = "0.10"
aes-gcm = "0.10"
zeroize = { version = "1.8", features = ["derive"] }
安全特性:
- 纯Rust实现:无C语言依赖,内存安全
- 经过审计:RustCrypto库经过安全审计
- 零化内存:zeroize确保密钥离开内存时被覆盖
| chacha20poly1305 | ChaCha20-Poly1305 | 软件实现高效,无硬件加速时性能好 |
| aes-gcm | AES-256-GCM | 硬件加速(AES-NI)时性能极佳 |
| zeroize | 内存安全 | 防止密钥残留在内存中 |
4. CLI/TUI界面
clap = { version = "4.5", features = ["derive", "color", "wrap_help"] }
console = "0.16.1"
indicatif = "0.18.3"
rpassword = "7.3"
| clap | 命令行解析 | 派生宏定义参数、彩色输出、自动换行帮助信息 |
| console | 终端样式 | 彩色输出、终端大小检测 |
| indicatif | 进度条 | 显示连接进度、流量进度条 |
| rpassword | 密码输入 | 终端隐藏输入,用于密钥输入 |
5. Unix平台支持
libc = "0.2" # C标准库FFI绑定
daemonize = "0.5" # 守护进程化
- libc:调用POSIX系统调用(如setuid, socket等)
- daemonize:将程序转为后台守护进程(-d参数)
6. Windows平台支持
windows = { version = "0.62.2", features = [
"Win32_Foundation", # Windows基础类型
"Win32_System_Threading", # 线程管理
"Win32_System_Console", # 控制台操作
"Win32_Networking_WinSock", # Windows Socket
"Win32_NetworkManagement_IpHelper", # IP助手函数
"Win32_NetworkManagement_Ndis", # 网络驱动接口
"Win32_System_IO", # IO操作
"Win32_Security", # 安全相关
"Win32_System_LibraryLoader", # 动态库加载
] }
wintun = "0.5" # Windows TUN驱动
ctrlc = "3.4" # Ctrl+C信号处理
Windows特定说明:
- windows crate:官方Windows API绑定,按需选择feature减少编译体积
- wintun:WireGuard开发的TUN驱动,用于创建虚拟网卡
- ctrlc:跨平台信号处理,在Windows上处理Ctrl+C和Ctrl+Break
⚙️ 编译优化配置
Release配置
[profile.release]
opt-level = 3 # 最大速度优化
lto = "fat" # 全程序链接时优化
codegen-units = 1 # 单代码生成单元(最大优化)
strip = true # 移除调试符号
panic = "abort" # panic直接终止(减少二进制体积)
优化效果:
- 性能:opt-level=3 + lto=fat 可提升20-30%运行速度
- 体积:strip + panic=abort 减少30-50%二进制大小
极致体积优化配置
[profile.release-small]
inherits = "release"
opt-level = "z" # 优化体积而非速度
lto = true # 链接时优化
strip = true # 移除符号
panic = "abort" # 无栈回溯
适用场景:
- 嵌入式设备(树莓派、路由器)
- 容器镜像(减少层大小)
- 快速分发(下载速度快)
🔄 交叉编译支持
项目提供了完整的交叉编译指南,支持:
| x86_64 | ✅ | ✅ | 服务器、PC |
| i686 | ✅ | ✅ | 旧PC |
| aarch64 | ✅ | ✅ | 树莓派4、AWS Graviton |
| armv7 | ✅ | ✅ | 树莓派3 |
| arm | ✅ | ✅ | 老旧ARM设备 |
构建命令示例
# 静态编译(单文件部署)
rustup target add x86_64-unknown-linux-musl
cargo build –release –target x86_64-unknown-linux-musl
# 使用cross简化交叉编译
cargo install cross
cross build –release –target aarch64-unknown-linux-musl
cross build –release –target armv7-unknown-linux-musleabihf
选择建议
| 通用Linux服务器 | x86_64-unknown-linux-gnu | glibc兼容性好 |
| Docker容器 | x86_64-unknown-linux-musl | 体积小、无依赖 |
| 树莓派4 | aarch64-unknown-linux-musl | 静态二进制,无依赖 |
| 树莓派3 | armv7-unknown-linux-musleabihf | 硬件浮点加速 |
| OpenWrt路由器 | arm-unknown-linux-musleabihf | 静态链接,不依赖libc |
🎯 项目结构总结
架构分层
#mermaid-svg-wNeUYPv99MFoTQij{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-wNeUYPv99MFoTQij .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wNeUYPv99MFoTQij .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wNeUYPv99MFoTQij .error-icon{fill:#552222;}#mermaid-svg-wNeUYPv99MFoTQij .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wNeUYPv99MFoTQij .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wNeUYPv99MFoTQij .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wNeUYPv99MFoTQij .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wNeUYPv99MFoTQij .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wNeUYPv99MFoTQij .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wNeUYPv99MFoTQij .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wNeUYPv99MFoTQij .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wNeUYPv99MFoTQij .marker.cross{stroke:#333333;}#mermaid-svg-wNeUYPv99MFoTQij svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wNeUYPv99MFoTQij p{margin:0;}#mermaid-svg-wNeUYPv99MFoTQij .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-wNeUYPv99MFoTQij .cluster-label text{fill:#333;}#mermaid-svg-wNeUYPv99MFoTQij .cluster-label span{color:#333;}#mermaid-svg-wNeUYPv99MFoTQij .cluster-label span p{background-color:transparent;}#mermaid-svg-wNeUYPv99MFoTQij .label text,#mermaid-svg-wNeUYPv99MFoTQij span{fill:#333;color:#333;}#mermaid-svg-wNeUYPv99MFoTQij .node rect,#mermaid-svg-wNeUYPv99MFoTQij .node circle,#mermaid-svg-wNeUYPv99MFoTQij .node ellipse,#mermaid-svg-wNeUYPv99MFoTQij .node polygon,#mermaid-svg-wNeUYPv99MFoTQij .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wNeUYPv99MFoTQij .rough-node .label text,#mermaid-svg-wNeUYPv99MFoTQij .node .label text,#mermaid-svg-wNeUYPv99MFoTQij .image-shape .label,#mermaid-svg-wNeUYPv99MFoTQij .icon-shape .label{text-anchor:middle;}#mermaid-svg-wNeUYPv99MFoTQij .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wNeUYPv99MFoTQij .rough-node .label,#mermaid-svg-wNeUYPv99MFoTQij .node .label,#mermaid-svg-wNeUYPv99MFoTQij .image-shape .label,#mermaid-svg-wNeUYPv99MFoTQij .icon-shape .label{text-align:center;}#mermaid-svg-wNeUYPv99MFoTQij .node.clickable{cursor:pointer;}#mermaid-svg-wNeUYPv99MFoTQij .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wNeUYPv99MFoTQij .arrowheadPath{fill:#333333;}#mermaid-svg-wNeUYPv99MFoTQij .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wNeUYPv99MFoTQij .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wNeUYPv99MFoTQij .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wNeUYPv99MFoTQij .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wNeUYPv99MFoTQij .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wNeUYPv99MFoTQij .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wNeUYPv99MFoTQij .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wNeUYPv99MFoTQij .cluster text{fill:#333;}#mermaid-svg-wNeUYPv99MFoTQij .cluster span{color:#333;}#mermaid-svg-wNeUYPv99MFoTQij div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-wNeUYPv99MFoTQij .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wNeUYPv99MFoTQij rect.text{fill:none;stroke-width:0;}#mermaid-svg-wNeUYPv99MFoTQij .icon-shape,#mermaid-svg-wNeUYPv99MFoTQij .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wNeUYPv99MFoTQij .icon-shape p,#mermaid-svg-wNeUYPv99MFoTQij .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wNeUYPv99MFoTQij .icon-shape rect,#mermaid-svg-wNeUYPv99MFoTQij .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wNeUYPv99MFoTQij .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wNeUYPv99MFoTQij .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wNeUYPv99MFoTQij :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
twocha-cli命令行界面
twocha-lib对外API
twocha-core核心逻辑
twocha-protocol协议定义
加密库RustCrypto
平台适配层libc/windows
设计哲学
技术选型亮点
| 加密 | RustCrypto | 纯Rust,内存安全,经过审计 |
| 配置 | TOML+Serde | 人类可读,强类型解析 |
| CLI | Clap | 功能强大,编译时检查 |
| 平台抽象 | libc/windows | 条件编译,原生性能 |
| 构建 | Cargo Workspace | 模块化,编译缓存 |
这个Cargo.toml不仅是一个依赖清单,更是项目架构设计的最佳实践展示,体现了Rust生态的成熟度和工程化水平。
