feat(rustdesk): 完整实现RustDesk协议和P2P连接

重大变更:
- 从prost切换到protobuf 3.4实现完整的RustDesk协议栈
- 新增P2P打洞模块(punch.rs)支持直连和中继回退
- 重构加密系统:临时Curve25519密钥对+Ed25519签名
- 完善HID适配器:支持CapsLock状态同步和修饰键映射
- 添加音频流支持:Opus编码+音频帧适配器
- 优化视频流:改进帧适配器和编码器协商
- 移除pacer.rs简化视频管道

扩展系统:
- 在设置向导中添加扩展步骤(ttyd/rustdesk切换)
- 扩展可用性检测和自动启动
- 新增WebConfig handler用于Web服务器配置

前端改进:
- SetupView增加第4步扩展配置
- 音频设备列表和配置界面
- 新增多语言支持(en-US/zh-CN)
- TypeScript类型生成更新

文档:
- 更新系统架构文档
- 完善config/hid/rustdesk/video/webrtc模块文档
This commit is contained in:
mofeng-git
2026-01-03 19:34:07 +08:00
parent cb7d9882a2
commit 0c82d1a840
49 changed files with 5470 additions and 1983 deletions

View File

@@ -87,7 +87,8 @@ One-KVM 是一个用 Rust 编写的轻量级、开源 IP-KVM 解决方案。它
│ │ Capture │ │ Controller │ │ Capture │ │
│ │ Encoder │ │ OTG Backend│ │ Encoder │ │
│ │ Streamer │ │ CH9329 │ │ Pipeline │ │
│ │ Pipeline │ │ DataChannel│ │ (Opus) │ │
│ │ Pipeline │ │ Monitor │ │ (Opus) │ │
│ │ Manager │ │ DataChan │ │ Shared │ │
│ └────────────┘ └────────────┘ └────────────┘ │
│ │ │ │ │
│ └───────────────────────────┼──────────────────────────┘ │
@@ -252,21 +253,21 @@ AppState 是整个应用的状态中枢,通过 `Arc` 包装的方式在所有
pub struct AppState {
// 配置和存储
config: ConfigStore, // SQLite 配置存储
sessions: SessionStore, // 内存会话存储
sessions: SessionStore, // 会话存储(内存)
users: UserStore, // SQLite 用户存储
// 核心服务
otg_service: Arc<OtgService>, // USB Gadget 统一管理
stream_manager: Arc<VideoStreamManager>, // 视频流管理器
hid: Arc<HidController>, // HID 控制器
msd: Arc<RwLock<Option<MsdController>>>, // MSD 控制器(可选)
atx: Arc<RwLock<Option<AtxController>>>, // ATX 控制器(可选)
audio: Arc<AudioController>, // 音频控制器
rustdesk: Arc<RwLock<Option<Arc<RustDeskService>>>>, // RustDesk可选
extensions: Arc<ExtensionManager>,// 扩展管理器
otg_service: Arc<OtgService>, // USB Gadget 统一管理HID/MSD 生命周期协调者)
stream_manager: Arc<VideoStreamManager>, // 视频流管理器MJPEG/WebRTC
hid: Arc<HidController>, // HID 控制器(键鼠控制)
msd: Arc<RwLock<Option<MsdController>>>, // MSD 控制器(可选虚拟U盘
atx: Arc<RwLock<Option<AtxController>>>, // ATX 控制器(可选,电源控制
audio: Arc<AudioController>, // 音频控制器ALSA + Opus
rustdesk: Arc<RwLock<Option<Arc<RustDeskService>>>>, // RustDesk可选,远程访问
extensions: Arc<ExtensionManager>,// 扩展管理器ttyd, gostc, easytier
// 通信和生命周期
events: Arc<EventBus>, // 事件总线
events: Arc<EventBus>, // 事件总线tokio broadcast channel
shutdown_tx: broadcast::Sender<()>, // 关闭信号
data_dir: PathBuf, // 数据目录
}
@@ -448,20 +449,29 @@ main()
├──► Initialize Core Services
│ │
│ ├──► EventBus::new()
│ │ └─► Create tokio broadcast channel
│ │
│ ├──► OtgService::new()
│ │ └─► Detect UDC device
│ │ └─► Detect UDC device (/sys/class/udc)
│ │ └─► Initialize OtgGadgetManager
│ │
│ ├──► HidController::new()
│ │ └─► Detect backend type (OTG/CH9329/None)
│ │ └─► Create controller with optional OtgService
│ │
│ ├──► HidController::init()
│ │ └─► Select backend (OTG/CH9329/None)
│ │ └─► Request HID function from OtgService
│ │ └─► Create HID devices (/dev/hidg0-3)
│ │ └─► Open device files with O_NONBLOCK
│ │ └─► Initialize HidHealthMonitor
│ │
│ ├──► MsdController::init() (if configured)
│ │ └─► Request MSD function from OtgService
│ │ └─► Create mass storage device
│ │ └─► Initialize Ventoy drive (if available)
│ │
│ ├──► AtxController::init() (if configured)
│ │ └─► Setup GPIO pins
│ │ └─► Setup GPIO pins or USB relay
│ │
│ ├──► AudioController::init()
│ │ └─► Open ALSA device
@@ -469,7 +479,8 @@ main()
│ │
│ ├──► VideoStreamManager::new()
│ │ └─► Initialize SharedVideoPipeline
│ │ └─► Setup encoder registry
│ │ └─► Setup encoder registry (H264/H265/VP8/VP9)
│ │ └─► Detect hardware acceleration (VAAPI/RKMPP/V4L2 M2M)
│ │
│ └──► RustDeskService::new() (if configured)
│ └─► Load/generate device ID and keys
@@ -521,15 +532,16 @@ One-KVM-RUST/
│ │ └── jpeg.rs
│ │
│ ├── hid/ # HID 模块
│ │ ├── mod.rs # HidController
│ │ ├── backend.rs # 后端抽象
│ │ ├── otg.rs # OTG 后端
│ │ ├── mod.rs # HidController(主控制器)
│ │ ├── backend.rs # HidBackend trait 和 HidBackendType
│ │ ├── otg.rs # OTG 后端USB Gadget HID
│ │ ├── ch9329.rs # CH9329 串口后端
│ │ ├── keymap.rs # 按键映射
│ │ ├── types.rs # 类型定义
│ │ ├── monitor.rs # 健康监视
│ │ ├── datachannel.rs # DataChannel 适配
│ │ ── websocket.rs # WebSocket 适配
│ │ ├── consumer.rs # Consumer Control usage codes
│ │ ├── keymap.rs # JS keyCode → USB HID 转换表
│ │ ├── types.rs # 事件类型定义
│ │ ├── monitor.rs # HidHealthMonitor错误跟踪与恢复
│ │ ── datachannel.rs # DataChannel 二进制协议解析
│ │ └── websocket.rs # WebSocket 二进制协议适配
│ │
│ ├── otg/ # USB OTG 模块
│ │ ├── mod.rs
@@ -839,17 +851,36 @@ encoder_registry.register("my-encoder", || Box::new(MyEncoder::new()));
### 9.2 添加新 HID 后端
```rust
// 1. 实现 HidBackend trait
impl HidBackend for MyBackend {
async fn send_keyboard(&self, event: &KeyboardEvent) -> Result<()>;
async fn send_mouse(&self, event: &MouseEvent) -> Result<()>;
fn info(&self) -> HidBackendInfo;
// ...
// 1. 在 backend.rs 中定义新后端类型
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum HidBackendType {
Otg,
Ch9329 { port: String, baud_rate: u32 },
MyBackend { /* 配置参数 */ }, // 新增
None,
}
// 2. HidController::init() 中添加分支
match config.backend {
HidBackendType::MyBackend => MyBackend::new(config),
// 2. 实现 HidBackend trait
#[async_trait]
impl HidBackend for MyBackend {
fn name(&self) -> &'static str { "MyBackend" }
async fn init(&self) -> Result<()> { /* ... */ }
async fn send_keyboard(&self, event: KeyboardEvent) -> Result<()> { /* ... */ }
async fn send_mouse(&self, event: MouseEvent) -> Result<()> { /* ... */ }
async fn send_consumer(&self, event: ConsumerEvent) -> Result<()> { /* ... */ }
async fn reset(&self) -> Result<()> { /* ... */ }
async fn shutdown(&self) -> Result<()> { /* ... */ }
fn supports_absolute_mouse(&self) -> bool { true }
fn screen_resolution(&self) -> Option<(u32, u32)> { None }
fn set_screen_resolution(&mut self, width: u32, height: u32) { /* ... */ }
}
// 3. 在 HidController::init() 中添加分支
match backend_type {
HidBackendType::MyBackend { /* params */ } => {
Box::new(MyBackend::new(/* params */)?)
}
// ...
}
```