n0-computer/iroh
GitHub: n0-computer/iroh
基于 Rust 和 QUIC 协议的模块化网络栈,用公钥替代 IP 地址实现跨 NAT 的自动打洞直连与中继回退。
Stars: 8064 | Forks: 374
让网络少点网络操作
[](https://docs.rs/iroh/) [](https://crates.io/crates/iroh) [](https://crates.io/crates/iroh) [](https://discord.com/invite/DpmJgtU7cW) [](https://www.youtube.com/@n0computer) [](LICENSE-MIT) [](LICENSE-APACHE) [](https://github.com/n0-computer/iroh/actions/workflows/ci.yml)## 什么是 iroh? Iroh 为您提供通过公钥进行拨号的 API。 您只需说“连接到那部手机”,无论它位于何处,iroh 都会为您找到并维护最快的连接。 ### 打洞 (Hole-punching) 最快的路由是直接连接,因此如有必要,iroh 会尝试打洞。 如果失败,它可以回退到开放的公共中继服务器生态系统。 为了确保这些连接尽可能快,我们[持续测量 iroh][iroh-perf]。 ### 基于 [QUIC] 构建 Iroh 使用 [noq] 在端点之间建立 [QUIC] 连接。 这样您就可以开箱即用地获得认证加密、具有流优先级的并发流、数据报传输,并避免队头阻塞。 ## 组合协议 使用基于 iroh 构建的现有协议,而不是自己编写: - [iroh-blobs]:用于基于 [BLAKE3] 的内容寻址 blob 传输,规模从千字节到太字节 - [iroh-gossip]:用于建立可扩展的发布-订阅覆盖网络,仅需普通手机可处理的资源 - [iroh-docs]:用于 [iroh-blobs] blob 的最终一致性键值存储 - [iroh-willow]:[willow protocol] 的(构建中)实现 ## 快速开始 ### Rust 库 在 Rust 中使用 iroh 最为简单。 使用 `cargo add iroh` 安装,然后在连接端: ``` const ALPN: &[u8] = b"iroh-example/echo/0"; let endpoint = Endpoint::bind().await?; // Open a connection to the accepting endpoint let conn = endpoint.connect(addr, ALPN).await?; // Open a bidirectional QUIC stream let (mut send, mut recv) = conn.open_bi().await?; // Send some data to be echoed send.write_all(b"Hello, world!").await?; send.finish()?; // Receive the echo let response = recv.read_to_end(1000).await?; assert_eq!(&response, b"Hello, world!"); // As the side receiving the last application data - say goodbye conn.close(0u32.into(), b"bye!"); // Close the endpoint and all its connections endpoint.close().await; ``` 在接受端: ``` let endpoint = Endpoint::bind().await?; let router = Router::builder(endpoint) .accept(ALPN.to_vec(), Arc::new(Echo)) .spawn() .await?; // The protocol definition: #[derive(Debug, Clone)] struct Echo; impl ProtocolHandler for Echo { async fn accept(&self, connection: Connection) -> Result<()> { let (mut send, mut recv) = connection.accept_bi().await?; // Echo any bytes received back directly. let bytes_sent = tokio::io::copy(&mut recv, &mut send).await?; send.finish()?; connection.closed().await; Ok(()) } } ``` 带有更多注释的完整示例代码可在 [`echo.rs`][echo-rs] 中找到。 或者使用现有的协议之一,例如 [iroh-blobs] 或 [iroh-gossip]。 ### 其他语言 如果您想从其他语言使用 iroh,请务必查看 [iroh-ffi],这是 FFI 绑定的仓库。 ### 链接 - [Iroh 介绍(视频)][iroh-yt-video] - [Iroh 文档][docs] - [Iroh 示例][Iroh Examples] - [Iroh 实验][Iroh Experiments] ## 仓库结构 此仓库包含一个 crate 工作区: - `iroh`:用于打洞和与中继通信的核心库。 - `iroh-relay`:中继服务器实现。这是我们在生产环境中运行的代码(您也可以!)。 - `iroh-base`:通用类型,如 `Hash`、密钥类型或 `RelayUrl`。 - `iroh-dns-server`:为 EndpointIds 的 `n0_discovery` 提供支持的 DNS 服务器实现,运行在 dns.iroh.link。 - `iroh-net-report`:分析主机的网络能力和 NAT。 ## 许可证 版权所有 2025 N0, INC. 此项目根据以下任一许可证授权: * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) 或 http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) 或 http://opensource.org/licenses/MIT) 由您选择。 ## 贡献 除非您明确声明,否则您提交的任何旨在包含本项目中的贡献,将按照 Apache-2.0 许可证的定义,按上述方式双重许可,无需任何附加条款或条件。
标签:Hole-punching, IP 地址批量处理, NAT穿透, P2P, QUIC, Rust, UDP, 中继服务器, 中间件, 公钥寻址, 内核驱动, 分布式系统, 加密通信, 去中心化, 可视化界面, 响应大小分析, 开源库, 搜索引擎爬虫, 模块化, 直连, 网络协议, 网络流量审计, 通知系统, 零信任网络