162 lines
4.1 KiB
Markdown
162 lines
4.1 KiB
Markdown
|
|
# LocalFinder 实现说明
|
||
|
|
|
||
|
|
## 📋 概述
|
||
|
|
|
||
|
|
已成功构建 `LocalFinder` 结构的基础框架,用于 Iroh 的局域网设备发现功能。由于 Iroh v0.97 API 发生重大变更,当前实现为占位符版本。
|
||
|
|
|
||
|
|
## ✅ 已完成的工作
|
||
|
|
|
||
|
|
### 1. 文件结构
|
||
|
|
|
||
|
|
创建了以下文件:
|
||
|
|
|
||
|
|
- **`src/iroh/local_find.rs`** - LocalFinder 核心实现
|
||
|
|
- **`src/iroh/mod.rs`** - Iroh 模块导出
|
||
|
|
- **Tauri Commands** - 在 `handlers/tauri_handlers.rs` 中添加了 5 个新的命令处理器
|
||
|
|
|
||
|
|
### 2. LocalFinder 结构
|
||
|
|
|
||
|
|
```rust
|
||
|
|
pub struct LocalFinder {
|
||
|
|
node_id: String,
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
#### 已实现的方法:
|
||
|
|
|
||
|
|
- `new()` - 创建局域网发现管理器(占位符)
|
||
|
|
- `local_id()` - 获取本地节点 ID
|
||
|
|
|
||
|
|
### 3. Tauri Commands
|
||
|
|
|
||
|
|
已添加以下命令供前端调用:
|
||
|
|
|
||
|
|
1. **`initialize_local_finder()`** - 初始化局域网发现管理器
|
||
|
|
2. **`start_discovery()`** - 开始设备发现(占位符)
|
||
|
|
3. **`stop_discovery()`** - 停止设备发现(占位符)
|
||
|
|
4. **`get_local_node_id()`** - 获取本地节点 ID
|
||
|
|
5. **`get_discovered_peers()`** - 获取发现的设备列表(占位符)
|
||
|
|
|
||
|
|
### 4. 集成到应用
|
||
|
|
|
||
|
|
- ✅ 更新了 `lib.rs` 导出 `LocalFinder`
|
||
|
|
- ✅ 在 Tauri 应用中注册了 `LocalFinder` 状态管理
|
||
|
|
- ✅ 将所有新 commands 添加到 invoke_handler
|
||
|
|
|
||
|
|
## ⚠️ 当前限制
|
||
|
|
|
||
|
|
### Iroh API 变更问题
|
||
|
|
|
||
|
|
Iroh v0.97 的 API 与早期版本有很大不同,主要问题包括:
|
||
|
|
|
||
|
|
1. **`Endpoint::builder()`** - 现在需要 `Preset` trait 参数
|
||
|
|
2. **`SecretKey::generate()`** - 需要特定类型的 RNG
|
||
|
|
3. **`MdnsAddressLookup`** - MDNS 发现 API 已变更
|
||
|
|
4. **依赖冲突** - `rand` 和 `rand_core` 版本兼容性问题
|
||
|
|
|
||
|
|
### 占位符实现
|
||
|
|
|
||
|
|
当前代码使用占位符实现,仅用于演示结构和接口设计。
|
||
|
|
|
||
|
|
## 🔧 下一步工作
|
||
|
|
|
||
|
|
### 需要查阅的文档
|
||
|
|
|
||
|
|
1. **Iroh v0.97 官方文档** - https://docs.rs/iroh/latest/iroh/
|
||
|
|
2. **Iroh GitHub 仓库** - https://github.com/n0-computer/iroh
|
||
|
|
3. **Iroh Examples** - 查看最新的示例代码
|
||
|
|
|
||
|
|
### 待实现的功能
|
||
|
|
|
||
|
|
1. **正确的 Endpoint 初始化**
|
||
|
|
```rust
|
||
|
|
// 需要找到正确的方式
|
||
|
|
let endpoint = Endpoint::builder(preset).bind().await?;
|
||
|
|
```
|
||
|
|
|
||
|
|
2. **SecretKey 生成**
|
||
|
|
```rust
|
||
|
|
// 需要使用正确的 RNG
|
||
|
|
let secret_key = SecretKey::generate(/* correct rng */);
|
||
|
|
```
|
||
|
|
|
||
|
|
3. **MDNS 发现集成**
|
||
|
|
```rust
|
||
|
|
// 需要查阅新的 API
|
||
|
|
let mdns = MdnsAddressLookup::new()?;
|
||
|
|
endpoint.discovery().add(mdns)?;
|
||
|
|
```
|
||
|
|
|
||
|
|
4. **设备连接和管理**
|
||
|
|
- `connect_to_peer()` - 连接到发现的设备
|
||
|
|
- `disconnect_peer()` - 断开连接
|
||
|
|
- `get_known_peers()` - 获取已知设备列表
|
||
|
|
|
||
|
|
## 📝 使用示例(当前占位符)
|
||
|
|
|
||
|
|
### Rust 代码
|
||
|
|
|
||
|
|
```rust
|
||
|
|
use crate::iroh::LocalFinder;
|
||
|
|
|
||
|
|
// 创建 LocalFinder
|
||
|
|
let finder = LocalFinder::new().await?;
|
||
|
|
|
||
|
|
// 获取节点 ID
|
||
|
|
let node_id = finder.local_id();
|
||
|
|
println!("Local node ID: {}", node_id);
|
||
|
|
```
|
||
|
|
|
||
|
|
### TypeScript/JavaScript 调用(前端)
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
// 初始化
|
||
|
|
const nodeId = await invoke('initialize_local_finder');
|
||
|
|
console.log(nodeId);
|
||
|
|
|
||
|
|
// 获取本地节点 ID
|
||
|
|
const localId = await invoke('get_local_node_id');
|
||
|
|
console.log(localId);
|
||
|
|
|
||
|
|
// 开始发现(占位符)
|
||
|
|
await invoke('start_discovery', { durationSecs: 60 });
|
||
|
|
|
||
|
|
// 获取发现的设备(占位符)
|
||
|
|
const peers = await invoke('get_discovered_peers');
|
||
|
|
console.log(peers);
|
||
|
|
```
|
||
|
|
|
||
|
|
## 🎯 建议
|
||
|
|
|
||
|
|
由于 Iroh API 频繁变更,建议:
|
||
|
|
|
||
|
|
1. **暂时移除 Iroh 依赖** - 避免编译错误
|
||
|
|
2. **使用条件编译** - 仅在需要时启用 Iroh 功能
|
||
|
|
3. **关注 Iroh 更新** - 订阅 Iroh 的 release notes
|
||
|
|
4. **考虑替代方案** - 如果 Iroh 不稳定,可以考虑其他 P2P 库
|
||
|
|
|
||
|
|
## 📦 依赖配置
|
||
|
|
|
||
|
|
当前 `Cargo.toml` 中的相关依赖:
|
||
|
|
|
||
|
|
```toml
|
||
|
|
[dependencies]
|
||
|
|
iroh = { version = "0.97", features = ["address-lookup-mdns"] }
|
||
|
|
iroh-tickets = "0.4"
|
||
|
|
tokio = "1.50.0"
|
||
|
|
anyhow = "1.0.102"
|
||
|
|
```
|
||
|
|
|
||
|
|
## 📊 项目状态
|
||
|
|
|
||
|
|
- ✅ **LocalFinder 结构** - 完成
|
||
|
|
- ✅ **Tauri 集成** - 完成
|
||
|
|
- ✅ **Commands 实现** - 完成(占位符)
|
||
|
|
- ⚠️ **实际功能** - 等待 Iroh API 研究
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
**创建时间**: 2026-04-03
|
||
|
|
**最后更新**: 2026-04-03
|
||
|
|
**状态**: 占位符实现,等待 Iroh API 研究
|